Implement start of SpeedGame

This commit is contained in:
Mikko Ahlroth 2019-10-22 22:19:10 +03:00
parent 0a4b6805f3
commit 919944f7c9
5 changed files with 53 additions and 14 deletions

View file

@ -15,6 +15,7 @@ struct NextOperation {
}; };
class BaseGame { class BaseGame {
protected:
Randomiser* randomiser; Randomiser* randomiser;
public: public:

View file

@ -1,26 +1,29 @@
#include "game_manager.hpp" #include "game_manager.hpp"
#include <Arduino.h> #include <Arduino.h>
#include "base_game.hpp"
#include "config.hpp" #include "config.hpp"
#include "debugger.hpp" #include "debugger.hpp"
#include "input.hpp"
#include "randomiser.hpp" #include "randomiser.hpp"
#include "speed_game.hpp"
GameManager::GameManager(Input* input, Randomiser* randomiser) 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..."); debugPrint("Initialising new game...");
currentGame = new SpeedGame(randomiser);
} }
void GameManager::loop() { void GameManager::loop() {
static auto prevState = ButtonState::OFF; if (state == State::GAME) {
auto nextOp = currentGame->loop();
input->process(); if (nextOp.type == NextOperationType::ADD_SCORE) {
auto status = input->getStatus(); currentScore += nextOp.scoreToAdd;
auto newState = status.buttons[0]; debugPrint("Added score!");
} else if (nextOp.type == NextOperationType::END) {
if (newState != prevState) { debugPrint("Game ended.");
prevState = newState; state = State::SCORE;
}
auto msg = String(static_cast<uint8_t>(newState));
debugPrint(msg, false);
} }
} }

View file

@ -1,3 +1,4 @@
#include "base_game.hpp"
#include "config.hpp" #include "config.hpp"
#include "input.hpp" #include "input.hpp"
#include "randomiser.hpp" #include "randomiser.hpp"
@ -11,6 +12,7 @@ class GameManager {
Randomiser* randomiser; Randomiser* randomiser;
State state; State state;
uint16_t currentScore; uint16_t currentScore;
BaseGame* currentGame;
public: public:
GameManager(Input* input, Randomiser* randomiser); GameManager(Input* input, Randomiser* randomiser);

View file

@ -1,8 +1,38 @@
#include "speed_game.hpp" #include "speed_game.hpp"
#include <Arduino.h>
#include "base_game.hpp" #include "base_game.hpp"
#include "config.hpp"
SpeedGame::SpeedGame(Randomiser* randomiser) 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);
}

View file

@ -7,11 +7,14 @@
/** /**
* The default speed test game where you need to press the buttons with an ever increasing speed. * 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 waitingBeeps[MAX_WAITING];
uint8_t waitingBeepsAmount; uint8_t waitingBeepsAmount;
uint32_t lastBeepAt;
uint16_t currentDelay;
public: public:
SpeedGame(Randomiser* randomiser); SpeedGame(Randomiser* randomiser);
NextOperation loop(); NextOperation loop();
uint8_t getNewBeep() const;
}; };