libecs-cpp 2.1.0
Entity Component System library in C++
Loading...
Searching...
No Matches
Entity.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <string>
4#include <memory>
5#include <type_traits>
7#include <libecs-cpp/json.hpp>
8
9namespace ecs
10{
11 class Container;
12 class Component;
13
21 class Entity
22 {
23 public:
29 Entity(ecs::Container *container, const std::string &handle);
31 nlohmann::json Export() const;
35 const std::string Handle;
43 std::shared_ptr<ecs::Component> Component(std::unique_ptr<ecs::Component> component);
49 template <class T = ecs::Component>
50 std::shared_ptr<T> ComponentGet(const std::string &type) const
51 {
52 const std::shared_ptr<ecs::Component> *found = this->componentFind(type);
53 if(found == nullptr)
54 {
55 return nullptr;
56 }
57 if constexpr(std::is_same_v<T, ecs::Component>)
58 {
59 return *found;
60 }
61 else
62 {
63 return std::dynamic_pointer_cast<T>(*found);
64 }
65 }
69 bool ComponentHas(const std::string &type) const;
72 void Destroy();
75 void ComponentDestroy(const std::string &type);
76 private:
77 const std::shared_ptr<ecs::Component> *componentFind(const std::string &type) const;
78 };
79}
A container: the entities, components, systems and resources that are updated together.
Definition Container.hpp:70
One thing in a container, to which components are attached.
Definition Entity.hpp:22
Entity(ecs::Container *container)
Creates an entity with a generated handle.
std::shared_ptr< ecs::Component > Component(std::unique_ptr< ecs::Component > component)
Attaches a component to this entity and returns the stored component.
Entity(ecs::Container *container, const std::string &handle)
Creates an entity with the given handle.
std::shared_ptr< T > ComponentGet(const std::string &type) const
Looks up this entity's component with the given type name, as class T.
Definition Entity.hpp:50
void ComponentDestroy(const std::string &type)
Removes this entity's component with the given type name.
nlohmann::json Export() const
Describes the entity and its components as JSON.
ecs::Container * Container
The container the entity belongs to.
Definition Entity.hpp:33
void Destroy()
Removes this entity and its components from the container.
const std::string Handle
The entity's handle, given at construction and never changed.
Definition Entity.hpp:35
bool ComponentHas(const std::string &type) const
Says whether this entity has a component stored under the given type name.
Definition Clock.hpp:10