]> Dogcows Code - chaz/carfire/blobdiff - Project06/CS 3505 Project 06/CS 3505 Project 06/Game06.cs
initial upload
[chaz/carfire] / Project06 / CS 3505 Project 06 / CS 3505 Project 06 / Game06.cs
diff --git a/Project06/CS 3505 Project 06/CS 3505 Project 06/Game06.cs b/Project06/CS 3505 Project 06/CS 3505 Project 06/Game06.cs
new file mode 100644 (file)
index 0000000..0a4a228
--- /dev/null
@@ -0,0 +1,230 @@
+using System;\r
+using System.Collections.Generic;\r
+using System.Linq;\r
+using Microsoft.Xna.Framework;\r
+using Microsoft.Xna.Framework.Audio;\r
+using Microsoft.Xna.Framework.Content;\r
+using Microsoft.Xna.Framework.GamerServices;\r
+using Microsoft.Xna.Framework.Graphics;\r
+using Microsoft.Xna.Framework.Input;\r
+using Microsoft.Xna.Framework.Media;\r
+using Microsoft.Xna.Framework.Net;\r
+using Microsoft.Xna.Framework.Storage;\r
+using CS_3505_Project_06.CS_3505;\r
+\r
+namespace CS_3505_Project_06\r
+{\r
+    /// <summary>\r
+    /// A game outline for testing network communications\r
+    /// </summary>\r
+    public class Game06 : Microsoft.Xna.Framework.Game\r
+    {\r
+        GraphicsDeviceManager graphics;\r
+        SpriteBatch spriteBatch;\r
+        SpriteFont font;\r
+        \r
+        IDeterministicGame deterministicGame;\r
+        TimeSpan targetTimeSpan;\r
+        Object[] playerIdentifiers = { "One", "Two", "Three", "Four" };  // Any objects will do, strings are easy to debug.\r
+\r
+        // For debugging\r
+\r
+        List<Keys> lastPressedKeys;\r
+        bool lastButtonPressed;\r
+        Object activePlayer;\r
+        bool paused;\r
+        long lastAutoPause;\r
+\r
+        // Constructor\r
+\r
+        public Game06()\r
+        {\r
+            graphics = new GraphicsDeviceManager(this);\r
+            Content.RootDirectory = "Content";\r
+\r
+            // Make the game object.  The game is currently called 'testHarness'.\r
+\r
+            deterministicGame = new TestHarness();\r
+            \r
+            // Debugging setup\r
+\r
+            lastPressedKeys = new List<Keys>();\r
+            activePlayer = playerIdentifiers[0];\r
+            paused = false;\r
+        }\r
+\r
+        /// <summary>\r
+        /// Allows the game to perform any initialization it needs to before starting to run.\r
+        /// This is where it can query for any required services and load any non-graphic\r
+        /// related content.  Calling base.Initialize will enumerate through any components\r
+        /// and initialize them as well.\r
+        /// </summary>\r
+        protected override void Initialize()\r
+        {\r
+            // Set a fixed time span of 1/60th of a second.\r
+\r
+            targetTimeSpan = new TimeSpan(166666); // In 100 nanosecond units = 16 666 600 nanoseconds\r
+            IsFixedTimeStep = true;\r
+            TargetElapsedTime = targetTimeSpan;\r
+\r
+            // Reset the game - indicate that player #1 (player 0) owns this instance of the game.\r
+\r
+            deterministicGame.ResetGame(playerIdentifiers, playerIdentifiers[0]);\r
+\r
+            // For debugging - reset the mouse position to the center of the window.\r
+\r
+            Mouse.SetPosition(400, 300);\r
+\r
+            // Allow the base class to initialize.\r
+\r
+            base.Initialize();\r
+        }\r
+\r
+        /// <summary>\r
+        /// LoadContent will be called once per game and is the place to load\r
+        /// all of your content.\r
+        /// </summary>\r
+        protected override void LoadContent()\r
+        {\r
+            // Create a new SpriteBatch, which can be used to draw textures.\r
+\r
+            spriteBatch = new SpriteBatch(GraphicsDevice);\r
+\r
+            // Let the game load its content.\r
+\r
+            font = Content.Load<SpriteFont>("InstructionFont");\r
+\r
+            deterministicGame.LoadContent(Content);\r
+        }\r
+\r
+        /// <summary>\r
+        /// UnloadContent will be called once per game and is the place to unload\r
+        /// all content.\r
+        /// </summary>\r
+        protected override void UnloadContent()\r
+        {\r
+            deterministicGame.UnloadContent();\r
+        }\r
+\r
+        /// <summary>\r
+        /// Allows the game to run logic such as updating the world,\r
+        /// checking for collisions, gathering input, and playing audio.\r
+        /// </summary>\r
+        /// <param name="gameTime">Provides a snapshot of timing values.</param>\r
+        protected override void Update(GameTime gameTime)\r
+        {\r
+            // Get user's input state.\r
+\r
+            KeyboardState keyState = Keyboard.GetState();\r
+            MouseState mouseState = Mouse.GetState();\r
+\r
+            // Make a list of the keys pressed or released this frame.\r
+\r
+            List<Keys> pressedKeys = new List<Keys>();\r
+            List<Keys> releasedKeys = new List<Keys>();\r
+\r
+            Keys[] pressedKeysArray = keyState.GetPressedKeys();\r
+            foreach (Keys k in pressedKeysArray)\r
+                if (!lastPressedKeys.Contains(k))\r
+                    pressedKeys.Add(k);\r
+                else\r
+                    lastPressedKeys.Remove(k);\r
+\r
+            releasedKeys = lastPressedKeys;\r
+            lastPressedKeys = new List<Keys>(pressedKeysArray);\r
+\r
+            // Get mouse button state.\r
+\r
+            bool buttonPressed = mouseState.LeftButton == ButtonState.Pressed;\r
+\r
+            /***** Begining of game logic. *****/\r
+\r
+            // Debug - allow user to exit.\r
+\r
+            if (pressedKeys.Contains(Keys.Escape))\r
+                this.Exit();\r
+\r
+            // Debug - allow user on this machine to direct input to any player's state in the game.\r
+\r
+            if (pressedKeys.Contains(Keys.F1)) activePlayer = playerIdentifiers[0];\r
+            if (pressedKeys.Contains(Keys.F2)) activePlayer = playerIdentifiers[1];\r
+            if (pressedKeys.Contains(Keys.F3)) activePlayer = playerIdentifiers[2];\r
+            if (pressedKeys.Contains(Keys.F4)) activePlayer = playerIdentifiers[3];\r
+\r
+            // Debug - allow user on this machine to pause/resume game state advances.\r
+\r
+            if (pressedKeys.Contains(Keys.F12) ||\r
+                pressedKeys.Contains(Keys.P) && (keyState.IsKeyDown(Keys.LeftControl) || keyState.IsKeyDown(Keys.RightControl)))\r
+            {\r
+                paused = !paused;\r
+                return;  // Don't update on pause start or stop\r
+            }\r
+\r
+            // Debug - automatically pause every 1000 frames.\r
+\r
+            if (deterministicGame.CurrentFrameNumber % 1000 == 0 && deterministicGame.CurrentFrameNumber != lastAutoPause)\r
+            {\r
+                paused = true;\r
+                lastAutoPause = deterministicGame.CurrentFrameNumber;\r
+            }\r
+\r
+            // Game update\r
+\r
+            // Direct inputs to the game engine - only report changes.\r
+\r
+            foreach (Keys k in pressedKeys)\r
+                deterministicGame.ApplyKeyInput(activePlayer, k, true);\r
+\r
+            foreach (Keys k in releasedKeys)\r
+                deterministicGame.ApplyKeyInput(activePlayer, k, false);\r
+\r
+            deterministicGame.ApplyMouseLocationInput(activePlayer, mouseState.X, mouseState.Y);\r
+\r
+            if (lastButtonPressed != buttonPressed)\r
+                deterministicGame.ApplyMouseButtonInput(activePlayer, buttonPressed);\r
+\r
+            lastButtonPressed = buttonPressed;\r
+\r
+            if (!paused)\r
+            {\r
+                // Advance the game engine.\r
+\r
+                deterministicGame.Update(targetTimeSpan);\r
+            }\r
+\r
+            /***** End of game logic. *****/\r
+\r
+            // Allow the superclass to do any needed updates (unknown purpose).\r
+\r
+            base.Update(gameTime);\r
+        }\r
+\r
+        /// <summary>\r
+        /// This is called when the game should draw itself.\r
+        /// </summary>\r
+        /// <param name="gameTime">Provides a snapshot of timing values.</param>\r
+        protected override void Draw(GameTime gameTime)\r
+        {\r
+            GraphicsDevice.Clear(new Color(16, 16, 16, 255));  // Needed by the test harness, should be removed for the real game.\r
+\r
+            spriteBatch.Begin();\r
+\r
+            // Draw a few instructions.\r
+\r
+            if (paused && gameTime.TotalRealTime.Milliseconds < 500)\r
+                spriteBatch.DrawString(font, "-=> Paused <=-", new Vector2(10, 130), Color.White);\r
+\r
+            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);\r
+            spriteBatch.DrawString(font, "Press [ESC] to exit and [F12] to pause/unpause.  Game auto-pauses every 1000 frames.", new Vector2(10, 570), Color.White);\r
+\r
+            // Let the game draw itself.\r
+\r
+            deterministicGame.Draw(spriteBatch);\r
+            \r
+\r
+            spriteBatch.End();\r
+\r
+            base.Draw(gameTime);\r
+        }\r
+    }\r
+}\r
This page took 0.025254 seconds and 4 git commands to generate.