-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameRoot.cs
More file actions
374 lines (302 loc) · 12.2 KB
/
GameRoot.cs
File metadata and controls
374 lines (302 loc) · 12.2 KB
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
using System;
using System.IO;
using System.Diagnostics;
using AndroidSpaceShip.Core;
using AndroidSpaceShip.Entities;
using AndroidSpaceShip.Screens;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using AndroidSpaceShip.Gui;
using Microsoft.Xna.Framework.Input.Touch;
namespace AndroidSpaceShip
{
public enum GameScreens
{
NONE = 0,
HOME = 1,
DEFAULT_GAME = 2,
SETTING = 3,
GAMEOVER = 4,
}
public class GameSettings
{
public float aspectRatio = 1f;
public bool vibration = true;
public bool audio = true;
public int bestKill = 0;
public int bestScore = 0;
public float bestTime = 0;
public bool init = false;
}
public class GameRoot : Game
{
public GraphicsDeviceManager graphicsDevice { get; private set; }
public SpriteBatch spriteBatch { get; private set; }
public SpriteFont font { get; private set; }
public Camera hudCamera { get; private set; }
public Vector2 cameraViewSize { get; private set; } = new Vector2(400, 1000);
public Action<long> Vibrate = (duration) => { };
public DefaultGame ScreenDefaultGame;
public Home ScreenHome;
public Setting ScreenSetting;
public GameOver ScreenGameOver;
public Screen currentScreen;
public Storage storage;
public GameSettings settings;
public float GuiScale = 3.5f;
public float FontScale = 0.25f;
private bool debugMode = false;
private IconButton debugButton = new IconButton();
public GameRoot()
{
graphicsDevice = new GraphicsDeviceManager(this)
{
IsFullScreen = true,
PreferHalfPixelOffset = true,
PreferMultiSampling = true,
HardwareModeSwitch = true,
SynchronizeWithVerticalRetrace = true,
GraphicsProfile = GraphicsProfile.HiDef,
SupportedOrientations = DisplayOrientation.Portrait,
/*
PreferredBackBufferWidth = GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Width,
PreferredBackBufferHeight = GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Height
*/
};
graphicsDevice.ApplyChanges();
Content.RootDirectory = "Content";
IsMouseVisible = true;
IsFixedTimeStep = false;
//IsFixedTimeStep = true;
//TargetElapsedTime = System.TimeSpan.FromSeconds(1d / 60);
}
protected override void Initialize()
{
this.graphicsDevice.PreferredBackBufferWidth = GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Width;
this.graphicsDevice.PreferredBackBufferHeight = GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Height;
this.graphicsDevice.ApplyChanges();
this.hudCamera = new Camera(GraphicsDevice.Viewport, cameraViewSize);
//this.hudCamera.Zoom = 0.5f;
this.hudCamera.UpdateMatrix();
this.ScreenDefaultGame = new DefaultGame(this);
this.ScreenDefaultGame.Id = (int)GameScreens.DEFAULT_GAME;
this.ScreenHome = new Home(this);
this.ScreenHome.Id = (int)GameScreens.HOME;
this.ScreenSetting = new Setting(this);
this.ScreenSetting.Id = (int)GameScreens.SETTING;
this.ScreenGameOver = new GameOver(this);
this.ScreenGameOver.Id = (int)GameScreens.GAMEOVER;
this.currentScreen = ScreenHome;
spriteBatch = new SpriteBatch(GraphicsDevice);
base.Initialize();
}
protected override void LoadContent()
{
font = Content.Load<SpriteFont>("font");
settings = new GameSettings();
settings.init = true;
settings.aspectRatio = GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.AspectRatio;
storage = new Storage();
this.LoadStorage();
this.ScreenDefaultGame.Load();
this.ScreenHome.Load();
this.ScreenSetting.Load();
this.ScreenGameOver.Load();
Texture2D debugIcon = Content.Load<Texture2D>("debug");
debugButton.Initialize(this.spriteBatch, this.Content, this.hudCamera);
debugButton.Load();
debugButton.Set(new Vector2(this.hudCamera.Size.X - 30f, this.hudCamera.Size.Y - 60f), debugIcon, this.GuiScale, true);
}
protected override void UnloadContent()
{
base.UnloadContent();
}
protected override void OnExiting(object sender, EventArgs args)
{
base.OnExiting(sender, args);
}
protected override void Update(GameTime gameTime)
{
float deltatime = (float)gameTime.ElapsedGameTime.TotalSeconds;
TouchCollection touchLocations = TouchPanel.GetState();
GameScreens next;
if ((next = (GameScreens)this.currentScreen.Update(deltatime, touchLocations)) != GameScreens.NONE)
{
if (next == GameScreens.HOME)
{
this.currentScreen = this.ScreenHome;
}
else if (next == GameScreens.DEFAULT_GAME)
{
this.currentScreen = this.ScreenDefaultGame;
this.currentScreen.Reset();
}
else if (next == GameScreens.SETTING)
{
this.currentScreen = this.ScreenSetting;
}
else if (next == GameScreens.GAMEOVER)
{
this.ScreenGameOver.Reset();
if (this.currentScreen.Id == (int)GameScreens.DEFAULT_GAME)
{
this.ScreenGameOver.backgroundY = this.ScreenDefaultGame.background.MoveY - (deltatime * this.ScreenDefaultGame.background.Speed) * 2;
this.ScreenGameOver.time = this.ScreenDefaultGame.time;
this.ScreenGameOver.kill = this.ScreenDefaultGame.player.Kill;
this.ScreenGameOver.score = (int)((this.ScreenDefaultGame.player.Kill + 1) * this.ScreenDefaultGame.time * 10);
}
this.currentScreen = this.ScreenGameOver;
}
}
this.UpdateDebugInformations(deltatime, touchLocations);
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.Black);
this.currentScreen.Draw();
//SpriteBatch.Begin(SpriteSortMode.FrontToBack, BlendState.NonPremultiplied, SamplerState.PointClamp, null, null, null, ScreenMatrix);
//Game Objects
//SpriteBatch.Begin(SpriteSortMode.Deferred, BlendState.NonPremultiplied, SamplerState.PointWrap, null, null, null, ScreenMatrix);
//Foreground Tile Layers
//SpriteBatch.Begin(SpriteSortMode.Deferred, BlendState.NonPremultiplied, SamplerState.PointClamp, null, null, null, ScreenMatrix);
//Normal Effects & Particles
//SpriteBatch.Begin(SpriteSortMode.Deferred, BlendState.Additive, SamplerState.PointClamp, null, null, null, ScreenMatrix);
//Other Effects & Particles
this.DrawDebugInformations(gameTime);
base.Draw(gameTime);
}
public void OnPause()
{
if (this.currentScreen != null)
{
this.currentScreen.OnPause();
}
}
public void OnResume()
{
if(this.currentScreen != null)
{
this.currentScreen.OnResume();
}
}
public void OnRestart()
{
}
public void ChangeViewport(int[] sizes)
{
this.graphicsDevice.PreferredBackBufferWidth = sizes[0];
this.graphicsDevice.PreferredBackBufferHeight = sizes[1];
this.graphicsDevice.ApplyChanges();
this.ScreenHome.camera.ChangeViewport(GraphicsDevice.Viewport);
this.ScreenSetting.camera.ChangeViewport(GraphicsDevice.Viewport);
this.ScreenDefaultGame.camera.ChangeViewport(GraphicsDevice.Viewport);
this.ScreenGameOver.camera.ChangeViewport(GraphicsDevice.Viewport);
}
public int[] CalcAspectRatio(int width, float aspectRatio)
{
int[] size = new int[2];
size[0] = width;
size[1] = (int)(width / aspectRatio);
return size;
}
private void LoadStorage()
{
if (storage.Init())
{
storage.Write(settings);
}
else
{
try
{
GameSettings testSettings = storage.Read().ToObject<GameSettings>();
if (testSettings.init)
{
settings = testSettings;
if (settings.aspectRatio != GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.AspectRatio)
{
int[] sizes = this.CalcAspectRatio(this.graphicsDevice.PreferredBackBufferWidth, this.settings.aspectRatio);
this.ChangeViewport(sizes);
}
}
else
{
storage.Write(settings);
}
}
catch
{
storage.Write(settings);
}
}
}
private void UpdateDebugInformations(float deltatime, TouchCollection touchLocations)
{
#if TEST || DEBUG
if (this.currentScreen.Id == (int)GameScreens.HOME)
{
this.debugButton.Update(deltatime, touchLocations);
if (this.debugButton.isClick())
{
this.debugMode = !this.debugMode;
}
}
#endif
}
private void DrawDebugInformations(GameTime gameTime)
{
#if TEST || DEBUG
if (this.currentScreen.Id == (int)GameScreens.HOME)
{
this.debugButton.Draw();
}
float deltatime = (float)gameTime.ElapsedGameTime.TotalSeconds;
if (deltatime == 0)
{
return;
}
if (!this.debugMode)
{
return;
}
spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.NonPremultiplied, SamplerState.PointClamp, null, null, null, this.hudCamera.Transform);
string info = $@" Graphics Adapter {GraphicsDevice.Adapter.Description}
Display Mode {GraphicsDevice.Adapter.CurrentDisplayMode}
FPS {1 / deltatime}
Deltatime {deltatime}
Resolution {graphicsDevice.PreferredBackBufferWidth}x{graphicsDevice.PreferredBackBufferHeight}
Aspect Ratio {(float)graphicsDevice.PreferredBackBufferWidth / (float)graphicsDevice.PreferredBackBufferHeight}
Screen Id {this.currentScreen.Id}
Storage Error {!storage.StorageOK}
Loading Settings {storage.Loading}";
Process proc = Process.GetCurrentProcess();
info += $@"
Name {proc.ProcessName}
Threads {proc.Threads.Count}
Memory {proc.PrivateMemorySize64 / (1024.0 * 1024.0)} Mo";
proc.Dispose();
info += $@"
Game
Position {this.ScreenDefaultGame.player.position}
Velocity {this.ScreenDefaultGame.player.velocity}
Life {this.ScreenDefaultGame.player.Life}
Kill {this.ScreenDefaultGame.player.Kill}
Time {this.ScreenDefaultGame.time}
Game Speed x{this.ScreenDefaultGame.GameSpeed}
New Cloud {this.ScreenDefaultGame.cloudManager.addTime}
New Enemy {this.ScreenDefaultGame.enemyManager.addTime}
";
#if DEBUG
info += "\n Mode DEBUG";
#else
info += "\n Mode TEST";
#endif
info += $"\n Version {System.Reflection.Assembly.GetExecutingAssembly().GetName().Version}";
spriteBatch.DrawString(font, info, Vector2.Zero, Color.White, 0, Vector2.Zero, FontScale / 2f, SpriteEffects.None, 0);
spriteBatch.End();
#endif
}
}
}