aboutsummaryrefslogtreecommitdiff
path: root/src/crepe/ComponentManager.h
diff options
context:
space:
mode:
authorWBoerenkamps <wrj.boerenkamps@student.avans.nl>2024-11-18 15:34:22 +0100
committerWBoerenkamps <wrj.boerenkamps@student.avans.nl>2024-11-18 15:34:22 +0100
commitb92da3c729c8fc7c002cb3acbb75c56cb63bd89e (patch)
tree454b64d31b7e0c9151da88a0d49ee782565f49e9 /src/crepe/ComponentManager.h
parent8b449e764b5c91cd3cb0c4fa404a290ab295a7ef (diff)
parent121b64b1cb6cfead5814070c8b0185d3d7308095 (diff)
Merge branch 'master' of https://github.com/lonkaars/crepe into wouter/events
Diffstat (limited to 'src/crepe/ComponentManager.h')
-rw-r--r--src/crepe/ComponentManager.h52
1 files changed, 39 insertions, 13 deletions
diff --git a/src/crepe/ComponentManager.h b/src/crepe/ComponentManager.h
index 9a2aa1d..2107453 100644
--- a/src/crepe/ComponentManager.h
+++ b/src/crepe/ComponentManager.h
@@ -1,34 +1,58 @@
#pragma once
-#include <cstdint>
#include <memory>
#include <typeindex>
#include <unordered_map>
#include <vector>
+#include "api/Vector2.h"
+
#include "Component.h"
namespace crepe {
+class GameObject;
+
/**
* \brief Manages all components
*
* This class manages all components. It provides methods to add, delete and get components.
*/
class ComponentManager {
+ // 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(); // dbg_trace
+ ~ComponentManager(); // dbg_trace
+
/**
- * \brief Get the instance of the ComponentManager
- *
- * \return The instance of the ComponentManager
+ * \brief Create a new game object using the component manager
+ *
+ * \param name Metadata::name (required)
+ * \param tag Metadata::tag (optional, empty by default)
+ * \param position Transform::position (optional, origin by default)
+ * \param rotation Transform::rotation (optional, 0 by default)
+ * \param scale Transform::scale (optional, 1 by default)
+ *
+ * \returns GameObject interface
+ *
+ * \note This method automatically assigns a new entity ID
*/
- static ComponentManager & get_instance();
- ComponentManager(const ComponentManager &) = delete;
- ComponentManager(ComponentManager &&) = delete;
- ComponentManager & operator=(const ComponentManager &) = delete;
- ComponentManager & operator=(ComponentManager &&) = delete;
- ~ComponentManager();
+ GameObject new_object(const std::string & name, const std::string & tag = "",
+ const Vector2 & 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;
/**
* \brief Add a component to the ComponentManager
*
@@ -76,6 +100,8 @@ public:
* This method deletes all components.
*/
void delete_all_components();
+
+public:
/**
* \brief Get all components of a specific type and id
*
@@ -99,9 +125,6 @@ public:
std::vector<std::reference_wrapper<T>> get_components_by_type() const;
private:
- ComponentManager();
-
-private:
/**
* \brief The components
*
@@ -114,6 +137,9 @@ private:
*/
std::unordered_map<std::type_index, std::vector<std::vector<std::unique_ptr<Component>>>>
components;
+
+ //! ID of next GameObject allocated by \c ComponentManager::new_object
+ game_object_id_t next_id = 0;
};
} // namespace crepe