aboutsummaryrefslogtreecommitdiff
path: root/extension/src
diff options
context:
space:
mode:
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
commit258a088018d36e987b3ffe4a9b418a6c21ad9217 (patch)
tree4ab10469ceff9a536587af6e9901a170c56af0ff /extension/src
parentf168c91ff266beda8066014257a30d93704882ee (diff)
parent04b213d4e20ca4e7ea66b059329771f6fd36c650 (diff)
Merge pull request #89 from OpenVic2Project/spring-cleaning
Spring Cleaning
Diffstat (limited to 'extension/src')
-rw-r--r--extension/src/Simulation.cpp64
-rw-r--r--extension/src/Simulation.hpp68
-rw-r--r--extension/src/TestSingleton.cpp36
-rw-r--r--extension/src/TestSingleton.hpp23
-rw-r--r--extension/src/openvic2/GameAdvancementHook.cpp69
-rw-r--r--extension/src/openvic2/GameAdvancementHook.hpp40
-rw-r--r--extension/src/register_types.cpp20
7 files changed, 109 insertions, 211 deletions
diff --git a/extension/src/Simulation.cpp b/extension/src/Simulation.cpp
deleted file mode 100644
index 5ea99c5..0000000
--- a/extension/src/Simulation.cpp
+++ /dev/null
@@ -1,64 +0,0 @@
-#include "Simulation.hpp"
-
-namespace OpenVic2 {
- void Simulation::togglePauseState() {
- this->isPaused = !isPaused;
- }
-
- bool Simulation::getPauseState() {
- return this->isPaused;
- }
-
- void Simulation::increaseSimulationSpeed() {
- switch (this->currentSpeed) {
- case(Speed::Speed1):
- this->currentSpeed = Speed::Speed2;
- break;
- case(Speed::Speed2):
- this->currentSpeed = Speed::Speed3;
- break;
- case(Speed::Speed3):
- this->currentSpeed = Speed::Speed4;
- break;
- case(Speed::Speed4):
- this->currentSpeed = Speed::Speed5;
- break;
- }
- }
-
- void Simulation::decreaseSimulationSpeed() {
- switch (this->currentSpeed) {
- case(Speed::Speed2):
- this->currentSpeed = Speed::Speed1;
- break;
- case(Speed::Speed3):
- this->currentSpeed = Speed::Speed2;
- break;
- case(Speed::Speed4):
- this->currentSpeed = Speed::Speed3;
- break;
- case(Speed::Speed5):
- this->currentSpeed = Speed::Speed4;
- break;
- }
- }
-
- void Simulation::setSimulationSpeed(Speed speed) {
- this->currentSpeed = speed;
- }
-
- int Simulation::getSimulationSpeed() {
- return static_cast<int>(this->currentSpeed);
- }
-
- void Simulation::conditionallyAdvanceSimulation() {
- if (!(this->isPaused)) {
- std::chrono::time_point<std::chrono::high_resolution_clock> previousTime = this->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() >= this->getSimulationSpeed()) {
- this->lastPolledTime = currentTime;
- this->inGameDate++;
- }
- }
- }
-}
diff --git a/extension/src/Simulation.hpp b/extension/src/Simulation.hpp
deleted file mode 100644
index 58ba7c7..0000000
--- a/extension/src/Simulation.hpp
+++ /dev/null
@@ -1,68 +0,0 @@
-#pragma once
-
-#include <godot_cpp/core/class_db.hpp>
-#include <vector>
-#include <chrono>
-#include "openvic2/Date.hpp"
-
-namespace OpenVic2 {
- class Simulation : public godot::Object {
- GDCLASS(Simulation, godot::Object)
- std::vector<uint64_t> exampleProvinces;
-
- enum class Speed { Speed1 = 4000, Speed2 = 3000, Speed3 = 2000, Speed4 = 1000, Speed5 = 100, Speed6 = 1 };
-
- std::chrono::time_point<std::chrono::high_resolution_clock> lastPolledTime;
- bool isPaused;
- Speed currentSpeed;
- Date inGameDate;
-
- //BEGIN BOILERPLATE
- inline static Simulation* _simulation = nullptr;
-
- protected:
- static void _bind_methods() {
- godot::ClassDB::bind_method(godot::D_METHOD("conductSimulationStep"), &Simulation::conductSimulationStep);
- godot::ClassDB::bind_method(godot::D_METHOD("queryProvinceSize"), &Simulation::queryProvinceSize);
- }
-
- public:
- inline static Simulation* get_singleton() { return _simulation; }
-
- inline Simulation() : inGameDate(1836, 1, 1) {
- ERR_FAIL_COND(_simulation != nullptr);
- _simulation = this;
- this->lastPolledTime = std::chrono::high_resolution_clock::now();
- this->isPaused = false;
- this->currentSpeed = Speed::Speed1;
- exampleProvinces.resize(10, 1);
- }
- inline ~Simulation() {
- ERR_FAIL_COND(_simulation != this);
- _simulation = nullptr;
- }
- //END BOILERPLATE
-
- void togglePauseState();
- bool getPauseState();
- void increaseSimulationSpeed();
- void decreaseSimulationSpeed();
- void setSimulationSpeed(Speed speed);
- int getSimulationSpeed();
-
- inline void conductSimulationStep() {
- for (uint64_t x = 0; x < exampleProvinces.size(); x++) {
- exampleProvinces[x] += (x + 1);
- }
- }
-
- inline uint64_t queryProvinceSize(uint64_t provinceID) {
- if (provinceID >= exampleProvinces.size()) {
- return 0;
- }
- return exampleProvinces[provinceID];
- }
-
- void conditionallyAdvanceSimulation();
- };
-} \ No newline at end of file
diff --git a/extension/src/TestSingleton.cpp b/extension/src/TestSingleton.cpp
deleted file mode 100644
index d9e2b03..0000000
--- a/extension/src/TestSingleton.cpp
+++ /dev/null
@@ -1,36 +0,0 @@
-#include "TestSingleton.hpp"
-
-#include <godot_cpp/core/class_db.hpp>
-#include <godot_cpp/variant/utility_functions.hpp>
-
-using namespace godot;
-using namespace OpenVic2;
-
-TestSingleton* TestSingleton::singleton = nullptr;
-
-void TestSingleton::_bind_methods()
-{
- ClassDB::bind_method(D_METHOD("hello_singleton"), &TestSingleton::hello_singleton);
-}
-
-TestSingleton* TestSingleton::get_singleton()
-{
- return singleton;
-}
-
-TestSingleton::TestSingleton()
-{
- ERR_FAIL_COND(singleton != nullptr);
- singleton = this;
-}
-
-TestSingleton::~TestSingleton()
-{
- ERR_FAIL_COND(singleton != this);
- singleton = nullptr;
-}
-
-void TestSingleton::hello_singleton()
-{
- UtilityFunctions::print("Hello GDExtension Singleton!");
-} \ No newline at end of file
diff --git a/extension/src/TestSingleton.hpp b/extension/src/TestSingleton.hpp
deleted file mode 100644
index 1261573..0000000
--- a/extension/src/TestSingleton.hpp
+++ /dev/null
@@ -1,23 +0,0 @@
-#pragma once
-
-#include <godot_cpp/classes/object.hpp>
-
-namespace OpenVic2 {
- class TestSingleton : public godot::Object
- {
- GDCLASS(TestSingleton, godot::Object)
-
- static TestSingleton* singleton;
-
- protected:
- static void _bind_methods();
-
- public:
- static TestSingleton* get_singleton();
-
- TestSingleton();
- ~TestSingleton();
-
- void hello_singleton();
- };
-} \ No newline at end of file
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
diff --git a/extension/src/register_types.cpp b/extension/src/register_types.cpp
index 9fd934e..10ed781 100644
--- a/extension/src/register_types.cpp
+++ b/extension/src/register_types.cpp
@@ -2,8 +2,6 @@
#include <godot_cpp/classes/engine.hpp>
-#include "TestSingleton.hpp"
-#include "Simulation.hpp"
#include "Checksum.hpp"
#include "LoadLocalisation.hpp"
#include "MapSingleton.hpp"
@@ -12,8 +10,6 @@
using namespace godot;
using namespace OpenVic2;
-static TestSingleton* _test_singleton;
-static Simulation* _simulation;
static Checksum* _checksum;
static LoadLocalisation* _load_localisation;
static MapSingleton* _map_singleton;
@@ -23,14 +19,6 @@ void initialize_openvic2_types(ModuleInitializationLevel p_level) {
return;
}
- ClassDB::register_class<TestSingleton>();
- _test_singleton = memnew(TestSingleton);
- Engine::get_singleton()->register_singleton("TestSingleton", TestSingleton::get_singleton());
-
- ClassDB::register_class<Simulation>();
- _simulation = memnew(Simulation);
- Engine::get_singleton()->register_singleton("Simulation", Simulation::get_singleton());
-
ClassDB::register_class<Checksum>();
_checksum = memnew(Checksum);
Engine::get_singleton()->register_singleton("Checksum", Checksum::get_singleton());
@@ -51,12 +39,6 @@ void uninitialize_openvic2_types(ModuleInitializationLevel p_level) {
return;
}
- Engine::get_singleton()->unregister_singleton("TestSingleton");
- memdelete(_test_singleton);
-
- Engine::get_singleton()->unregister_singleton("Simulation");
- memdelete(_simulation);
-
Engine::get_singleton()->unregister_singleton("Checksum");
memdelete(_checksum);
@@ -68,9 +50,7 @@ void uninitialize_openvic2_types(ModuleInitializationLevel p_level) {
}
extern "C" {
-
// Initialization.
-
GDExtensionBool GDE_EXPORT openvic2_library_init(GDExtensionInterface const* p_interface, const GDExtensionClassLibraryPtr p_library, GDExtensionInitialization* r_initialization) {
GDExtensionBinding::InitObject init_obj(p_interface, p_library, r_initialization);