-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStateMachine.cpp
More file actions
39 lines (35 loc) · 1.39 KB
/
StateMachine.cpp
File metadata and controls
39 lines (35 loc) · 1.39 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
#include "Arduino.h"
#include "StateMachine.h"
StateMachine::StateMachine(Menu** currentMenu, Player** player, World** world, LedControl* lc, int* diff) : currentMenu(currentMenu), player(player), world(world),
lc(lc), diff(diff) {
this->state = BrowsingMenus;
}
void StateMachine::updateState() {
if(this->state == BrowsingMenus && (*this->currentMenu)->isPlaying()) {
this->state = PlayingGame;
*this->player = new Player(MAX_LIVES, START_HEIGHT);
*this->world = new World(this->lc, *this->player, *this->diff);
(*this->player)->setWorld(*this->world);
// recreate display menu so it uses the memory addresses of the current player
Menu* oldMenu = (*this->currentMenu);
oldMenu->freeOptions();
delete oldMenu;
(*this->currentMenu) = createDisplayMenu();
}
else if(this->state == PlayingGame && (*this->player)->getLives() <= 0) {
this->state = BrowsingMenus;
(*this->currentMenu)->clear();
Menu* oldMenu = (*this->currentMenu);
if(RWHelper::getLastHigh() < (*this->player)->getHeight()) {
(*this->currentMenu) = createCongratulationsMenu();
}
else {
(*this->currentMenu) = createGameOverMenu();
}
oldMenu->freeOptions();
delete oldMenu;
this->lc->clearDisplay(0);
delete *this->world;
delete *this->player;
}
}