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
25template <typename COMPONENT>
27
28//------------------------------------------------------------------------------
32enum ComponentFlags : uint32_t
33{
38 COMPONENTFLAG_DECAY = 1 << 0
39};
40
41//------------------------------------------------------------------------------
47{
48 uint32_t size = 0;
49 uint32_t capacity = 0;
50 void* buffer = nullptr;
51};
52
53//------------------------------------------------------------------------------
59template <typename COMPONENT_TYPE>
61{
62 using OnInitFunc = void (*)(Game::World*, Game::Entity, COMPONENT_TYPE*);
63
65 bool decay = false;
67 OnInitFunc OnInit = nullptr;
68};
69
70//------------------------------------------------------------------------------
75{
76public:
78 template <typename T>
79 explicit ComponentInterface(Util::StringAtom name, T const& defaultValue, uint32_t flags)
80 : Attribute(name, defaultValue, flags)
81 {
82 this->componentName = T::Traits::name;
83 this->fullyQualifiedName = T::Traits::fully_qualified_name;
84 this->numFields = T::Traits::num_fields;
85 this->fieldNames = (const char**)T::Traits::field_names;
86 this->fieldTypenames = (const char**)T::Traits::field_typenames;
87 this->fieldByteOffsets = (const size_t*)T::Traits::field_byte_offsets;
88 }
89
90 using ComponentInitFunc = void (*)(Game::World*, Game::Entity, void*);
92
93 const char* GetName() const { return componentName; }
94 const char* GetFullyQualifiedName() const { return fullyQualifiedName; }
95 const char** GetFieldNames() const { return fieldNames; };
96 const char** GetFieldTypenames() const { return fieldTypenames; };
97 const size_t* GetFieldByteOffsets() const { return fieldByteOffsets; };
98 size_t const GetNumFields() const { return numFields; };
99
100private:
101 const char* componentName = nullptr;
102 const char* fullyQualifiedName = nullptr;
103 const char** fieldNames = nullptr;
104 const char** fieldTypenames = nullptr;
105 const size_t* fieldByteOffsets = nullptr;
106 size_t numFields = 0;
107};
108
109//------------------------------------------------------------------------------
113//------------------------------------------------------------------------------
114
115//------------------------------------------------------------------------------
118template <typename COMPONENT>
119inline ComponentId
121{
122#if !PUBLIC_BUILD
124 {
125 n_error(
126 "Component '%s' is not registered! Make sure you call Game::RegisterComponent<T>() for all component types before "
127 "using them.",
128 COMPONENT::Traits::name
129 );
130 }
131#endif
133}
134
135//------------------------------------------------------------------------------
138inline ComponentId
143
144} // namespace Game
These are registered to the attribute registry so that we can add more functionality to attributes.
Definition component.h:75
const size_t * fieldByteOffsets
Definition component.h:105
const char * fullyQualifiedName
Definition component.h:102
const char * GetFullyQualifiedName() const
Definition component.h:94
const char ** GetFieldNames() const
Definition component.h:95
const char ** fieldNames
Definition component.h:103
const char ** GetFieldTypenames() const
Definition component.h:96
const char * componentName
Definition component.h:101
const char ** fieldTypenames
Definition component.h:104
void(*)(Game::World *, Game::Entity, void *) ComponentInitFunc
Definition component.h:90
const size_t * GetFieldByteOffsets() const
Definition component.h:97
ComponentInitFunc Init
Definition component.h:91
size_t numFields
Definition component.h:106
ComponentInterface(Util::StringAtom name, T const &defaultValue, uint32_t flags)
construct from template type, with default value.
Definition component.h:79
const char * GetName() const
Definition component.h:93
size_t const GetNumFields() const
Definition component.h:98
A container of entities, their components, and processors.
Definition attribute.h:29
Attribute()=default
default constructor
Util::StringAtom name
name of attribute
Definition attribute.h:49
static AttributeId GetAttributeId(Util::StringAtom name)
get attribute id from name
Definition attributeregistry.h:212
static bool IsRegistered()
Check if a type is registered.
Definition attributeregistry.h:79
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:60
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
ComponentId GetComponentId()
Returns a component id, based on template type.
Definition component.h:120
AttributeId GetAttributeId()
Definition attributeregistry.h:67
Contains data for components flagged with COMPONENTFLAG_DECAY, that has been deleted this frame.
Definition component.h:47
uint32_t capacity
Definition component.h:49
void * buffer
Definition component.h:50
uint32_t size
Definition component.h:48
Used for registering component types to the game world.
Definition component.h:61
OnInitFunc OnInit
initialization function to run for the component, or nullptr if not needed.
Definition component.h:67
bool decay
Set to true if the component should end up in the decay buffer before being completely destroyed.
Definition component.h:65
void(*)(Game::World *, Game::Entity, COMPONENT_TYPE *) OnInitFunc
Definition component.h:62
An entity is essentially just an Id with some utility functions attached.
Definition entity.h:35
Definition attributeid.h:19