#define SINGLE_TEST 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 { /// /// This class is responsible for controlling what draws to the screen when the game is running. /// public class Display { List mProjectiles = new List(); List mCharacters = new List(); Texture2D everything; Map mMap; int currentCenterX = 5; int currentCenterY = 5; #if SINGLE_TEST List mLastPressedKeys = new List(); #endif public Display() { /* mMap = aMap; mCharacters = characters; */ } /// /// LoadContent will be called once per game and is the place to load /// all of your content. /// public void LoadContent(ContentManager contentManager) { everything = contentManager.Load("cs"); mMap = contentManager.Load("Maps/stable"); Map.DefaultTile = contentManager.Load("default"); mMap.CenterCell = new Vector2(currentCenterX,currentCenterY); //Debugging... Spawn eight projectiles. //Diagonals mProjectiles.Add(new Projectile(mMap, everything, new Vector2(5,5), 10, 10, 300, 300)); mProjectiles.Add(new Projectile(mMap, everything, new Vector2(-5, 5), 10, 10, 300, 300)); mProjectiles.Add(new Projectile(mMap, everything, new Vector2(5, -5), 10, 10, 300, 300)); mProjectiles.Add(new Projectile(mMap, everything, new Vector2(-5, -5), 10, 10, 300, 300)); //Vertical and horizontal mProjectiles.Add(new Projectile(mMap, everything, new Vector2(0, 5), 10, 10, 300, 300)); mProjectiles.Add(new Projectile(mMap, everything, new Vector2(-5, 0), 10, 10, 300, 300)); 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 } /// /// UnloadContent will be called once per game and is the place to unload /// all content. /// public void UnloadContent() { // TODO: Unload any non ContentManager content here } /// /// Allows the game to run logic such as updating the world, /// checking for collisions, gathering input, and playing audio. /// /// Provides a snapshot of timing values. public void Update(TimeSpan timespan) { //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 for (int i = 0; i < mProjectiles.Count; i++ ) { mProjectiles[i].Update(timespan); if (!mMap.IsCellOpen(new Point(mProjectiles[i].GridX, mProjectiles[i].GridY))) { mProjectiles.RemoveAt(i); i--; } } //Check for collisons for (int j = 0; j < mCharacters.Count; j++) { for (int i = 0; i < mProjectiles.Count; i++) { if (mProjectiles[i].GridX == mCharacters[j].GridX && mProjectiles[i].GridY == mCharacters[j].GridY) { mCharacters[j].causeDamageTo(mProjectiles[i].Damage); Console.WriteLine(mCharacters[j].Health); mProjectiles.RemoveAt(i); i--; } } } } /// /// This is called when the game should draw itself. /// /// Used to draw with public void Draw(SpriteBatch spriteBatch) { mMap.Draw(spriteBatch); foreach(Projectile projectile in mProjectiles) { //Debug - follow a projectile to make sure following is working. if (mProjectiles.IndexOf(projectile) == 6) mMap.CenterCell = new Vector2(projectile.GridX, projectile.GridY); projectile.Draw(spriteBatch); } foreach(IPlayer character in mCharacters) { character.Draw(spriteBatch); } } public void AddCharacters(IPlayer player) { mCharacters.Add(player); } } }