Fix Entity functions sofar

This commit is contained in:
maple
2025-11-10 21:16:06 +00:00
parent 650925b4e1
commit af7adaa6f2
2 changed files with 9 additions and 6 deletions

View File

@@ -7,7 +7,7 @@
class Entity { class Entity {
public: public:
template <typename CompT, typename... Args> template <typename CompT, typename... Args>
Component *AddComponent(Args &&...args); CompT *AddComponent(Args &&...args);
template <typename CompT> CompT *GetComponent(); template <typename CompT> CompT *GetComponent();
template <typename CompT> bool HasComponent(); template <typename CompT> bool HasComponent();

View File

@@ -7,14 +7,17 @@
#include <vector> #include <vector>
template <typename CompT, typename... Args> template <typename CompT, typename... Args>
Component *Entity::AddComponent(Args &&...args) { CompT *Entity::AddComponent(Args &&...args) {
components.push_back(std::make_unique<CompT>(std::forward<Args>(args)...)); CompT *component = new CompT(std::forward<Args>(args)...);
return components.back().get(); component->owner = this;
components.emplace_back(component);
return component;
} }
template <typename CompT> CompT *Entity::GetComponent() { template <typename CompT> CompT *Entity::GetComponent() {
for (const auto &comp_ptr : components) { for (auto &comp_ptr : components) {
return dynamic_cast<CompT *>(comp_ptr.get()); if (CompT *casted = dynamic_cast<CompT *>(comp_ptr.get()))
return casted;
} }
} }