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 LocalNetworkGamer LocalGamer
66 return mNetworkSession.LocalGamers[0];
71 NetworkSession CreateSession()
73 return CreateSession(mGame.MaximumSupportedPlayers);
76 public NetworkSession CreateSession(int maxGamers)
78 Debug.Assert(mNetworkSession == null);
80 mNetworkSession = NetworkSession.Create(NetworkSessionType.SystemLink, 1, maxGamers);
81 mNetworkSession.AllowHostMigration = true;
82 mNetworkSession.AllowJoinInProgress = false;
84 return mNetworkSession;
88 public AvailableNetworkSessionCollection FindSessions()
90 return NetworkSession.Find(NetworkSessionType.SystemLink, 1, new NetworkSessionProperties());
93 public NetworkSession JoinSession(AvailableNetworkSession availableSession)
95 Debug.Assert(mNetworkSession == null);
97 mNetworkSession = NetworkSession.Join(availableSession);
99 return mNetworkSession;
103 public void LeaveSession()
105 Debug.Assert(mNetworkSession != null);
107 mNetworkSession.Dispose();
108 mNetworkSession = null;
112 public void SimulateBadNetwork()
114 Debug.Assert(mNetworkSession != null);
116 mNetworkSession.SimulatedLatency = new TimeSpan(0, 0, 0, 0, 200);
117 mNetworkSession.SimulatedPacketLoss = 0.1f;
121 public void Update(GameTime gameTime)
123 if (mNetworkSession == null)
125 mLobby.Update(gameTime, this);
129 mNetworkSession.Update();
131 if (mNetworkSession.SessionState == NetworkSessionState.Lobby)
133 if (mNetworkSession.IsHost &&
134 mNetworkSession.AllGamers.Count >= mGame.MinimumSupportedPlayers &&
135 mNetworkSession.IsEveryoneReady)
137 mNetworkSession.StartGame();
138 mNetworkSession.ResetReady();
142 mLobby.Update(gameTime, this);
145 else if (mNetworkSession.SessionState == NetworkSessionState.Playing)
147 // TODO: in-game update stuff
148 UpdateTestHarness(gameTime);
150 mGame.Update(mTargetTimeSpan);
155 public void Draw(GameTime gameTime, SpriteBatch spriteBatch)
157 mLobby.Draw(spriteBatch);
158 DrawTestHarness(gameTime, spriteBatch);
164 void UpdateTestHarness(GameTime gameTime)
166 // Get user's input state.
168 KeyboardState keyState = Keyboard.GetState();
169 MouseState mouseState = Mouse.GetState();
171 // Make a list of the keys pressed or released this frame.
173 List<Keys> pressedKeys = new List<Keys>();
174 List<Keys> releasedKeys = new List<Keys>();
176 Keys[] pressedKeysArray = keyState.GetPressedKeys();
177 foreach (Keys k in pressedKeysArray)
178 if (!lastPressedKeys.Contains(k))
181 lastPressedKeys.Remove(k);
183 releasedKeys = lastPressedKeys;
184 lastPressedKeys = new List<Keys>(pressedKeysArray);
186 // Get mouse button state.
188 bool buttonPressed = mouseState.LeftButton == ButtonState.Pressed;
190 /***** Begining of game logic. *****/
192 // Debug - allow user on this machine to direct input to any player's state in the game.
194 if (pressedKeys.Contains(Keys.F1)) activePlayer = playerIdentifiers[0];
195 if (pressedKeys.Contains(Keys.F2)) activePlayer = playerIdentifiers[1];
196 if (pressedKeys.Contains(Keys.F3)) activePlayer = playerIdentifiers[2];
197 if (pressedKeys.Contains(Keys.F4)) activePlayer = playerIdentifiers[3];
199 // Debug - allow user on this machine to pause/resume game state advances.
201 if (pressedKeys.Contains(Keys.F12) ||
202 pressedKeys.Contains(Keys.P) && (keyState.IsKeyDown(Keys.LeftControl) || keyState.IsKeyDown(Keys.RightControl)))
205 return; // Don't update on pause start or stop
208 // Debug - automatically pause every 1000 frames.
210 if (mGame.CurrentFrameNumber % 1000 == 0 && mGame.CurrentFrameNumber != lastAutoPause)
213 lastAutoPause = mGame.CurrentFrameNumber;
217 //if (pressedKeys.Contains(Keys.Escape))
222 // Direct inputs to the game engine - only report changes.
224 foreach (Keys k in pressedKeys)
225 mGame.ApplyKeyInput(activePlayer, k, true);
227 foreach (Keys k in releasedKeys)
228 mGame.ApplyKeyInput(activePlayer, k, false);
230 mGame.ApplyMouseLocationInput(activePlayer, mouseState.X, mouseState.Y);
232 if (lastButtonPressed != buttonPressed)
233 mGame.ApplyMouseButtonInput(activePlayer, buttonPressed);
235 lastButtonPressed = buttonPressed;
239 // Advance the game engine.
241 mGame.Update(mTargetTimeSpan);
245 void DrawTestHarness(GameTime gameTime, SpriteBatch spriteBatch)
248 // BEGIN: Test harness stuff.
249 if (paused && gameTime.TotalRealTime.Milliseconds < 500)
250 spriteBatch.DrawString(font, "-=> Paused <=-", new Vector2(10, 130), Color.White);
252 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);
253 spriteBatch.DrawString(font, "Press [ESC] to exit and [F12] to pause/unpause. Game auto-pauses every 1000 frames.", new Vector2(10, 570), Color.White);
254 //END: Test harness stuff.