mirror of
https://gitlab.com/voidframe/voidframe-cpp.git
synced 2026-10-12 02:10:08 -04:00
Add worlds
This commit is contained in:
1 parent
27eb6ff7bb
commit
f22b25642b
5 files changed
+56
-6
No files matched your search
@@ -7,3 +7,5 @@ void Entity::Update() {
|
||||
comp.get()->Update();
|
||||
}
|
||||
}
|
||||
|
||||
void Entity::Destroy() { world->Destroy(this); }
|
||||
+5
-4
@@ -6,11 +6,11 @@
|
||||
using namespace VoidFrame;
|
||||
|
||||
template <typename CompT, typename... Args>
|
||||
CompT *Entity::AddComponent(Args &&...args) {
|
||||
Entity *Entity::AddComponent(Args &&...args) {
|
||||
CompT *component = new CompT(std::forward<Args>(args)...);
|
||||
component->owner = this;
|
||||
components.emplace_back(component);
|
||||
return component;
|
||||
return this;
|
||||
}
|
||||
|
||||
template <typename CompT> CompT *Entity::GetComponent() {
|
||||
@@ -24,11 +24,12 @@ template <typename CompT> bool Entity::HasComponent() {
|
||||
return GetComponent<CompT>() != nullptr;
|
||||
}
|
||||
|
||||
template <typename CompT> void Entity::RemoveComponent() {
|
||||
template <typename CompT> Entity *Entity::RemoveComponent() {
|
||||
components.erase(std::remove_if(components.begin(), components.end(),
|
||||
[](const std::unique_ptr<Component> &comp) {
|
||||
[&](const std::unique_ptr<Component> &comp) {
|
||||
return dynamic_cast<CompT *>(comp.get()) !=
|
||||
nullptr;
|
||||
}),
|
||||
components.end());
|
||||
return this;
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
#include "../include/world.h"
|
||||
#include <algorithm>
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
|
||||
using namespace VoidFrame;
|
||||
|
||||
Entity *World::Spawn() {
|
||||
Entity *entity = new Entity;
|
||||
entity->world = this;
|
||||
entities.emplace_back(entity);
|
||||
return entity;
|
||||
}
|
||||
|
||||
void World::Destroy(Entity *entity) {
|
||||
entities.erase(std::remove_if(entities.begin(), entities.end(),
|
||||
[&](const std::unique_ptr<Entity> &entityPtr) {
|
||||
return entityPtr.get() == entity;
|
||||
}));
|
||||
}
|
||||
Reference in new issue
Block a user