mirror of
https://gitlab.com/voidframe/voidframe-cpp.git
synced 2026-06-29 21:45:11 -04:00
Add worlds
This commit is contained in:
@@ -1,25 +1,34 @@
|
||||
#ifndef VOID_ENTITY_H
|
||||
#define VOID_ENTITY_H
|
||||
#include "component.h"
|
||||
#include "world.h"
|
||||
#include <algorithm>
|
||||
#include <cstdint>
|
||||
#include <memory>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
namespace VoidFrame {
|
||||
|
||||
class World;
|
||||
|
||||
class Entity {
|
||||
public:
|
||||
template <typename CompT, typename... Args>
|
||||
CompT *AddComponent(Args &&...args);
|
||||
Entity *AddComponent(Args &&...args);
|
||||
template <typename CompT> CompT *GetComponent();
|
||||
template <typename CompT> bool HasComponent();
|
||||
template <typename CompT> void RemoveComponent();
|
||||
template <typename CompT> Entity *RemoveComponent();
|
||||
|
||||
void Destroy();
|
||||
void Update();
|
||||
|
||||
private:
|
||||
Entity();
|
||||
std::vector<std::unique_ptr<Component>> components;
|
||||
World *world;
|
||||
|
||||
friend class World;
|
||||
};
|
||||
|
||||
#include "../src/entity_i.cpp"
|
||||
|
||||
18
include/world.h
Normal file
18
include/world.h
Normal file
@@ -0,0 +1,18 @@
|
||||
#pragma once
|
||||
#include "entity.h"
|
||||
#include <cstdint>
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
|
||||
namespace VoidFrame {
|
||||
|
||||
class World {
|
||||
public:
|
||||
Entity *Spawn();
|
||||
void Destroy(Entity *entity);
|
||||
|
||||
private:
|
||||
std::vector<std::unique_ptr<Entity>> entities;
|
||||
};
|
||||
|
||||
} // namespace VoidFrame
|
||||
@@ -7,3 +7,5 @@ void Entity::Update() {
|
||||
comp.get()->Update();
|
||||
}
|
||||
}
|
||||
|
||||
void Entity::Destroy() { world->Destroy(this); }
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
20
src/world.cpp
Normal file
20
src/world.cpp
Normal file
@@ -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