diff options
author | Loek Le Blansch <loek@pipeframe.xyz> | 2024-12-11 21:49:32 +0100 |
---|---|---|
committer | Loek Le Blansch <loek@pipeframe.xyz> | 2024-12-11 21:49:32 +0100 |
commit | b9694e8cc6b85a0089f97ff3c21862adc75a7ee5 (patch) | |
tree | d5e85a8000e1b78f5617a792d9acd64f9465ecb5 | |
parent | 000062b462a3af86db4dac4d8c9e5ef32feb2996 (diff) |
working proof of concept replay system
-rw-r--r-- | src/crepe/manager/SystemManager.cpp | 22 | ||||
-rw-r--r-- | src/crepe/manager/SystemManager.h | 6 | ||||
-rw-r--r-- | src/crepe/system/ReplaySystem.cpp | 33 | ||||
-rw-r--r-- | src/crepe/system/ReplaySystem.h | 8 | ||||
-rw-r--r-- | src/example/replay.cpp | 20 |
5 files changed, 65 insertions, 24 deletions
diff --git a/src/crepe/manager/SystemManager.cpp b/src/crepe/manager/SystemManager.cpp index 8fd80a7..db7430e 100644 --- a/src/crepe/manager/SystemManager.cpp +++ b/src/crepe/manager/SystemManager.cpp @@ -8,6 +8,7 @@ #include "../system/RenderSystem.h" #include "../system/ScriptSystem.h" #include "../system/EventSystem.h" +#include "../system/ReplaySystem.h" #include "SystemManager.h" @@ -15,6 +16,7 @@ using namespace crepe; using namespace std; SystemManager::SystemManager(Mediator & mediator) : Manager(mediator) { + this->load_system<ReplaySystem>(); this->load_system<ScriptSystem>(); this->load_system<AISystem>(); this->load_system<PhysicsSystem>(); @@ -43,3 +45,23 @@ void SystemManager::frame_update() { } } +SystemManager::Snapshot SystemManager::save() { + Snapshot snapshot; + for (auto & [type, system] : this->systems) { + snapshot[type] = system->active; + } + return snapshot; +} + +void SystemManager::restore(const Snapshot & snapshot) { + for (auto & [type, active] : snapshot) { + this->systems[type]->active = active; + } +} + +void SystemManager::disable_all() { + for (auto & [type, system] : this->systems) { + system->active = false; + } +} + diff --git a/src/crepe/manager/SystemManager.h b/src/crepe/manager/SystemManager.h index 5726a5c..6cf7f2b 100644 --- a/src/crepe/manager/SystemManager.h +++ b/src/crepe/manager/SystemManager.h @@ -48,6 +48,12 @@ public: */ template <class T> T & get_system(); + +public: + typedef std::unordered_map<std::type_index, bool> Snapshot; + Snapshot save(); + void restore(const Snapshot & snapshot); + void disable_all(); }; } // namespace crepe diff --git a/src/crepe/system/ReplaySystem.cpp b/src/crepe/system/ReplaySystem.cpp index 5a90752..a6b8fb1 100644 --- a/src/crepe/system/ReplaySystem.cpp +++ b/src/crepe/system/ReplaySystem.cpp @@ -1,8 +1,8 @@ -#include "ScriptSystem.h" - +#include "../util/Log.h" #include "../manager/ReplayManager.h" #include "../manager/SystemManager.h" +#include "RenderSystem.h" #include "ReplaySystem.h" using namespace crepe; @@ -45,6 +45,7 @@ void ReplaySystem::update_playing() { ReplayManager::Recording & recording = replay.recording; if (recording.frames.size() == recording.frame) { + dbg_log("Finished playback"); this->playback_end(); return; } @@ -52,27 +53,33 @@ void ReplaySystem::update_playing() { ComponentManager & components = this->mediator.component_manager; ComponentManager::Snapshot & frame = recording.frames.at(recording.frame); + dbg_logf("Playing recording frame {}", recording.frame); components.restore(frame); recording.frame++; } void ReplaySystem::playback_begin() { SystemManager & systems = this->mediator.system_manager; - systems.get_system<ScriptSystem>().active = false; - // TODO: store system active state - // TODO: disable most systems - // TODO: store components snapshot -} + ComponentManager & components = this->mediator.component_manager; -void ReplaySystem::playback_end() { - ReplayManager & replay = this->mediator.replay_manager; + this->playback = { + .components = components.save(), + .systems = systems.save(), + }; - replay.state = ReplayManager::IDLE; + systems.disable_all(); + systems.get_system<RenderSystem>().active = true; + systems.get_system<ReplaySystem>().active = true; +} +void ReplaySystem::playback_end() { SystemManager & systems = this->mediator.system_manager; - systems.get_system<ScriptSystem>().active = true; + ComponentManager & components = this->mediator.component_manager; - // TODO: restore system active state snapshot - // TODO: restore components snapshot + components.restore(this->playback.components); + systems.restore(this->playback.systems); + + ReplayManager & replay = this->mediator.replay_manager; + replay.state = ReplayManager::IDLE; } diff --git a/src/crepe/system/ReplaySystem.h b/src/crepe/system/ReplaySystem.h index fb40176..6f6fce4 100644 --- a/src/crepe/system/ReplaySystem.h +++ b/src/crepe/system/ReplaySystem.h @@ -1,6 +1,7 @@ #pragma once #include "../manager/ReplayManager.h" +#include "../manager/SystemManager.h" #include "System.h" @@ -17,7 +18,12 @@ private: void update_recording(); void update_playing(); - std::unordered_map<std::type_index, bool> system_active_snapshot; + struct Snapshot { + ComponentManager::Snapshot components; + SystemManager::Snapshot systems; + }; + Snapshot playback; + void playback_begin(); void playback_end(); }; diff --git a/src/example/replay.cpp b/src/example/replay.cpp index 11413fa..e7f4e6d 100644 --- a/src/example/replay.cpp +++ b/src/example/replay.cpp @@ -45,20 +45,20 @@ public: switch (i++) { default: break; case 10: - mgr.record_start(); - break; + mgr.record_start(); + break; case 60: - this->recording = mgr.record_end(); - break; + this->recording = mgr.record_end(); + break; case 70: - mgr.play(this->recording); - break; + mgr.play(this->recording); + break; case 71: - mgr.release(this->recording); - break; + mgr.release(this->recording); + break; case 72: - throw; - break; + throw; + break; }; } }; |