-
Notifications
You must be signed in to change notification settings - Fork 0
/
03Menu.cpp
105 lines (89 loc) · 2.85 KB
/
03Menu.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
#include "00Names.hpp"
void mainMenu();
String savepath = "/saves/Dev";
GState g_State;
Game gameSelect = Game(nullptr, mainMenu);
Function games[] = {
Mario::start,
Asteroids::start,
Tetris::start,
};
String gameNames[] = {
"Mario",
"Asteroids",
"Tetris",
};
void load() {
File file = SD.open(savepath + "/progress.dat", FILE_READ);
if (!file) {
file.close();
saveState();
file = SD.open(savepath + "/progress.dat", FILE_READ);
}
while (file.available()) {
String property = readstr(file);
if (property == "nGames") g_State.nGames = read<uint8_t>(file);
if (property == "mario.balance") g_State.mario.balance = read<uint32_t>(file);
if (property == "mario.level") g_State.mario.level = read<int16_t>(file);
if (property == "mario.state") g_State.mario.state = read<GState::MainQuest>(file);
if (property.substring(0, 16) == "mario.inventory.") g_State.mario.inventory[property.substring(16)] = read<uint32_t>(file);
}
file.close();
Script::load();
Mario::load();
game = gameSelect;
}
void saveState() {
SD.mkdir(savepath);
File file = SD.open(savepath + "/progress.dat", FILE_WRITE);
writeProperty(file, g_State.nGames, "nGames");
writeProperty(file, g_State.mario.balance, "mario.balance");
writeProperty(file, g_State.mario.level, "mario.level");
writeProperty(file, g_State.mario.state, "mario.state");
for (auto& entry : g_State.mario.inventory) {
writeProperty(file, entry.second, "mario.inventory." + entry.first);
}
file.close();
Script::save();
if (game.save) game.save();
}
static float reseting = 0;
void fileIO() {
if (reseting > 2 && buttonX.bReleased) {
removeDirectory(savepath);
load();
buttonX.bReleased = false;
reseting = 0;
}
static uint32_t timer; // Auto Save
if (millis() - timer > 10000) {
timer = millis();
saveState();
}
if (game.fileIO) game.fileIO();
}
void mainMenu() {
if (reseting > 2) {
oled::clear();
gui::centerText("X To reset", 0);
gui::centerText("Y To quit", oled::getCharHeight());
if (buttonY.bPressed) reseting = 0;
return;
}
static int pointer = 0;
static int secretStage = 0;
if (bJoyMoved) pointer = wrap(pointer + joy.y, g_State.nGames);
else if (buttonX.bReleased) games[pointer](), secretStage = 0;
if (bJoyMoved) {
if (joy.x > 0 && secretStage == 0) secretStage = 1;
else if (joy.x > 0 && secretStage == 1) secretStage = 2;
else if (joy.x < 0 && secretStage == 2) secretStage = 3;
else if (joy.x > 0 && secretStage == 3) /*Events::HappyBirthday::start(), */ secretStage = 0;
else if (joy != 0) secretStage = 0;
}
if (buttonY.bHeld) reseting += deltaTime;
else reseting = 0;
oled::clear();
gui::drawList("Games", gameNames, g_State.nGames, pointer);
gui::textAt(savepath.substring(savepath.lastIndexOf('/') + 1), 0, oled::height - oled::getCharHeight());
}