diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/crepe/api/Script.hpp | 3 | ||||
-rw-r--r-- | src/crepe/manager/ReplayManager.cpp | 28 | ||||
-rw-r--r-- | src/crepe/manager/ReplayManager.h | 33 | ||||
-rw-r--r-- | src/crepe/system/ReplaySystem.cpp | 49 |
4 files changed, 56 insertions, 57 deletions
diff --git a/src/crepe/api/Script.hpp b/src/crepe/api/Script.hpp index 225a51c..547fb8b 100644 --- a/src/crepe/api/Script.hpp +++ b/src/crepe/api/Script.hpp @@ -1,6 +1,7 @@ #pragma once #include "../manager/ComponentManager.h" +#include "../manager/ReplayManager.h" #include "BehaviorScript.h" #include "Script.h" @@ -36,6 +37,8 @@ void Script::subscribe_internal(const EventHandler<EventType> & callback, [this, callback](const EventType & data) -> bool { bool & active = this->active; if (!active) return false; + ReplayManager & replay = this->mediator->replay_manager; + if (replay.get_state() == ReplayManager::PLAYING) return false; return callback(data); }, channel); diff --git a/src/crepe/manager/ReplayManager.cpp b/src/crepe/manager/ReplayManager.cpp index 81ff114..ab8a5a0 100644 --- a/src/crepe/manager/ReplayManager.cpp +++ b/src/crepe/manager/ReplayManager.cpp @@ -37,3 +37,31 @@ void ReplayManager::release(recording_t handle) { this->memory.erase(handle); } +void ReplayManager::frame_record() { + ComponentManager & components = this->mediator.component_manager; + Recording & recording = this->recording; + + recording.frames.push_back(components.save()); + recording.frame++; +} + +bool ReplayManager::frame_step() { + ComponentManager & components = this->mediator.component_manager; + Recording & recording = this->recording; + + ComponentManager::Snapshot & frame = recording.frames.at(recording.frame); + + components.restore(frame); + recording.frame++; + + if (recording.frame < recording.frames.size()) return false; + // end of recording + recording.frame = 0; + this->state = IDLE; + return true; +} + +ReplayManager::State ReplayManager::get_state() const { + return this->state; +} + diff --git a/src/crepe/manager/ReplayManager.h b/src/crepe/manager/ReplayManager.h index 672d093..5794761 100644 --- a/src/crepe/manager/ReplayManager.h +++ b/src/crepe/manager/ReplayManager.h @@ -15,33 +15,34 @@ typedef size_t recording_t; class ReplayManager : public Manager { public: ReplayManager(Mediator & mediator); - friend class ReplaySystem; - -protected: - void record_frame(); - -private: - struct Recording { - size_t frame = 0; - std::vector<ComponentManager::Snapshot> frames; - }; +public: + void record_start(); + recording_t record_end(); + void play(recording_t handle); + void release(recording_t handle); + +public: enum State { IDLE, RECORDING, PLAYING, }; + State get_state() const; +public: + void frame_record(); + bool frame_step(); + +private: + struct Recording { + size_t frame = 0; + std::vector<ComponentManager::Snapshot> frames; + }; State state = IDLE; OptionalRef<Recording> recording; recording_t id = -1; - std::unordered_map<recording_t, std::unique_ptr<Recording>> memory; -public: - void record_start(); - recording_t record_end(); - void play(recording_t handle); - void release(recording_t handle); }; } // namespace crepe diff --git a/src/crepe/system/ReplaySystem.cpp b/src/crepe/system/ReplaySystem.cpp index a6b8fb1..39b5c14 100644 --- a/src/crepe/system/ReplaySystem.cpp +++ b/src/crepe/system/ReplaySystem.cpp @@ -1,4 +1,3 @@ -#include "../util/Log.h" #include "../manager/ReplayManager.h" #include "../manager/SystemManager.h" @@ -10,52 +9,23 @@ using namespace std; void ReplaySystem::fixed_update() { ReplayManager & replay = this->mediator.replay_manager; + ReplayManager::State state = replay.get_state(); + ReplayManager::State last_state = this->last_state; + this->last_state = state; - switch (replay.state) { + switch (state) { case ReplayManager::IDLE: break; case ReplayManager::RECORDING: { - this->update_recording(); + replay.frame_record(); break; } case ReplayManager::PLAYING: { - this->update_playing(); + if (last_state != ReplayManager::PLAYING) this->playback_begin(); + bool last = replay.frame_step(); + if (last) this->playback_end(); break; } } - - this->last_state = replay.state; -} - -void ReplaySystem::update_recording() { - ReplayManager & replay = this->mediator.replay_manager; - ComponentManager & components = this->mediator.component_manager; - - ReplayManager::Recording & recording = replay.recording; - recording.frames.push_back(components.save()); - recording.frame++; -} - -void ReplaySystem::update_playing() { - ReplayManager & replay = this->mediator.replay_manager; - - if (this->last_state != ReplayManager::PLAYING) { - this->playback_begin(); - } - - ReplayManager::Recording & recording = replay.recording; - - if (recording.frames.size() == recording.frame) { - dbg_log("Finished playback"); - this->playback_end(); - return; - } - - 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() { @@ -78,8 +48,5 @@ void ReplaySystem::playback_end() { components.restore(this->playback.components); systems.restore(this->playback.systems); - - ReplayManager & replay = this->mediator.replay_manager; - replay.state = ReplayManager::IDLE; } |