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<LocalNetworkGamer> LocalGamers
76 return mNetworkSession.LocalGamers;
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;
119 public void LeaveSession()
121 Debug.Assert(mNetworkSession != null);
123 mNetworkSession.Dispose();
124 mNetworkSession = null;
128 public void SimulateBadNetwork()
130 Debug.Assert(mNetworkSession != null);
132 mNetworkSession.SimulatedLatency = new TimeSpan(0, 0, 0, 0, 200);
133 mNetworkSession.SimulatedPacketLoss = 0.1f;
137 public void Update(GameTime gameTime)
139 if (mNetworkSession == null)
141 mLobby.Update(gameTime, this);
145 mNetworkSession.Update();
147 if (mNetworkSession.SessionState == NetworkSessionState.Lobby)
149 if (mNetworkSession.IsHost &&
150 mNetworkSession.AllGamers.Count >= mGame.MinimumSupportedPlayers &&
151 mNetworkSession.IsEveryoneReady)
153 mNetworkSession.StartGame();
154 mNetworkSession.ResetReady();
158 mLobby.Update(gameTime, this);
161 else if (mNetworkSession.SessionState == NetworkSessionState.Playing)
163 // TODO: in-game update stuff
164 UpdateTestHarness(gameTime);
166 mGame.Update(mTargetTimeSpan);
171 public void Draw(GameTime gameTime, SpriteBatch spriteBatch)
173 mLobby.Draw(spriteBatch);
174 DrawTestHarness(gameTime, spriteBatch);
180 void UpdateTestHarness(GameTime gameTime)
182 // Get user's input state.
184 KeyboardState keyState = Keyboard.GetState();
185 MouseState mouseState = Mouse.GetState();
187 // Make a list of the keys pressed or released this frame.
189 List<Keys> pressedKeys = new List<Keys>();
190 List<Keys> releasedKeys = new List<Keys>();
192 Keys[] pressedKeysArray = keyState.GetPressedKeys();
193 foreach (Keys k in pressedKeysArray)
194 if (!lastPressedKeys.Contains(k))
197 lastPressedKeys.Remove(k);
199 releasedKeys = lastPressedKeys;
200 lastPressedKeys = new List<Keys>(pressedKeysArray);
202 // Get mouse button state.
204 bool buttonPressed = mouseState.LeftButton == ButtonState.Pressed;
206 /***** Begining of game logic. *****/
208 // Debug - allow user on this machine to direct input to any player's state in the game.
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];
215 // Debug - allow user on this machine to pause/resume game state advances.
217 if (pressedKeys.Contains(Keys.F12) ||
218 pressedKeys.Contains(Keys.P) && (keyState.IsKeyDown(Keys.LeftControl) || keyState.IsKeyDown(Keys.RightControl)))
221 return; // Don't update on pause start or stop
224 // Debug - automatically pause every 1000 frames.
226 if (mGame.CurrentFrameNumber % 1000 == 0 && mGame.CurrentFrameNumber != lastAutoPause)
229 lastAutoPause = mGame.CurrentFrameNumber;
233 //if (pressedKeys.Contains(Keys.Escape))
238 // Direct inputs to the game engine - only report changes.
240 foreach (Keys k in pressedKeys)
241 mGame.ApplyKeyInput(activePlayer, k, true);
243 foreach (Keys k in releasedKeys)
244 mGame.ApplyKeyInput(activePlayer, k, false);
246 mGame.ApplyMouseLocationInput(activePlayer, mouseState.X, mouseState.Y);
248 if (lastButtonPressed != buttonPressed)
249 mGame.ApplyMouseButtonInput(activePlayer, buttonPressed);
251 lastButtonPressed = buttonPressed;
255 // Advance the game engine.
257 mGame.Update(mTargetTimeSpan);
261 void DrawTestHarness(GameTime gameTime, SpriteBatch spriteBatch)
264 // BEGIN: Test harness stuff.
265 if (paused && gameTime.TotalRealTime.Milliseconds < 500)
266 spriteBatch.DrawString(font, "-=> Paused <=-", new Vector2(10, 130), Color.White);
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.