From 5bce8d78c01bd51fa4b0cfa5c8afa1b011a70596 Mon Sep 17 00:00:00 2001 From: Mikko Ahlroth Date: Thu, 17 Oct 2019 18:51:01 +0300 Subject: [PATCH] Restructure stuff --- nopeustesti.ino | 10 ++++------ src/base_game.cpp | 4 ++++ src/base_game.hpp | 23 +++++++++++++++++++++++ src/debugger.cpp | 6 ++---- src/debugger.hpp | 2 +- src/game.cpp | 35 ----------------------------------- src/game.hpp | 20 -------------------- src/game_manager.cpp | 13 +++++++++++++ src/game_manager.hpp | 17 +++++++++++++++++ src/menu.cpp | 0 src/menu.hpp | 0 src/randomiser.cpp | 8 ++------ src/randomiser.hpp | 2 +- src/speed_game.cpp | 8 ++++++++ src/speed_game.hpp | 14 ++++++++++++++ 15 files changed, 89 insertions(+), 73 deletions(-) create mode 100644 src/base_game.cpp create mode 100644 src/base_game.hpp delete mode 100644 src/game.cpp delete mode 100644 src/game.hpp create mode 100644 src/game_manager.cpp create mode 100644 src/game_manager.hpp create mode 100644 src/menu.cpp create mode 100644 src/menu.hpp create mode 100644 src/speed_game.cpp create mode 100644 src/speed_game.hpp diff --git a/nopeustesti.ino b/nopeustesti.ino index a65e193..1e13385 100644 --- a/nopeustesti.ino +++ b/nopeustesti.ino @@ -1,17 +1,15 @@ #include "src/debugger.hpp" -#include "src/game.hpp" +#include "src/game_manager.hpp" #include "src/randomiser.hpp" Debugger* debugger; Randomiser* randomiser; -Game* game; +GameManager* game; void setup() { debugger = new Debugger(9600); randomiser = new Randomiser(analogRead(0)); - game = new Game(debugger, randomiser); + game = new GameManager(debugger, randomiser); } -void loop() { - game->loop(); -} +void loop() { game->loop(); } diff --git a/src/base_game.cpp b/src/base_game.cpp new file mode 100644 index 0000000..a7f19aa --- /dev/null +++ b/src/base_game.cpp @@ -0,0 +1,4 @@ +#include "base_game.hpp" + +BaseGame::BaseGame(Debugger* debugger, Randomiser* randomiser) + : debugger(debugger), randomiser(randomiser) {} diff --git a/src/base_game.hpp b/src/base_game.hpp new file mode 100644 index 0000000..7083fa6 --- /dev/null +++ b/src/base_game.hpp @@ -0,0 +1,23 @@ +#include +#include "debugger.hpp" +#include "randomiser.hpp" + +enum class NextOperationType { NOOP, END, ADD_SCORE }; + +/** + * Next operation to be run by the game manager when the current loop has ended. + */ +struct NextOperation { + NextOperationType type; + uint8_t scoreToAdd; +}; + +class BaseGame { + Debugger* debugger; + Randomiser* randomiser; + + public: + BaseGame(Debugger* debugger, Randomiser* randomiser); + + virtual NextOperation loop() = 0; +}; diff --git a/src/debugger.cpp b/src/debugger.cpp index 55190a3..0133b8e 100644 --- a/src/debugger.cpp +++ b/src/debugger.cpp @@ -3,11 +3,9 @@ #include #include "config.hpp" -Debugger::Debugger(uint16_t port) : serialPort(port) { - Serial.begin(serialPort); -} +Debugger::Debugger(uint16_t port) : serialPort(port) { Serial.begin(serialPort); } -void Debugger::print(String msg, bool newline) { +void Debugger::print(const String msg, const bool newline) const { if (DEBUG) { auto msgCopy = String(msg); diff --git a/src/debugger.hpp b/src/debugger.hpp index cc74e13..ec2c302 100644 --- a/src/debugger.hpp +++ b/src/debugger.hpp @@ -12,5 +12,5 @@ class Debugger { public: Debugger(uint16_t port); - void print(String msg, bool newline = true); + void print(const String msg, const bool newline = true) const; }; diff --git a/src/game.cpp b/src/game.cpp deleted file mode 100644 index e9ccaaf..0000000 --- a/src/game.cpp +++ /dev/null @@ -1,35 +0,0 @@ -#include "game.hpp" - -#include -#include "config.hpp" -#include "debugger.hpp" -#include "randomiser.hpp" - -Game::Game(Debugger* debugger, Randomiser* randomiser) - : debugger(debugger), - randomiser(randomiser), - state(State::MENU), - currentDelay(DELAY_START), - currentScore(0), - waitingBeeps{}, - waitingBeepsAmount(0) { - debugger->print("Initialising new game..."); -} - -void Game::loop() { - ++currentScore; - - auto msg = String("Delay: "); - msg.concat(currentDelay); - debugger->print(msg); - - msg = String("Score: "); - msg.concat(currentScore); - debugger->print(msg); - - msg = String(randomiser->get()); - debugger->print(msg); - - delay(currentDelay); - currentDelay = DELAY_DECREMENT(currentDelay); -} diff --git a/src/game.hpp b/src/game.hpp deleted file mode 100644 index 8fd222f..0000000 --- a/src/game.hpp +++ /dev/null @@ -1,20 +0,0 @@ -#include "config.hpp" -#include "debugger.hpp" -#include "randomiser.hpp" - -enum class State { MENU, GAME, SCORE }; - -class Game { - Debugger* debugger; - Randomiser* randomiser; - State state; - uint16_t currentDelay; - uint16_t currentScore; - uint8_t waitingBeeps[MAX_WAITING]; - uint8_t waitingBeepsAmount; - - public: - Game(Debugger* debugger, Randomiser* randomiser); - - void loop(); -}; diff --git a/src/game_manager.cpp b/src/game_manager.cpp new file mode 100644 index 0000000..d695547 --- /dev/null +++ b/src/game_manager.cpp @@ -0,0 +1,13 @@ +#include "game_manager.hpp" + +#include +#include "config.hpp" +#include "debugger.hpp" +#include "randomiser.hpp" + +GameManager::GameManager(Debugger* debugger, Randomiser* randomiser) + : debugger(debugger), randomiser(randomiser), state(State::MENU), currentScore(0) { + debugger->print("Initialising new game..."); +} + +void GameManager::loop() {} diff --git a/src/game_manager.hpp b/src/game_manager.hpp new file mode 100644 index 0000000..9a247a1 --- /dev/null +++ b/src/game_manager.hpp @@ -0,0 +1,17 @@ +#include "config.hpp" +#include "debugger.hpp" +#include "randomiser.hpp" + +enum class State { MENU, INIT, GAME, SCORE }; + +class GameManager { + Debugger* debugger; + Randomiser* randomiser; + State state; + uint16_t currentScore; + + public: + GameManager(Debugger* debugger, Randomiser* randomiser); + + void loop(); +}; diff --git a/src/menu.cpp b/src/menu.cpp new file mode 100644 index 0000000..e69de29 diff --git a/src/menu.hpp b/src/menu.hpp new file mode 100644 index 0000000..e69de29 diff --git a/src/randomiser.cpp b/src/randomiser.cpp index fbdb7b4..472fd2b 100644 --- a/src/randomiser.cpp +++ b/src/randomiser.cpp @@ -3,10 +3,6 @@ #include #include "config.hpp" -Randomiser::Randomiser(uint32_t seed) { - randomSeed(seed); -} +Randomiser::Randomiser(uint32_t seed) { randomSeed(seed); } -int32_t Randomiser::get() { - return random(0, BUTTONS); -} +int32_t Randomiser::get() const { return random(0, BUTTONS); } diff --git a/src/randomiser.hpp b/src/randomiser.hpp index 23848cd..2b50ea9 100644 --- a/src/randomiser.hpp +++ b/src/randomiser.hpp @@ -5,5 +5,5 @@ class Randomiser { public: Randomiser(uint32_t seed); - int32_t get(); + int32_t get() const; }; diff --git a/src/speed_game.cpp b/src/speed_game.cpp new file mode 100644 index 0000000..95412f3 --- /dev/null +++ b/src/speed_game.cpp @@ -0,0 +1,8 @@ +#include "speed_game.hpp" + +#include "base_game.hpp" + +SpeedGame::SpeedGame(Debugger* debugger, Randomiser* randomiser) + : BaseGame(debugger, randomiser), waitingBeeps{}, waitingBeepsAmount(0) {} + +NextOperation SpeedGame::loop() {} diff --git a/src/speed_game.hpp b/src/speed_game.hpp new file mode 100644 index 0000000..c22d6fa --- /dev/null +++ b/src/speed_game.hpp @@ -0,0 +1,14 @@ +#include "base_game.hpp" +#include "config.hpp" + +/** + * The default speed test game where you need to press the buttons with an ever increasing speed. + */ +class SpeedGame : BaseGame { + uint8_t waitingBeeps[MAX_WAITING]; + uint8_t waitingBeepsAmount; + + public: + using BaseGame::BaseGame; + NextOperation loop(); +};