From: Kyle Date: Fri, 23 Apr 2010 03:33:30 +0000 (+0000) Subject: Seperated GameLogic from the Display X-Git-Url: https://git.brokenzipper.com/gitweb?a=commitdiff_plain;h=594f1f722dc8e405dff12baad8c2ef5481fa3fa7;p=chaz%2Fcarfire Seperated GameLogic from the Display git-svn-id: https://bd85.net/svn/cs3505_group@137 92bb83a3-7c8f-8a45-bc97-515c4e399668 --- diff --git a/CarFire/CarFire/CarFire/CarFire.csproj b/CarFire/CarFire/CarFire/CarFire.csproj index b067b27..4353712 100644 --- a/CarFire/CarFire/CarFire/CarFire.csproj +++ b/CarFire/CarFire/CarFire/CarFire.csproj @@ -84,6 +84,7 @@ + diff --git a/CarFire/CarFire/CarFire/Display.cs b/CarFire/CarFire/CarFire/Display.cs index 01fbbba..c6ae062 100644 --- a/CarFire/CarFire/CarFire/Display.cs +++ b/CarFire/CarFire/CarFire/Display.cs @@ -20,18 +20,17 @@ namespace CarFire /// public class Display { - List mProjectiles = new List(); - //List mCharacters = new List(); - //IPlayer[] mCharacters = new IPlayer[4]; Texture2D everything; Texture2D projectile1; Game mGame; + GameLogic mGameLogic; #if SINGLE_TEST List mLastPressedKeys = new List(); #endif public Display(Game game) { mGame = game; + mGameLogic = new GameLogic(game); /* mMap = aMap; mCharacters = characters; @@ -62,98 +61,9 @@ namespace CarFire /// checking for collisions, gathering input, and playing audio. /// /// Provides a snapshot of timing values. - public void Update(TimeSpan timespan, GameState state, int thisPlayer) + public void Update(TimeSpan timeSpan,int thisPlayer) { - - //INPUT - testing input... has to be through network later -#if SINGLE_TEST - KeyboardState keyState = Keyboard.GetState(); - - List pressedKeys = new List(); - List releasedKeys = new List(); - - Keys[] pressedKeysArray = keyState.GetPressedKeys(); - foreach (Keys k in pressedKeysArray) - { - if (!mLastPressedKeys.Contains(k)) pressedKeys.Add(k); - else mLastPressedKeys.Remove(k); - } - - releasedKeys = mLastPressedKeys; - mLastPressedKeys = new List(pressedKeysArray); - //Just apply input for the first player - mCharacters[0].MovePlayer(pressedKeys); - if (pressedKeys.Contains(Keys.Enter) && !releasedKeys.Contains(Keys.Enter)) - { - mProjectiles.Add(new Projectile(mMap, everything, new Vector2(-5,0), mCharacters[0].GridX +1, mCharacters[0].GridY+1, (int)mCharacters[0].GridX*(int)Map.PixelsToUnitSquares,(int)mCharacters[0].GridY*(int)Map.PixelsToUnitSquares)); - } - mMap.CenterCell = new Vector2(mCharacters[0].GridX, mCharacters[0].GridY); -#endif - - //Handle projectiles - update and check for wall collisions - for (int i = 0; i < mProjectiles.Count; i++ ) - { - bool removed = false; - if (!mGame.State.Map.IsCellOpen(new Point(mProjectiles[i].Coordinates.X, mProjectiles[i].Coordinates.Y))) - { - - mProjectiles.RemoveAt(i); - removed = true; - i--; - } - if(!removed) - mProjectiles[i].Update(timespan); - - } - //Check for collisons - for (int j = 0; j < mGame.State.mCharacters.Length; j++) - { - - if(mGame.State.mCharacters[j] != null) - for (int i = 0; i < mProjectiles.Count; i++) - { - if (mProjectiles[i].Coordinates.X == mGame.State.mCharacters[j].Coordinates.X && mProjectiles[i].Coordinates.Y == mGame.State.mCharacters[j].Coordinates.Y) - { - mGame.State.mCharacters[j].causeDamageTo(mProjectiles[i].Damage); - Console.WriteLine(mGame.State.mCharacters[j].Health); - mProjectiles.RemoveAt(i); - i--; - } - } - } - //Update input for each player - for (int i = 0; i < mGame.State.NumberOfPlayers; i++) - { - //If player has not selected a player yet let them select one. - if (mGame.State.mCharacters[i] == null) - { - if (mGame.State.GetKeysDown(i).Contains(Keys.Enter)) - { - //mCharacters[i] = new Human(mGame, "", everything, projectile1, this, mGame.State.Map.GetStartingPositionForPlayer(i + 1)); - } - } - //Regular player input updates - else - { - - mGame.State.mCharacters[i].UpdateInput(timespan, mGame.State.GetKeysDown(i)); - - } - } - if (mGame.State.mCharacters[thisPlayer] != null) - { - mGame.State.Map.CenterCell = mGame.State.mCharacters[thisPlayer].Position; - } - //Handle wall collisions of projectiles again... - for (int i = 0; i < mProjectiles.Count; i++) - { - if (!mGame.State.Map.IsCellOpen(new Point(mProjectiles[i].Coordinates.X, mProjectiles[i].Coordinates.Y))) - { - mProjectiles.RemoveAt(i); - i--; - } - - } + mGameLogic.Update(timeSpan, thisPlayer); #if INGAME_ZOOM if (Keyboard.GetState().IsKeyDown(Keys.PageUp)) mGame.State.Map.Zoom = mGame.State.Map.Zoom + 0.5f; @@ -170,7 +80,7 @@ namespace CarFire mGame.State.Map.Draw(spriteBatch); mGame.State.Entities.ForEach(delegate(IEntity e) { e.Draw(spriteBatch); }); - foreach(Projectile projectile in mProjectiles) + foreach(Projectile projectile in mGame.State.mProjectiles) { projectile.Draw(spriteBatch); } @@ -185,14 +95,7 @@ namespace CarFire } } - /// - /// Add a projectile to the Display. - /// - /// - public void AddProjectiles(Projectile projectile) - { - mProjectiles.Add(projectile); - } + } } diff --git a/CarFire/CarFire/CarFire/Game.cs b/CarFire/CarFire/CarFire/Game.cs index be18be7..937cd4d 100644 --- a/CarFire/CarFire/CarFire/Game.cs +++ b/CarFire/CarFire/CarFire/Game.cs @@ -24,6 +24,7 @@ namespace CarFire public Map Map; public List Entities = new List(); + public List mProjectiles = new List(); public Player[] mCharacters = new Player[4]; public Display mDisplay; @@ -380,7 +381,7 @@ namespace CarFire public long Update(TimeSpan elapsedTime) { State.AdvanceFrame(mInputs, elapsedTime.Milliseconds); // Apply the inputs, advance game state. - State.mDisplay.Update(elapsedTime, State, mThisPlayerID); + State.mDisplay.Update(elapsedTime, mThisPlayerID); State.Entities.ForEach(delegate(IEntity e) { e.Update(elapsedTime); }); mInputs = new NextInputs(State.NumberOfPlayers); // Start with inputs cleared on the next frame. //mDisplay.Update(elapsedTime); diff --git a/CarFire/CarFire/CarFire/GameLogic.cs b/CarFire/CarFire/CarFire/GameLogic.cs new file mode 100644 index 0000000..6f859d0 --- /dev/null +++ b/CarFire/CarFire/CarFire/GameLogic.cs @@ -0,0 +1,87 @@ +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 +{ + class GameLogic + { + Game mGame; + public GameLogic(Game game) + { + mGame = game; + } + public void Update(TimeSpan timespan, int thisPlayer) + { + //Handle projectiles - update and check for wall collisions + for (int i = 0; i < mGame.State.mProjectiles.Count; i++) + { + bool removed = false; + if (!mGame.State.Map.IsCellOpen(new Point(mGame.State.mProjectiles[i].Coordinates.X, mGame.State.mProjectiles[i].Coordinates.Y))) + { + + mGame.State.mProjectiles.RemoveAt(i); + removed = true; + i--; + } + if (!removed) + mGame.State.mProjectiles[i].Update(timespan); + + } + //Check for collisons + for (int j = 0; j < mGame.State.mCharacters.Length; j++) + { + + if (mGame.State.mCharacters[j] != null) + for (int i = 0; i < mGame.State.mProjectiles.Count; i++) + { + if (mGame.State.mProjectiles[i].Coordinates.X == mGame.State.mCharacters[j].Coordinates.X && mGame.State.mProjectiles[i].Coordinates.Y == mGame.State.mCharacters[j].Coordinates.Y) + { + mGame.State.mCharacters[j].causeDamageTo(mGame.State.mProjectiles[i].Damage); + Console.WriteLine(mGame.State.mCharacters[j].Health); + mGame.State.mProjectiles.RemoveAt(i); + i--; + } + } + } + //Update input for each player + for (int i = 0; i < mGame.State.NumberOfPlayers; i++) + { + //If player has not selected a player yet let them select one. + if (mGame.State.mCharacters[i] == null) + { + if (mGame.State.GetKeysDown(i).Contains(Keys.Enter)) + { + //mCharacters[i] = new Human(mGame, "", everything, projectile1, this, mGame.State.Map.GetStartingPositionForPlayer(i + 1)); + } + } + //Regular player input updates + else + { + + mGame.State.mCharacters[i].UpdateInput(timespan, mGame.State.GetKeysDown(i)); + + } + } + if (mGame.State.mCharacters[thisPlayer] != null) + { + mGame.State.Map.CenterCell = mGame.State.mCharacters[thisPlayer].Position; + } + //Handle wall collisions of projectiles again... + for (int i = 0; i < mGame.State.mProjectiles.Count; i++) + { + if (!mGame.State.Map.IsCellOpen(new Point(mGame.State.mProjectiles[i].Coordinates.X, mGame.State.mProjectiles[i].Coordinates.Y))) + { + mGame.State.mProjectiles.RemoveAt(i); + i--; + } + + } + } + } +} diff --git a/CarFire/CarFire/CarFire/Ranged.cs b/CarFire/CarFire/CarFire/Ranged.cs index 02593ab..b1203dd 100644 --- a/CarFire/CarFire/CarFire/Ranged.cs +++ b/CarFire/CarFire/CarFire/Ranged.cs @@ -85,7 +85,7 @@ namespace CarFire toShoot.Normalize(); toShoot *= projectileSpeed; projectileCoolDown = shootCoolDown; - Game.State.mDisplay.AddProjectiles(new Projectile(Game, projectileModel, + Game.State.mProjectiles.Add(new Projectile(Game, projectileModel, toShoot, new Point(startX, startY), PlayerIndex, Damage)); } }