/
LabGame.cs
266 lines (221 loc) · 9.15 KB
/
LabGame.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
// Copyright (c) 2010-2013 SharpDX - Alexandre Mutel
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
using SharpDX;
using SharpDX.Toolkit;
using System;
using System.Collections.Generic;
using Windows.UI.Input;
using Windows.UI.Core;
using Windows.Devices.Sensors;
namespace Project
{
// Use this namespace here in case we need to use Direct3D11 namespace as well, as this
// namespace will override the Direct3D11.
using SharpDX.Toolkit.Graphics;
using SharpDX.Toolkit.Input;
public class LabGame : Game
{
private GraphicsDeviceManager graphicsDeviceManager;
public List<GameObject> gameObjects;
private Stack<GameObject> addedGameObjects;
private Stack<GameObject> removedGameObjects;
private KeyboardManager keyboardManager;
public KeyboardState keyboardState;
private Player player;
private Crosshair crosshair;
public AccelerometerReading accelerometerReading;
public GyrometerReading gyrometerReading;
public GameInput input;
public int score;
public MainPage mainPage;
public SpriteBatch spriteBatch;
private SpriteFont arial16BMFont;
// TASK 4: Use this to represent difficulty
public float difficulty;
// Represents the camera's position and orientation
public Camera camera;
// Graphics assets
public Assets assets;
// Random number generator
public Random random;
// World boundaries that indicate where the edge of the screen is for the camera.
public float boundaryLeft;
public float boundaryRight;
public float boundaryTop;
public float boundaryBottom;
public bool started = false;
/// <summary>
/// Initializes a new instance of the <see cref="LabGame" /> class.
/// </summary>
public LabGame(MainPage mainPage)
{
// Creates a graphics manager. This is mandatory.
graphicsDeviceManager = new GraphicsDeviceManager(this);
// Setup the relative directory to the executable directory
// for loading contents with the ContentManager
Content.RootDirectory = "Content";
// Create the keyboard manager
keyboardManager = new KeyboardManager(this);
assets = new Assets(this);
random = new Random();
input = new GameInput();
// Set boundaries.
boundaryLeft = -4.5f * Camera.MaxModelSize / 5;
boundaryRight = 4.5f * Camera.MaxModelSize / 5;
boundaryTop = 4 * Camera.MaxModelSize / 5;
boundaryBottom = -4.5f;
// Initialise event handling.
input.gestureRecognizer.Tapped += Tapped;
input.gestureRecognizer.ManipulationStarted += OnManipulationStarted;
input.gestureRecognizer.ManipulationUpdated += OnManipulationUpdated;
input.gestureRecognizer.ManipulationCompleted += OnManipulationCompleted;
this.mainPage = mainPage;
score = 0;
difficulty = 1;
}
protected override void LoadContent()
{
// Initialise game object containers.
gameObjects = new List<GameObject>();
addedGameObjects = new Stack<GameObject>();
removedGameObjects = new Stack<GameObject>();
arial16BMFont = Content.Load<SpriteFont>("Arial16");
spriteBatch = ToDisposeContent(new SpriteBatch(GraphicsDevice));
// Create game objects.
player = new Player(this);
gameObjects.Add(player);
gameObjects.Add(new EnemyController(this));
gameObjects.Add(new AsteroidController(this));
crosshair = new Crosshair(this);
gameObjects.Add(crosshair);
// Create an input layout from the vertices
base.LoadContent();
}
protected override void Initialize()
{
Window.Title = "Lab 4";
camera = new Camera(this);
base.Initialize();
}
protected override void Update(GameTime gameTime)
{
if (started)
{
keyboardState = keyboardManager.GetState();
flushAddedAndRemovedGameObjects();
accelerometerReading = input.accelerometer.GetCurrentReading();
gyrometerReading = input.gyrometer.GetCurrentReading();
camera.Update();
for (int i = 0; i < gameObjects.Count; i++)
{
gameObjects[i].Update(gameTime);
}
mainPage.UpdateScore(score);
if (keyboardState.IsKeyDown(Keys.Escape))
{
this.Exit();
this.Dispose();
App.Current.Exit();
}
// Handle base.Update
}
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
if (started)
{
// Clears the screen with the Color.CornflowerBlue
//GraphicsDevice.Clear(Color.CornflowerBlue);
GraphicsDevice.Clear(Color.Black);
for (int i = 0; i < gameObjects.Count; i++)
{
gameObjects[i].Draw(gameTime);
}
}
// Debug
spriteBatch.Begin();
spriteBatch.DrawString(arial16BMFont, camera.angleX.ToString(), new Vector2(16, 40), Color.White);
spriteBatch.DrawString(arial16BMFont, camera.angleY.ToString(), new Vector2(16, 70), Color.White);
spriteBatch.DrawString(arial16BMFont, (Math.PI/8).ToString(), new Vector2(16, 100), Color.White);
spriteBatch.DrawString(arial16BMFont, crosshair.pos.Y.ToString(), new Vector2(16, 130), Color.White);
spriteBatch.End();
// Handle base.Draw
base.Draw(gameTime);
}
// Count the number of game objects for a certain type.
public int Count(GameObjectType type)
{
int count = 0;
foreach (var obj in gameObjects)
{
if (obj.type == type) { count++; }
}
return count;
}
// Add a new game object.
public void Add(GameObject obj)
{
if (!gameObjects.Contains(obj) && !addedGameObjects.Contains(obj))
{
addedGameObjects.Push(obj);
}
}
// Remove a game object.
public void Remove(GameObject obj)
{
if (gameObjects.Contains(obj) && !removedGameObjects.Contains(obj))
{
removedGameObjects.Push(obj);
}
}
// Process the buffers of game objects that need to be added/removed.
private void flushAddedAndRemovedGameObjects()
{
while (addedGameObjects.Count > 0) { gameObjects.Add(addedGameObjects.Pop()); }
while (removedGameObjects.Count > 0) { gameObjects.Remove(removedGameObjects.Pop()); }
}
public void OnManipulationStarted(GestureRecognizer sender, ManipulationStartedEventArgs args)
{
// Pass Manipulation events to the game objects.
}
public void Tapped(GestureRecognizer sender, TappedEventArgs args)
{
// Pass Manipulation events to the game objects.
foreach (var obj in gameObjects)
{
obj.Tapped(sender, args);
}
}
public void OnManipulationUpdated(GestureRecognizer sender, ManipulationUpdatedEventArgs args)
{
camera.pos.Z = camera.pos.Z * args.Delta.Scale;
// Update camera position for all game objects
foreach (var obj in gameObjects)
{
if (obj.basicEffect != null) { obj.basicEffect.View = camera.View; }
obj.OnManipulationUpdated(sender, args);
}
}
public void OnManipulationCompleted(GestureRecognizer sender, ManipulationCompletedEventArgs args)
{
}
}
}