Restructure stuff

This commit is contained in:
Mikko Ahlroth 2019-10-17 18:51:01 +03:00
parent e24b8d0591
commit 5bce8d78c0
15 changed files with 89 additions and 73 deletions

View file

@ -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(); }

4
src/base_game.cpp Normal file
View 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
View 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;
};

View file

@ -3,11 +3,9 @@
#include <Arduino.h>
#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);

View file

@ -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;
};

View file

@ -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);
}

View file

@ -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
View 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
View 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
View file

0
src/menu.hpp Normal file
View file

View file

@ -3,10 +3,6 @@
#include <Arduino.h>
#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); }

View file

@ -5,5 +5,5 @@
class Randomiser {
public:
Randomiser(uint32_t seed);
int32_t get();
int32_t get() const;
};

8
src/speed_game.cpp Normal file
View 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
View 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();
};