diff options
author | Hop311 <Hop3114@gmail.com> | 2023-09-09 23:49:54 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-09-09 23:49:54 +0200 |
commit | 6278a35f4704574933464700026d8deb997da5c1 (patch) | |
tree | eb36a9b030b263d825eb93638e64deb0dbd38a78 /src/openvic-simulation/GameAdvancementHook.cpp | |
parent | bec619fc8f554cb075fcef2428f3b6bdb5e88e82 (diff) | |
parent | 3d7fbd9b376811ca0ed226fa78bcc8b6279ba8dc (diff) |
Merge pull request #14 from OpenVicProject/dataloading
Dataloading scaffolding + basic culture and pop history loading
Diffstat (limited to 'src/openvic-simulation/GameAdvancementHook.cpp')
-rw-r--r-- | src/openvic-simulation/GameAdvancementHook.cpp | 76 |
1 files changed, 76 insertions, 0 deletions
diff --git a/src/openvic-simulation/GameAdvancementHook.cpp b/src/openvic-simulation/GameAdvancementHook.cpp new file mode 100644 index 0000000..ac16158 --- /dev/null +++ b/src/openvic-simulation/GameAdvancementHook.cpp @@ -0,0 +1,76 @@ +#include "GameAdvancementHook.hpp" + +using namespace OpenVic; + +const std::vector<std::chrono::milliseconds> GameAdvancementHook::GAME_SPEEDS = { + std::chrono::milliseconds { 4000 }, + std::chrono::milliseconds { 3000 }, + std::chrono::milliseconds { 2000 }, + std::chrono::milliseconds { 1000 }, + std::chrono::milliseconds { 100 }, + std::chrono::milliseconds { 1 } +}; + +GameAdvancementHook::GameAdvancementHook(AdvancementFunction tickFunction, + RefreshFunction updateFunction, bool startPaused, speed_t startingSpeed) + : triggerFunction { tickFunction }, + refreshFunction { updateFunction }, + isPaused { startPaused } { + lastPolledTime = std::chrono::high_resolution_clock::now(); + setSimulationSpeed(startingSpeed); +} + +void GameAdvancementHook::setSimulationSpeed(speed_t speed) { + if (speed < 0) + currentSpeed = 0; + else if (speed >= GAME_SPEEDS.size()) + currentSpeed = GAME_SPEEDS.size() - 1; + else + currentSpeed = speed; +} + +GameAdvancementHook::speed_t GameAdvancementHook::getSimulationSpeed() const { + return currentSpeed; +} + +void GameAdvancementHook::increaseSimulationSpeed() { + setSimulationSpeed(currentSpeed + 1); +} + +void GameAdvancementHook::decreaseSimulationSpeed() { + setSimulationSpeed(currentSpeed - 1); +} + +bool GameAdvancementHook::canIncreaseSimulationSpeed() const { + return currentSpeed + 1 < GAME_SPEEDS.size(); +} + +bool GameAdvancementHook::canDecreaseSimulationSpeed() const { + return currentSpeed > 0; +} + +GameAdvancementHook& GameAdvancementHook::operator++() { + increaseSimulationSpeed(); + return *this; +}; + +GameAdvancementHook& GameAdvancementHook::operator--() { + decreaseSimulationSpeed(); + return *this; +}; + +void GameAdvancementHook::conditionallyAdvanceGame() { + if (!isPaused) { + time_point_t currentTime = std::chrono::high_resolution_clock::now(); + if (std::chrono::duration_cast<std::chrono::milliseconds>(currentTime - lastPolledTime) >= GAME_SPEEDS[currentSpeed]) { + lastPolledTime = currentTime; + if (triggerFunction) triggerFunction(); + } + } + if (refreshFunction) refreshFunction(); +} + +void GameAdvancementHook::reset() { + isPaused = true; + currentSpeed = 0; +} |