mirror of
https://gitlab.com/voidframe/voidframe-cpp.git
synced 2026-06-29 21:45:11 -04:00
Namespace project & add RemoveComponent
This commit is contained in:
@@ -1,4 +1,6 @@
|
||||
#pragma once
|
||||
|
||||
namespace VoidFrame {
|
||||
class Entity;
|
||||
|
||||
class Component {
|
||||
@@ -12,3 +14,4 @@ public:
|
||||
private:
|
||||
Entity *owner;
|
||||
};
|
||||
} // namespace VoidFrame
|
||||
|
||||
@@ -1,15 +1,20 @@
|
||||
#ifndef VOID_ENTITY_H
|
||||
#define VOID_ENTITY_H
|
||||
#include "component.h"
|
||||
#include <algorithm>
|
||||
#include <memory>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
namespace VoidFrame {
|
||||
|
||||
class Entity {
|
||||
public:
|
||||
template <typename CompT, typename... Args>
|
||||
CompT *AddComponent(Args &&...args);
|
||||
template <typename CompT> CompT *GetComponent();
|
||||
template <typename CompT> bool HasComponent();
|
||||
template <typename CompT> void RemoveComponent();
|
||||
|
||||
void Update();
|
||||
|
||||
@@ -18,4 +23,5 @@ private:
|
||||
};
|
||||
|
||||
#include "../src/entity_i.cpp"
|
||||
} // namespace VoidFrame
|
||||
#endif // VOID_ENTITY_H
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
#include "../include/entity.h"
|
||||
|
||||
using namespace VoidFrame;
|
||||
|
||||
void Entity::Update() {
|
||||
for (auto &comp : components) {
|
||||
comp.get()->Update();
|
||||
|
||||
@@ -2,9 +2,8 @@
|
||||
#ifndef VOID_ENTITY_H
|
||||
#include "../include/entity.h"
|
||||
#endif
|
||||
#include <memory>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
using namespace VoidFrame;
|
||||
|
||||
template <typename CompT, typename... Args>
|
||||
CompT *Entity::AddComponent(Args &&...args) {
|
||||
@@ -24,3 +23,12 @@ template <typename CompT> CompT *Entity::GetComponent() {
|
||||
template <typename CompT> bool Entity::HasComponent() {
|
||||
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());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user