diff options
author | ClarkeCode <33846391+ClarkeCode@users.noreply.github.com> | 2023-04-17 22:38:34 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-04-17 22:38:34 +0200 |
commit | 258a088018d36e987b3ffe4a9b418a6c21ad9217 (patch) | |
tree | 4ab10469ceff9a536587af6e9901a170c56af0ff /extension/src/openvic2/GameAdvancementHook.cpp | |
parent | f168c91ff266beda8066014257a30d93704882ee (diff) | |
parent | 04b213d4e20ca4e7ea66b059329771f6fd36c650 (diff) |
Merge pull request #89 from OpenVic2Project/spring-cleaning
Spring Cleaning
Diffstat (limited to 'extension/src/openvic2/GameAdvancementHook.cpp')
-rw-r--r-- | extension/src/openvic2/GameAdvancementHook.cpp | 69 |
1 files changed, 69 insertions, 0 deletions
diff --git a/extension/src/openvic2/GameAdvancementHook.cpp b/extension/src/openvic2/GameAdvancementHook.cpp new file mode 100644 index 0000000..3db3224 --- /dev/null +++ b/extension/src/openvic2/GameAdvancementHook.cpp @@ -0,0 +1,69 @@ +#include "GameAdvancementHook.hpp" + +namespace OpenVic2 { + GameAdvancementHook::GameAdvancementHook(AdvancementFunction function, bool startPaused, GameSpeed startingSpeed) { + triggerFunction = function; + lastPolledTime = std::chrono::high_resolution_clock::now(); + isPaused = startPaused; + currentSpeed = startingSpeed; + } + + void GameAdvancementHook::increaseSimulationSpeed() { + switch (currentSpeed) { + case(GameSpeed::Speed1): + currentSpeed = GameSpeed::Speed2; + break; + case(GameSpeed::Speed2): + currentSpeed = GameSpeed::Speed3; + break; + case(GameSpeed::Speed3): + currentSpeed = GameSpeed::Speed4; + break; + case(GameSpeed::Speed4): + currentSpeed = GameSpeed::Speed5; + break; + } + } + + void GameAdvancementHook::decreaseSimulationSpeed() { + switch (currentSpeed) { + case(GameSpeed::Speed2): + currentSpeed = GameSpeed::Speed1; + break; + case(GameSpeed::Speed3): + currentSpeed = GameSpeed::Speed2; + break; + case(GameSpeed::Speed4): + currentSpeed = GameSpeed::Speed3; + break; + case(GameSpeed::Speed5): + currentSpeed = GameSpeed::Speed4; + break; + } + } + + GameAdvancementHook GameAdvancementHook::operator++(int) { + GameAdvancementHook oldCopy = *this; + increaseSimulationSpeed(); + return oldCopy; + }; + + GameAdvancementHook GameAdvancementHook::operator--(int) { + GameAdvancementHook oldCopy = *this; + decreaseSimulationSpeed(); + return oldCopy; + }; + + void GameAdvancementHook::conditionallyAdvanceGame() { + if (!isPaused) { + std::chrono::time_point<std::chrono::high_resolution_clock> previousTime = lastPolledTime; + std::chrono::time_point<std::chrono::high_resolution_clock> currentTime = std::chrono::high_resolution_clock::now(); + if (std::chrono::duration_cast<std::chrono::milliseconds>(currentTime - previousTime).count() >= static_cast<int64_t>(currentSpeed)) { + lastPolledTime = currentTime; + if (triggerFunction) { + triggerFunction(); + } + } + } + } +}
\ No newline at end of file |