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