]> Dogcows Code - chaz/carfire/blob - Project06/CS 3505 Project 06/CS 3505 Project 06/NetworkGame.cs
testing
[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 }
126
127 public void LeaveSession()
128 {
129 Debug.Assert(mNetworkSession != null);
130
131 mNetworkSession.Dispose();
132 mNetworkSession = null;
133 }
134
135
136 public void SimulateBadNetwork()
137 {
138 Debug.Assert(mNetworkSession != null);
139
140 mNetworkSession.SimulatedLatency = new TimeSpan(0, 0, 0, 0, 200);
141 mNetworkSession.SimulatedPacketLoss = 0.1f;
142 }
143
144
145 public void Update(GameTime gameTime)
146 {
147 if (mNetworkSession == null)
148 {
149 mLobby.Update(gameTime, this);
150 }
151 else
152 {
153 mNetworkSession.Update();
154
155 if (mNetworkSession.SessionState == NetworkSessionState.Lobby)
156 {
157 if (mNetworkSession.IsHost &&
158 mNetworkSession.AllGamers.Count >= mGame.MinimumSupportedPlayers &&
159 mNetworkSession.IsEveryoneReady)
160 {
161 mNetworkSession.StartGame();
162 mNetworkSession.ResetReady();
163 }
164 else
165 {
166 mLobby.Update(gameTime, this);
167 }
168 }
169 else if (mNetworkSession.SessionState == NetworkSessionState.Playing)
170 {
171 // TODO: in-game update stuff
172 UpdateTestHarness(gameTime);
173
174 mGame.Update(mTargetTimeSpan);
175 }
176 }
177 }
178
179 public void Draw(GameTime gameTime, SpriteBatch spriteBatch)
180 {
181 mLobby.Draw(spriteBatch);
182 DrawTestHarness(gameTime, spriteBatch);
183 }
184
185
186
187
188 void UpdateTestHarness(GameTime gameTime)
189 {
190 // Get user's input state.
191
192 KeyboardState keyState = Keyboard.GetState();
193 MouseState mouseState = Mouse.GetState();
194
195 // Make a list of the keys pressed or released this frame.
196
197 List<Keys> pressedKeys = new List<Keys>();
198 List<Keys> releasedKeys = new List<Keys>();
199
200 Keys[] pressedKeysArray = keyState.GetPressedKeys();
201 foreach (Keys k in pressedKeysArray)
202 if (!lastPressedKeys.Contains(k))
203 pressedKeys.Add(k);
204 else
205 lastPressedKeys.Remove(k);
206
207 releasedKeys = lastPressedKeys;
208 lastPressedKeys = new List<Keys>(pressedKeysArray);
209
210 // Get mouse button state.
211
212 bool buttonPressed = mouseState.LeftButton == ButtonState.Pressed;
213
214 /***** Begining of game logic. *****/
215
216 // Debug - allow user on this machine to direct input to any player's state in the game.
217
218 if (pressedKeys.Contains(Keys.F1)) activePlayer = playerIdentifiers[0];
219 if (pressedKeys.Contains(Keys.F2)) activePlayer = playerIdentifiers[1];
220 if (pressedKeys.Contains(Keys.F3)) activePlayer = playerIdentifiers[2];
221 if (pressedKeys.Contains(Keys.F4)) activePlayer = playerIdentifiers[3];
222
223 // Debug - allow user on this machine to pause/resume game state advances.
224
225 if (pressedKeys.Contains(Keys.F12) ||
226 pressedKeys.Contains(Keys.P) && (keyState.IsKeyDown(Keys.LeftControl) || keyState.IsKeyDown(Keys.RightControl)))
227 {
228 paused = !paused;
229 return; // Don't update on pause start or stop
230 }
231
232 // Debug - automatically pause every 1000 frames.
233
234 if (mGame.CurrentFrameNumber % 1000 == 0 && mGame.CurrentFrameNumber != lastAutoPause)
235 {
236 paused = true;
237 lastAutoPause = mGame.CurrentFrameNumber;
238 }
239
240
241 //if (pressedKeys.Contains(Keys.Escape))
242 // this.Exit();
243
244 // Game update
245
246 // Direct inputs to the game engine - only report changes.
247
248 foreach (Keys k in pressedKeys)
249 mGame.ApplyKeyInput(activePlayer, k, true);
250
251 foreach (Keys k in releasedKeys)
252 mGame.ApplyKeyInput(activePlayer, k, false);
253
254 mGame.ApplyMouseLocationInput(activePlayer, mouseState.X, mouseState.Y);
255
256 if (lastButtonPressed != buttonPressed)
257 mGame.ApplyMouseButtonInput(activePlayer, buttonPressed);
258
259 lastButtonPressed = buttonPressed;
260
261 if (!paused)
262 {
263 // Advance the game engine.
264
265 mGame.Update(mTargetTimeSpan);
266 }
267 }
268
269 void DrawTestHarness(GameTime gameTime, SpriteBatch spriteBatch)
270 {
271
272 // BEGIN: Test harness stuff.
273 if (paused && gameTime.TotalRealTime.Milliseconds < 500)
274 spriteBatch.DrawString(font, "-=> Paused <=-", new Vector2(10, 130), Color.White);
275
276 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);
277 spriteBatch.DrawString(font, "Press [ESC] to exit and [F12] to pause/unpause. Game auto-pauses every 1000 frames.", new Vector2(10, 570), Color.White);
278 //END: Test harness stuff.
279
280 }
281 }
282 }
This page took 0.041381 seconds and 4 git commands to generate.