From f31fe51445e412355ef197ea87da3b07f7fc1c70 Mon Sep 17 00:00:00 2001 From: Kyle Date: Fri, 23 Apr 2010 00:57:48 +0000 Subject: [PATCH] Split human class into Melee and ranged... probably should do some sort of inheritance... later. git-svn-id: https://bd85.net/svn/cs3505_group@133 92bb83a3-7c8f-8a45-bc97-515c4e399668 --- CarFire/CarFire/CarFire/CarFire.csproj | 3 +- CarFire/CarFire/CarFire/Display.cs | 2 +- CarFire/CarFire/CarFire/Game.cs | 2 +- CarFire/CarFire/CarFire/IPlayer.cs | 4 +- CarFire/CarFire/CarFire/Melee.cs | 145 ++++++++++++++++++ CarFire/CarFire/CarFire/Projectile.cs | 5 +- .../CarFire/CarFire/{Human.cs => Ranged.cs} | 43 +++--- 7 files changed, 179 insertions(+), 25 deletions(-) create mode 100644 CarFire/CarFire/CarFire/Melee.cs rename CarFire/CarFire/CarFire/{Human.cs => Ranged.cs} (89%) diff --git a/CarFire/CarFire/CarFire/CarFire.csproj b/CarFire/CarFire/CarFire/CarFire.csproj index 0b85b76..d7f666f 100644 --- a/CarFire/CarFire/CarFire/CarFire.csproj +++ b/CarFire/CarFire/CarFire/CarFire.csproj @@ -85,13 +85,14 @@ + - + diff --git a/CarFire/CarFire/CarFire/Display.cs b/CarFire/CarFire/CarFire/Display.cs index 5b22c8c..c6a1e44 100644 --- a/CarFire/CarFire/CarFire/Display.cs +++ b/CarFire/CarFire/CarFire/Display.cs @@ -137,7 +137,7 @@ namespace CarFire else { - mGame.State.mCharacters[i].MovePlayer(timespan, mGame.State.GetKeysDown(i)); + mGame.State.mCharacters[i].UpdateInput(timespan, mGame.State.GetKeysDown(i)); } } diff --git a/CarFire/CarFire/CarFire/Game.cs b/CarFire/CarFire/CarFire/Game.cs index 4aa1cfe..3fd5ef0 100644 --- a/CarFire/CarFire/CarFire/Game.cs +++ b/CarFire/CarFire/CarFire/Game.cs @@ -384,7 +384,7 @@ namespace CarFire allCharactersSelected = false; if (State.GetKeysDown(i).Contains(Keys.Enter)) { - State.mCharacters[i] = new Human(this, "", State.Map.GetStartingPositionForPlayer(i + 1), i); + State.mCharacters[i] = new Ranged(this, "", State.Map.GetStartingPositionForPlayer(i + 1), i); State.mCharacters[i].LoadContent(mContentManager); } } diff --git a/CarFire/CarFire/CarFire/IPlayer.cs b/CarFire/CarFire/CarFire/IPlayer.cs index f90fc15..a22a9de 100644 --- a/CarFire/CarFire/CarFire/IPlayer.cs +++ b/CarFire/CarFire/CarFire/IPlayer.cs @@ -17,11 +17,13 @@ namespace CarFire public interface IPlayer : ICharacter { - void MovePlayer(TimeSpan timeSpan, List keysPressed); + void UpdateInput(TimeSpan timeSpan, List keysPressed); int Score { get; } void powerUp(int amount); void Spawn(Vector2 spawn); bool alive { get; } + void Attack(); + void UpdatePosition(TimeSpan timeSpan, List keysPressed); } public interface IMonster : ICharacter diff --git a/CarFire/CarFire/CarFire/Melee.cs b/CarFire/CarFire/CarFire/Melee.cs new file mode 100644 index 0000000..0d69287 --- /dev/null +++ b/CarFire/CarFire/CarFire/Melee.cs @@ -0,0 +1,145 @@ +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 class Melee : IPlayer + { + //The number of frames between each projectile is spawned. + const int shootCoolDown = 18; + String CharName; + Game game; + Texture2D charModel; + Texture2D projectileModel; + int health; + int damage; + int range; + int score; + + MovementManager mMotion; + bool visible; + + //Used to draw projectiles + int projectileSpeed; + int projectileCoolDown; + int mPlayerIndex; + + + public Melee(Game theGame, String Name, Point position, int playerIndex) + { + game = theGame; + CharName = Name; + health = 100; + score = 0; + visible = false; + projectileSpeed = 8; + mPlayerIndex = playerIndex; + + // Speed is the number of grid cells you can move through per second. + mMotion = new MovementManager(position, 4.0f); + } + + public void LoadContent(ContentManager contentManager) + { + charModel = contentManager.Load("cs"); //change to charModel when designed + projectileModel = contentManager.Load("projectile"); //change to a projectile model later + + } + + + public void Update(TimeSpan timeSpan) + { + } + /// + /// This method will draw a character to the screen. + /// + /// + public void Draw(SpriteBatch spriteBatch) + { + Rectangle position = game.State.Map.GetRectangleFromCoordinates(mMotion.Position); + spriteBatch.Draw(charModel, position, Color.White); + } + + public int Health { get { return health; } } + public int Score { get { return score; } } + public bool alive { get { return health > 0; } } + + public Vector2 Position { get { return mMotion.Position; } } + public Point Coordinates { get { return mMotion.Coordinates; } } + + public void causeDamageTo(int amount) + { + health -= amount; + } + + /// + /// 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); + + if (projectileCoolDown > 0) + projectileCoolDown--; + else if (projectileCoolDown == 0) + { + if (keysPressed.Contains(Keys.Space)) + { + Attack(); + } + } + } + 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); + Point destination = MovementManager.GetNeighborCell(mMotion.Coordinates, moveLeft, moveRight, moveUp, moveDown); + if (!keysPressed.Contains(Keys.LeftControl)) + { + if (game.IsCellOpen(destination)) + { + mMotion.Update(timeSpan, moveLeft, moveRight, moveUp, moveDown); + } + else + { + mMotion.Update(timeSpan); + } + } + else + { + mMotion.LockUpdate(timeSpan, moveLeft, moveRight, moveUp, moveDown); + } + } + public void Attack() + { + //melee attack + + } + public void powerUp(int amount) + { + health += amount; + } + + public void Spawn(Vector2 spawn) + { + //gridX = (int)spawn.X; + //gridY = (int)spawn.Y; + visible = true; + } + + + } +} \ No newline at end of file diff --git a/CarFire/CarFire/CarFire/Projectile.cs b/CarFire/CarFire/CarFire/Projectile.cs index 6603272..95d0d6e 100644 --- a/CarFire/CarFire/CarFire/Projectile.cs +++ b/CarFire/CarFire/CarFire/Projectile.cs @@ -43,7 +43,8 @@ namespace CarFire Texture2D _projectileModel, Vector2 _velocity, Point _position, - int characterNumber) + int characterNumber, + int _damage) { mGame = theGame; @@ -51,7 +52,7 @@ namespace CarFire projectileModel = _projectileModel; velocity = _velocity; mPosition = _position; - damage = 20; + damage = _damage; // Speed is the number of grid cells you can move through per second. mMotion = new MovementManager(mPosition, velocity.Length()); } diff --git a/CarFire/CarFire/CarFire/Human.cs b/CarFire/CarFire/CarFire/Ranged.cs similarity index 89% rename from CarFire/CarFire/CarFire/Human.cs rename to CarFire/CarFire/CarFire/Ranged.cs index 0d65d49..b859cd6 100644 --- a/CarFire/CarFire/CarFire/Human.cs +++ b/CarFire/CarFire/CarFire/Ranged.cs @@ -9,7 +9,7 @@ using Microsoft.Xna.Framework.Input; namespace CarFire { - public class Human : IPlayer + public class Ranged : IPlayer { //The number of frames between each projectile is spawned. const int shootCoolDown = 18; @@ -31,7 +31,7 @@ namespace CarFire int mPlayerIndex; - public Human(Game theGame, String Name, Point position, int playerIndex) + public Ranged(Game theGame, String Name, Point position, int playerIndex) { game = theGame; CharName = Name; @@ -85,7 +85,22 @@ namespace CarFire /// 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 MovePlayer(TimeSpan timeSpan, List keysPressed) + public void UpdateInput(TimeSpan timeSpan, List keysPressed) + { + + UpdatePosition(timeSpan, keysPressed); + + if (projectileCoolDown > 0) + projectileCoolDown--; + else if (projectileCoolDown == 0) + { + if (keysPressed.Contains(Keys.Space)) + { + Attack(); + } + } + } + public void UpdatePosition(TimeSpan timeSpan, List keysPressed) { bool moveLeft = keysPressed.Contains(Keys.Left); bool moveRight = keysPressed.Contains(Keys.Right); @@ -107,15 +122,10 @@ namespace CarFire { mMotion.LockUpdate(timeSpan, moveLeft, moveRight, moveUp, moveDown); } - - - if (projectileCoolDown > 0) - projectileCoolDown--; - else if (projectileCoolDown == 0) - { - if (keysPressed.Contains(Keys.Space)) - { - float velocityX = 0; + } + public void Attack() + { + float velocityX = 0; float velocityY = 0; int startX = Coordinates.X; int startY = Coordinates.Y; @@ -144,14 +154,9 @@ namespace CarFire toShoot *= projectileSpeed; projectileCoolDown = shootCoolDown; game.State.mDisplay.AddProjectiles(new Projectile(game, projectileModel, - toShoot, new Point(startX, startY), mPlayerIndex)); - - - } - } + toShoot, new Point(startX, startY), mPlayerIndex, damage)); + } - - public void powerUp(int amount) { health += amount; -- 2.45.2