2 using System.Collections.Generic;
4 using Microsoft.Xna.Framework;
5 using Microsoft.Xna.Framework.Audio;
6 using Microsoft.Xna.Framework.Content;
7 using Microsoft.Xna.Framework.GamerServices;
8 using Microsoft.Xna.Framework.Graphics;
9 using Microsoft.Xna.Framework.Input;
10 using Microsoft.Xna.Framework.Media;
11 using Microsoft.Xna.Framework.Net;
12 using Microsoft.Xna.Framework.Storage;
13 using CS_3505_Project_06.CS_3505;
15 namespace CS_3505_Project_06
18 /// A game outline for testing network communications
20 public class Game06 : Microsoft.Xna.Framework.Game
22 GraphicsDeviceManager graphics;
23 SpriteBatch spriteBatch;
26 IDeterministicGame deterministicGame;
27 TimeSpan targetTimeSpan;
28 Object[] playerIdentifiers = { "One", "Two", "Three", "Four" }; // Any objects will do, strings are easy to debug.
32 List<Keys> lastPressedKeys;
33 bool lastButtonPressed;
42 graphics = new GraphicsDeviceManager(this);
43 Content.RootDirectory = "Content";
45 // Make the game object. The game is currently called 'testHarness'.
47 deterministicGame = new TestHarness();
51 lastPressedKeys = new List<Keys>();
52 activePlayer = playerIdentifiers[0];
57 /// Allows the game to perform any initialization it needs to before starting to run.
58 /// This is where it can query for any required services and load any non-graphic
59 /// related content. Calling base.Initialize will enumerate through any components
60 /// and initialize them as well.
62 protected override void Initialize()
64 // Set a fixed time span of 1/60th of a second.
66 targetTimeSpan = new TimeSpan(166666); // In 100 nanosecond units = 16 666 600 nanoseconds
67 IsFixedTimeStep = true;
68 TargetElapsedTime = targetTimeSpan;
70 // Reset the game - indicate that player #1 (player 0) owns this instance of the game.
72 deterministicGame.ResetGame(playerIdentifiers, playerIdentifiers[0]);
74 // For debugging - reset the mouse position to the center of the window.
76 Mouse.SetPosition(400, 300);
78 // Allow the base class to initialize.
84 /// LoadContent will be called once per game and is the place to load
85 /// all of your content.
87 protected override void LoadContent()
89 // Create a new SpriteBatch, which can be used to draw textures.
91 spriteBatch = new SpriteBatch(GraphicsDevice);
93 // Let the game load its content.
95 font = Content.Load<SpriteFont>("InstructionFont");
97 deterministicGame.LoadContent(Content);
101 /// UnloadContent will be called once per game and is the place to unload
104 protected override void UnloadContent()
106 deterministicGame.UnloadContent();
110 /// Allows the game to run logic such as updating the world,
111 /// checking for collisions, gathering input, and playing audio.
113 /// <param name="gameTime">Provides a snapshot of timing values.</param>
114 protected override void Update(GameTime gameTime)
116 // Get user's input state.
118 KeyboardState keyState = Keyboard.GetState();
119 MouseState mouseState = Mouse.GetState();
121 // Make a list of the keys pressed or released this frame.
123 List<Keys> pressedKeys = new List<Keys>();
124 List<Keys> releasedKeys = new List<Keys>();
126 Keys[] pressedKeysArray = keyState.GetPressedKeys();
127 foreach (Keys k in pressedKeysArray)
128 if (!lastPressedKeys.Contains(k))
131 lastPressedKeys.Remove(k);
133 releasedKeys = lastPressedKeys;
134 lastPressedKeys = new List<Keys>(pressedKeysArray);
136 // Get mouse button state.
138 bool buttonPressed = mouseState.LeftButton == ButtonState.Pressed;
140 /***** Begining of game logic. *****/
142 // Debug - allow user to exit.
144 if (pressedKeys.Contains(Keys.Escape))
147 // Debug - allow user on this machine to direct input to any player's state in the game.
149 if (pressedKeys.Contains(Keys.F1)) activePlayer = playerIdentifiers[0];
150 if (pressedKeys.Contains(Keys.F2)) activePlayer = playerIdentifiers[1];
151 if (pressedKeys.Contains(Keys.F3)) activePlayer = playerIdentifiers[2];
152 if (pressedKeys.Contains(Keys.F4)) activePlayer = playerIdentifiers[3];
154 // Debug - allow user on this machine to pause/resume game state advances.
156 if (pressedKeys.Contains(Keys.F12) ||
157 pressedKeys.Contains(Keys.P) && (keyState.IsKeyDown(Keys.LeftControl) || keyState.IsKeyDown(Keys.RightControl)))
160 return; // Don't update on pause start or stop
163 // Debug - automatically pause every 1000 frames.
165 if (deterministicGame.CurrentFrameNumber % 1000 == 0 && deterministicGame.CurrentFrameNumber != lastAutoPause)
168 lastAutoPause = deterministicGame.CurrentFrameNumber;
173 // Direct inputs to the game engine - only report changes.
175 foreach (Keys k in pressedKeys)
176 deterministicGame.ApplyKeyInput(activePlayer, k, true);
178 foreach (Keys k in releasedKeys)
179 deterministicGame.ApplyKeyInput(activePlayer, k, false);
181 deterministicGame.ApplyMouseLocationInput(activePlayer, mouseState.X, mouseState.Y);
183 if (lastButtonPressed != buttonPressed)
184 deterministicGame.ApplyMouseButtonInput(activePlayer, buttonPressed);
186 lastButtonPressed = buttonPressed;
190 // Advance the game engine.
192 deterministicGame.Update(targetTimeSpan);
195 /***** End of game logic. *****/
197 // Allow the superclass to do any needed updates (unknown purpose).
199 base.Update(gameTime);
203 /// This is called when the game should draw itself.
205 /// <param name="gameTime">Provides a snapshot of timing values.</param>
206 protected override void Draw(GameTime gameTime)
208 GraphicsDevice.Clear(new Color(16, 16, 16, 255)); // Needed by the test harness, should be removed for the real game.
212 // Draw a few instructions.
214 if (paused && gameTime.TotalRealTime.Milliseconds < 500)
215 spriteBatch.DrawString(font, "-=> Paused <=-", new Vector2(10, 130), Color.White);
217 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);
218 spriteBatch.DrawString(font, "Press [ESC] to exit and [F12] to pause/unpause. Game auto-pauses every 1000 frames.", new Vector2(10, 570), Color.White);
220 // Let the game draw itself.
222 deterministicGame.Draw(spriteBatch);