aboutsummaryrefslogtreecommitdiff
path: root/src/crepe
diff options
context:
space:
mode:
Diffstat (limited to 'src/crepe')
-rw-r--r--src/crepe/api/BehaviorScript.hpp8
-rw-r--r--src/crepe/api/Script.cpp4
-rw-r--r--src/crepe/api/Script.h11
-rw-r--r--src/crepe/api/Script.hpp8
-rw-r--r--src/crepe/system/ScriptSystem.cpp30
-rw-r--r--src/crepe/system/ScriptSystem.h11
6 files changed, 24 insertions, 48 deletions
diff --git a/src/crepe/api/BehaviorScript.hpp b/src/crepe/api/BehaviorScript.hpp
index 5b5a418..bd59337 100644
--- a/src/crepe/api/BehaviorScript.hpp
+++ b/src/crepe/api/BehaviorScript.hpp
@@ -15,10 +15,10 @@ BehaviorScript & BehaviorScript::set_script(Args &&... args) {
static_assert(std::is_base_of<Script, T>::value);
Script * s = new T(std::forward<Args>(args)...);
- s->game_object_id_ref = &this->game_object_id;
- s->active_ref = &this->active;
- s->component_manager_ref = &this->component_manager;
- s->event_manager_ref = &EventManager::get_instance();
+ s->game_object_id = this->game_object_id;
+ s->active = this->active;
+ s->component_manager = this->component_manager;
+ s->event_manager = EventManager::get_instance();
this->script = std::unique_ptr<Script>(s);
return *this;
diff --git a/src/crepe/api/Script.cpp b/src/crepe/api/Script.cpp
index 0e73848..8bb3ef6 100644
--- a/src/crepe/api/Script.cpp
+++ b/src/crepe/api/Script.cpp
@@ -3,7 +3,7 @@
using namespace crepe;
Script::~Script() {
- EventManager & evmgr = *this->event_manager_ref;
+ EventManager & evmgr = this->event_manager;
for (auto id : this->listeners) {
evmgr.unsubscribe(id);
}
@@ -11,6 +11,6 @@ Script::~Script() {
template <>
void Script::subscribe(const EventHandler<CollisionEvent> & callback) {
- const game_object_id_t & game_object_id = *this->game_object_id_ref;
+ const game_object_id_t & game_object_id = this->game_object_id;
this->subscribe_internal(callback, game_object_id);
}
diff --git a/src/crepe/api/Script.h b/src/crepe/api/Script.h
index 43efd15..6418b04 100644
--- a/src/crepe/api/Script.h
+++ b/src/crepe/api/Script.h
@@ -2,6 +2,7 @@
#include <vector>
+#include "../util/OptionalRef.h"
#include "../types.h"
#include "EventManager.h"
@@ -150,18 +151,16 @@ private:
* implement a non-default constructor (e.g. for passing references to their own concrete
* Script classes).
*
- * \todo These should be converted to OptionalRef<> once `loek/util` is merged
- *
* \{
*/
//! Game object ID of game object parent BehaviorScript is attached to
- const game_object_id_t * game_object_id_ref = nullptr;
+ OptionalRef<const game_object_id_t> game_object_id;
//! Reference to parent component
- bool * active_ref = nullptr;
+ OptionalRef<bool> active;
//! Reference to component manager instance
- ComponentManager * component_manager_ref = nullptr;
+ OptionalRef<ComponentManager> component_manager;
//! Reference to event manager instance
- EventManager * event_manager_ref = nullptr;
+ OptionalRef<EventManager> event_manager;
//! \}
private:
diff --git a/src/crepe/api/Script.hpp b/src/crepe/api/Script.hpp
index e94278d..a2463bf 100644
--- a/src/crepe/api/Script.hpp
+++ b/src/crepe/api/Script.hpp
@@ -20,9 +20,9 @@ T & Script::get_component() const {
template <typename T>
RefVector<T> Script::get_components() const {
- ComponentManager & mgr = *this->component_manager_ref;
+ ComponentManager & mgr = this->component_manager;
- return mgr.get_components_by_id<T>(*this->game_object_id_ref);
+ return mgr.get_components_by_id<T>(this->game_object_id);
}
template <typename... Args>
@@ -33,10 +33,10 @@ void Script::logf(Args &&... args) {
template <typename EventType>
void Script::subscribe_internal(const EventHandler<EventType> & callback,
event_channel_t channel) {
- EventManager & mgr = *this->event_manager_ref;
+ EventManager & mgr = this->event_manager;
subscription_t listener = mgr.subscribe<EventType>(
[this, callback](const EventType & data) -> bool {
- bool & active = *this->active_ref;
+ bool & active = this->active;
if (!active) return false;
return callback(data);
},
diff --git a/src/crepe/system/ScriptSystem.cpp b/src/crepe/system/ScriptSystem.cpp
index c33309c..d54bbc9 100644
--- a/src/crepe/system/ScriptSystem.cpp
+++ b/src/crepe/system/ScriptSystem.cpp
@@ -1,5 +1,3 @@
-#include <functional>
-
#include "../ComponentManager.h"
#include "../api/BehaviorScript.h"
#include "../api/Script.h"
@@ -12,30 +10,20 @@ using namespace crepe;
void ScriptSystem::update() {
dbg_trace();
- RefVector<Script> scripts = this->get_scripts();
-
- for (auto & script_ref : scripts) {
- Script & script = script_ref.get();
- if (!script.initialized) {
- script.init();
- script.initialized = true;
- }
- script.update();
- }
-}
-
-RefVector<Script> ScriptSystem::get_scripts() const {
- RefVector<Script> scripts = {};
ComponentManager & mgr = this->component_manager;
RefVector<BehaviorScript> behavior_scripts = mgr.get_components_by_type<BehaviorScript>();
- for (auto behavior_script_ref : behavior_scripts) {
- BehaviorScript & behavior_script = behavior_script_ref.get();
+ for (BehaviorScript & behavior_script : behavior_scripts) {
if (!behavior_script.active) continue;
+
Script * script = behavior_script.script.get();
if (script == nullptr) continue;
- scripts.push_back(*script);
- }
- return scripts;
+ if (!script->initialized) {
+ script->init();
+ script->initialized = true;
+ }
+ script->update();
+ }
}
+
diff --git a/src/crepe/system/ScriptSystem.h b/src/crepe/system/ScriptSystem.h
index 32e1fcd..936e9ca 100644
--- a/src/crepe/system/ScriptSystem.h
+++ b/src/crepe/system/ScriptSystem.h
@@ -2,8 +2,6 @@
#include "System.h"
-#include "../types.h"
-
namespace crepe {
class Script;
@@ -25,15 +23,6 @@ public:
* the \c BehaviorScript instance.
*/
void update() override;
-
-private:
- /**
- * \brief Aggregate all active \c BehaviorScript components and return a list
- * of references to their \c Script instances (utility)
- *
- * \returns List of active \c Script instances
- */
- RefVector<Script> get_scripts() const;
};
} // namespace crepe