-
Notifications
You must be signed in to change notification settings - Fork 0
/
Game.cpp
123 lines (113 loc) · 3.2 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
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
#include "Game.h"
Game::Game(Buttons *buttons, DemoMode *demoMode) :
buttons(buttons), demoMode(demoMode), state(GameState::WELCOME), maximumRoundNumber(0), roundNumber(0) {
}
void Game::welcome() {
maximumRoundNumber = 4;
roundNumber = 0;
state = GameState::WELCOME;
}
void Game::startOver() {
maximumRoundNumber = 0;
lastButtonPressedAt = millis();
for (int16_t i = 0; i < GAME_ROUNDS_MAXIMUM; ++i) {
rounds[i] = 0;
}
increase();
}
void Game::increase() {
rounds[maximumRoundNumber] = random(0, 4);
maximumRoundNumber++;
roundNumber = 0;
state = GameState::PLAYBACK;
}
void Game::tick() {
switch (state) {
case GameState::WELCOME: {
if (millis() - transitionAt > 500) {
if (roundNumber < maximumRoundNumber) {
buttons->play(roundNumber);
roundNumber++;
}
else {
startOver();
}
transitionAt = millis();
}
break;
}
case GameState::INACTIVE: {
buttons->tick();
int8_t pressed = buttons->dequeuePress();
if (pressed >= 0) {
startOver();
}
else {
demoMode->tick();
}
break;
}
case GameState::WAITING: {
buttons->tick();
if (millis() - lastButtonPressedAt > 500) {
buttons->off();
}
int8_t pressed = buttons->dequeuePress();
if (pressed >= 0) {
lastButtonPressedAt = millis();
if (pressed == rounds[roundNumber]) {
buttons->play(pressed);
roundNumber++;
if (roundNumber == maximumRoundNumber) {
state = GameState::SUCCESS;
transitionAt = millis();
}
}
else {
buttons->fail();
state = GameState::FAIL;
transitionAt = millis();
}
}
if (lastButtonPressedAt > 0 && millis() - lastButtonPressedAt > 1000 * 30) {
lastButtonPressedAt = 0;
state = GameState::INACTIVE;
demoMode->start();
transitionAt = millis();
}
break;
}
case GameState::PLAYBACK: {
if (millis() - transitionAt > 400) {
if (buttons->anyOn()) {
buttons->off();
}
if (millis() - transitionAt > 500) {
if (roundNumber < maximumRoundNumber) {
buttons->play(rounds[roundNumber]);
roundNumber++;
}
else {
roundNumber = 0;
buttons->off();
state = GameState::WAITING;
}
transitionAt = millis();
}
}
break;
}
case GameState::SUCCESS: {
if (millis() - transitionAt > 1000) {
increase();
}
break;
}
case GameState::FAIL: {
if (millis() - transitionAt > 1500) {
startOver();
}
break;
}
}
}