init issue fixed
This commit is contained in:
79
src/game.cpp
79
src/game.cpp
@@ -4,26 +4,37 @@
|
||||
#include "simpleled.h"
|
||||
#include "magicSwitchBoard.h"
|
||||
#include "chainGame.h"
|
||||
#include "detectled.h"
|
||||
//#include "detectled.h"
|
||||
|
||||
std::vector<c_game *> gameslist;
|
||||
uint64_t GameSelectTimer = 0;
|
||||
e_state currentState = state_init;
|
||||
bool gamesConstructed = false;
|
||||
|
||||
c_game *game_simpleled = new c_simpleLed(YELLOW);
|
||||
c_game *game_magicswitchboard = new c_magicSwitchBoard(RED);
|
||||
c_game *game_chaingame = new c_chaingame(GREEN);
|
||||
c_game *game_detectled = new c_detectled(NONE);
|
||||
// c_game *game_detectled = new c_detectled(NONE);
|
||||
|
||||
void setGameState(e_state newstate);
|
||||
void constructGames(void);
|
||||
|
||||
void initGames(void)
|
||||
{
|
||||
constructGames();
|
||||
setGameState(state_init);
|
||||
}
|
||||
|
||||
void constructGames(void)
|
||||
{
|
||||
if (!gamesConstructed)
|
||||
{
|
||||
gameslist.clear();
|
||||
gameslist.push_back(game_simpleled);
|
||||
gameslist.push_back(game_magicswitchboard);
|
||||
gameslist.push_back(game_chaingame);
|
||||
gameslist.push_back(game_detectled);
|
||||
|
||||
activateGame(simpleled);
|
||||
gamesConstructed = true;
|
||||
}
|
||||
}
|
||||
|
||||
c_game *getGame(e_game game)
|
||||
@@ -160,6 +171,10 @@ void HandleGameSelectTimeout(void)
|
||||
void HandleGameIdle(void)
|
||||
{
|
||||
e_game nextGame = none;
|
||||
if(!gamesConstructed)
|
||||
{
|
||||
initGames();
|
||||
}
|
||||
|
||||
for (auto &&thisgame : gameslist)
|
||||
{
|
||||
@@ -167,22 +182,25 @@ void HandleGameIdle(void)
|
||||
{
|
||||
// prepare for next game
|
||||
nextGame = thisgame->getIndex();
|
||||
turnOffLed(thisgame->getGameColor());
|
||||
turnOnLed(thisgame->getGameColor());
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// wait for all buttons to be switched off
|
||||
if (!anybutton())
|
||||
{
|
||||
if (nextGame != none)
|
||||
{
|
||||
activateGame(nextGame);
|
||||
currentState = state_play;
|
||||
setGameState(state_play);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void setGameState(e_state newstate)
|
||||
{
|
||||
currentState = state_play;
|
||||
currentState = newstate;
|
||||
}
|
||||
|
||||
e_state getCurrentState(void)
|
||||
@@ -196,6 +214,24 @@ void handleGames(void)
|
||||
|
||||
switch (getCurrentState())
|
||||
{
|
||||
case state_init:
|
||||
{
|
||||
disableAllGames();
|
||||
setGameState(state_init);
|
||||
GameSelectTimer = 0;
|
||||
|
||||
if (anybutton())
|
||||
{
|
||||
setGameState(state_idle);
|
||||
}
|
||||
else
|
||||
{
|
||||
activateGame(simpleled);
|
||||
setGameState(state_play);
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case state_idle:
|
||||
{
|
||||
HandleGameIdle();
|
||||
@@ -207,32 +243,9 @@ void handleGames(void)
|
||||
runGames();
|
||||
}
|
||||
break;
|
||||
|
||||
case state_init:
|
||||
default:
|
||||
{
|
||||
//delay(1000);
|
||||
turnOffAllLed();
|
||||
activateGame(simpleled);
|
||||
|
||||
for (auto &&thisgame : gameslist)
|
||||
{
|
||||
if (onlyButton(thisgame->getGameColor()))
|
||||
{
|
||||
//prepare for next game
|
||||
activateGame(thisgame->getIndex());
|
||||
turnOnLed(thisgame->getGameColor());
|
||||
break;
|
||||
currentState = state_init;
|
||||
}
|
||||
}
|
||||
|
||||
//wait for all buttons idle
|
||||
while (anybutton())
|
||||
{
|
||||
}
|
||||
|
||||
turnOffAllLed();
|
||||
setGameState(state_play);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -48,12 +48,15 @@ public:
|
||||
e_ledcolor getGameColor(void) { return _gamecolor; }
|
||||
};
|
||||
|
||||
e_game getActiveGame(void);
|
||||
c_game *getGame(e_game game, e_ledcolor gamecolor);
|
||||
void runGame(e_game game);
|
||||
void runGames(void);
|
||||
void activateGame(e_game nextgame);
|
||||
void disableAllGames(void);
|
||||
|
||||
void initGames(void);
|
||||
void handleGames();
|
||||
|
||||
|
||||
#endif //GAMEH
|
||||
@@ -150,7 +150,7 @@ void handlePowerState(void)
|
||||
{
|
||||
if (!buttonread)
|
||||
{
|
||||
powerstate = on;
|
||||
powerstate = powerinit;
|
||||
powerOn();
|
||||
turnOffAllLed();
|
||||
delay(200);
|
||||
@@ -164,6 +164,12 @@ void handlePowerState(void)
|
||||
}
|
||||
}
|
||||
break;
|
||||
case powerinit:
|
||||
{
|
||||
initGames();
|
||||
powerstate = on;
|
||||
}
|
||||
break;
|
||||
case on:
|
||||
{
|
||||
if (buttonPower.pressedFor(100))
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
#include "led.h"
|
||||
#include "buttons.h"
|
||||
#include "board.h"
|
||||
#include "game.h"
|
||||
|
||||
#ifdef BTN_PWR
|
||||
#include "JC_Button.h"
|
||||
@@ -21,6 +22,7 @@ typedef enum
|
||||
off,
|
||||
poweringOn,
|
||||
poweringOn2,
|
||||
powerinit,
|
||||
on,
|
||||
poweringOff,
|
||||
poweringOff2,
|
||||
|
||||
Reference in New Issue
Block a user