]> Dogcows Code - chaz/carfire/blob - Project06/CS 3505 Project 06/CS 3505 Project 06/NetworkGame.cs
abe36ba4a4d4ca1dcd6d84affc91fbad5766960e
[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 LocalNetworkGamer LocalGamer
63 {
64 get
65 {
66 return mNetworkSession.LocalGamers[0];
67 }
68 }
69
70
71 NetworkSession CreateSession()
72 {
73 return CreateSession(mGame.MaximumSupportedPlayers);
74 }
75
76 public NetworkSession CreateSession(int maxGamers)
77 {
78 Debug.Assert(mNetworkSession == null);
79
80 mNetworkSession = NetworkSession.Create(NetworkSessionType.SystemLink, 1, maxGamers);
81 mNetworkSession.AllowHostMigration = true;
82 mNetworkSession.AllowJoinInProgress = false;
83
84 return mNetworkSession;
85 }
86
87
88 public AvailableNetworkSessionCollection FindSessions()
89 {
90 return NetworkSession.Find(NetworkSessionType.SystemLink, 1, new NetworkSessionProperties());
91 }
92
93 public NetworkSession JoinSession(AvailableNetworkSession availableSession)
94 {
95 Debug.Assert(mNetworkSession == null);
96
97 mNetworkSession = NetworkSession.Join(availableSession);
98
99 return mNetworkSession;
100 }
101
102
103 public void LeaveSession()
104 {
105 Debug.Assert(mNetworkSession != null);
106
107 mNetworkSession.Dispose();
108 mNetworkSession = null;
109 }
110
111
112 public void SimulateBadNetwork()
113 {
114 Debug.Assert(mNetworkSession != null);
115
116 mNetworkSession.SimulatedLatency = new TimeSpan(0, 0, 0, 0, 200);
117 mNetworkSession.SimulatedPacketLoss = 0.1f;
118 }
119
120
121 public void Update(GameTime gameTime)
122 {
123 if (mNetworkSession == null)
124 {
125 mLobby.Update(gameTime, this);
126 }
127 else
128 {
129 mNetworkSession.Update();
130
131 if (mNetworkSession.SessionState == NetworkSessionState.Lobby)
132 {
133 if (mNetworkSession.IsHost &&
134 mNetworkSession.AllGamers.Count >= mGame.MinimumSupportedPlayers &&
135 mNetworkSession.IsEveryoneReady)
136 {
137 mNetworkSession.StartGame();
138 mNetworkSession.ResetReady();
139 }
140 else
141 {
142 mLobby.Update(gameTime, this);
143 }
144 }
145 else if (mNetworkSession.SessionState == NetworkSessionState.Playing)
146 {
147 // TODO: in-game update stuff
148 UpdateTestHarness(gameTime);
149
150 mGame.Update(mTargetTimeSpan);
151 }
152 }
153 }
154
155 public void Draw(GameTime gameTime, SpriteBatch spriteBatch)
156 {
157 mLobby.Draw(spriteBatch);
158 DrawTestHarness(gameTime, spriteBatch);
159 }
160
161
162
163
164 void UpdateTestHarness(GameTime gameTime)
165 {
166 // Get user's input state.
167
168 KeyboardState keyState = Keyboard.GetState();
169 MouseState mouseState = Mouse.GetState();
170
171 // Make a list of the keys pressed or released this frame.
172
173 List<Keys> pressedKeys = new List<Keys>();
174 List<Keys> releasedKeys = new List<Keys>();
175
176 Keys[] pressedKeysArray = keyState.GetPressedKeys();
177 foreach (Keys k in pressedKeysArray)
178 if (!lastPressedKeys.Contains(k))
179 pressedKeys.Add(k);
180 else
181 lastPressedKeys.Remove(k);
182
183 releasedKeys = lastPressedKeys;
184 lastPressedKeys = new List<Keys>(pressedKeysArray);
185
186 // Get mouse button state.
187
188 bool buttonPressed = mouseState.LeftButton == ButtonState.Pressed;
189
190 /***** Begining of game logic. *****/
191
192 // Debug - allow user on this machine to direct input to any player's state in the game.
193
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];
198
199 // Debug - allow user on this machine to pause/resume game state advances.
200
201 if (pressedKeys.Contains(Keys.F12) ||
202 pressedKeys.Contains(Keys.P) && (keyState.IsKeyDown(Keys.LeftControl) || keyState.IsKeyDown(Keys.RightControl)))
203 {
204 paused = !paused;
205 return; // Don't update on pause start or stop
206 }
207
208 // Debug - automatically pause every 1000 frames.
209
210 if (mGame.CurrentFrameNumber % 1000 == 0 && mGame.CurrentFrameNumber != lastAutoPause)
211 {
212 paused = true;
213 lastAutoPause = mGame.CurrentFrameNumber;
214 }
215
216
217 //if (pressedKeys.Contains(Keys.Escape))
218 // this.Exit();
219
220 // Game update
221
222 // Direct inputs to the game engine - only report changes.
223
224 foreach (Keys k in pressedKeys)
225 mGame.ApplyKeyInput(activePlayer, k, true);
226
227 foreach (Keys k in releasedKeys)
228 mGame.ApplyKeyInput(activePlayer, k, false);
229
230 mGame.ApplyMouseLocationInput(activePlayer, mouseState.X, mouseState.Y);
231
232 if (lastButtonPressed != buttonPressed)
233 mGame.ApplyMouseButtonInput(activePlayer, buttonPressed);
234
235 lastButtonPressed = buttonPressed;
236
237 if (!paused)
238 {
239 // Advance the game engine.
240
241 mGame.Update(mTargetTimeSpan);
242 }
243 }
244
245 void DrawTestHarness(GameTime gameTime, SpriteBatch spriteBatch)
246 {
247
248 // BEGIN: Test harness stuff.
249 if (paused && gameTime.TotalRealTime.Milliseconds < 500)
250 spriteBatch.DrawString(font, "-=> Paused <=-", new Vector2(10, 130), Color.White);
251
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.
255
256 }
257 }
258 }
This page took 0.03848 seconds and 3 git commands to generate.