/* * Flotsam.cs * Authors: August Zinsser, Adam Nabinger * Copyright (c) 2007-2008 Cornell University This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ using System; using System.Text; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; using Random=Util.Random; namespace MaritimeDefender { /// /// The items that the friendly ships in Meteor Madness throw to the player /// They upgrade the player's weapon and grant him/her a resource when the player's ship collides with this /// class Flotsam : UFO { #region Fields // The resource to pick up private readonly Pickup mResource; // The amount of the resource to be collected private int mValue; // Reference to the current Maritime Defender game screen private readonly MaritimeDefender m_Game; #endregion #region Predicate /// /// Let's the game know when the collectable can be disposed of /// public static readonly Predicate Destroyed = delegate(Flotsam o) { return !o.mAlive; }; #endregion #region Creation /// /// Constructor /// /// Type of resource to award the player for collecting this powerup /// Amount of that resource to award /// Path name of texture to use for the collectable /// The width of the collectable /// The height of the collectable /// The layer depth of the collectable /// Reference to the currenct Maritime Defender game screen public Flotsam(Pickup resourceType, int value, string texturePath, float width, float height, float depth, MaritimeDefender p_Game) : base(texturePath, width, height, depth, 10) { mResource = resourceType; mValue = value; // Add a bit of rotation for aesthetics mAngVel = Random.NextFloat(-5f, 5f); mRot = Random.NextFloat(MathHelper.TwoPi); m_Game = p_Game; } #endregion #region Update /// /// Updates the collectable /// /// Time passed in game /// The player ship to react with public void Update(GameTime gameTime, Entity playerShip) { base.Update(gameTime); if (mResource == Pickup.Credits) { // Home in on the player Vector3 oldTraj = mVel; float speed = Math.Max(mVel.Length(), (m_Game.ShooterSpeed * 10f)); float correctionFactor = 1 - Math.Min(.925f, mPos.Z); Vector3 correctiveTraj; if (playerShip != null) { correctiveTraj = playerShip.Position - mPos; } else { correctiveTraj = mPos; } correctiveTraj.Normalize(); correctiveTraj = Vector3.Multiply(correctiveTraj, correctionFactor); oldTraj.Normalize(); oldTraj = Vector3.Multiply(oldTraj, 1 - correctionFactor); mVel = correctiveTraj + oldTraj; mVel = Vector3.Multiply(mVel, speed); } } #endregion #region Collision /// /// Awards the appropriate resources and upgrades the players' weapons /// /// The player to be awarded from picking up the collectable public override void CollideWithPlayer(Entity player) { int screenCenterX = m_Game.GraphicsDevice.Viewport.Width >> 1; int screenCenterY = m_Game.GraphicsDevice.Viewport.Height >> 1; Rectangle proj = Space3D.Project(player.X, player.Y, player.Z, this.Width, this.Height); mAlive = false; m_Game.Log(EventCode.MD_ShooterPlayerCollectibleCollision, EventCode.MD_ShooterPlayerCollectibleCollision.ToString() + " Collectible=" + mResource.ToString()); // Gather the resource // TODO: Scoring? //m_Game.Score.Gather(mResource, mValue); if (mResource == Pickup.Weapon && player is CheetahFighter) { ((CheetahFighter)player).UpgradeWeapon(); } if (m_Game.DisplayWaitTimer < 0) { StringBuilder text = new StringBuilder("+"); text.Append(mResource); m_Game.AddNewMessage( text.ToString(), null, @"Fonts\dialogfont", new Vector2(proj.X - screenCenterX, proj.Y - screenCenterY), new Vector2(0, -50f), (mResource == Pickup.Weapon ? Color.Aqua : Color.LimeGreen), 1f, 1f, true); m_Game.ResetDisplayWaitTimer(); } } #endregion #region State Modifiers /// /// Deals the specified amount of damage to this flotsam, potentially destroying it /// /// Amount of damage to deal to this collectable public override void GetHit(int damage) { mHitPoints -= damage; if (mHitPoints < 0) Destroy(); } /// /// Flotsam does not react, so this method does nothing /// /// The ship to react to /// False always public override bool React(CheetahFighter playerShip) { return false; } /// /// Destroys this collectable /// public override void Destroy() { mAlive = false; } #endregion } }