mirror of
https://gitlab.com/voidframe/voidframe-cpp.git
synced 2026-06-19 17:55:13 -04:00
Compare commits
2 Commits
27eb6ff7bb
...
70df396822
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
70df396822 | ||
|
|
f22b25642b |
@@ -1,25 +1,34 @@
|
|||||||
#ifndef VOID_ENTITY_H
|
#ifndef VOID_ENTITY_H
|
||||||
#define VOID_ENTITY_H
|
#define VOID_ENTITY_H
|
||||||
#include "component.h"
|
#include "component.h"
|
||||||
|
#include "world.h"
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
|
#include <cstdint>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <utility>
|
#include <utility>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
namespace VoidFrame {
|
namespace VoidFrame {
|
||||||
|
|
||||||
|
class World;
|
||||||
|
|
||||||
class Entity {
|
class Entity {
|
||||||
public:
|
public:
|
||||||
template <typename CompT, typename... Args>
|
template <typename CompT, typename... Args>
|
||||||
CompT *AddComponent(Args &&...args);
|
Entity *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();
|
template <typename CompT> Entity *RemoveComponent();
|
||||||
|
|
||||||
|
void Destroy();
|
||||||
void Update();
|
void Update();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
Entity();
|
||||||
std::vector<std::unique_ptr<Component>> components;
|
std::vector<std::unique_ptr<Component>> components;
|
||||||
|
World *world;
|
||||||
|
|
||||||
|
friend class World;
|
||||||
};
|
};
|
||||||
|
|
||||||
#include "../src/entity_i.cpp"
|
#include "../src/entity_i.cpp"
|
||||||
|
|||||||
20
include/world.h
Normal file
20
include/world.h
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
#pragma once
|
||||||
|
#include "entity.h"
|
||||||
|
#include <cstdint>
|
||||||
|
#include <memory>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
namespace VoidFrame {
|
||||||
|
|
||||||
|
class World {
|
||||||
|
public:
|
||||||
|
Entity *Spawn();
|
||||||
|
void Destroy(Entity *entity);
|
||||||
|
|
||||||
|
void Update();
|
||||||
|
|
||||||
|
private:
|
||||||
|
std::vector<std::unique_ptr<Entity>> entities;
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace VoidFrame
|
||||||
@@ -7,3 +7,5 @@ void Entity::Update() {
|
|||||||
comp.get()->Update();
|
comp.get()->Update();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Entity::Destroy() { world->Destroy(this); }
|
||||||
|
|||||||
@@ -6,11 +6,11 @@
|
|||||||
using namespace VoidFrame;
|
using namespace VoidFrame;
|
||||||
|
|
||||||
template <typename CompT, typename... Args>
|
template <typename CompT, typename... Args>
|
||||||
CompT *Entity::AddComponent(Args &&...args) {
|
Entity *Entity::AddComponent(Args &&...args) {
|
||||||
CompT *component = new CompT(std::forward<Args>(args)...);
|
CompT *component = new CompT(std::forward<Args>(args)...);
|
||||||
component->owner = this;
|
component->owner = this;
|
||||||
components.emplace_back(component);
|
components.emplace_back(component);
|
||||||
return component;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename CompT> CompT *Entity::GetComponent() {
|
template <typename CompT> CompT *Entity::GetComponent() {
|
||||||
@@ -24,11 +24,12 @@ template <typename CompT> bool Entity::HasComponent() {
|
|||||||
return GetComponent<CompT>() != nullptr;
|
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(),
|
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()) !=
|
return dynamic_cast<CompT *>(comp.get()) !=
|
||||||
nullptr;
|
nullptr;
|
||||||
}),
|
}),
|
||||||
components.end());
|
components.end());
|
||||||
|
return this;
|
||||||
}
|
}
|
||||||
|
|||||||
26
src/world.cpp
Normal file
26
src/world.cpp
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
#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;
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
|
||||||
|
void World::Update() {
|
||||||
|
for (auto &entity : entities) {
|
||||||
|
entity->Update();
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user