15 #ifndef CORGI_COMPONENT_H_
16 #define CORGI_COMPONENT_H_
18 #include <unordered_map>
69 entity = std::move(src.entity);
70 data = std::move(src.data);
81 entity = std::move(src.entity);
82 data = std::move(src.data);
140 component_data->entity = entity;
142 return &(component_data->data);
170 RemoveEntityInternal(entity);
189 RemoveEntityInternal(entity);
268 if (data_index == kInvalidComponentIndex) {
271 ComponentData* element_data =
component_data_.GetElementData(data_index);
272 return (element_data !=
nullptr) ? &(element_data->data) :
nullptr;
345 template <
typename ComponentDataType>
361 template <
typename ComponentDataType>
365 ->HasDataForEntity(entity);
380 template <
typename ComponentDataType>
391 template <
typename ComponentDataType>
475 : kInvalidComponentIndex;
499 #endif // CORGI_COMPONENT_H_
std::unique_ptr< uint8_t, std::function< void(uint8_t *)> > RawDataUniquePtr
A pointer type for exported raw data.
Definition: component_interface.h:63
std::unordered_map< EntityIdType, ComponentIndex > component_index_lookup_
A map, for translating unique entity IDs into vectorpool indexes.
Definition: component.h:493
A Component is an object that encapsulates all data and logic for Entities of a particular type...
Definition: component.h:43
virtual void SetComponentIdOnDataType(ComponentId id)
Sets the Component ID on the data type.
Definition: component.h:452
ComponentDataType * Data(const EntityRef &entity)
A utility function for retrieving the Component data for an Entity from a specific Component...
Definition: component.h:346
T * GetComponent()
A helper function for getting a particular Component, given the Component's data type.
Definition: entity_manager.h:111
ComponentDataType * GetComponent()
A utility function for retrieving a reference to a specific Component object, by type.
Definition: component.h:392
Component()
Construct a Component without an EntityManager.
Definition: component.h:103
virtual EntityIterator end()
Gets an iterator that points to the end of the list of all entites in the Component.
Definition: component.h:207
A templated struct for holding type-dependent data.
Definition: component_id_lookup.h:36
virtual void SetEntityManager(EntityManager *entity_manager)
Set the EntityManager for this Component.
Definition: component.h:436
ComponentData(ComponentData &&src)
Construct a new ComponentData from an existing ComponentData.
Definition: component.h:68
T * AddEntity(EntityRef &entity)
Adds an Entity to the list that this Component is tracking.
Definition: component.h:154
virtual void Cleanup()
Override this function with any code that executes when this Component is removed from the EntityMana...
Definition: component.h:423
virtual void ClearComponentData()
Clears all tracked Component data.
Definition: component.h:327
The EntityManager is the code that manages all Entities and Components in the game. Normally the game will instantiate EntityManager, and then use it to create and control all of its Entities.
Definition: entity_manager.h:61
int WorldTime
A typedef that represents time in the game.
Definition: entity_common.h:49
T * GetComponentData(const EntityRef &entity)
Helper function for marshalling data from a Component.
Definition: entity_manager.h:83
virtual void InitEntity(EntityRef &)
Override this function with code that should be executed when an Entity is added to the Component...
Definition: component.h:407
ComponentDataType * Data(const EntityRef &entity) const
A utility function for retrieving the Component data for an Entity from a specific Component...
Definition: component.h:381
virtual RawDataUniquePtr ExportRawData(const EntityRef &) const
Override this function to return raw data that can be read back later.
Definition: component.h:416
T data
The data to associate with the Entity.
Definition: component.h:62
virtual void UpdateAllEntities(WorldTime)
Updates all Entities. This is normally called, once per frame, by the EntityManager.
Definition: component.h:211
virtual const void * GetComponentDataAsVoid(const EntityRef &entity) const
Gets the data for a given Entity as a const void pointer.
Definition: component.h:252
T * GetComponentData(const EntityRef &entity)
Gets the data for a given Entity.
Definition: component.h:287
AllocationLocation
kAddToFront allocates from the front of the pool. kAddToBack allocates from the back of the pool...
Definition: vector_pool.h:34
virtual void CleanupEntity(EntityRef &)
Override this function with any code that needs to be executed when an Entity is removed from this Co...
Definition: component.h:427
A pool allocator, implemented as a vector-based pair of linked lists.
Definition: vector_pool.h:43
virtual EntityIterator begin()
Gets an iterator that will iterate over every Entity associated with the Component, starting from the beginning.
Definition: component.h:200
ComponentId GetComponentId()
A helper function to get the component ID for a given Component.
Definition: entity_manager.h:196
virtual void Init()
Override this function with code that should be executed when the Component is added to the EntityMan...
Definition: component.h:403
A reference object for pointing into the vector pool. It acts as a pointer for vector pool elements a...
Definition: vector_pool.h:72
virtual ~Component()
Destructor for a Component.
Definition: component.h:106
T * AddEntity(EntityRef &entity, AllocationLocation alloc_location)
Adds an Entity to the list that this Component is tracking.
Definition: component.h:131
static ComponentId GetComponentId()
Get the ID for this Component.
Definition: component.h:443
EntityManager * entity_manager_
A pointer to the EntityManager for this Component. This is the main point of contact for Components t...
Definition: component.h:487
virtual void RemoveEntity(EntityRef &entity)
Removes an Entity from the list of Entities.
Definition: component.h:164
virtual EntityIterator RemoveEntity(EntityIterator iter)
Removes an Entity from the list of Entities.
Definition: component.h:187
T value_type
The templated data type stored by this Component.
Definition: component.h:100
VectorPool< ComponentData >::Iterator EntityIterator
An iterator to iterate through all of the Entities in the Component.
Definition: component.h:95
uint16_t ComponentIndex
A ComponentIndex is a value used to represent the location of a piece of ComponentData, normally inside of a VectorPool.
Definition: entity_common.h:59
virtual void AddEntityGenerically(EntityRef &entity)
Provides an alternate way to add Entities if you do not care about the returned data structure...
Definition: component.h:117
A structure of data that is associated with each Entity.
Definition: component.h:50
size_t GetComponentDataIndex(const EntityRef &entity) const
Get the index of the Component data for a given Entity.
Definition: component.h:472
ComponentData & operator=(ComponentData &&src)
Move a referenced ComponentData into this ComponentData.
Definition: component.h:80
const T * GetComponentData(const EntityRef &entity) const
Gets the data for a given Entity.
Definition: component.h:322
const T * GetComponentData(size_t data_index) const
Gets the Component data stored at a given index.
Definition: component.h:306
VectorPool< ComponentData > component_data_
Storage for all of the data for the Component.
Definition: component.h:481
An Iterator for the VectorPool.
Definition: vector_pool.h:51
bool IsRegisteredWithComponent(const EntityRef &entity)
A utility function for checking if an entity is registered with a particular component.
Definition: component.h:362
virtual void * GetComponentDataAsVoid(const EntityRef &entity)
Gets the data for a given Entity as a void pointer.
Definition: component.h:233
T * GetComponentData(size_t data_index)
Gets the Component data stored at a given index.
Definition: component.h:267
uint16_t ComponentId
This represents the ID of a Component.
Definition: entity_common.h:36
An interface that provides basic Component functionality. All Components will inherit from this class...
Definition: component_interface.h:57
ComponentData()
The default constructor for an empty ComponentData.
Definition: component.h:52
virtual bool HasDataForEntity(const EntityRef &entity)
Checks if this component contains any data associated with the supplied entity.
Definition: component.h:215
EntityRef entity
The Entity associated with this data.
Definition: component.h:57