]> Dogcows Code - chaz/carfire/blob - 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
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
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;
14
15 namespace CS_3505_Project_06
16 {
17 /// <summary>
18 /// A game outline for testing network communications
19 /// </summary>
20 public class Game06 : Microsoft.Xna.Framework.Game
21 {
22 GraphicsDeviceManager graphics;
23 SpriteBatch spriteBatch;
24 SpriteFont font;
25
26 IDeterministicGame deterministicGame;
27 TimeSpan targetTimeSpan;
28 Object[] playerIdentifiers = { "One", "Two", "Three", "Four" }; // Any objects will do, strings are easy to debug.
29
30 // For debugging
31
32 List<Keys> lastPressedKeys;
33 bool lastButtonPressed;
34 Object activePlayer;
35 bool paused;
36 long lastAutoPause;
37
38 // Constructor
39
40 public Game06()
41 {
42 graphics = new GraphicsDeviceManager(this);
43 Content.RootDirectory = "Content";
44
45 // Make the game object. The game is currently called 'testHarness'.
46
47 deterministicGame = new TestHarness();
48
49 // Debugging setup
50
51 lastPressedKeys = new List<Keys>();
52 activePlayer = playerIdentifiers[0];
53 paused = false;
54 }
55
56 /// <summary>
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.
61 /// </summary>
62 protected override void Initialize()
63 {
64 // Set a fixed time span of 1/60th of a second.
65
66 targetTimeSpan = new TimeSpan(166666); // In 100 nanosecond units = 16 666 600 nanoseconds
67 IsFixedTimeStep = true;
68 TargetElapsedTime = targetTimeSpan;
69
70 // Reset the game - indicate that player #1 (player 0) owns this instance of the game.
71
72 deterministicGame.ResetGame(playerIdentifiers, playerIdentifiers[0]);
73
74 // For debugging - reset the mouse position to the center of the window.
75
76 Mouse.SetPosition(400, 300);
77
78 // Allow the base class to initialize.
79
80 base.Initialize();
81 }
82
83 /// <summary>
84 /// LoadContent will be called once per game and is the place to load
85 /// all of your content.
86 /// </summary>
87 protected override void LoadContent()
88 {
89 // Create a new SpriteBatch, which can be used to draw textures.
90
91 spriteBatch = new SpriteBatch(GraphicsDevice);
92
93 // Let the game load its content.
94
95 font = Content.Load<SpriteFont>("InstructionFont");
96
97 deterministicGame.LoadContent(Content);
98 }
99
100 /// <summary>
101 /// UnloadContent will be called once per game and is the place to unload
102 /// all content.
103 /// </summary>
104 protected override void UnloadContent()
105 {
106 deterministicGame.UnloadContent();
107 }
108
109 /// <summary>
110 /// Allows the game to run logic such as updating the world,
111 /// checking for collisions, gathering input, and playing audio.
112 /// </summary>
113 /// <param name="gameTime">Provides a snapshot of timing values.</param>
114 protected override void Update(GameTime gameTime)
115 {
116 // Get user's input state.
117
118 KeyboardState keyState = Keyboard.GetState();
119 MouseState mouseState = Mouse.GetState();
120
121 // Make a list of the keys pressed or released this frame.
122
123 List<Keys> pressedKeys = new List<Keys>();
124 List<Keys> releasedKeys = new List<Keys>();
125
126 Keys[] pressedKeysArray = keyState.GetPressedKeys();
127 foreach (Keys k in pressedKeysArray)
128 if (!lastPressedKeys.Contains(k))
129 pressedKeys.Add(k);
130 else
131 lastPressedKeys.Remove(k);
132
133 releasedKeys = lastPressedKeys;
134 lastPressedKeys = new List<Keys>(pressedKeysArray);
135
136 // Get mouse button state.
137
138 bool buttonPressed = mouseState.LeftButton == ButtonState.Pressed;
139
140 /***** Begining of game logic. *****/
141
142 // Debug - allow user to exit.
143
144 if (pressedKeys.Contains(Keys.Escape))
145 this.Exit();
146
147 // Debug - allow user on this machine to direct input to any player's state in the game.
148
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];
153
154 // Debug - allow user on this machine to pause/resume game state advances.
155
156 if (pressedKeys.Contains(Keys.F12) ||
157 pressedKeys.Contains(Keys.P) && (keyState.IsKeyDown(Keys.LeftControl) || keyState.IsKeyDown(Keys.RightControl)))
158 {
159 paused = !paused;
160 return; // Don't update on pause start or stop
161 }
162
163 // Debug - automatically pause every 1000 frames.
164
165 if (deterministicGame.CurrentFrameNumber % 1000 == 0 && deterministicGame.CurrentFrameNumber != lastAutoPause)
166 {
167 paused = true;
168 lastAutoPause = deterministicGame.CurrentFrameNumber;
169 }
170
171 // Game update
172
173 // Direct inputs to the game engine - only report changes.
174
175 foreach (Keys k in pressedKeys)
176 deterministicGame.ApplyKeyInput(activePlayer, k, true);
177
178 foreach (Keys k in releasedKeys)
179 deterministicGame.ApplyKeyInput(activePlayer, k, false);
180
181 deterministicGame.ApplyMouseLocationInput(activePlayer, mouseState.X, mouseState.Y);
182
183 if (lastButtonPressed != buttonPressed)
184 deterministicGame.ApplyMouseButtonInput(activePlayer, buttonPressed);
185
186 lastButtonPressed = buttonPressed;
187
188 if (!paused)
189 {
190 // Advance the game engine.
191
192 deterministicGame.Update(targetTimeSpan);
193 }
194
195 /***** End of game logic. *****/
196
197 // Allow the superclass to do any needed updates (unknown purpose).
198
199 base.Update(gameTime);
200 }
201
202 /// <summary>
203 /// This is called when the game should draw itself.
204 /// </summary>
205 /// <param name="gameTime">Provides a snapshot of timing values.</param>
206 protected override void Draw(GameTime gameTime)
207 {
208 GraphicsDevice.Clear(new Color(16, 16, 16, 255)); // Needed by the test harness, should be removed for the real game.
209
210 spriteBatch.Begin();
211
212 // Draw a few instructions.
213
214 if (paused && gameTime.TotalRealTime.Milliseconds < 500)
215 spriteBatch.DrawString(font, "-=> Paused <=-", new Vector2(10, 130), Color.White);
216
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);
219
220 // Let the game draw itself.
221
222 deterministicGame.Draw(spriteBatch);
223
224
225 spriteBatch.End();
226
227 base.Draw(gameTime);
228 }
229 }
230 }
This page took 0.049598 seconds and 4 git commands to generate.