2 using System.Collections.Generic;
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;
12 namespace CS_3505_Project_06
14 public class NetworkGame
16 NetworkSession mNetworkSession;
19 IDeterministicGame mGame;
21 TimeSpan mTargetTimeSpan = new TimeSpan(166666);
22 public TimeSpan TargetTimeSpan
26 return mTargetTimeSpan;
30 List<Keys> lastPressedKeys;
31 bool lastButtonPressed;
33 Object[] playerIdentifiers = { "One", "Two", "Three", "Four" }; // Any objects will do, strings are easy to debug.
41 public SpriteFont font;
44 public NetworkGame(ILobby lobby, IDeterministicGame game)
46 Debug.Assert(lobby != null && game != null);
51 // Begin: Test harness stuff
52 lastPressedKeys = new List<Keys>();
53 activePlayer = playerIdentifiers[0];
56 // Reset the game - indicate that player #1 (player 0) owns this instance of the game.
58 mGame.ResetGame(playerIdentifiers, playerIdentifiers[0]);
62 public LocalNetworkGamer LocalGamer
66 return mNetworkSession.LocalGamers[0];
70 // I added this as I needed a way to display all gamers not just the first gamer
72 public GamerCollection<NetworkGamer> NetworkGamers
76 return mNetworkSession.AllGamers;
81 public NetworkSession CreateSession()
83 return CreateSession(mGame.MaximumSupportedPlayers);
86 public NetworkSession CreateSession(int maxGamers)
88 Debug.Assert(mNetworkSession == null);
90 mNetworkSession = NetworkSession.Create(NetworkSessionType.SystemLink, 1, maxGamers);
91 mNetworkSession.AllowHostMigration = true;
92 mNetworkSession.AllowJoinInProgress = false;
94 return mNetworkSession;
97 // added so I can test if sessionExists and thus be able to call things on NetworkGame safely
99 public bool sessionExists()
101 return mNetworkSession != null;
104 public AvailableNetworkSessionCollection FindSessions()
106 return NetworkSession.Find(NetworkSessionType.SystemLink, 1, new NetworkSessionProperties());
109 public NetworkSession JoinSession(AvailableNetworkSession availableSession)
111 Debug.Assert(mNetworkSession == null);
113 mNetworkSession = NetworkSession.Join(availableSession);
115 return mNetworkSession;
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.
122 public void StartGame()
124 mNetworkSession.StartGame();
125 mNetworkSession.ResetReady();
128 public void LeaveSession()
130 Debug.Assert(mNetworkSession != null);
132 mNetworkSession.Dispose();
133 mNetworkSession = null;
137 public void SimulateBadNetwork()
139 Debug.Assert(mNetworkSession != null);
141 mNetworkSession.SimulatedLatency = new TimeSpan(0, 0, 0, 0, 200);
142 mNetworkSession.SimulatedPacketLoss = 0.1f;
146 public void Update(GameTime gameTime)
148 if (mNetworkSession == null)
150 mLobby.Update(gameTime, this);
154 mNetworkSession.Update();
156 if (mNetworkSession.SessionState == NetworkSessionState.Lobby)
158 if (mNetworkSession.IsHost &&
159 mNetworkSession.AllGamers.Count >= mGame.MinimumSupportedPlayers &&
160 mNetworkSession.IsEveryoneReady)
162 mNetworkSession.StartGame();
163 mNetworkSession.ResetReady();
167 mLobby.Update(gameTime, this);
170 else if (mNetworkSession.SessionState == NetworkSessionState.Playing)
172 // TODO: in-game update stuff
173 UpdateTestHarness(gameTime);
175 mGame.Update(mTargetTimeSpan);
180 public void Draw(GameTime gameTime, SpriteBatch spriteBatch)
182 if (mNetworkSession != null)
184 if (mNetworkSession.SessionState == NetworkSessionState.Playing)
185 DrawTestHarness(gameTime, spriteBatch);
187 mLobby.Draw(spriteBatch);
190 mLobby.Draw(spriteBatch);
197 void UpdateTestHarness(GameTime gameTime)
199 // Get user's input state.
201 KeyboardState keyState = Keyboard.GetState();
202 MouseState mouseState = Mouse.GetState();
204 // Make a list of the keys pressed or released this frame.
206 List<Keys> pressedKeys = new List<Keys>();
207 List<Keys> releasedKeys = new List<Keys>();
209 Keys[] pressedKeysArray = keyState.GetPressedKeys();
210 foreach (Keys k in pressedKeysArray)
211 if (!lastPressedKeys.Contains(k))
214 lastPressedKeys.Remove(k);
216 releasedKeys = lastPressedKeys;
217 lastPressedKeys = new List<Keys>(pressedKeysArray);
219 // Get mouse button state.
221 bool buttonPressed = mouseState.LeftButton == ButtonState.Pressed;
223 /***** Begining of game logic. *****/
225 // Debug - allow user on this machine to direct input to any player's state in the game.
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];
232 // Debug - allow user on this machine to pause/resume game state advances.
234 if (pressedKeys.Contains(Keys.F12) ||
235 pressedKeys.Contains(Keys.P) && (keyState.IsKeyDown(Keys.LeftControl) || keyState.IsKeyDown(Keys.RightControl)))
238 return; // Don't update on pause start or stop
241 // Debug - automatically pause every 1000 frames.
243 if (mGame.CurrentFrameNumber % 1000 == 0 && mGame.CurrentFrameNumber != lastAutoPause)
246 lastAutoPause = mGame.CurrentFrameNumber;
250 //if (pressedKeys.Contains(Keys.Escape))
255 // Direct inputs to the game engine - only report changes.
257 foreach (Keys k in pressedKeys)
258 mGame.ApplyKeyInput(activePlayer, k, true);
260 foreach (Keys k in releasedKeys)
261 mGame.ApplyKeyInput(activePlayer, k, false);
263 mGame.ApplyMouseLocationInput(activePlayer, mouseState.X, mouseState.Y);
265 if (lastButtonPressed != buttonPressed)
266 mGame.ApplyMouseButtonInput(activePlayer, buttonPressed);
268 lastButtonPressed = buttonPressed;
272 // Advance the game engine.
274 mGame.Update(mTargetTimeSpan);
278 void DrawTestHarness(GameTime gameTime, SpriteBatch spriteBatch)
281 // BEGIN: Test harness stuff.
282 if (paused && gameTime.TotalRealTime.Milliseconds < 500)
283 spriteBatch.DrawString(font, "-=> Paused <=-", new Vector2(10, 130), Color.White);
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.