From d0bdd76b2cfd38fe985a7493f42b5d6e0f79ac91 Mon Sep 17 00:00:00 2001 From: brady Date: Thu, 15 Apr 2010 07:29:46 +0000 Subject: [PATCH] Basic Changes to Character, Small Changes to Display as well to make it so a Player is Created and Drawn, things still need to be moved around and tweeked. Feel free to change anything git-svn-id: https://bd85.net/svn/cs3505_group@91 92bb83a3-7c8f-8a45-bc97-515c4e399668 --- CarFire/CarFire/CarFire/Display.cs | 12 ++++-- CarFire/CarFire/CarFire/Game.cs | 61 ++++++++++++++++++++++++++++++ CarFire/CarFire/CarFire/Human.cs | 11 ++++-- CarFire/CarFire/CarFire/IPlayer.cs | 6 ++- 4 files changed, 81 insertions(+), 9 deletions(-) diff --git a/CarFire/CarFire/CarFire/Display.cs b/CarFire/CarFire/CarFire/Display.cs index 9bfefef..ed6ab45 100644 --- a/CarFire/CarFire/CarFire/Display.cs +++ b/CarFire/CarFire/CarFire/Display.cs @@ -15,7 +15,7 @@ namespace CarFire public class Display { List mProjectiles = new List(); - List mCharacters = new List(); + List mCharacters = new List(); Map mMap; int currentCenterX = 5; int currentCenterY = 5; @@ -49,6 +49,7 @@ namespace CarFire mProjectiles.Add(new Projectile(mMap, everything, new Vector2(5, 0), 10, 10, 300, 300)); mProjectiles.Add(new Projectile(mMap, everything, new Vector2(0, -5), 10, 10, 300, 300)); + // TODO: use this.Content to load your game content here } @@ -89,7 +90,7 @@ namespace CarFire //Debug - not sure if you can remove while doing for each //Alternative - while loop, and decrement projectile counter if projectile is removed. mProjectiles.Remove(mProjectiles[i]); - mCharacters[j].Health -= mProjectiles[i].Damage; + mCharacters[j].causeDamageTo(mProjectiles[i].Damage); } } } @@ -110,11 +111,16 @@ namespace CarFire projectile.Draw(spriteBatch); } - foreach(Character character in mCharacters) + foreach(IPlayer character in mCharacters) { character.Draw(spriteBatch); } } + + public void AddCharacters(IPlayer player) + { + mCharacters.Add(player); + } } } diff --git a/CarFire/CarFire/CarFire/Game.cs b/CarFire/CarFire/CarFire/Game.cs index d213f2e..a2b6caf 100644 --- a/CarFire/CarFire/CarFire/Game.cs +++ b/CarFire/CarFire/CarFire/Game.cs @@ -9,27 +9,76 @@ using Microsoft.Xna.Framework.Input; namespace CarFire { + //code from Prof Jensen's TestHarness + // This class encapsulates inputs from the players. + public class NextInputs + { + public List[] keysPressed; + public List[] keysReleased; + public int[] mouseLocationX; + public int[] mouseLocationY; + public bool[] mouseLocationChanged; + public bool[] mousePressed; + public bool[] mousePressedChanged; + + public NextInputs() + { + keysPressed = new List[4]; + keysReleased = new List[4]; + mouseLocationX = new int[4]; + mouseLocationY = new int[4]; + mouseLocationChanged = new bool[4]; + mousePressed = new bool[4]; + mousePressedChanged = new bool[4]; + for (int i = 0; i < 4; i++) + keysPressed[i] = new List(); + for (int i = 0; i < 4; i++) + keysReleased[i] = new List(); + } + } + public class Game : IDeterministicGame { #region IDeterministicGame Members List mPlayers; + NextInputs inputs; + Object[] playerIdentifiers; Display mDisplay; Map mMap; public Game() { mDisplay = new Display(); + mPlayers = new List(); } public void LoadContent(ContentManager contentManager) { //Texture2D everything = contentManager.Load("default"); mDisplay.LoadContent(contentManager); + int currentCenterX = 5; //Creates a map like the one in Display + int currentCenterY = 5; + mMap = contentManager.Load("Maps/stable"); + Map.DefaultTile = contentManager.Load("default"); + mMap.CenterCell = new Vector2(currentCenterX, currentCenterY); + + Human player = new Human(mMap, ""); + player.LoadContent(contentManager); + mPlayers.Add(player); + mDisplay.AddCharacters(player); } public void UnloadContent() { } + private int idPlayer(Object playerIdentifier) + { + for (int i = 0; i < playerIdentifiers.Length; i++) + if (playerIdentifiers[i] == playerIdentifier) + return i; + throw new Exception("Illegal player identifier" + playerIdentifier); + } + public Vector2 PreferredScreenSize { get { return new Vector2(800, 600); } @@ -47,6 +96,10 @@ namespace CarFire public void ResetGame(object[] playerIdentifiers, object thisPlayer) { + foreach (IPlayer player in mPlayers) + { + player.Spawn(mMap.CenterCell); + } } public long CurrentFrameNumber @@ -61,6 +114,14 @@ namespace CarFire public void ApplyKeyInput(object playerIdentifier, Keys key, bool isKeyPressed) { + //code from Prof Jensen's TestHarness + int player = idPlayer(playerIdentifier); + + if (isKeyPressed && !inputs.keysPressed[player].Contains(key)) + inputs.keysPressed[player].Add(key); + + if (!isKeyPressed && !inputs.keysReleased[player].Contains(key)) + inputs.keysReleased[player].Add(key); } public void ApplyMouseLocationInput(object playerIdentifier, int x, int y) diff --git a/CarFire/CarFire/CarFire/Human.cs b/CarFire/CarFire/CarFire/Human.cs index e0f6cc5..acb0d3b 100644 --- a/CarFire/CarFire/CarFire/Human.cs +++ b/CarFire/CarFire/CarFire/Human.cs @@ -38,7 +38,7 @@ namespace CarFire visible = false; } - public void LoadContent(ContentManager contentManager, GraphicsDeviceManager graphics) + public void LoadContent(ContentManager contentManager) { charModel = contentManager.Load("deselectBox"); //change to charModel when designed } @@ -56,9 +56,12 @@ namespace CarFire public long Draw(SpriteBatch spriteBatch) { + spriteBatch.Draw(charModel, theMap.GetRectangleFromCoordinates(gridX, gridY), Color.White); return 0; } + public int GridX { get { return gridX; } set { gridX = value; } } + public int GridY { get { return gridY; } set { gridY = value; } } public int Health { get { return health; } } public int Score { get { return score; } } public bool alive { get { return health > 0; } } @@ -130,10 +133,10 @@ namespace CarFire health += amount; } - public void Spawn(Point mapPoint) + public void Spawn(Vector2 spawn) { - gridX = mapPoint.X; - gridY = mapPoint.Y; + gridX = (int)spawn.X; + gridY = (int)spawn.Y; visible = true; } diff --git a/CarFire/CarFire/CarFire/IPlayer.cs b/CarFire/CarFire/CarFire/IPlayer.cs index 7eb7611..3b98301 100644 --- a/CarFire/CarFire/CarFire/IPlayer.cs +++ b/CarFire/CarFire/CarFire/IPlayer.cs @@ -11,12 +11,14 @@ namespace CarFire { public interface ICharacter { - void LoadContent(ContentManager contentManager, GraphicsDeviceManager graphics); + void LoadContent(ContentManager contentManager); void UnloadContent(); long Update(GameTime gameTime, NetworkManager networkGame); long Draw(SpriteBatch spriteBatch); int Health { get; } void causeDamageTo(int amount); + int GridX { get; set; } + int GridY { get; set; } } public interface IPlayer : ICharacter @@ -24,7 +26,7 @@ namespace CarFire void MovePlayer(List keysPressed); int Score { get; } void powerUp(int amount); - void Spawn(Point mapPoint); + void Spawn(Vector2 spawn); bool alive { get; } } -- 2.45.2