Restructure stuff
This commit is contained in:
parent
e24b8d0591
commit
5bce8d78c0
15 changed files with 89 additions and 73 deletions
|
@ -1,17 +1,15 @@
|
||||||
#include "src/debugger.hpp"
|
#include "src/debugger.hpp"
|
||||||
#include "src/game.hpp"
|
#include "src/game_manager.hpp"
|
||||||
#include "src/randomiser.hpp"
|
#include "src/randomiser.hpp"
|
||||||
|
|
||||||
Debugger* debugger;
|
Debugger* debugger;
|
||||||
Randomiser* randomiser;
|
Randomiser* randomiser;
|
||||||
Game* game;
|
GameManager* game;
|
||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
debugger = new Debugger(9600);
|
debugger = new Debugger(9600);
|
||||||
randomiser = new Randomiser(analogRead(0));
|
randomiser = new Randomiser(analogRead(0));
|
||||||
game = new Game(debugger, randomiser);
|
game = new GameManager(debugger, randomiser);
|
||||||
}
|
}
|
||||||
|
|
||||||
void loop() {
|
void loop() { game->loop(); }
|
||||||
game->loop();
|
|
||||||
}
|
|
||||||
|
|
4
src/base_game.cpp
Normal file
4
src/base_game.cpp
Normal file
|
@ -0,0 +1,4 @@
|
||||||
|
#include "base_game.hpp"
|
||||||
|
|
||||||
|
BaseGame::BaseGame(Debugger* debugger, Randomiser* randomiser)
|
||||||
|
: debugger(debugger), randomiser(randomiser) {}
|
23
src/base_game.hpp
Normal file
23
src/base_game.hpp
Normal file
|
@ -0,0 +1,23 @@
|
||||||
|
#include <Arduino.h>
|
||||||
|
#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;
|
||||||
|
};
|
|
@ -3,11 +3,9 @@
|
||||||
#include <Arduino.h>
|
#include <Arduino.h>
|
||||||
#include "config.hpp"
|
#include "config.hpp"
|
||||||
|
|
||||||
Debugger::Debugger(uint16_t port) : serialPort(port) {
|
Debugger::Debugger(uint16_t port) : serialPort(port) { Serial.begin(serialPort); }
|
||||||
Serial.begin(serialPort);
|
|
||||||
}
|
|
||||||
|
|
||||||
void Debugger::print(String msg, bool newline) {
|
void Debugger::print(const String msg, const bool newline) const {
|
||||||
if (DEBUG) {
|
if (DEBUG) {
|
||||||
auto msgCopy = String(msg);
|
auto msgCopy = String(msg);
|
||||||
|
|
||||||
|
|
|
@ -12,5 +12,5 @@ class Debugger {
|
||||||
public:
|
public:
|
||||||
Debugger(uint16_t port);
|
Debugger(uint16_t port);
|
||||||
|
|
||||||
void print(String msg, bool newline = true);
|
void print(const String msg, const bool newline = true) const;
|
||||||
};
|
};
|
||||||
|
|
35
src/game.cpp
35
src/game.cpp
|
@ -1,35 +0,0 @@
|
||||||
#include "game.hpp"
|
|
||||||
|
|
||||||
#include <Arduino.h>
|
|
||||||
#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);
|
|
||||||
}
|
|
20
src/game.hpp
20
src/game.hpp
|
@ -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();
|
|
||||||
};
|
|
13
src/game_manager.cpp
Normal file
13
src/game_manager.cpp
Normal file
|
@ -0,0 +1,13 @@
|
||||||
|
#include "game_manager.hpp"
|
||||||
|
|
||||||
|
#include <Arduino.h>
|
||||||
|
#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() {}
|
17
src/game_manager.hpp
Normal file
17
src/game_manager.hpp
Normal file
|
@ -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();
|
||||||
|
};
|
0
src/menu.cpp
Normal file
0
src/menu.cpp
Normal file
0
src/menu.hpp
Normal file
0
src/menu.hpp
Normal file
|
@ -3,10 +3,6 @@
|
||||||
#include <Arduino.h>
|
#include <Arduino.h>
|
||||||
#include "config.hpp"
|
#include "config.hpp"
|
||||||
|
|
||||||
Randomiser::Randomiser(uint32_t seed) {
|
Randomiser::Randomiser(uint32_t seed) { randomSeed(seed); }
|
||||||
randomSeed(seed);
|
|
||||||
}
|
|
||||||
|
|
||||||
int32_t Randomiser::get() {
|
int32_t Randomiser::get() const { return random(0, BUTTONS); }
|
||||||
return random(0, BUTTONS);
|
|
||||||
}
|
|
||||||
|
|
|
@ -5,5 +5,5 @@
|
||||||
class Randomiser {
|
class Randomiser {
|
||||||
public:
|
public:
|
||||||
Randomiser(uint32_t seed);
|
Randomiser(uint32_t seed);
|
||||||
int32_t get();
|
int32_t get() const;
|
||||||
};
|
};
|
||||||
|
|
8
src/speed_game.cpp
Normal file
8
src/speed_game.cpp
Normal file
|
@ -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() {}
|
14
src/speed_game.hpp
Normal file
14
src/speed_game.hpp
Normal file
|
@ -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();
|
||||||
|
};
|
Reference in a new issue