]> Dogcows Code - chaz/carfire/blob - Project06/CS 3505 Project 06/CS 3505 Project 06/NetworkGame.cs
bacfb19074d77dfc8686f2395fc1c162d20dbcb1
[chaz/carfire] / Project06 / CS 3505 Project 06 / CS 3505 Project 06 / NetworkGame.cs
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using Microsoft.Xna.Framework.Net;
6 using System.Diagnostics;
7 using Microsoft.Xna.Framework.GamerServices;
8 using Microsoft.Xna.Framework.Graphics;
9 using Microsoft.Xna.Framework;
10 using Microsoft.Xna.Framework.Input;
11
12 namespace CS_3505_Project_06
13 {
14 public class NetworkGame
15 {
16 NetworkSession mNetworkSession;
17
18 ILobby mLobby;
19 IDeterministicGame mGame;
20
21 TimeSpan mTargetTimeSpan = new TimeSpan(166666);
22 public TimeSpan TargetTimeSpan
23 {
24 get
25 {
26 return mTargetTimeSpan;
27 }
28 }
29
30 List<Keys> lastPressedKeys;
31 bool lastButtonPressed;
32
33 Object[] playerIdentifiers = { "One", "Two", "Three", "Four" }; // Any objects will do, strings are easy to debug.
34
35 // For debugging
36
37 Object activePlayer;
38 bool paused;
39 long lastAutoPause;
40
41 public SpriteFont font;
42
43
44 public NetworkGame(ILobby lobby, IDeterministicGame game)
45 {
46 Debug.Assert(lobby != null && game != null);
47
48 mLobby = lobby;
49 mGame = game;
50
51 // Begin: Test harness stuff
52 lastPressedKeys = new List<Keys>();
53 activePlayer = playerIdentifiers[0];
54 paused = false;
55
56 // Reset the game - indicate that player #1 (player 0) owns this instance of the game.
57
58 mGame.ResetGame(playerIdentifiers, playerIdentifiers[0]);
59 }
60
61
62 public LocalNetworkGamer LocalGamer
63 {
64 get
65 {
66 return mNetworkSession.LocalGamers[0];
67 }
68 }
69
70 // I added this as I needed a way to display all gamers not just the first gamer
71 // -Brady
72 public GamerCollection<NetworkGamer> NetworkGamers
73 {
74 get
75 {
76 return mNetworkSession.AllGamers;
77 }
78 }
79
80
81 public NetworkSession CreateSession()
82 {
83 return CreateSession(mGame.MaximumSupportedPlayers);
84 }
85
86 public NetworkSession CreateSession(int maxGamers)
87 {
88 Debug.Assert(mNetworkSession == null);
89
90 mNetworkSession = NetworkSession.Create(NetworkSessionType.SystemLink, 1, maxGamers);
91 mNetworkSession.AllowHostMigration = true;
92 mNetworkSession.AllowJoinInProgress = false;
93
94 return mNetworkSession;
95 }
96
97 // added so I can test if sessionExists and thus be able to call things on NetworkGame safely
98 // -Brady
99 public bool sessionExists()
100 {
101 return mNetworkSession != null;
102 }
103
104 public AvailableNetworkSessionCollection FindSessions()
105 {
106 return NetworkSession.Find(NetworkSessionType.SystemLink, 1, new NetworkSessionProperties());
107 }
108
109 public NetworkSession JoinSession(AvailableNetworkSession availableSession)
110 {
111 Debug.Assert(mNetworkSession == null);
112
113 mNetworkSession = NetworkSession.Join(availableSession);
114
115 return mNetworkSession;
116 }
117
118 // added to begin the game. I made the LobbyGUI make sure that only the host will call it when everyone is ready.
119 // This is already taken care of in the update method below. But it may be nice to allow the host to signal the start
120 // rather then having it start automatically. Just a suggestion.
121 // -Brady
122 public void StartGame()
123 {
124 mNetworkSession.StartGame();
125 mNetworkSession.ResetReady();
126 }
127
128 public void LeaveSession()
129 {
130 Debug.Assert(mNetworkSession != null);
131
132 mNetworkSession.Dispose();
133 mNetworkSession = null;
134 }
135
136
137 public void SimulateBadNetwork()
138 {
139 Debug.Assert(mNetworkSession != null);
140
141 mNetworkSession.SimulatedLatency = new TimeSpan(0, 0, 0, 0, 200);
142 mNetworkSession.SimulatedPacketLoss = 0.1f;
143 }
144
145
146 public void Update(GameTime gameTime)
147 {
148 if (mNetworkSession == null)
149 {
150 mLobby.Update(gameTime, this);
151 }
152 else
153 {
154 mNetworkSession.Update();
155
156 if (mNetworkSession.SessionState == NetworkSessionState.Lobby)
157 {
158 if (mNetworkSession.IsHost &&
159 mNetworkSession.AllGamers.Count >= mGame.MinimumSupportedPlayers &&
160 mNetworkSession.IsEveryoneReady)
161 {
162 mNetworkSession.StartGame();
163 mNetworkSession.ResetReady();
164 }
165 else
166 {
167 mLobby.Update(gameTime, this);
168 }
169 }
170 else if (mNetworkSession.SessionState == NetworkSessionState.Playing)
171 {
172 // TODO: in-game update stuff
173 UpdateTestHarness(gameTime);
174
175 mGame.Update(mTargetTimeSpan);
176 }
177 }
178 }
179
180 public void Draw(GameTime gameTime, SpriteBatch spriteBatch)
181 {
182 if (mNetworkSession != null)
183 {
184 if (mNetworkSession.SessionState != NetworkSessionState.Playing)
185 DrawTestHarness(gameTime, spriteBatch);
186 else
187 mLobby.Draw(spriteBatch);
188 }
189 else
190 mLobby.Draw(spriteBatch);
191
192 }
193
194
195
196
197 void UpdateTestHarness(GameTime gameTime)
198 {
199 // Get user's input state.
200
201 KeyboardState keyState = Keyboard.GetState();
202 MouseState mouseState = Mouse.GetState();
203
204 // Make a list of the keys pressed or released this frame.
205
206 List<Keys> pressedKeys = new List<Keys>();
207 List<Keys> releasedKeys = new List<Keys>();
208
209 Keys[] pressedKeysArray = keyState.GetPressedKeys();
210 foreach (Keys k in pressedKeysArray)
211 if (!lastPressedKeys.Contains(k))
212 pressedKeys.Add(k);
213 else
214 lastPressedKeys.Remove(k);
215
216 releasedKeys = lastPressedKeys;
217 lastPressedKeys = new List<Keys>(pressedKeysArray);
218
219 // Get mouse button state.
220
221 bool buttonPressed = mouseState.LeftButton == ButtonState.Pressed;
222
223 /***** Begining of game logic. *****/
224
225 // Debug - allow user on this machine to direct input to any player's state in the game.
226
227 if (pressedKeys.Contains(Keys.F1)) activePlayer = playerIdentifiers[0];
228 if (pressedKeys.Contains(Keys.F2)) activePlayer = playerIdentifiers[1];
229 if (pressedKeys.Contains(Keys.F3)) activePlayer = playerIdentifiers[2];
230 if (pressedKeys.Contains(Keys.F4)) activePlayer = playerIdentifiers[3];
231
232 // Debug - allow user on this machine to pause/resume game state advances.
233
234 if (pressedKeys.Contains(Keys.F12) ||
235 pressedKeys.Contains(Keys.P) && (keyState.IsKeyDown(Keys.LeftControl) || keyState.IsKeyDown(Keys.RightControl)))
236 {
237 paused = !paused;
238 return; // Don't update on pause start or stop
239 }
240
241 // Debug - automatically pause every 1000 frames.
242
243 if (mGame.CurrentFrameNumber % 1000 == 0 && mGame.CurrentFrameNumber != lastAutoPause)
244 {
245 paused = true;
246 lastAutoPause = mGame.CurrentFrameNumber;
247 }
248
249
250 //if (pressedKeys.Contains(Keys.Escape))
251 // this.Exit();
252
253 // Game update
254
255 // Direct inputs to the game engine - only report changes.
256
257 foreach (Keys k in pressedKeys)
258 mGame.ApplyKeyInput(activePlayer, k, true);
259
260 foreach (Keys k in releasedKeys)
261 mGame.ApplyKeyInput(activePlayer, k, false);
262
263 mGame.ApplyMouseLocationInput(activePlayer, mouseState.X, mouseState.Y);
264
265 if (lastButtonPressed != buttonPressed)
266 mGame.ApplyMouseButtonInput(activePlayer, buttonPressed);
267
268 lastButtonPressed = buttonPressed;
269
270 if (!paused)
271 {
272 // Advance the game engine.
273
274 mGame.Update(mTargetTimeSpan);
275 }
276 }
277
278 void DrawTestHarness(GameTime gameTime, SpriteBatch spriteBatch)
279 {
280
281 // BEGIN: Test harness stuff.
282 if (paused && gameTime.TotalRealTime.Milliseconds < 500)
283 spriteBatch.DrawString(font, "-=> Paused <=-", new Vector2(10, 130), Color.White);
284
285 spriteBatch.DrawString(font, "Press [F1]...[F4] to simulate input for each player. Click X's to end game or terminate player.", new Vector2(10, 540), Color.White);
286 spriteBatch.DrawString(font, "Press [ESC] to exit and [F12] to pause/unpause. Game auto-pauses every 1000 frames.", new Vector2(10, 570), Color.White);
287 //END: Test harness stuff.
288
289 }
290 }
291 }
This page took 0.046461 seconds and 3 git commands to generate.