aboutsummaryrefslogtreecommitdiff
path: root/src/crepe
diff options
context:
space:
mode:
authorLoek Le Blansch <loek@pipeframe.xyz>2024-10-06 17:39:15 +0200
committerLoek Le Blansch <loek@pipeframe.xyz>2024-10-06 17:39:15 +0200
commit2969fe8c0fca4826ca129fe12d2e125bb7955c78 (patch)
treeb7514048a8894cc5c64657e817765eb4e860187c /src/crepe
parent8a509ad4c5e15fbf7eaade1c8bf4834f0d0069c5 (diff)
WIP ScriptSystem
Diffstat (limited to 'src/crepe')
-rw-r--r--src/crepe/CMakeLists.txt4
-rw-r--r--src/crepe/ComponentManager.hpp14
-rw-r--r--src/crepe/Script.cpp7
-rw-r--r--src/crepe/Script.h16
-rw-r--r--src/crepe/ScriptSystem.cpp22
-rw-r--r--src/crepe/ScriptSystem.h18
-rw-r--r--src/crepe/System.h23
-rw-r--r--src/crepe/api/BehaviorScript.cpp10
-rw-r--r--src/crepe/api/BehaviorScript.h17
-rw-r--r--src/crepe/api/CMakeLists.txt5
-rw-r--r--src/crepe/api/Component.h10
11 files changed, 130 insertions, 16 deletions
diff --git a/src/crepe/CMakeLists.txt b/src/crepe/CMakeLists.txt
index d7d563e..d85aef0 100644
--- a/src/crepe/CMakeLists.txt
+++ b/src/crepe/CMakeLists.txt
@@ -8,6 +8,8 @@ target_sources(crepe PUBLIC
Collider.cpp
Rigidbody.cpp
Sprite.cpp
+ ScriptSystem.cpp
+ Script.cpp
)
target_sources(crepe PUBLIC FILE_SET HEADERS FILES
@@ -22,6 +24,8 @@ target_sources(crepe PUBLIC FILE_SET HEADERS FILES
Collider.h
Rigidbody.h
Sprite.h
+ System.h
+ ScriptSystem.h
)
add_subdirectory(api)
diff --git a/src/crepe/ComponentManager.hpp b/src/crepe/ComponentManager.hpp
index 999cdcf..084cd33 100644
--- a/src/crepe/ComponentManager.hpp
+++ b/src/crepe/ComponentManager.hpp
@@ -1,13 +1,17 @@
#pragma once
+#include <type_traits>
+
#include "ComponentManager.h"
namespace crepe {
-template <typename T, typename... Args>
+template <class T, typename... Args>
void ComponentManager::add_component(uint32_t id, Args &&... args) {
using namespace std;
+ static_assert(is_base_of<Component, T>::value, "add_component must recieve a derivative class of Component");
+
// Determine the type of T (this is used as the key of the unordered_map<>)
type_index type = typeid(T);
@@ -23,9 +27,11 @@ void ComponentManager::add_component(uint32_t id, Args &&... args) {
components[type].resize(id + 1);
}
- // Create a new component of type T using perfect forwarding and store its
- // unique_ptr in the vector<>
- components[type][id].push_back(make_unique<T>(forward<Args>(args)...));
+ // Create a new component of type T (arguments directly forwarded). The
+ // constructor must be called by ComponentManager.
+ T * instance = new T(forward<Args>(args)...);
+ // store its unique_ptr in the vector<>
+ components[type][id].push_back(unique_ptr<T>(instance));
}
template <typename T>
diff --git a/src/crepe/Script.cpp b/src/crepe/Script.cpp
new file mode 100644
index 0000000..42e3666
--- /dev/null
+++ b/src/crepe/Script.cpp
@@ -0,0 +1,7 @@
+#include "Script.h"
+
+using namespace crepe;
+
+void Script::init() { }
+void Script::update() { }
+
diff --git a/src/crepe/Script.h b/src/crepe/Script.h
new file mode 100644
index 0000000..ba4073a
--- /dev/null
+++ b/src/crepe/Script.h
@@ -0,0 +1,16 @@
+#pragma once
+
+namespace crepe {
+
+class Script {
+protected:
+ virtual void init();
+ virtual void update();
+ // NOTE: additional *events* (like unity's OnDisable and OnEnable) should be
+ // implemented as member methods in derivative user script classes and
+ // registered in init(), otherwise this class will balloon in size with each
+ // added event.
+};
+
+}
+
diff --git a/src/crepe/ScriptSystem.cpp b/src/crepe/ScriptSystem.cpp
new file mode 100644
index 0000000..e301c71
--- /dev/null
+++ b/src/crepe/ScriptSystem.cpp
@@ -0,0 +1,22 @@
+#include "util/log.h"
+
+#include "ScriptSystem.h"
+
+using namespace crepe;
+
+ScriptSystem::ScriptSystem() {
+ dbg_trace();
+}
+ScriptSystem::~ScriptSystem() {
+ dbg_trace();
+}
+
+ScriptSystem & ScriptSystem::get_instance() {
+ static ScriptSystem instance;
+ return instance;
+}
+
+void ScriptSystem::update() {
+ dbg_trace();
+}
+
diff --git a/src/crepe/ScriptSystem.h b/src/crepe/ScriptSystem.h
new file mode 100644
index 0000000..e1ed290
--- /dev/null
+++ b/src/crepe/ScriptSystem.h
@@ -0,0 +1,18 @@
+#pragma once
+
+#include "System.h"
+
+namespace crepe {
+
+class ScriptSystem : public System {
+public:
+ static ScriptSystem & get_instance();
+ virtual void update();
+
+private:
+ ScriptSystem();
+ ~ScriptSystem();
+};
+
+}
+
diff --git a/src/crepe/System.h b/src/crepe/System.h
new file mode 100644
index 0000000..3fe3d66
--- /dev/null
+++ b/src/crepe/System.h
@@ -0,0 +1,23 @@
+#pragma once
+
+namespace crepe {
+
+class System {
+public:
+ static System & get_instance();
+ virtual void update() = 0;
+
+protected:
+ System() { };
+ virtual ~System() { };
+
+private:
+ // singleton
+ System(const System &) = delete;
+ System(System &&) = delete;
+ System & operator=(const System &) = delete;
+ System & operator=(System &&) = delete;
+};
+
+}
+
diff --git a/src/crepe/api/BehaviorScript.cpp b/src/crepe/api/BehaviorScript.cpp
new file mode 100644
index 0000000..2dd933e
--- /dev/null
+++ b/src/crepe/api/BehaviorScript.cpp
@@ -0,0 +1,10 @@
+#include "../util/log.h"
+
+#include "BehaviorScript.h"
+
+using namespace crepe::api;
+
+BehaviorScript::BehaviorScript() {
+ dbg_trace();
+}
+
diff --git a/src/crepe/api/BehaviorScript.h b/src/crepe/api/BehaviorScript.h
new file mode 100644
index 0000000..e9542c1
--- /dev/null
+++ b/src/crepe/api/BehaviorScript.h
@@ -0,0 +1,17 @@
+#pragma once
+
+#include "../Script.h"
+#include "../Component.h"
+
+namespace crepe::api {
+
+class BehaviorScript : public Script, public Component {
+ // only allow ComponentManager to instantiate scripts
+ friend class ComponentManager;
+
+protected:
+ BehaviorScript();
+};
+
+}
+
diff --git a/src/crepe/api/CMakeLists.txt b/src/crepe/api/CMakeLists.txt
index 9548594..86623de 100644
--- a/src/crepe/api/CMakeLists.txt
+++ b/src/crepe/api/CMakeLists.txt
@@ -1,9 +1,10 @@
target_sources(crepe PUBLIC
# AudioSource.cpp
+ BehaviorScript.cpp
)
target_sources(crepe PUBLIC FILE_SET HEADERS FILES
- AudioSource.h
- Component.h
+ # AudioSource.h
+ BehaviorScript.h
)
diff --git a/src/crepe/api/Component.h b/src/crepe/api/Component.h
deleted file mode 100644
index d5e0499..0000000
--- a/src/crepe/api/Component.h
+++ /dev/null
@@ -1,10 +0,0 @@
-#pragma once
-
-namespace crepe::api {
-
-class Component {
-public:
- bool active;
-};
-
-} // namespace crepe::api