\r
-//#define DEBUG\r
+// Make sure DEBUG is undefined when turning in the project\r
+// or the grader will wonder why it's so laggy.\r
+#undef DEBUG\r
\r
using System;\r
using System.Collections.Generic;\r
}\r
else if (mNetworkSession.SessionState == NetworkSessionState.Playing)\r
{\r
- if (mGame.IsGameOver(LocalGamerInfo) || mGame.IsTerminated(LocalGamerInfo))\r
+ if (mGame.IsTerminated(LocalGamerInfo))\r
{\r
- // TODO: Should support moving back to the session lobby.\r
LeaveSession();\r
return;\r
}\r
+ else if (mGame.IsGameOver(LocalGamerInfo))\r
+ {\r
+ ApplyEvents(LocalGamerInfo, GetEventsFromInput());\r
+ mGame.Update(mTargetTimeSpan);\r
+ return;\r
+ }\r
\r
if (HaveNeededEvents)\r
{\r
}\r
else // Stall!\r
{\r
- if (mStallCount == 0)\r
- {\r
-#if DEBUG\r
- Console.WriteLine("STAL: ====");\r
-#endif\r
- }\r
- else if (mStallCount % 60 == 0)\r
- {\r
- // DEBUG\r
- //Console.WriteLine("Stalled for " + mStallCount + " frames.");\r
- }\r
-\r
mStallCount++;\r
\r
// Send a reliable event packet to each stalled gamer.\r
if (mStallCount == 1)\r
{\r
+#if DEBUG\r
+ Console.WriteLine("STAL: ====");\r
+#endif\r
+\r
foreach (GamerInfo gamerInfo in GamerArray)\r
{\r
if (gamerInfo.HighestFrameNumber < mGame.CurrentFrameNumber)\r
}\r
}\r
}\r
-\r
- /*if (mStallCount > StallTimeout)\r
+ else if (mStallCount > 600)\r
{\r
- DropLostGamers();\r
- mStallCount = 0;\r
+ Console.WriteLine("One or more players have stalled excessively. Leaving session...");\r
+ LeaveSession();\r
}\r
- else if (mStallCount == 1)\r
- {\r
- SendLocalEvents\r
- }\r
- else if (mStallCount % 60 == 0)\r
- {\r
- } TODO */\r
}\r
}\r
}\r
enum PacketType\r
{\r
Chat = 1,\r
- Event = 2,\r
- Stall = 3\r
+ Event = 2\r
}\r
\r
enum EventType\r
senderInfo.HighestFrameNumber = frameNumber;\r
break;\r
\r
- case PacketType.Stall:\r
-\r
- GamerInfo senderInfo2 = mGamers[sender.Id];\r
-\r
- byte numStalledPeers = mPacketReader.ReadByte();\r
- byte[] stalledPeers = mPacketReader.ReadBytes(numStalledPeers);\r
-\r
- // TODO\r
- break;\r
-\r
default:\r
\r
Console.WriteLine("Received unknown packet type: " + (int)packetId);\r
\r
foreach (GamerInfo gamerInfo in mGamers.Values)\r
{\r
+ if (mGame.IsGameOver(gamerInfo)) continue;\r
if (gamerInfo.HighestFrameNumber < currentFrame) return false;\r
}\r
\r
foreach (GamerInfo gamerInfo in GamerArray)\r
{\r
if (gamerInfo.Events[index] == null) continue;\r
+ ApplyEvents(gamerInfo, gamerInfo.Events[index]);\r
+ gamerInfo.Events[index] = null;\r
+ }\r
+ }\r
\r
- foreach (EventInfo eventInfo in gamerInfo.Events[index])\r
+ void ApplyEvents(GamerInfo gamerInfo, List<EventInfo> events)\r
+ {\r
+ foreach (EventInfo eventInfo in events)\r
+ {\r
+ KeyboardEventInfo keyboardEventInfo = eventInfo as KeyboardEventInfo;\r
+ if (keyboardEventInfo != null)\r
{\r
- KeyboardEventInfo keyboardEventInfo = eventInfo as KeyboardEventInfo;\r
- if (keyboardEventInfo != null)\r
- {\r
#if DEBUG\r
- Console.WriteLine(" KEY: " + keyboardEventInfo.FrameOfApplication + "\t" + keyboardEventInfo.Key + "," + keyboardEventInfo.IsKeyDown);\r
+ Console.WriteLine(" KEY: " + keyboardEventInfo.FrameOfApplication + "\t" + keyboardEventInfo.Key + "," + keyboardEventInfo.IsKeyDown);\r
#endif\r
\r
- mGame.ApplyKeyInput(gamerInfo, keyboardEventInfo.Key, keyboardEventInfo.IsKeyDown);\r
- continue;\r
- }\r
+ mGame.ApplyKeyInput(gamerInfo, keyboardEventInfo.Key, keyboardEventInfo.IsKeyDown);\r
+ continue;\r
+ }\r
\r
- MouseButtonEventInfo mouseButtonEventInfo = eventInfo as MouseButtonEventInfo;\r
- if (mouseButtonEventInfo != null)\r
- {\r
+ MouseButtonEventInfo mouseButtonEventInfo = eventInfo as MouseButtonEventInfo;\r
+ if (mouseButtonEventInfo != null)\r
+ {\r
#if DEBUG\r
- Console.WriteLine(" BTN: " + mouseButtonEventInfo.FrameOfApplication + "\t" + mouseButtonEventInfo.IsButtonDown);\r
+ Console.WriteLine(" BTN: " + mouseButtonEventInfo.FrameOfApplication + "\t" + mouseButtonEventInfo.IsButtonDown);\r
#endif\r
\r
- mGame.ApplyMouseButtonInput(gamerInfo, mouseButtonEventInfo.IsButtonDown);\r
- continue;\r
- }\r
+ mGame.ApplyMouseButtonInput(gamerInfo, mouseButtonEventInfo.IsButtonDown);\r
+ continue;\r
+ }\r
\r
- MouseMotionEventInfo mouseMotionEventInfo = eventInfo as MouseMotionEventInfo;\r
- if (mouseMotionEventInfo != null)\r
- {\r
+ MouseMotionEventInfo mouseMotionEventInfo = eventInfo as MouseMotionEventInfo;\r
+ if (mouseMotionEventInfo != null)\r
+ {\r
#if DEBUG\r
- Console.WriteLine(" MMV: " + mouseMotionEventInfo.FrameOfApplication + "\t" + mouseMotionEventInfo.X + "," + mouseMotionEventInfo.Y);\r
+ Console.WriteLine(" MMV: " + mouseMotionEventInfo.FrameOfApplication + "\t" + mouseMotionEventInfo.X + "," + mouseMotionEventInfo.Y);\r
#endif\r
\r
- mGame.ApplyMouseLocationInput(gamerInfo, mouseMotionEventInfo.X, mouseMotionEventInfo.Y);\r
- continue;\r
- }\r
+ mGame.ApplyMouseLocationInput(gamerInfo, mouseMotionEventInfo.X, mouseMotionEventInfo.Y);\r
+ continue;\r
}\r
-\r
- gamerInfo.Events[index] = null;\r
}\r
}\r
\r