-
Notifications
You must be signed in to change notification settings - Fork 0
/
Menu.cpp
81 lines (77 loc) · 2.26 KB
/
Menu.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
#include "Arduino.h"
#include "Menu.h"
#include "Util.h"
#include "Math.h"
#include "Snake.h"
#include "Pong.h"
// Set current state to PLAYING (it means we're currently using menu)
Menu::Menu() : Game(GameState::PLAYING), mSelectedGame(0)
{
// Available game titles
mTitles[0] = "Snake";
mTitles[1] = "Pong";
}
/*
This function is called in loop()
It takes as argument an int input; input is the IR signal received.
This function, works as a state machine (states define in GameState structure)
*/
void Menu::Update(int input)
{
if (mState == GameState::PLAYING)
{
switch (input)
{
// move up the "<" cursor
case UP_KEY:
mSelectedGame = posmod(--mSelectedGame, NUMBER_OF_GAMES);
if(gSpeakerOn) musicPlayer.beep(10);
break;
// analog to UP_KEY case
case DOWN_KEY:
mSelectedGame = posmod(++mSelectedGame, NUMBER_OF_GAMES);
if(gSpeakerOn) musicPlayer.beep(10);
break;
// play selected game
case PLAY_PAUSE_KEY:
if(gSpeakerOn) musicPlayer.beep(10);
if (mGame != NULL)
delete mGame;
if (mSelectedGame == 0) // snake
mGame = new SnakeGame(snakeMap);
else if (mSelectedGame == 1)
mGame = new PongGame(snakeMap);
// Pause menu and start selected game
mState = GameState::PAUSE;
break;
}
Draw();
}
else if (mState == GameState::PAUSE)
{
mGame->Update(input);
if (mGame->GetState() == GameState::GO_MENU)
mState = GameState::PLAYING;
}
}
void Menu::Draw()
{
u8g2.firstPage();
do
{
u8g2.setCursor(20, 13);
u8g2.print(F("Choose a game:"));
// Draw the menu
for (uint8_t i = 0; i < NUMBER_OF_GAMES; ++i)
{
u8g2.setCursor(20, 13 * (i + 2));
// If it's the game pointed by cursor (i.e. mSelectedGame) draw also " <" pointer near game name
if (i == mSelectedGame)
{
u8g2.print(mTitles[i]);
u8g2.print(F(" <"));
}
else u8g2.print(mTitles[i]);
}
} while (u8g2.nextPage());
}