-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame.cpp
More file actions
102 lines (83 loc) · 2.76 KB
/
Copy pathgame.cpp
File metadata and controls
102 lines (83 loc) · 2.76 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
#include "game.h"
void game::_eventHandler( char* lastButtonPressPtr )
{
if( *lastButtonPressPtr != 0 )
{
String buttonPressed = String( *lastButtonPressPtr );
if( buttonPressed == "F" )
{
flags::isJoystickConnected = false;
game::_currentGameState = game::gameState::JOYSTICK_NOT_CONNECTED;
}
if( buttonPressed == "T" )
{
flags::isJoystickConnected = true;
game::_currentGameState = game::gameState::INIT;
}
switch( game::_currentGameState )
{
case game::gameState::SELECT_DIFFICULTY:
if( buttonPressed == "B" || buttonPressed == "Y" || buttonPressed == "A" )
{
if( buttonPressed == "B" ) config::setDifficulty( config::difficulty::EASY );
else if( buttonPressed == "Y" ) config::setDifficulty( config::difficulty::MEDIUM );
else if( buttonPressed == "A" ) config::setDifficulty( config::difficulty::HARD );
game::_hasDifficultyBeenSet = true;
}
break;
case game::gameState::GAME:
snake::enqueueDirection( buttonPressed );
break;
}
*lastButtonPressPtr = 0;
}
}
void game::game( char* lastButtonPressPtr )
{
unsigned long currentTime = millis();
bool update = currentTime - game::_lastTime >= config::getGameConfig().UPDATE_DELAY;
game::_eventHandler( lastButtonPressPtr );
switch( game::_currentGameState )
{
case game::gameState::INIT:
leds::init();
flags::canProcessInput = true;
game::_currentGameState = game::gameState::SELECT_DIFFICULTY;
break;
case game::gameState::SELECT_DIFFICULTY:
leds::displayConfigScene();
if( game::_hasDifficultyBeenSet )
{
game::_currentGameState = game::gameState::STARTUP_SEQUENCE;
flags::canProcessInput = false;
}
break;
case game::gameState::STARTUP_SEQUENCE:
if( !sequence::isStartupSequenceDone ) sequence::startupSequence();
else
{
snake::initBoard();
snake::initSnake();
snake::initFood();
game::_currentGameState = game::gameState::GAME;
}
break;
case game::gameState::GAME:
if( update )
{
game::_lastTime = currentTime;
snake::move();
leds::displayBoard( snake::board,snake::linearSnakeHeadIndex );
leds::sevenSegmentDisplayWrite( snake::snakeVec.size() - config::INIT_SNAKE_LENGTH );
if( snake::hasGameEnded() ) game::_currentGameState = game::gameState::ENDING_SEQUENCE;
}
break;
case game::gameState::ENDING_SEQUENCE:
if( snake::hasWon() ) sequence::gamewonSequence();
else sequence::gameoverSequence();
softwareReset();
case game::gameState::JOYSTICK_NOT_CONNECTED:
leds::displayColor( colors::BLUE );
break;
}
}