]> Dogcows Code - chaz/carfire/blob - CarFire/CarFire/CarFire/Display.cs
01fbbba44d946e7988c29b5cb79a339581d24fd6
[chaz/carfire] / CarFire / CarFire / CarFire / Display.cs
1 #undef SINGLE_TEST
2
3 // Define INGAME_ZOOM to allow zooming in and out with
4 // the PageUp and PageDown keys.
5 #define INGAME_ZOOM
6
7 using System;
8 using System.Collections.Generic;
9 using System.Linq;
10 using System.Text;
11 using Microsoft.Xna.Framework;
12 using Microsoft.Xna.Framework.Content;
13 using Microsoft.Xna.Framework.Graphics;
14 using Microsoft.Xna.Framework.Input;
15
16 namespace CarFire
17 {
18 /// <summary>
19 /// This class is responsible for controlling what draws to the screen when the game is running.
20 /// </summary>
21 public class Display
22 {
23 List<Projectile> mProjectiles = new List<Projectile>();
24 //List<IPlayer> mCharacters = new List<IPlayer>();
25 //IPlayer[] mCharacters = new IPlayer[4];
26 Texture2D everything;
27 Texture2D projectile1;
28 Game mGame;
29 #if SINGLE_TEST
30 List<Keys> mLastPressedKeys = new List<Keys>();
31 #endif
32 public Display(Game game)
33 {
34 mGame = game;
35 /*
36 mMap = aMap;
37 mCharacters = characters;
38 */
39 }
40
41 /// <summary>
42 /// LoadContent will be called once per game and is the place to load
43 /// all of your content.
44 /// </summary>
45 public void LoadContent(ContentManager contentManager)
46 {
47 everything = contentManager.Load<Texture2D>("cs");
48 projectile1 = contentManager.Load<Texture2D>("projectile");
49 }
50
51 /// <summary>
52 /// UnloadContent will be called once per game and is the place to unload
53 /// all content.
54 /// </summary>
55 public void UnloadContent()
56 {
57 // TODO: Unload any non ContentManager content here
58 }
59
60 /// <summary>
61 /// Allows the game to run logic such as updating the world,
62 /// checking for collisions, gathering input, and playing audio.
63 /// </summary>
64 /// <param name="gameTime">Provides a snapshot of timing values.</param>
65 public void Update(TimeSpan timespan, GameState state, int thisPlayer)
66 {
67
68 //INPUT - testing input... has to be through network later
69 #if SINGLE_TEST
70 KeyboardState keyState = Keyboard.GetState();
71
72 List<Keys> pressedKeys = new List<Keys>();
73 List<Keys> releasedKeys = new List<Keys>();
74
75 Keys[] pressedKeysArray = keyState.GetPressedKeys();
76 foreach (Keys k in pressedKeysArray)
77 {
78 if (!mLastPressedKeys.Contains(k)) pressedKeys.Add(k);
79 else mLastPressedKeys.Remove(k);
80 }
81
82 releasedKeys = mLastPressedKeys;
83 mLastPressedKeys = new List<Keys>(pressedKeysArray);
84 //Just apply input for the first player
85 mCharacters[0].MovePlayer(pressedKeys);
86 if (pressedKeys.Contains(Keys.Enter) && !releasedKeys.Contains(Keys.Enter))
87 {
88 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));
89 }
90 mMap.CenterCell = new Vector2(mCharacters[0].GridX, mCharacters[0].GridY);
91 #endif
92
93 //Handle projectiles - update and check for wall collisions
94 for (int i = 0; i < mProjectiles.Count; i++ )
95 {
96 bool removed = false;
97 if (!mGame.State.Map.IsCellOpen(new Point(mProjectiles[i].Coordinates.X, mProjectiles[i].Coordinates.Y)))
98 {
99
100 mProjectiles.RemoveAt(i);
101 removed = true;
102 i--;
103 }
104 if(!removed)
105 mProjectiles[i].Update(timespan);
106
107 }
108 //Check for collisons
109 for (int j = 0; j < mGame.State.mCharacters.Length; j++)
110 {
111
112 if(mGame.State.mCharacters[j] != null)
113 for (int i = 0; i < mProjectiles.Count; i++)
114 {
115 if (mProjectiles[i].Coordinates.X == mGame.State.mCharacters[j].Coordinates.X && mProjectiles[i].Coordinates.Y == mGame.State.mCharacters[j].Coordinates.Y)
116 {
117 mGame.State.mCharacters[j].causeDamageTo(mProjectiles[i].Damage);
118 Console.WriteLine(mGame.State.mCharacters[j].Health);
119 mProjectiles.RemoveAt(i);
120 i--;
121 }
122 }
123 }
124 //Update input for each player
125 for (int i = 0; i < mGame.State.NumberOfPlayers; i++)
126 {
127 //If player has not selected a player yet let them select one.
128 if (mGame.State.mCharacters[i] == null)
129 {
130 if (mGame.State.GetKeysDown(i).Contains(Keys.Enter))
131 {
132 //mCharacters[i] = new Human(mGame, "", everything, projectile1, this, mGame.State.Map.GetStartingPositionForPlayer(i + 1));
133 }
134 }
135 //Regular player input updates
136 else
137 {
138
139 mGame.State.mCharacters[i].UpdateInput(timespan, mGame.State.GetKeysDown(i));
140
141 }
142 }
143 if (mGame.State.mCharacters[thisPlayer] != null)
144 {
145 mGame.State.Map.CenterCell = mGame.State.mCharacters[thisPlayer].Position;
146 }
147 //Handle wall collisions of projectiles again...
148 for (int i = 0; i < mProjectiles.Count; i++)
149 {
150 if (!mGame.State.Map.IsCellOpen(new Point(mProjectiles[i].Coordinates.X, mProjectiles[i].Coordinates.Y)))
151 {
152 mProjectiles.RemoveAt(i);
153 i--;
154 }
155
156 }
157
158 #if INGAME_ZOOM
159 if (Keyboard.GetState().IsKeyDown(Keys.PageUp)) mGame.State.Map.Zoom = mGame.State.Map.Zoom + 0.5f;
160 if (Keyboard.GetState().IsKeyDown(Keys.PageDown)) mGame.State.Map.Zoom = mGame.State.Map.Zoom - 0.5f;
161 #endif
162 }
163
164 /// <summary>
165 /// This is called when the game should draw itself.
166 /// </summary>
167 /// <param name="spriteBatch">Used to draw with</param>
168 public void Draw(SpriteBatch spriteBatch)
169 {
170 mGame.State.Map.Draw(spriteBatch);
171 mGame.State.Entities.ForEach(delegate(IEntity e) { e.Draw(spriteBatch); });
172
173 foreach(Projectile projectile in mProjectiles)
174 {
175 projectile.Draw(spriteBatch);
176 }
177 for(int i = 0; i < mGame.State.NumberOfPlayers; i++)//IPlayer character in mCharacters)
178 {
179
180 if (mGame.State.mCharacters[i] != null)
181 {
182 mGame.State.mCharacters[i].Draw(spriteBatch);
183
184 }
185 }
186
187 }
188 /// <summary>
189 /// Add a projectile to the Display.
190 /// </summary>
191 /// <param name="projectile"></param>
192 public void AddProjectiles(Projectile projectile)
193 {
194 mProjectiles.Add(projectile);
195 }
196 }
197 }
198
This page took 0.037088 seconds and 3 git commands to generate.