-
Notifications
You must be signed in to change notification settings - Fork 0
/
Game.cpp
77 lines (61 loc) · 1.5 KB
/
Game.cpp
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
#include "pch.h"
#include "Game.h"
#include "PhatLeetLib.h"
Game::Game(Sprites& sprites) :
sprites(sprites),
world ({})
{
scenes.active = GameSceneId::Intro;
}
bool Game::WantQuit()
{
return
::WantQuit()
|| IsKeyDown(VK_ESCAPE);
}
void Game::GameLoop()
{
PerformanceTimer timer;
bool need_begin = true;
while (!WantQuit())
{
auto elapsed = timer.ElapsedSinceLast() * AdvancePerSecond;
time += elapsed;
bool is_done = false;
do
{
Scene& scene = scenes.FromSceneId(scenes.active);
if (need_begin)
scene.Begin(world);
need_begin = false;
scene.Advance(elapsed);
scene.Render();
if (scenes.active != GameSceneId::GameOver &&
GetWorld().player.IsActive() && GetWorld().player.GetLife() == 0)
{
scenes.active = GameSceneId::GameOver;
need_begin = true;
continue;
}
is_done = scene.IsDone();
if (is_done)
{
if (scenes.active == GameSceneId::GameOver)
scenes.active = GameSceneId::Intro;
else if (scenes.active == GameSceneId::AmbushVictory )
scenes.active = GameSceneId::AmbushGameplay; // Give us more money to develop more
else
scenes.active = (GameSceneId)((int)scenes.active + 1);
if (scenes.active == GameSceneId::End)
scenes.active = GameSceneId::Intro;
need_begin = true;
}
} while (is_done);
Flip();
}
}
Game& GetGame()
{
static Game game (GetSprites()); // Attempt for re-entrancy will deadlock here. A good thing. No initialization of world must refer to game.
return game;
}