Nebula
Loading...
Searching...
No Matches
component.h
Go to the documentation of this file.
1#pragma once
2//------------------------------------------------------------------------------
9//------------------------------------------------------------------------------
11#include "game/componentid.h"
12#include "game/entity.h"
15#include "util/bitfield.h"
16
17namespace Game
18{
19
20class World;
21
23ComponentId GetComponentId(Util::StringAtom name);
25template <typename COMPONENT>
27
28//------------------------------------------------------------------------------
43
44//------------------------------------------------------------------------------
50{
51 uint32_t size = 0;
52 uint32_t capacity = 0;
53 void* buffer = nullptr;
54};
55
56//------------------------------------------------------------------------------
62template <typename COMPONENT_TYPE>
64{
65 using OnInitFunc = void (*)(Game::World*, Game::Entity, COMPONENT_TYPE*);
66 using OnDecayFunc = void (*)(Game::World*, Game::Entity, COMPONENT_TYPE*);
67
69 bool decay = false;
71 bool runtime = false;
73 OnInitFunc OnInit = nullptr;
79};
80
81//------------------------------------------------------------------------------
86{
87public:
89 template <typename T>
90 explicit ComponentInterface(Util::StringAtom name, T const& defaultValue, uint32_t flags)
91 : Attribute(name, defaultValue, flags)
92 {
93 this->componentName = T::Traits::name;
94 this->namespaceName = T::Traits::namespace_name;
95 this->fullyQualifiedName = T::Traits::fully_qualified_name;
96 this->numFields = T::Traits::num_fields;
97 this->fieldNames = (const char**)T::Traits::field_names;
98 this->fieldTypenames = (const char**)T::Traits::field_typenames;
99 this->fieldByteOffsets = (const size_t*)T::Traits::field_byte_offsets;
100 }
101
102 using ComponentInitFunc = void (*)(Game::World*, Game::Entity, void*);
103 using ComponentDecayFunc = void (*)(Game::World*, Game::Entity, void*);
106
107 const char* GetName() const { return componentName; }
108 const char* GetNamespace() const { return namespaceName; }
109 const char* GetFullyQualifiedName() const { return fullyQualifiedName; }
110 const char** GetFieldNames() const { return fieldNames; };
111 const char** GetFieldTypenames() const { return fieldTypenames; };
112 const size_t* GetFieldByteOffsets() const { return fieldByteOffsets; };
113 size_t const GetNumFields() const { return numFields; };
114
115private:
116 const char* componentName = nullptr;
117 const char* namespaceName = nullptr;
118 const char* fullyQualifiedName = nullptr;
119 const char** fieldNames = nullptr;
120 const char** fieldTypenames = nullptr;
121 const size_t* fieldByteOffsets = nullptr;
122 size_t numFields = 0;
123};
124
125//------------------------------------------------------------------------------
129//------------------------------------------------------------------------------
130
131//------------------------------------------------------------------------------
134template <typename COMPONENT>
135inline ComponentId
137{
138#if !PUBLIC_BUILD
140 {
141 n_error(
142 "Component '%s' is not registered! Make sure you call Game::RegisterComponent<T>() for all component types before "
143 "using them.",
144 COMPONENT::Traits::name
145 );
146 }
147#endif
149}
150
151//------------------------------------------------------------------------------
154inline ComponentId
159
160} // namespace Game
ComponentDecayFunc Decay
Definition component.h:105
const size_t * fieldByteOffsets
Definition component.h:121
const char * GetNamespace() const
Definition component.h:108
const char * fullyQualifiedName
Definition component.h:118
void(*)(Game::World *, Game::Entity, void *) ComponentDecayFunc
Definition component.h:103
const char * GetFullyQualifiedName() const
Definition component.h:109
const char ** GetFieldNames() const
Definition component.h:110
const char ** fieldNames
Definition component.h:119
const char ** GetFieldTypenames() const
Definition component.h:111
const char * componentName
Definition component.h:116
const char ** fieldTypenames
Definition component.h:120
void(*)(Game::World *, Game::Entity, void *) ComponentInitFunc
Definition component.h:102
const size_t * GetFieldByteOffsets() const
Definition component.h:112
const char * namespaceName
Definition component.h:117
ComponentInitFunc Init
Definition component.h:104
size_t numFields
Definition component.h:122
ComponentInterface(Util::StringAtom name, T const &defaultValue, uint32_t flags)
construct from template type, with default value.
Definition component.h:90
const char * GetName() const
Definition component.h:107
size_t const GetNumFields() const
Definition component.h:113
A container of entities, their components, and processors.
Definition world.h:92
Definition attribute.h:29
Util::StringAtom name
name of attribute
Definition attribute.h:49
Attribute(Util::StringAtom name, T const &defaultValue, uint32_t flags)
construct from template type, with default value.
Definition attribute.h:62
static AttributeId GetAttributeId(Util::StringAtom name)
get attribute id from name
Definition attributeregistry.h:213
static bool IsRegistered()
Check if a type is registered.
Definition attributeregistry.h:80
A StringAtom.
Definition stringatom.h:22
void __cdecl n_error(const char *msg,...)
This function is called when a serious situation is encountered which requires abortion of the applic...
Definition debug.cc:138
Game::EditorState.
Definition graphicsmanager.h:66
MemDb::AttributeId ComponentId
Definition componentid.h:15
ComponentFlags
Specifies special behaviour for a components.
Definition component.h:33
@ COMPONENTFLAG_NONE
regular component
Definition component.h:35
@ COMPONENTFLAG_DECAY
Component will decay.
Definition component.h:38
@ COMPONENTFLAG_RUNTIME
Component only holds state of a running world, like handles to what a manager created for the entity.
Definition component.h:41
ComponentId GetComponentId(Util::StringAtom name)
Returns a component id.
Definition component.h:155
AttributeId GetAttributeId()
Definition attributeregistry.h:67
Contains data for components flagged with COMPONENTFLAG_DECAY, that has been deleted this frame.
Definition component.h:50
uint32_t capacity
Definition component.h:52
void * buffer
Definition component.h:53
uint32_t size
Definition component.h:51
Used for registering component types to the game world.
Definition component.h:64
OnInitFunc OnInit
initialization function to run for the component, or nullptr if not needed.
Definition component.h:73
OnDecayFunc OnDecay
function to run when the component is removed from a live entity, or when the entity is deleted.
Definition component.h:78
bool runtime
Set to true if the component only holds state of a running world, it is left out when levels are save...
Definition component.h:71
void(*)(Game::World *, Game::Entity, COMPONENT_TYPE *) OnDecayFunc
Definition component.h:66
bool decay
Set to true if the component should end up in the decay buffer before being completely destroyed.
Definition component.h:69
void(*)(Game::World *, Game::Entity, COMPONENT_TYPE *) OnInitFunc
Definition component.h:65
An entity is essentially just an Id with some utility functions attached.
Definition entity.h:35