4 using System.Collections.Generic;
7 using Microsoft.Xna.Framework;
8 using Microsoft.Xna.Framework.Content;
9 using Microsoft.Xna.Framework.Graphics;
10 using Microsoft.Xna.Framework.Input;
15 /// This class is responsible for controlling what draws to the screen when the game is running.
19 bool playerChosen = false;
20 List<Projectile> mProjectiles = new List<Projectile>();
21 //List<IPlayer> mCharacters = new List<IPlayer>();
22 IPlayer[] mCharacters = new IPlayer[4];
25 int currentCenterX = 5;
26 int currentCenterY = 5;
28 List<Keys> mLastPressedKeys = new List<Keys>();
34 mCharacters = characters;
39 /// LoadContent will be called once per game and is the place to load
40 /// all of your content.
42 public void LoadContent(ContentManager contentManager)
44 everything = contentManager.Load<Texture2D>("cs");
45 mMap = contentManager.Load<Map>("Maps/stable");
46 Map.DefaultTile = contentManager.Load<Texture2D>("default");
47 mMap.CenterCell = new Vector2(currentCenterX,currentCenterY);
48 //Debugging... Spawn eight projectiles.
51 mProjectiles.Add(new Projectile(mMap, everything, new Vector2(5,5), 10, 10, 300, 300));
52 mProjectiles.Add(new Projectile(mMap, everything, new Vector2(-5, 5), 10, 10, 300, 300));
53 mProjectiles.Add(new Projectile(mMap, everything, new Vector2(5, -5), 10, 10, 300, 300));
54 mProjectiles.Add(new Projectile(mMap, everything, new Vector2(-5, -5), 10, 10, 300, 300));
55 //Vertical and horizontal
56 mProjectiles.Add(new Projectile(mMap, everything, new Vector2(0, 5), 10, 10, 300, 300));
57 mProjectiles.Add(new Projectile(mMap, everything, new Vector2(-5, 0), 10, 10, 300, 300));
58 mProjectiles.Add(new Projectile(mMap, everything, new Vector2(5, 0), 10, 10, 300, 300));
59 mProjectiles.Add(new Projectile(mMap, everything, new Vector2(0, -5), 10, 10, 300, 300));
63 // TODO: use this.Content to load your game content here
67 /// UnloadContent will be called once per game and is the place to unload
70 public void UnloadContent()
72 // TODO: Unload any non ContentManager content here
76 /// Allows the game to run logic such as updating the world,
77 /// checking for collisions, gathering input, and playing audio.
79 /// <param name="gameTime">Provides a snapshot of timing values.</param>
80 public void Update(TimeSpan timespan, GameState state)
83 //INPUT - testing input... has to be through network later
85 KeyboardState keyState = Keyboard.GetState();
87 List<Keys> pressedKeys = new List<Keys>();
88 List<Keys> releasedKeys = new List<Keys>();
90 Keys[] pressedKeysArray = keyState.GetPressedKeys();
91 foreach (Keys k in pressedKeysArray)
93 if (!mLastPressedKeys.Contains(k)) pressedKeys.Add(k);
94 else mLastPressedKeys.Remove(k);
97 releasedKeys = mLastPressedKeys;
98 mLastPressedKeys = new List<Keys>(pressedKeysArray);
99 //Just apply input for the first player
100 mCharacters[0].MovePlayer(pressedKeys);
101 if (pressedKeys.Contains(Keys.Enter) && !releasedKeys.Contains(Keys.Enter))
103 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));
105 mMap.CenterCell = new Vector2(mCharacters[0].GridX, mCharacters[0].GridY);
108 for (int i = 0; i < 4; i++)
110 if (mCharacters[i] == null)
112 if(state.keysDown[i].Contains(Keys.Enter))
114 mCharacters[i] = (new Human(mMap, "", everything, everything, this));
115 mCharacters[i].GridX = mMap.GetStartingPositionForPlayer(i+1).X;
116 mCharacters[i].GridY = mMap.GetStartingPositionForPlayer(i+1).Y;
121 mMap.CenterCell = new Vector2(mCharacters[0].GridX, mCharacters[0].GridY);
122 mCharacters[i].MovePlayer(state.keysDown[i]);
125 for (int i = 0; i < mProjectiles.Count; i++ )
127 mProjectiles[i].Update(timespan);
128 if (!mMap.IsCellOpen(new Point(mProjectiles[i].GridX, mProjectiles[i].GridY)))
131 mProjectiles.RemoveAt(i);
135 //Check for collisons
136 for (int j = 0; j < mCharacters.Length; j++)
139 if(mCharacters[j] != null)
140 for (int i = 0; i < mProjectiles.Count; i++)
142 if (mProjectiles[i].GridX == mCharacters[j].GridX && mProjectiles[i].GridY == mCharacters[j].GridY)
144 mCharacters[j].causeDamageTo(mProjectiles[i].Damage);
145 Console.WriteLine(mCharacters[j].Health);
146 mProjectiles.RemoveAt(i);
154 /// This is called when the game should draw itself.
156 /// <param name="spriteBatch">Used to draw with</param>
157 public void Draw(SpriteBatch spriteBatch)
159 mMap.Draw(spriteBatch);
160 foreach(Projectile projectile in mProjectiles)
162 projectile.Draw(spriteBatch);
165 for(int i = 0; i < 4; i++)//IPlayer character in mCharacters)
167 if(mCharacters[i] != null)
168 mCharacters[i].Draw(spriteBatch);
172 public void AddCharacters(IPlayer player)
174 mCharacters[mCharacters.Length] = player;
175 //mCharacters.Add(player);
178 public void AddProjectiles(Projectile projectile)
180 mProjectiles.Add(projectile);