]> Dogcows Code - chaz/carfire/blob - CarFire/CarFire/CarFire/Game.cs
Incorporated networking into the game
[chaz/carfire] / CarFire / CarFire / CarFire / Game.cs
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using Microsoft.Xna.Framework;
6 using Microsoft.Xna.Framework.Content;
7 using Microsoft.Xna.Framework.Graphics;
8 using Microsoft.Xna.Framework.Input;
9
10 namespace CarFire
11 {
12 // Everything in objects built from this class represent the critical game state
13 public class GameState
14 {
15 public long frameNumber;
16
17 private long checksum;
18 public long Checksum { get { return checksum; } }
19
20 public bool[] isGameOver;
21 public bool[] isTerminated;
22
23 // Since this is not a game, I'll just keep track of the user inputs as the game state.
24
25 public int[] mouseLocationX;
26 public int[] mouseLocationY;
27 public bool[] mouseButton;
28 public List<Keys>[] keysDown;
29 public int[] keypressCount;
30
31 public long elapsedTime;
32
33 /* Constructor */
34 public GameState()
35 {
36 frameNumber = 0;
37 checksum = 0;
38
39 isGameOver = new bool[4];
40 isTerminated = new bool[4];
41
42 mouseLocationX = new int[4];
43 mouseLocationY = new int[4];
44 mouseButton = new bool[4];
45 keysDown = new List<Keys>[4];
46 for (int i = 0; i < 4; i++)
47 keysDown[i] = new List<Keys>();
48 keypressCount = new int[4];
49
50 elapsedTime = 0;
51
52 checksum = 0;
53 }
54
55 /* The game engine! */
56 public void advanceFrame(NextInputs inputs, long milliseconds)
57 {
58 // Advance frame number
59 frameNumber++;
60
61 // Advance game - for the test harness, just record statistics and input states.
62
63 elapsedTime += milliseconds;
64
65 for (int player = 0; player < 4; player++)
66 {
67
68 //if (isGameOver[player])
69 // continue;
70
71 if (inputs.mousePressedChanged[player])
72 mouseButton[player] = inputs.mousePressed[player];
73
74 if (inputs.mouseLocationChanged[player])
75 {
76 mouseLocationX[player] = inputs.mouseLocationX[player];
77 mouseLocationY[player] = inputs.mouseLocationY[player];
78 }
79
80 foreach (Keys k in inputs.keysPressed[player])
81 if (!keysDown[player].Contains(k))
82 {
83 keysDown[player].Add(k);
84 keypressCount[player]++;
85 }
86
87 foreach (Keys k in inputs.keysReleased[player])
88 keysDown[player].Remove(k);
89
90 // If the mouse was pressed for a certain player, activate game over or terminated states as appropriate
91
92 if (inputs.mousePressed[player])
93 for (int p = 0; p < 4; p++)
94 {
95 int x = 200 * p + 10;
96 int y = 220;
97
98 if (mouseLocationX[player] >= x && mouseLocationY[player] >= y &&
99 mouseLocationX[player] < x + 25 && mouseLocationY[player] < y + 25)
100 {
101 isGameOver[p] = true;
102 }
103 y += 25;
104 if (mouseLocationX[player] >= x && mouseLocationY[player] >= y &&
105 mouseLocationX[player] < x + 25 && mouseLocationY[player] < y + 25)
106 {
107 isGameOver[p] = true;
108 isTerminated[p] = true;
109 }
110 }
111
112
113 }
114
115 // Advance the checksum.
116
117 computeChecksum();
118
119 }
120
121 /* Just hash the values */
122 private long computeChecksum()
123 {
124 checksum += frameNumber;
125 for (int i = 0; i < 4; i++)
126 {
127 checksum = checksum + keypressCount[i];
128 checksum = checksum * 3 + (isGameOver[i] ? 1 : 2);
129 checksum = checksum * 3 + (isTerminated[i] ? 1 : 2);
130 foreach (Keys k in keysDown[i])
131 checksum = checksum * 257 + (int)k;
132 checksum = checksum * 25789 + mouseLocationX[i] * 259 + mouseLocationY[i] + 375;
133 checksum = checksum * 3 + (mouseButton[i] ? 1 : 2);
134
135 }
136 checksum += elapsedTime;
137
138 return checksum;
139 }
140 }
141 //code from Prof Jensen's TestHarness
142 // This class encapsulates inputs from the players.
143 public class NextInputs
144 {
145 public List<Keys>[] keysPressed;
146 public List<Keys>[] keysReleased;
147 public int[] mouseLocationX;
148 public int[] mouseLocationY;
149 public bool[] mouseLocationChanged;
150 public bool[] mousePressed;
151 public bool[] mousePressedChanged;
152
153 public NextInputs()
154 {
155 keysPressed = new List<Keys>[4];
156 keysReleased = new List<Keys>[4];
157 mouseLocationX = new int[4];
158 mouseLocationY = new int[4];
159 mouseLocationChanged = new bool[4];
160 mousePressed = new bool[4];
161 mousePressedChanged = new bool[4];
162 for (int i = 0; i < 4; i++)
163 keysPressed[i] = new List<Keys>();
164 for (int i = 0; i < 4; i++)
165 keysReleased[i] = new List<Keys>();
166 }
167
168 }
169
170 public class Game : IDeterministicGame
171 {
172 #region IDeterministicGame Members
173 List<IPlayer> mPlayers;
174 NextInputs inputs;
175 Object[] playerIdentifiers;
176 Display mDisplay;
177 Map mMap;
178
179 GameState state;
180 int thisPlayerID;
181
182 public Game()
183 {
184 mDisplay = new Display();
185 mPlayers = new List<IPlayer>();
186 playerIdentifiers = new Object[4];
187 }
188 public void LoadContent(ContentManager contentManager)
189 {
190 //Texture2D everything = contentManager.Load<Texture2D>("default");
191 mDisplay.LoadContent(contentManager);
192 int currentCenterX = 5; //Creates a map like the one in Display
193 int currentCenterY = 5;
194 mMap = contentManager.Load<Map>("Maps/stable");
195 Map.DefaultTile = contentManager.Load<Texture2D>("default");
196 mMap.CenterCell = new Vector2(currentCenterX, currentCenterY);
197
198 }
199
200 public void UnloadContent()
201 {
202 }
203
204 private int idPlayer(Object playerIdentifier)
205 {
206 for (int i = 0; i < playerIdentifiers.Length; i++)
207 if (playerIdentifiers[i] == playerIdentifier)
208 return i;
209 throw new Exception("Illegal player identifier" + playerIdentifier);
210 }
211
212 public Vector2 PreferredScreenSize
213 {
214 get { return new Vector2(800, 600); }
215 }
216
217 public int MinimumSupportedPlayers
218 {
219 get { return 1; }
220 }
221
222 public int MaximumSupportedPlayers
223 {
224 get { return 4; }
225 }
226
227 public void ResetGame(object[] PlayerIdentifiers, object thisPlayer)
228 {
229 // Now the test harness will at least run with less than 4 players...
230 int numPlayers = PlayerIdentifiers.Count();
231 for (int i = 0; i < numPlayers; i++)
232 this.playerIdentifiers[i] = PlayerIdentifiers[i];
233
234 // Create new game state and inputs objects.
235
236 state = new GameState();
237 inputs = new NextInputs();
238
239 // Record 'this' player.
240
241 this.thisPlayerID = idPlayer(thisPlayer);
242 /*
243 mPlayers.Clear();
244 for (int i = 0; i < PlayerIdentifiers.Length; i++)
245 {
246 Human player = new Human(mMap, "");
247 mPlayers.Add(player);
248 mDisplay.AddCharacters(player);
249 mPlayers.Add(player);
250 mDisplay.AddCharacters(player);
251 }
252 this.playerIdentifiers = PlayerIdentifiers;
253 for (int i = 0; i < mPlayers.Count; i++)
254 {
255 Point starting = mMap.GetStartingPositionForPlayer(i + 1);
256 mPlayers[i].Spawn(new Vector2(starting.X, starting.Y));
257 }
258 */
259
260 }
261
262 public long CurrentFrameNumber
263 {
264 get { return state.frameNumber; }
265 }
266
267 public long CurrentChecksum
268 {
269 get { return 0; }
270 }
271
272 public void ApplyKeyInput(object playerIdentifier, Keys key, bool isKeyPressed)
273 {
274 //code from Prof Jensen's TestHarness
275 int player = idPlayer(playerIdentifier);
276
277 if (isKeyPressed && !inputs.keysPressed[player].Contains(key))
278 inputs.keysPressed[player].Add(key);
279
280 if (!isKeyPressed && !inputs.keysReleased[player].Contains(key))
281 inputs.keysReleased[player].Add(key);
282
283 }
284
285 public void ApplyMouseLocationInput(object playerIdentifier, int x, int y)
286 {
287
288 }
289
290 public void ApplyMouseButtonInput(object playerIdentifier, bool isButtonPressed)
291 {
292
293 }
294
295 public bool IsGameOver(object playerIdentifier)
296 {
297 return false;
298 }
299
300 public bool IsTerminated(object playerIdentifier)
301 {
302 return false;
303 }
304
305 public long Update(TimeSpan elapsedTime)
306 {
307 state.advanceFrame(inputs, elapsedTime.Milliseconds); // Apply the inputs, advance game state.
308 mDisplay.Update(elapsedTime, state);
309 inputs = new NextInputs(); // Start with inputs cleared on the next frame.
310 //mDisplay.Update(elapsedTime);
311 return state.frameNumber;
312
313 }
314
315 public long Draw(SpriteBatch spriteBatch)
316 {
317 mDisplay.Draw(spriteBatch);
318 return CurrentFrameNumber;
319 }
320
321 #endregion
322 }
323 }
This page took 0.043517 seconds and 5 git commands to generate.