aboutsummaryrefslogtreecommitdiff
path: root/src/openvic/GameAdvancementHook.cpp
diff options
context:
space:
mode:
author Hop311 <Hop3114@gmail.com>2023-05-16 21:25:14 +0200
committer GitHub <noreply@github.com>2023-05-16 21:25:14 +0200
commitcedac2d020ae7e54d8fc5c21e390a306050bc220 (patch)
tree440634772615531e704a5554aa59c9890cd9cd85 /src/openvic/GameAdvancementHook.cpp
parent339e0278a2064f7eeb152fe8c5778840b609e9f3 (diff)
parent42d9d1d5417deb5979a9d5775cfe97dcff4b77ba (diff)
Merge pull request #3 from OpenVicProject/openvic-rename
Changed from OpenVic2 to OpenVic
Diffstat (limited to 'src/openvic/GameAdvancementHook.cpp')
-rw-r--r--src/openvic/GameAdvancementHook.cpp72
1 files changed, 72 insertions, 0 deletions
diff --git a/src/openvic/GameAdvancementHook.cpp b/src/openvic/GameAdvancementHook.cpp
new file mode 100644
index 0000000..0fcdd03
--- /dev/null
+++ b/src/openvic/GameAdvancementHook.cpp
@@ -0,0 +1,72 @@
+#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) {
+ 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 - lastPolledTime) >= GAME_SPEEDS[currentSpeed]) {
+ lastPolledTime = currentTime;
+ if (triggerFunction) triggerFunction();
+ }
+ }
+ if (refreshFunction) refreshFunction();
+}
+
+void GameAdvancementHook::reset() {
+ isPaused = true;
+ currentSpeed = 0;
+}