From 919944f7c938fccb25b467d247b87610bbe00a8d Mon Sep 17 00:00:00 2001 From: Mikko Ahlroth Date: Tue, 22 Oct 2019 22:19:10 +0300 Subject: [PATCH] Implement start of SpeedGame --- src/base_game.hpp | 1 + src/game_manager.cpp | 25 ++++++++++++++----------- src/game_manager.hpp | 2 ++ src/speed_game.cpp | 34 ++++++++++++++++++++++++++++++++-- src/speed_game.hpp | 5 ++++- 5 files changed, 53 insertions(+), 14 deletions(-) diff --git a/src/base_game.hpp b/src/base_game.hpp index 6e955fc..f81b9da 100644 --- a/src/base_game.hpp +++ b/src/base_game.hpp @@ -15,6 +15,7 @@ struct NextOperation { }; class BaseGame { + protected: Randomiser* randomiser; public: diff --git a/src/game_manager.cpp b/src/game_manager.cpp index c35c596..dfdae03 100644 --- a/src/game_manager.cpp +++ b/src/game_manager.cpp @@ -1,26 +1,29 @@ #include "game_manager.hpp" #include +#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(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; + } } } diff --git a/src/game_manager.hpp b/src/game_manager.hpp index c055d8c..f34c2f6 100644 --- a/src/game_manager.hpp +++ b/src/game_manager.hpp @@ -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); diff --git a/src/speed_game.cpp b/src/speed_game.cpp index aa2eda3..ba46b66 100644 --- a/src/speed_game.cpp +++ b/src/speed_game.cpp @@ -1,8 +1,38 @@ #include "speed_game.hpp" +#include #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(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(beep); +} diff --git a/src/speed_game.hpp b/src/speed_game.hpp index 108bd2a..7ef3a2e 100644 --- a/src/speed_game.hpp +++ b/src/speed_game.hpp @@ -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; };