aboutsummaryrefslogtreecommitdiff
path: root/extension/src/openvic2
diff options
context:
space:
mode:
author ClarkeCode <clarke.john.robert@gmail.com>2023-04-15 05:41:57 +0200
committer ClarkeCode <clarke.john.robert@gmail.com>2023-04-17 22:04:28 +0200
commit04b213d4e20ca4e7ea66b059329771f6fd36c650 (patch)
tree4ab10469ceff9a536587af6e9901a170c56af0ff /extension/src/openvic2
parentf168c91ff266beda8066014257a30d93704882ee (diff)
Removing TestSingleton
Extracted game advancement behaviour to separate class
Diffstat (limited to 'extension/src/openvic2')
-rw-r--r--extension/src/openvic2/GameAdvancementHook.cpp69
-rw-r--r--extension/src/openvic2/GameAdvancementHook.hpp40
2 files changed, 109 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
diff --git a/extension/src/openvic2/GameAdvancementHook.hpp b/extension/src/openvic2/GameAdvancementHook.hpp
new file mode 100644
index 0000000..72af4ac
--- /dev/null
+++ b/extension/src/openvic2/GameAdvancementHook.hpp
@@ -0,0 +1,40 @@
+#pragma once
+
+#include <chrono>
+#include <functional>
+
+namespace OpenVic2 {
+ //Value of different game speeds are the minimum number of miliseconds before the simulation advances
+ enum class GameSpeed {
+ Speed1 = 4000,
+ Speed2 = 3000,
+ Speed3 = 2000,
+ Speed4 = 1000,
+ Speed5 = 100,
+ Speed6 = 1
+ };
+
+ //Conditionally advances game with provided behaviour
+ //Class governs game speed and pause state
+ class GameAdvancementHook {
+ public:
+ using AdvancementFunction = std::function<void()>;
+
+ private:
+ std::chrono::time_point<std::chrono::high_resolution_clock> 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;
+
+ public:
+ bool isPaused;
+ GameSpeed currentSpeed;
+
+ GameAdvancementHook(AdvancementFunction function = nullptr, bool startPaused = false, GameSpeed startingSpeed = GameSpeed::Speed1);
+
+ void increaseSimulationSpeed();
+ void decreaseSimulationSpeed();
+ GameAdvancementHook operator++(int);
+ GameAdvancementHook operator--(int);
+ void conditionallyAdvanceGame();
+ };
+} \ No newline at end of file