aboutsummaryrefslogtreecommitdiff
path: root/src/crepe/manager
diff options
context:
space:
mode:
Diffstat (limited to 'src/crepe/manager')
-rw-r--r--src/crepe/manager/CMakeLists.txt5
-rw-r--r--src/crepe/manager/ComponentManager.cpp28
-rw-r--r--src/crepe/manager/ComponentManager.h28
-rw-r--r--src/crepe/manager/LoopTimerManager.h4
-rw-r--r--src/crepe/manager/Mediator.h4
-rw-r--r--src/crepe/manager/ReplayManager.cpp67
-rw-r--r--src/crepe/manager/ReplayManager.h53
-rw-r--r--src/crepe/manager/SystemManager.cpp67
-rw-r--r--src/crepe/manager/SystemManager.h61
-rw-r--r--src/crepe/manager/SystemManager.hpp40
10 files changed, 342 insertions, 15 deletions
diff --git a/src/crepe/manager/CMakeLists.txt b/src/crepe/manager/CMakeLists.txt
index f73e165..48e444f 100644
--- a/src/crepe/manager/CMakeLists.txt
+++ b/src/crepe/manager/CMakeLists.txt
@@ -6,6 +6,8 @@ target_sources(crepe PUBLIC
SceneManager.cpp
LoopTimerManager.cpp
ResourceManager.cpp
+ ReplayManager.cpp
+ SystemManager.cpp
)
target_sources(crepe PUBLIC FILE_SET HEADERS FILES
@@ -21,5 +23,8 @@ target_sources(crepe PUBLIC FILE_SET HEADERS FILES
LoopTimerManager.h
ResourceManager.h
ResourceManager.hpp
+ ReplayManager.h
+ SystemManager.h
+ SystemManager.hpp
)
diff --git a/src/crepe/manager/ComponentManager.cpp b/src/crepe/manager/ComponentManager.cpp
index df30d27..5f5c050 100644
--- a/src/crepe/manager/ComponentManager.cpp
+++ b/src/crepe/manager/ComponentManager.cpp
@@ -53,7 +53,7 @@ GameObject ComponentManager::new_object(const string & name, const string & tag,
this->next_id++;
}
- GameObject object{*this, this->next_id, name, tag, position, rotation, scale};
+ GameObject object{this->mediator, this->next_id, name, tag, position, rotation, scale};
this->next_id++;
return object;
@@ -72,3 +72,29 @@ set<game_object_id_t> ComponentManager::get_objects_by_tag(const string & tag) c
return this->get_objects_by_predicate<Metadata>(
[tag](const Metadata & data) { return data.tag == tag; });
}
+
+ComponentManager::Snapshot ComponentManager::save() {
+ Snapshot snapshot{};
+ for (const auto & [type, by_id_index] : this->components) {
+ for (game_object_id_t id = 0; id < by_id_index.size(); id++) {
+ const auto & components = by_id_index[id];
+ for (size_t index = 0; index < components.size(); index++) {
+ const Component & component = *components[index];
+ snapshot.components.push_back(SnapshotComponent{
+ .type = type,
+ .id = id,
+ .index = index,
+ .component = component.save(),
+ });
+ }
+ }
+ }
+ return snapshot;
+}
+
+void ComponentManager::restore(const Snapshot & snapshot) {
+ for (const SnapshotComponent & info : snapshot.components) {
+ this->components[info.type][info.id][info.index]->restore(*info.component);
+ }
+}
+
diff --git a/src/crepe/manager/ComponentManager.h b/src/crepe/manager/ComponentManager.h
index 19a8e81..457a196 100644
--- a/src/crepe/manager/ComponentManager.h
+++ b/src/crepe/manager/ComponentManager.h
@@ -21,13 +21,6 @@ class GameObject;
* This class manages all components. It provides methods to add, delete and get components.
*/
class ComponentManager : public Manager {
- // TODO: This relation should be removed! I (loek) believe that the scene manager should
- // create/destroy components because the GameObject's are stored in concrete Scene classes,
- // which will in turn call GameObject's destructor, which will in turn call
- // ComponentManager::delete_components_by_id or something. This is a pretty major change, so
- // here is a comment and temporary fix instead :tada:
- friend class SceneManager;
-
public:
ComponentManager(Mediator & mediator);
~ComponentManager(); // dbg_trace
@@ -49,12 +42,7 @@ public:
const vec2 & position = {0, 0}, double rotation = 0,
double scale = 1);
-protected:
- /**
- * GameObject is used as an interface to add/remove components, and the game programmer is
- * supposed to use it instead of interfacing with the component manager directly.
- */
- friend class GameObject;
+public:
/**
* \brief Add a component to the ComponentManager
*
@@ -154,6 +142,20 @@ public:
template <typename T>
RefVector<T> get_components_by_tag(const std::string & tag) const;
+ struct SnapshotComponent {
+ std::type_index type;
+ game_object_id_t id;
+ size_t index;
+ std::unique_ptr<Component> component;
+ };
+ struct Snapshot {
+ // TODO: some kind of hash code that ensures components exist in all the same places as
+ // this snapshot
+ std::vector<SnapshotComponent> components;
+ };
+ Snapshot save();
+ void restore(const Snapshot &);
+
private:
/**
* \brief Get object IDs by predicate function
diff --git a/src/crepe/manager/LoopTimerManager.h b/src/crepe/manager/LoopTimerManager.h
index 91403e4..139294d 100644
--- a/src/crepe/manager/LoopTimerManager.h
+++ b/src/crepe/manager/LoopTimerManager.h
@@ -6,6 +6,8 @@
namespace crepe {
+class Engine;
+
typedef std::chrono::duration<double> duration_t;
typedef std::chrono::duration<unsigned long long, std::micro> elapsed_time_t;
@@ -107,7 +109,7 @@ public:
private:
//! Friend relation to use start,enforce_frame_rate,get_lag,update,advance_fixed_update.
- friend class LoopManager;
+ friend class Engine;
/**
* \brief Start the loop timer.
*
diff --git a/src/crepe/manager/Mediator.h b/src/crepe/manager/Mediator.h
index a336410..842f1de 100644
--- a/src/crepe/manager/Mediator.h
+++ b/src/crepe/manager/Mediator.h
@@ -11,6 +11,8 @@ class LoopTimerManager;
class SaveManager;
class ResourceManager;
class SDLContext;
+class ReplayManager;
+class SystemManager;
/**
* Struct to pass references to classes that would otherwise need to be singletons down to
@@ -32,6 +34,8 @@ struct Mediator {
OptionalRef<LoopTimerManager> loop_timer;
OptionalRef<SaveManager> save_manager;
OptionalRef<ResourceManager> resource_manager;
+ OptionalRef<ReplayManager> replay_manager;
+ OptionalRef<SystemManager> system_manager;
};
} // namespace crepe
diff --git a/src/crepe/manager/ReplayManager.cpp b/src/crepe/manager/ReplayManager.cpp
new file mode 100644
index 0000000..ab8a5a0
--- /dev/null
+++ b/src/crepe/manager/ReplayManager.cpp
@@ -0,0 +1,67 @@
+#include <format>
+
+#include "ReplayManager.h"
+#include "Manager.h"
+
+using namespace crepe;
+using namespace std;
+
+ReplayManager::ReplayManager(Mediator & mediator) : Manager(mediator) {
+ mediator.replay_manager = *this;
+}
+
+void ReplayManager::record_start() {
+ if (this->state == RECORDING) this->release(this->id);
+ this->id++;
+ this->memory[this->id] = make_unique<Recording>();
+ this->recording = *this->memory.at(this->id);
+ this->state = RECORDING;
+}
+
+recording_t ReplayManager::record_end() {
+ this->state = IDLE;
+ return this->id;
+}
+
+void ReplayManager::play(recording_t handle) {
+ if (!this->memory.contains(handle))
+ throw out_of_range(format("ReplayManager: no recording for handle {}", handle));
+ this->recording = *this->memory.at(handle);
+ this->recording->frame = 0;
+ this->state = PLAYING;
+}
+
+void ReplayManager::release(recording_t handle) {
+ if (!this->memory.contains(handle))
+ return;
+ 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
new file mode 100644
index 0000000..7be18f3
--- /dev/null
+++ b/src/crepe/manager/ReplayManager.h
@@ -0,0 +1,53 @@
+#pragma once
+
+#include <unordered_map>
+
+#include "Manager.h"
+#include "ComponentManager.h"
+#include "util/OptionalRef.h"
+
+namespace crepe {
+
+class ReplaySystem;
+
+typedef size_t recording_t;
+
+/**
+ * \brief Replay manager
+ *
+ */
+class ReplayManager : public Manager {
+ // TODO: Delete recordings at end of scene
+public:
+ ReplayManager(Mediator & mediator);
+
+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;
+};
+
+} // namespace crepe
diff --git a/src/crepe/manager/SystemManager.cpp b/src/crepe/manager/SystemManager.cpp
new file mode 100644
index 0000000..db7430e
--- /dev/null
+++ b/src/crepe/manager/SystemManager.cpp
@@ -0,0 +1,67 @@
+#include "../system/AISystem.h"
+#include "../system/AnimatorSystem.h"
+#include "../system/AudioSystem.h"
+#include "../system/CollisionSystem.h"
+#include "../system/InputSystem.h"
+#include "../system/ParticleSystem.h"
+#include "../system/PhysicsSystem.h"
+#include "../system/RenderSystem.h"
+#include "../system/ScriptSystem.h"
+#include "../system/EventSystem.h"
+#include "../system/ReplaySystem.h"
+
+#include "SystemManager.h"
+
+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>();
+ this->load_system<CollisionSystem>();
+ this->load_system<AnimatorSystem>();
+ this->load_system<ParticleSystem>();
+ this->load_system<RenderSystem>();
+ this->load_system<InputSystem>();
+ this->load_system<EventSystem>();
+ this->load_system<AudioSystem>();
+
+ this->mediator.system_manager = *this;
+}
+
+void SystemManager::fixed_update() {
+ for (auto & [type, system] : this->systems) {
+ if (!system->active) continue;
+ system->fixed_update();
+ }
+}
+
+void SystemManager::frame_update() {
+ for (auto & [type, system] : this->systems) {
+ if (!system->active) continue;
+ system->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
new file mode 100644
index 0000000..6cf7f2b
--- /dev/null
+++ b/src/crepe/manager/SystemManager.h
@@ -0,0 +1,61 @@
+#pragma once
+
+#include "../system/System.h"
+
+#include "Manager.h"
+
+namespace crepe {
+
+class SystemManager : public Manager {
+public:
+ SystemManager(Mediator &);
+
+ /**
+ * \brief Per-frame update.
+ *
+ * Updates the game state based on the elapsed time since the last frame.
+ */
+ void frame_update();
+
+ /**
+ * \brief Fixed update executed at a fixed rate.
+ *
+ * This function updates physics and game logic based on LoopTimer's fixed_delta_time.
+ */
+ void fixed_update();
+
+private:
+ /**
+ * \brief Collection of System instances
+ *
+ * This map holds System instances indexed by the system's class typeid. It is filled in the
+ * constructor of \c SystemManager using SystemManager::load_system.
+ */
+ std::unordered_map<std::type_index, std::unique_ptr<System>> systems;
+ /**
+ * \brief Initialize a system
+ * \tparam T System type (must be derivative of \c System)
+ */
+ template <class T>
+ void load_system();
+
+public:
+ /**
+ * \brief Retrieve a reference to ECS system
+ * \tparam T System type
+ * \returns Reference to system instance
+ * \throws std::runtime_error if the System is not initialized
+ */
+ 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
+
+#include "SystemManager.hpp"
diff --git a/src/crepe/manager/SystemManager.hpp b/src/crepe/manager/SystemManager.hpp
new file mode 100644
index 0000000..46ada5f
--- /dev/null
+++ b/src/crepe/manager/SystemManager.hpp
@@ -0,0 +1,40 @@
+#pragma once
+
+#include <memory>
+#include <cassert>
+#include <format>
+
+#include "SystemManager.h"
+
+namespace crepe {
+
+template <class T>
+T & SystemManager::get_system() {
+ using namespace std;
+ static_assert(is_base_of<System, T>::value, "get_system must recieve a derivative class of System");
+
+ const type_info & type = typeid(T);
+ if (!this->systems.contains(type))
+ throw runtime_error(format("SystemManager: {} is not initialized", type.name()));
+
+ System * system = this->systems.at(type).get();
+ T * concrete_system = dynamic_cast<T *>(system);
+ assert(concrete_system != nullptr);
+
+ return *concrete_system;
+}
+
+template <class T>
+void SystemManager::load_system() {
+ using namespace std;
+ static_assert(is_base_of<System, T>::value,
+ "load_system must recieve a derivative class of System");
+
+ const type_info & type = typeid(T);
+ if (this->systems.contains(type))
+ throw runtime_error(format("SystemManager: {} is already initialized", type.name()));
+ System * system = new T(this->mediator);
+ this->systems[type] = unique_ptr<System>(system);
+}
+
+} // namespace crepe