-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgametitle.cpp
More file actions
115 lines (95 loc) · 2.5 KB
/
Copy pathgametitle.cpp
File metadata and controls
115 lines (95 loc) · 2.5 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
#include "gametitle.h"
#include "buttonwidget.h"
#include "data_title.h"
#include "gamecredits.h"
#include "gamerunning.h"
#include "gamebuino_fix.h"
#include <cassert>
namespace {
uint16_t gameStartSound[] = {
0x0005,
0x178, 0x17A, 0x27C, 0x17E, 0x180, 0x286, 0x188,
0x0000
};
const char* PLAY_TEXT = "PLAY";
ButtonWidget::Parameters startWidgetParameters = {
{ 25, 47 },
{ 5, 52 },
ButtonWidget::BLINK_A,
PLAY_TEXT
};
const char* CREDITS_TEXT = "CREDITS";
ButtonWidget::Parameters creditWidgetParameters = {
{ 30, 47 },
{ 2, 52 },
ButtonWidget::BLINK_MENU,
CREDITS_TEXT
};
uint8_t TIME_FOR_ALTERNATE_WIDGET = 50;
}
GameTitle::GameTitle()
: titleImage(new Gamebuino_Meta::Image(getTitleData()))
, startGameDisplay(new ButtonWidget(startWidgetParameters))
, goToCreditsDisplay(new ButtonWidget(creditWidgetParameters))
, alternateTimer(TIME_FOR_ALTERNATE_WIDGET)
{
gb.sound.play(gameStartSound);
}
void GameTitle::update()
{
gb.display.drawImage(0, 0, *titleImage);
gb.display.setColor(Color::gray);
gb.display.setFontSize(1);
gb.display.setCursor(64, 59);
gb.display.println("v0.9");
widgetUpdateAndDisplay();
if (gb.buttons.pressed(BUTTON_A)) {
start_game();
} else if (gb.buttons.pressed(BUTTON_MENU)) {
start_credits();
}
}
void GameTitle::widgetUpdateAndDisplay()
{
if (alternateTimer == 0) {
alternateTimer = TIME_FOR_ALTERNATE_WIDGET;
currentWidgetDisplayed = currentWidgetDisplayed == 0 ? 1 : 0;
} else {
alternateTimer -= 1;
}
if (currentWidgetDisplayed == 0) {
startGameDisplay->update();
startGameDisplay->display();
} else {
goToCreditsDisplay->update();
goToCreditsDisplay->display();
}
}
void GameTitle::start_game()
{
action = GO_TO_GAME;
}
void GameTitle::start_credits()
{
action = GO_TO_CREDITS;
}
bool GameTitle::finished()
{
return action != STAY_HERE;
}
std::unique_ptr<GameState> GameTitle::new_state()
{
assert(finished());
auto& take_action = action;
auto state = [take_action]() -> std::unique_ptr<GameState> {
switch (take_action) {
case GO_TO_GAME:
return std::unique_ptr<GameState>(new GameRunning());
case GO_TO_CREDITS:
return std::unique_ptr<GameState>(new GameCredits());
}
return {};
}();
action = STAY_HERE;
return state;
};