-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHighscore.ino
More file actions
94 lines (84 loc) · 2.62 KB
/
Highscore.ino
File metadata and controls
94 lines (84 loc) · 2.62 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
void resetHighscores() {
const int delay = 2000;
static bool reseted = false;
static unsigned long timer = 0;
if (!reseted) {
timer = millis();
reseted = true;
gameSettings.highscoresCount = 0;
saveToEEPROM();
printLcdLines("Highscores", "reseted!", OPTION_NONE);
timer = millis();
}
if (millis() - timer > delay) {
reseted = false;
gameState = SETTINGS;
}
}
bool isNewHighscore() {
if (gameSettings.highscoresCount < MAX_HIGHSCORES_COUNT) {
return true;
}
return gameScore > gameSettings.highscores[MAX_HIGHSCORES_COUNT - 1].score;
}
void insertNewHighscore(const String& name) {
int& highscoresCount = gameSettings.highscoresCount;
byte position = MAX_HIGHSCORES_COUNT - 1;
if (highscoresCount < MAX_HIGHSCORES_COUNT) {
position = highscoresCount; // 1 after the end
highscoresCount++;
}
while (position > 0 && gameSettings.highscores[position - 1].score < gameScore) {
strcpy(gameSettings.highscores[position].name, gameSettings.highscores[position - 1].name);
gameSettings.highscores[position].score = gameSettings.highscores[position - 1].score;
position--;
}
strcpy(gameSettings.highscores[position].name, name.c_str());
gameSettings.highscores[position].score = gameScore;
saveToEEPROM();
}
void newHighscore() {
static bool menuChanged = true;
static String name = "AAAAA";
static byte cursorPos = 0;
const int LETTER_START_INDEX = 5;
const int MAX_CURSOR_POS = 5;
const int ARROW_INDEX = 11;
if (menuChanged) {
menuChanged = false;
printLcdLines("New highscore!", getNewHighscoreText(name).c_str(), OPTION_NONE);
if (cursorPos >= 0 && cursorPos <= MAX_CURSOR_POS-1) {
lcd.setCursor(cursorPos + LETTER_START_INDEX, 1);
} else {
lcd.setCursor(ARROW_INDEX, 1);
}
lcd.blink();
}
if (joyState == JOY_LEFT && cursorPos > 0) {
cursorPos--;
menuChanged = true;
} else if (joyState == JOY_RIGHT && cursorPos < MAX_CURSOR_POS) {
cursorPos++;
menuChanged = true;
} else if ((joyState == JOY_UP || joyState == JOY_DOWN) && cursorPos != MAX_CURSOR_POS) {
char letter = name.charAt(cursorPos);
if (joyState == JOY_UP) {
letter = letter + 1 > 'Z' ? 'A' : letter + 1;
} else {
letter = letter - 1 < 'A' ? 'Z' : letter - 1;
}
name.setCharAt(cursorPos, letter);
menuChanged = true;
}
if (joyPress && cursorPos == MAX_CURSOR_POS) {
insertNewHighscore(name);
lcd.noBlink();
menuChanged = true;
cursorPos = 0;
name = String("AAAAA");
gameState = MAINMENU;
}
}
String getNewHighscoreText(String name) {
return String("Name ") + name + String(" >");
}