aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorLoek Le Blansch <loek@pipeframe.xyz>2024-11-05 18:48:46 +0100
committerLoek Le Blansch <loek@pipeframe.xyz>2024-11-05 18:48:46 +0100
commit3fe7400de095756362b999908fd2a2ba3b71a848 (patch)
tree8a6c6d4213fdab076a451f37e6471ca5965b2c8f /src
parentae6a103946e437ca85cc69c5fc2cbf68d35ffeae (diff)
merge #18
Diffstat (limited to 'src')
-rw-r--r--src/crepe/Component.cpp1
-rw-r--r--src/crepe/Component.h2
-rw-r--r--src/crepe/ComponentManager.hpp3
-rw-r--r--src/crepe/Metadata.cpp5
-rw-r--r--src/crepe/Metadata.h6
-rw-r--r--src/crepe/api/GameObject.cpp21
-rw-r--r--src/crepe/api/GameObject.h5
-rw-r--r--src/crepe/api/Transform.cpp3
-rw-r--r--src/crepe/api/Transform.h4
-rw-r--r--src/example/ecs.cpp16
-rw-r--r--src/example/rendering.cpp1
11 files changed, 32 insertions, 35 deletions
diff --git a/src/crepe/Component.cpp b/src/crepe/Component.cpp
index 78b47fa..41e7273 100644
--- a/src/crepe/Component.cpp
+++ b/src/crepe/Component.cpp
@@ -4,4 +4,3 @@ using namespace crepe;
Component::Component(uint32_t id) : game_object_id(id), active(true) {}
-int Component::get_instances_max() const { return -1; }
diff --git a/src/crepe/Component.h b/src/crepe/Component.h
index 039836e..8db9b2a 100644
--- a/src/crepe/Component.h
+++ b/src/crepe/Component.h
@@ -13,7 +13,7 @@ protected:
public:
virtual ~Component() = default;
- virtual int get_instances_max() const;
+ virtual int get_instances_max() const { return -1; }
public:
uint32_t game_object_id;
diff --git a/src/crepe/ComponentManager.hpp b/src/crepe/ComponentManager.hpp
index f469d12..e74f2e9 100644
--- a/src/crepe/ComponentManager.hpp
+++ b/src/crepe/ComponentManager.hpp
@@ -36,12 +36,13 @@ T & ComponentManager::add_component(uint32_t id, Args &&... args) {
// Check if the vector size is not greater than get_instances_max
if (instance->get_instances_max() != -1
&& components[type][id].size() >= instance->get_instances_max()) {
+ // TODO: Exception
throw std::runtime_error(
"Exceeded maximum number of instances for this component type");
}
// store its unique_ptr in the vector<>
- components[type][id].push_back(move(instance));
+ components[type][id].push_back(std::move(instance));
return *instance;
}
diff --git a/src/crepe/Metadata.cpp b/src/crepe/Metadata.cpp
index d362e0a..1ba150a 100644
--- a/src/crepe/Metadata.cpp
+++ b/src/crepe/Metadata.cpp
@@ -3,7 +3,6 @@
using namespace crepe;
using namespace std;
-Metadata::Metadata(uint32_t gameObjectId, string name, string tag)
- : Component(gameObjectId), name(name), tag(tag) {}
+Metadata::Metadata(uint32_t game_object_id, const string & name, const string & tag)
+ : Component(game_object_id), name(name), tag(tag) {}
-int Metadata::get_instances_max() const { return 1; }
diff --git a/src/crepe/Metadata.h b/src/crepe/Metadata.h
index 2f08476..b946fd0 100644
--- a/src/crepe/Metadata.h
+++ b/src/crepe/Metadata.h
@@ -9,13 +9,13 @@ namespace crepe {
class Metadata : public Component {
public:
- Metadata(uint32_t game_object_id, std::string name, std::string tag);
- int get_instances_max() const;
+ Metadata(uint32_t game_object_id, const std::string & name, const std::string & tag);
+ virtual int get_instances_max() const { return 1; }
public:
std::string name;
std::string tag;
- uint32_t parent = UINT32_MAX;
+ uint32_t parent = -1;
std::vector<uint32_t> children;
};
diff --git a/src/crepe/api/GameObject.cpp b/src/crepe/api/GameObject.cpp
index 2592d2d..8a1a235 100644
--- a/src/crepe/api/GameObject.cpp
+++ b/src/crepe/api/GameObject.cpp
@@ -6,20 +6,21 @@
using namespace crepe;
using namespace std;
-GameObject::GameObject(uint32_t id, std::string name, std::string tag,
- Point position, double rotation, double scale)
- : id(id) {
+GameObject::GameObject(uint32_t id, std::string name, std::string tag, const Point & position, double rotation, double scale) : id(id) {
ComponentManager & mgr = ComponentManager::get_instance();
mgr.add_component<Transform>(this->id, position, rotation, scale);
mgr.add_component<Metadata>(this->id, name, tag);
}
-void GameObject::set_parent(GameObject & parent) {
+void GameObject::set_parent(const GameObject & parent) {
auto & mgr = ComponentManager::get_instance();
- vector<reference_wrapper<Metadata>> thisMetadata
- = mgr.get_components_by_id<Metadata>(this->id);
- vector<reference_wrapper<Metadata>> parentMetadata
- = mgr.get_components_by_id<Metadata>(parent.id);
- thisMetadata.at(0).get().parent = parent.id;
- parentMetadata.at(0).get().children.push_back(this->id);
+
+ // set parent on own Metadata component
+ vector<reference_wrapper<Metadata>> this_metadata = mgr.get_components_by_id<Metadata>(this->id);
+ this_metadata.at(0).get().parent = parent.id;
+
+ // add own id to children list of parent's Metadata component
+ vector<reference_wrapper<Metadata>> parent_metadata = mgr.get_components_by_id<Metadata>(parent.id);
+ parent_metadata.at(0).get().children.push_back(this->id);
}
+
diff --git a/src/crepe/api/GameObject.h b/src/crepe/api/GameObject.h
index dcd33ad..4c87639 100644
--- a/src/crepe/api/GameObject.h
+++ b/src/crepe/api/GameObject.h
@@ -9,9 +9,8 @@ namespace crepe {
class GameObject {
public:
- GameObject(uint32_t id, std::string name, std::string tag, Point position,
- double rotation, double scale);
- void set_parent(GameObject & parent);
+ GameObject(uint32_t id, std::string name, std::string tag, const Point & position, double rotation, double scale);
+ void set_parent(const GameObject & parent);
template <typename T, typename... Args>
T & add_component(Args &&... args);
diff --git a/src/crepe/api/Transform.cpp b/src/crepe/api/Transform.cpp
index a80aff3..9c9bb06 100644
--- a/src/crepe/api/Transform.cpp
+++ b/src/crepe/api/Transform.cpp
@@ -8,11 +8,10 @@
using namespace crepe;
-Transform::Transform(uint32_t game_id, Point point, double rot, double scale)
+Transform::Transform(uint32_t game_id, const Point & point, double rot, double scale)
: Component(game_id), position(point), rotation(rot), scale(scale) {
dbg_trace();
}
Transform::~Transform() { dbg_trace(); }
-int Transform::get_instances_max() const { return 1; }
diff --git a/src/crepe/api/Transform.h b/src/crepe/api/Transform.h
index f918115..557061b 100644
--- a/src/crepe/api/Transform.h
+++ b/src/crepe/api/Transform.h
@@ -14,9 +14,9 @@ class Transform : public Component {
// works similar (or the same) as those found in GLSL?
public:
- Transform(uint32_t id, Point, double, double);
+ Transform(uint32_t id, const Point &, double, double);
~Transform();
- int get_instances_max() const;
+ virtual int get_instances_max() const { return 1; }
//! Translation (shift)
Point position;
//! Rotation, in radians
diff --git a/src/example/ecs.cpp b/src/example/ecs.cpp
index eb5eeba..6f9752e 100644
--- a/src/example/ecs.cpp
+++ b/src/example/ecs.cpp
@@ -12,16 +12,16 @@ int main() {
// Create a few GameObjects
try {
GameObject body(0, "body", "person", Point{0, 0}, 0, 1);
- GameObject rightLeg(1, "rightLeg", "person", Point{1, 1}, 0, 1);
- GameObject leftLeg(2, "leftLeg", "person", Point{1, 1}, 0, 1);
- GameObject rightFoot(3, "rightFoot", "person", Point{2, 2}, 0, 1);
- GameObject leftFoot(4, "leftFoot", "person", Point{2, 2}, 0, 1);
+ GameObject right_leg(1, "rightLeg", "person", Point{1, 1}, 0, 1);
+ GameObject left_leg(2, "leftLeg", "person", Point{1, 1}, 0, 1);
+ GameObject right_foot(3, "rightFoot", "person", Point{2, 2}, 0, 1);
+ GameObject left_foot(4, "leftFoot", "person", Point{2, 2}, 0, 1);
// Set the parent of each GameObject
- rightFoot.set_parent(rightLeg);
- leftFoot.set_parent(leftLeg);
- rightLeg.set_parent(body);
- leftLeg.set_parent(body);
+ right_foot.set_parent(right_leg);
+ left_foot.set_parent(left_leg);
+ right_leg.set_parent(body);
+ left_leg.set_parent(body);
// Adding a second Transform component is not allowed and will invoke an exception
body.add_component<Transform>(Point{10, 10}, 0, 1);
diff --git a/src/example/rendering.cpp b/src/example/rendering.cpp
index f3d8a7d..3fe43d6 100644
--- a/src/example/rendering.cpp
+++ b/src/example/rendering.cpp
@@ -17,7 +17,6 @@ using namespace std;
using namespace crepe;
int main() {
-
dbg_trace();
auto obj = GameObject(0, "name", "tag", Point{0, 0}, 1, 1);