Cogs.Core
Entity.cpp
1#include "Entity.h"
2#include "Component.h"
3
4#include "../Reflection/Field.h"
5#include "../Reflection/TypeDatabase.h"
6
7#include <cassert>
8
10{
11 Reflection::Field fields[] = {
13 };
14
15 Reflection::TypeDatabase::createAbstractType<Entity>().setFields(fields);
16}
17
19{
20 if (auto * component = handle.resolve(); component) {
21 this->components.add(handle);
22 component->setContainer(this);
23 }
24 else {
25 assert(false);
26 }
27}
28
30{
31 if(auto * component = handle.resolve(); component) {
32 component->setContainer(nullptr);
33 }
34
35 this->components.remove(handle);
36}
37
39{
40 auto handle = getComponentHandle(type);
41
42 return handle.resolve();
43}
44
46{
47 return getComponentHandle(type.getTypeId());
48}
49
51{
52 for (const auto & c : components) {
53 if (c.typeId == id) return c;
54 }
55
56 for (const auto & c : components) {
57 auto & componentType = Reflection::TypeDatabase::getType(c.typeId);
58
59 if (componentType.canCastTo(id)) {
60 return c;
61 }
62 }
63
65}
66
68{
69 return getComponentPtr(Reflection::TypeDatabase::getType(componentName));
70}
71
73{
74 auto handle = getComponentHandle(id);
75
76 return handle.resolve();
77}
78
80{
81 collection.clear();
82
83 auto & componentType = Reflection::TypeDatabase::getType(componentName);
84
85 for (const auto & c : components) {
86 auto& type = Reflection::TypeDatabase::getType(c.typeId);
87 if (type.canCastTo(componentType)) {
88 collection.add(c);
89 }
90 }
91}
92
94{
95 return getComponentHandle(Reflection::TypeDatabase::getType(componentName));
96}
97
99{
100 this->name = std::string(name);
101}
A collection of components, held by component handles.
COGSFOUNDATION_API void add(ComponentHandle component)
Adds the given component handle to the collection.
COGSFOUNDATION_API void clear()
Clears the contents of the collection.
Base class for Component instances.
Definition: Component.h:143
void removeComponent(ComponentHandle component)
Definition: Entity.cpp:29
T * getComponent() const
Get a pointer to the first component implementing the given type in the entity.
Definition: Entity.h:35
Component * getComponentPtr(const Reflection::Type &type) const
Get a pointer to the first component implementing the given type.
Definition: Entity.cpp:38
static void registerType()
Register the Entity type in the global type database.
Definition: Entity.cpp:9
const ComponentCollectionBase & getComponents() const
Get the collection of Component instances owned by this entity.
Definition: Entity.h:127
void addComponent(ComponentHandle component)
Definition: Entity.cpp:18
size_t id
Unique identifier.
Definition: Entity.h:147
ComponentHandle getComponentHandle() const
Get a component handle to the first component implementing the given type.
Definition: Entity.h:90
void setName(const StringView &name)
Definition: Entity.cpp:98
Field definition describing a single data member of a data structure.
Definition: Field.h:68
static const Type & getType()
Get the Type of the given template argument.
Definition: TypeDatabase.h:168
Represents a discrete type definition, describing a native type class.
Definition: Type.h:89
constexpr TypeId getTypeId() const
Get the unique Reflection::TypeId of this instance.
Definition: Type.h:325
Provides a weakly referenced view over the contents of a string.
Definition: StringView.h:24
uint16_t TypeId
Built in type used to uniquely identify a single type instance.
Definition: Name.h:48
Handle to a Component instance.
Definition: Component.h:67
COGSFOUNDATION_API class Component * resolve() const
Resolve the handle, returning a pointer to the held Component instance.
Definition: Component.cpp:65
static ComponentHandle Empty()
Returns an empty, invalid handle. Will evaluate to false if tested against using operator bool().
Definition: Component.h:119
Represents an unique name.
Definition: Name.h:70