From 7772f8871348b7b52cb0a478bb76df68d8799a07 Mon Sep 17 00:00:00 2001 From: Hop311 Date: Fri, 8 Sep 2023 17:12:22 +0100 Subject: More refactoring and duplicate code removal --- src/openvic-simulation/GameAdvancementHook.hpp | 44 ++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 src/openvic-simulation/GameAdvancementHook.hpp (limited to 'src/openvic-simulation/GameAdvancementHook.hpp') diff --git a/src/openvic-simulation/GameAdvancementHook.hpp b/src/openvic-simulation/GameAdvancementHook.hpp new file mode 100644 index 0000000..59e43a4 --- /dev/null +++ b/src/openvic-simulation/GameAdvancementHook.hpp @@ -0,0 +1,44 @@ +#pragma once + +#include +#include +#include + +namespace OpenVic { + // Conditionally advances game with provided behaviour + // Class governs game speed and pause state + class GameAdvancementHook { + public: + using AdvancementFunction = std::function; + using RefreshFunction = std::function; + using speed_t = int8_t; + + // Minimum number of miliseconds before the simulation advances + static const std::vector GAME_SPEEDS; + + private: + using time_point_t = std::chrono::time_point; + + time_point_t lastPolledTime; + // A function pointer that advances the simulation, intended to be a capturing lambda or something similar. May need to be reworked later + AdvancementFunction triggerFunction; + RefreshFunction refreshFunction; + speed_t currentSpeed; + + public: + bool isPaused; + + GameAdvancementHook(AdvancementFunction tickFunction, RefreshFunction updateFunction, bool startPaused = true, speed_t startingSpeed = 0); + + void setSimulationSpeed(speed_t speed); + speed_t getSimulationSpeed() const; + void increaseSimulationSpeed(); + void decreaseSimulationSpeed(); + bool canIncreaseSimulationSpeed() const; + bool canDecreaseSimulationSpeed() const; + GameAdvancementHook& operator++(); + GameAdvancementHook& operator--(); + void conditionallyAdvanceGame(); + void reset(); + }; +} \ No newline at end of file -- cgit v1.2.3-56-ga3b1