-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGame.hpp
99 lines (80 loc) · 2.44 KB
/
Game.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#ifndef GAME_HPP
#define GAME_HPP
#include "Audio.hpp"
#include "EventQueue.hpp"
#include "Storage.hpp"
// do NOT add Arduino stuff here
#include <cstddef>
#include <cstdint>
#include <functional>
class Game
{
public:
enum class Event
{
START_RESET = 1, // start the game/time OR reset the game (e.g. to clear the display)
WIRE_CONTACT, // wire has been touched
END_CONTACT, // end position has been reached
SET_HARDCORE, // enable hardcore mode
SET_TIMED, // enable timed mode
RESET_HIGHSCORE
};
enum class GameMode
{
HARDCORE, // fastest wins, first contact means death
TIMED, // fastest wins; each contact is penalized (with some seconds) -> could be exploited!
};
struct Result
{
bool won = false;
size_t contacts = 0;
size_t runtimeInMs = 0; // written once finished
uint32_t startTime = 0; // written on start
};
public:
/**
* @brief Game creates a game
* @param timerFunction function to get the current application runtime in ms; required for timing
*/
Game(std::function<uint32_t()> timerFunction, std::function<void(Audio::Sound)> audioCallback);
/**
* @brief setupMode changes to the given mode
* @note Any running game is aborted.
*/
void setupMode(GameMode mode);
void pushEvent(Event const& event);
void processEvents();
size_t getRuntimeInMs() const;
bool isRunning() const { return state == State::RUNNING; }
bool hasWon() const { return state == State::FINISHED && currentResult.won; }
Result getResult() const { return currentResult; }
private:
enum class State
{
RUNNING, //! game is running
FINISHED, //! game has been finished
WAITING, //! game is ready to be started
};
struct Config
{
GameMode mode = GameMode::HARDCORE;
bool deathOnContact = true;
size_t contactPenalty = 0; //! number of ms to add for each contact
};
private:
void doStart();
void doReset();
void handleWireContact();
void handleEndContact();
uint32_t getCurrentTime() const;
void processHighscore();
private:
State state = State::WAITING;
Config config;
Result currentResult;
Storage storage;
EventQueue<Event> queue;
std::function<uint32_t()> timerFunction;
std::function<void(Audio::Sound)> audioCallback;
};
#endif // GAME_HPP