Implement start of SpeedGame
This commit is contained in:
parent
0a4b6805f3
commit
919944f7c9
5 changed files with 53 additions and 14 deletions
|
@ -15,6 +15,7 @@ struct NextOperation {
|
|||
};
|
||||
|
||||
class BaseGame {
|
||||
protected:
|
||||
Randomiser* randomiser;
|
||||
|
||||
public:
|
||||
|
|
|
@ -1,26 +1,29 @@
|
|||
#include "game_manager.hpp"
|
||||
|
||||
#include <Arduino.h>
|
||||
#include "base_game.hpp"
|
||||
#include "config.hpp"
|
||||
#include "debugger.hpp"
|
||||
#include "input.hpp"
|
||||
#include "randomiser.hpp"
|
||||
#include "speed_game.hpp"
|
||||
|
||||
GameManager::GameManager(Input* input, Randomiser* randomiser)
|
||||
: input(input), randomiser(randomiser), state(State::MENU), currentScore(0) {
|
||||
: input(input), randomiser(randomiser), state(State::GAME), currentScore(0) {
|
||||
debugPrint("Initialising new game...");
|
||||
currentGame = new SpeedGame(randomiser);
|
||||
}
|
||||
|
||||
void GameManager::loop() {
|
||||
static auto prevState = ButtonState::OFF;
|
||||
if (state == State::GAME) {
|
||||
auto nextOp = currentGame->loop();
|
||||
|
||||
input->process();
|
||||
auto status = input->getStatus();
|
||||
auto newState = status.buttons[0];
|
||||
|
||||
if (newState != prevState) {
|
||||
prevState = newState;
|
||||
|
||||
auto msg = String(static_cast<uint8_t>(newState));
|
||||
debugPrint(msg, false);
|
||||
if (nextOp.type == NextOperationType::ADD_SCORE) {
|
||||
currentScore += nextOp.scoreToAdd;
|
||||
debugPrint("Added score!");
|
||||
} else if (nextOp.type == NextOperationType::END) {
|
||||
debugPrint("Game ended.");
|
||||
state = State::SCORE;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
#include "base_game.hpp"
|
||||
#include "config.hpp"
|
||||
#include "input.hpp"
|
||||
#include "randomiser.hpp"
|
||||
|
@ -11,6 +12,7 @@ class GameManager {
|
|||
Randomiser* randomiser;
|
||||
State state;
|
||||
uint16_t currentScore;
|
||||
BaseGame* currentGame;
|
||||
|
||||
public:
|
||||
GameManager(Input* input, Randomiser* randomiser);
|
||||
|
|
|
@ -1,8 +1,38 @@
|
|||
#include "speed_game.hpp"
|
||||
|
||||
#include <Arduino.h>
|
||||
#include "base_game.hpp"
|
||||
#include "config.hpp"
|
||||
|
||||
SpeedGame::SpeedGame(Randomiser* randomiser)
|
||||
: BaseGame(randomiser), waitingBeeps{}, waitingBeepsAmount(0) {}
|
||||
: BaseGame(randomiser), waitingBeeps{}, waitingBeepsAmount(0), currentDelay(DELAY_START) {
|
||||
lastBeepAt = millis();
|
||||
}
|
||||
|
||||
NextOperation SpeedGame::loop() {}
|
||||
NextOperation SpeedGame::loop() {
|
||||
const auto now = millis();
|
||||
|
||||
if ((now - lastBeepAt) >= static_cast<uint32_t>(currentDelay)) {
|
||||
lastBeepAt = now;
|
||||
waitingBeeps[waitingBeepsAmount] = getNewBeep();
|
||||
++waitingBeepsAmount;
|
||||
currentDelay = DELAY_DECREMENT(currentDelay);
|
||||
|
||||
if (waitingBeepsAmount == MAX_WAITING) {
|
||||
return {NextOperationType::END, 0};
|
||||
} else {
|
||||
return {NextOperationType::ADD_SCORE, 1};
|
||||
}
|
||||
}
|
||||
|
||||
return {NextOperationType::NOOP, 0};
|
||||
}
|
||||
|
||||
uint8_t SpeedGame::getNewBeep() const {
|
||||
uint16_t beep = 0;
|
||||
do {
|
||||
beep = randomiser->get();
|
||||
} while (waitingBeepsAmount > 0 && beep == waitingBeeps[waitingBeepsAmount - 1]);
|
||||
|
||||
return static_cast<uint8_t>(beep);
|
||||
}
|
||||
|
|
|
@ -7,11 +7,14 @@
|
|||
/**
|
||||
* The default speed test game where you need to press the buttons with an ever increasing speed.
|
||||
*/
|
||||
class SpeedGame : BaseGame {
|
||||
class SpeedGame : public BaseGame {
|
||||
uint8_t waitingBeeps[MAX_WAITING];
|
||||
uint8_t waitingBeepsAmount;
|
||||
uint32_t lastBeepAt;
|
||||
uint16_t currentDelay;
|
||||
|
||||
public:
|
||||
SpeedGame(Randomiser* randomiser);
|
||||
NextOperation loop();
|
||||
uint8_t getNewBeep() const;
|
||||
};
|
||||
|
|
Reference in a new issue