Compare commits

..

2 Commits

Author SHA1 Message Date
Maple Redleaf
27eb6ff7bb Cleanup component.h 2025-11-11 11:20:15 -06:00
Maple Redleaf
a730e82247 Namespace project & add RemoveComponent 2025-11-11 11:19:31 -06:00
4 changed files with 22 additions and 7 deletions

View File

@@ -1,14 +1,13 @@
#pragma once #pragma once
namespace VoidFrame {
class Entity; class Entity;
class Component { class Component {
public: public:
virtual void Init();
virtual void Draw();
virtual void Update(); virtual void Update();
virtual void FixedUpdate();
virtual void Destroy();
private: private:
Entity *owner; Entity *owner;
}; };
} // namespace VoidFrame

View File

@@ -1,15 +1,20 @@
#ifndef VOID_ENTITY_H #ifndef VOID_ENTITY_H
#define VOID_ENTITY_H #define VOID_ENTITY_H
#include "component.h" #include "component.h"
#include <algorithm>
#include <memory> #include <memory>
#include <utility>
#include <vector> #include <vector>
namespace VoidFrame {
class Entity { class Entity {
public: public:
template <typename CompT, typename... Args> template <typename CompT, typename... Args>
CompT *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();
template <typename CompT> void RemoveComponent();
void Update(); void Update();
@@ -18,4 +23,5 @@ private:
}; };
#include "../src/entity_i.cpp" #include "../src/entity_i.cpp"
} // namespace VoidFrame
#endif // VOID_ENTITY_H #endif // VOID_ENTITY_H

View File

@@ -1,5 +1,7 @@
#include "../include/entity.h" #include "../include/entity.h"
using namespace VoidFrame;
void Entity::Update() { void Entity::Update() {
for (auto &comp : components) { for (auto &comp : components) {
comp.get()->Update(); comp.get()->Update();

View File

@@ -2,9 +2,8 @@
#ifndef VOID_ENTITY_H #ifndef VOID_ENTITY_H
#include "../include/entity.h" #include "../include/entity.h"
#endif #endif
#include <memory>
#include <utility> using namespace VoidFrame;
#include <vector>
template <typename CompT, typename... Args> template <typename CompT, typename... Args>
CompT *Entity::AddComponent(Args &&...args) { CompT *Entity::AddComponent(Args &&...args) {
@@ -24,3 +23,12 @@ template <typename CompT> CompT *Entity::GetComponent() {
template <typename CompT> bool Entity::HasComponent() { template <typename CompT> bool Entity::HasComponent() {
return GetComponent<CompT>() != nullptr; return GetComponent<CompT>() != nullptr;
} }
template <typename CompT> void Entity::RemoveComponent() {
components.erase(std::remove_if(components.begin(), components.end(),
[](const std::unique_ptr<Component> &comp) {
return dynamic_cast<CompT *>(comp.get()) !=
nullptr;
}),
components.end());
}