From 04b213d4e20ca4e7ea66b059329771f6fd36c650 Mon Sep 17 00:00:00 2001 From: ClarkeCode Date: Fri, 14 Apr 2023 23:41:57 -0400 Subject: Removing TestSingleton Extracted game advancement behaviour to separate class --- extension/src/Simulation.cpp | 64 ------------------------ extension/src/Simulation.hpp | 68 ------------------------- extension/src/TestSingleton.cpp | 36 -------------- extension/src/TestSingleton.hpp | 23 --------- extension/src/openvic2/GameAdvancementHook.cpp | 69 ++++++++++++++++++++++++++ extension/src/openvic2/GameAdvancementHook.hpp | 40 +++++++++++++++ extension/src/register_types.cpp | 20 -------- 7 files changed, 109 insertions(+), 211 deletions(-) delete mode 100644 extension/src/Simulation.cpp delete mode 100644 extension/src/Simulation.hpp delete mode 100644 extension/src/TestSingleton.cpp delete mode 100644 extension/src/TestSingleton.hpp create mode 100644 extension/src/openvic2/GameAdvancementHook.cpp create mode 100644 extension/src/openvic2/GameAdvancementHook.hpp (limited to 'extension') 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(this->currentSpeed); - } - - void Simulation::conditionallyAdvanceSimulation() { - if (!(this->isPaused)) { - std::chrono::time_point previousTime = this->lastPolledTime; - std::chrono::time_point currentTime = std::chrono::high_resolution_clock::now(); - if (std::chrono::duration_cast(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 -#include -#include -#include "openvic2/Date.hpp" - -namespace OpenVic2 { - class Simulation : public godot::Object { - GDCLASS(Simulation, godot::Object) - std::vector exampleProvinces; - - enum class Speed { Speed1 = 4000, Speed2 = 3000, Speed3 = 2000, Speed4 = 1000, Speed5 = 100, Speed6 = 1 }; - - std::chrono::time_point 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 -#include - -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 - -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 previousTime = lastPolledTime; + std::chrono::time_point currentTime = std::chrono::high_resolution_clock::now(); + if (std::chrono::duration_cast(currentTime - previousTime).count() >= static_cast(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 +#include + +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; + + private: + std::chrono::time_point 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 -#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(); - _test_singleton = memnew(TestSingleton); - Engine::get_singleton()->register_singleton("TestSingleton", TestSingleton::get_singleton()); - - ClassDB::register_class(); - _simulation = memnew(Simulation); - Engine::get_singleton()->register_singleton("Simulation", Simulation::get_singleton()); - ClassDB::register_class(); _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); -- cgit v1.2.3-56-ga3b1