using System; using System.Collections.Generic; using System.Linq; using System.Text; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Content; using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Input; namespace CarFire { public abstract class Player : ICharacter { #region Member variables //The number of frames between each projectile is spawned. const float basePlayerSpeed = 8.0f; const int shootCoolDown = 18; String CharName; Game game; int playerHealth; int playerDamage; int score; MovementManager mMotion; List mInventory = new List(4); int mPlayerIndex; #endregion #region Public Properties public int Health { get { return playerHealth; } set{playerHealth = value;} } public int Score { get { return score; } set { score = value; } } public bool alive { get { return playerHealth > 0; } } public Game Game { get { return game; } } public MovementManager Motion { get { return mMotion; } } public int PlayerIndex { get { return mPlayerIndex; } } public bool IsCollidable { get { return true; } } public Vector2 Position { get { return mMotion.Position; } } public Point Coordinates { get { return mMotion.Coordinates; } set { mMotion.Coordinates = value; mMotion = new MovementManager(value, basePlayerSpeed); } } public char Identifier { get { return mPlayerIndex.ToString()[0]; } } public int Damage { get { return playerDamage; } set { playerDamage = value; } } public List Inventory { get { return mInventory; } } #endregion #region Public Methods public Player(Game theGame, String Name, Point position, int playerIndex, int health, int damage) { game = theGame; CharName = Name; score = 0; playerHealth = health; playerDamage = damage; mPlayerIndex = playerIndex; // Speed is the number of grid cells you can move through per second. mMotion = new MovementManager(position, basePlayerSpeed); } public void causeDamageTo(int amount) { playerHealth -= amount; } public void Update(TimeSpan timespan) { } public void Update(TimeSpan timeSpan, List keysPressed) { UpdatePosition(timeSpan, keysPressed); Attack(keysPressed); UpdateFrame(timeSpan); } /// /// Moves the current player being controlled based on a given set of key presses. /// The player can only move one grid space per movePlayer call. Thus this method /// is made to be called ever update. The player will only move if the grid space /// that is being moved to is an open space. /// /// A general list of keys that are pressed. Other keys can be included but only direction keys will be used public void UpdateInput(TimeSpan timeSpan, List keysPressed) { UpdatePosition(timeSpan, keysPressed); Attack(keysPressed); } public void UpdatePosition(TimeSpan timeSpan, List keysPressed) { bool moveLeft = keysPressed.Contains(Keys.Left); bool moveRight = keysPressed.Contains(Keys.Right); bool moveUp = keysPressed.Contains(Keys.Up); bool moveDown = keysPressed.Contains(Keys.Down); List possibleDestinations = new List(3); possibleDestinations.Add(MovementManager.GetNeighbor(mMotion.Coordinates, moveLeft, moveRight, moveUp, moveDown)); possibleDestinations.AddRange(MovementManager.GetNeighbors(mMotion.Coordinates, possibleDestinations[0])); Direction direction = Direction.None; foreach (Point destination in possibleDestinations) { if (game.IsCellOpen(destination)) { direction = MovementManager.GetDirection(mMotion.Coordinates, destination); break; } } if (direction != Direction.None && !keysPressed.Contains(Keys.LeftControl)) mMotion.Update(timeSpan, direction); else mMotion.LockUpdate(timeSpan, moveLeft, moveRight, moveUp, moveDown); } public void powerUp(int amount) { playerHealth += amount; } public void Spawn(Vector2 spawn) { } public void AddHealth(int healthBoost) { Health += healthBoost; } public void IncreaseDamage(int damageBoost) { Damage += damageBoost; } #endregion #region Abstract Methods public abstract void PlayAttackSound(); public abstract void PlayDieSound(); public abstract void LoadContent(ContentManager contentManager); /// /// This method will draw a character to the screen. /// /// public abstract void Draw(SpriteBatch spriteBatch); public abstract void Attack(List keysPressed); public abstract void UpdateFrame(TimeSpan timeSpan); #endregion } }