15 #ifndef CORGI_COMPONENT_LIBRARY_ENTITY_FACTORY_H_
16 #define CORGI_COMPONENT_LIBRARY_ENTITY_FACTORY_H_
20 #include <unordered_map>
23 #include "flatbuffers/flatbuffers.h"
24 #include "library_components_generated.h"
27 namespace component_library {
54 : max_component_id_(0),
55 debug_entity_creation_(false) {}
107 std::vector<corgi::EntityRef>* entities_loaded);
117 std::unique_ptr<std::string> new_data);
155 const char* table_name);
164 if (data_type >= data_type_to_component_id_.size())
166 return data_type_to_component_id_[data_type];
176 if (component_id >= component_id_to_data_type_.size())
return kDataTypeNone;
177 return component_id_to_data_type_[component_id];
188 if (component_id >= component_id_to_table_name_.size())
return "";
189 return component_id_to_table_name_[component_id].c_str();
208 std::vector<uint8_t>* entity_serialized_output);
224 const std::vector<std::vector<uint8_t>>& entity_definitions,
225 std::vector<uint8_t>* entity_list_serialized);
260 std::vector<const void*>* entity_defs) = 0;
279 const void* entity_definition,
280 std::vector<const void*>* component_defs) = 0;
296 std::vector<uint8_t>* request) = 0;
312 const std::vector<const void*>& component_data,
313 std::vector<uint8_t>* entity_definition) = 0;
328 std::vector<uint8_t>* entity_list) = 0;
369 return flatbuffer_binary_schema_data_;
377 return prototype_data_;
382 std::string flatbuffer_binary_schema_data_;
385 std::unordered_map<std::string, std::unique_ptr<std::string>> loaded_files_;
389 std::vector<std::unique_ptr<std::string>> stale_files_;
392 std::unordered_map<std::string, const void*> prototype_data_;
396 std::unordered_map<std::string, std::vector<uint8_t>> prototype_requests_;
399 std::vector<corgi::ComponentId> data_type_to_component_id_;
400 std::vector<unsigned int> component_id_to_data_type_;
403 std::vector<std::string> component_id_to_table_name_;
409 bool debug_entity_creation_;
416 #endif // CORGI_COMPONENT_LIBRARY_ENTITY_FACTORY_H_
bool WillBeKeptInMemory(const void *pointer)
Check if the given pointer points to something that will be kept in memory for the lifetime of the En...
virtual bool CreateEntityList(const std::vector< const void * > &entity_defs, std::vector< uint8_t > *entity_list)=0
Handles building an Entity list flatbuffer from a collection of individual Entity flatbuffers...
static const int kErrorLoadingEntities
The return value for LoadEntitiesFromFile and LoadEntityListFromMemory if the methods are unable to r...
Definition: entity_factory.h:50
void SetComponentType(corgi::ComponentId component_id, unsigned int data_type, const char *table_name)
When you register each Component with the Entity system, it will get a component ID. This factory needs to know the Component ID assigned for each Component data type (the data_type() in the flatbuffer union).
bool debug_entity_creation() const
Check if debug logging is enabled.
Definition: entity_factory.h:236
const char * ComponentIdToTableName(corgi::ComponentId component_id)
Get the table name for a Component, given a Component ID.
Definition: entity_factory.h:187
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
corgi::ComponentId DataTypeToComponentId(unsigned int data_type)
Get the Component Id for a given data type specifier.
Definition: entity_factory.h:163
void set_debug_entity_creation(bool b)
Enable debug logging during Entity creation.
Definition: entity_factory.h:241
corgi::EntityRef CreateEntityFromData(const void *data, corgi::EntityManager *entity_manager)
Initialize an Entity from an Entity definition.
bool AddEntityLibrary(const char *entity_library_file)
Add a file from which you can load Entity prototype definitions.
virtual bool CreateEntityDefinition(const std::vector< const void * > &component_data, std::vector< uint8_t > *entity_definition)=0
Handles building a single Entity definition flatbuffer from a list of an Entity's Component definitio...
virtual void LoadEntityData(const void *def, corgi::EntityManager *entity_manager, corgi::EntityRef &entity, bool is_prototype)
Creates an Entity from the Entity definition, recursively building up from the prototypes.
virtual bool SerializeEntityList(const std::vector< std::vector< uint8_t >> &entity_definitions, std::vector< uint8_t > *entity_list_serialized)
After you call SerializeEntity on a few Entities, you may call this method to put them into a proper ...
EntityFactory()
The default constructor for an empty EntityFactory.
Definition: entity_factory.h:53
const ComponentId kInvalidComponent
A sentinel value to represent an invalid Component.
Definition: entity_common.h:44
unsigned int ComponentIdToDataType(corgi::ComponentId component_id)
Get the data type specifier for a Component, given a Component ID.
Definition: entity_factory.h:175
void SetFlatbufferSchema(const char *binary_schema_filename)
This factory and its subclasses need to know how to parse the Entity FlatBuffers using reflection...
virtual bool ReadEntityList(const void *entity_list, std::vector< const void * > *entity_defs)=0
Handles reading an Entity list and extracting the individual Entity data definitions.
virtual corgi::EntityRef CreateEntityFromPrototype(const char *prototype_name, corgi::EntityManager *entity_manager)
Initialize an entity from a given prototype.
virtual bool SerializeEntity(corgi::EntityRef &entity, corgi::EntityManager *entity_manager, std::vector< uint8_t > *entity_serialized_output)
Serialize an Entity into whatever binary type you are using for them.
void OverrideCachedFile(const char *filename, std::unique_ptr< std::string > new_data)
Override a cached file with data from memory that will persist until exit.
virtual bool CreatePrototypeRequest(const char *prototype_name, std::vector< uint8_t > *request)=0
Creates an Entity list that contains a single Entity definition, which contains a single Component de...
virtual int LoadEntitiesFromFile(const char *filename, corgi::EntityManager *entity_manager)
Load Entities from a given Entity list file.
A reference object for pointing into the vector pool. It acts as a pointer for vector pool elements a...
Definition: vector_pool.h:72
An interface for an Entity factory, which creates Entities for a given EntityManager.
Definition: entity_manager.h:408
An EntityFactory builds Entities based on prototypes, using FlatBuffers to specify the raw data for E...
Definition: entity_factory.h:37
static const unsigned int kDataTypeNone
This is equivalent to the *_NONE value of the FlatBuffer union enum.
Definition: entity_factory.h:43
int LoadEntityListFromMemory(const void *raw_entity_list, corgi::EntityManager *entity_manager, std::vector< corgi::EntityRef > *entities_loaded)
Loads a list of Entities from an in-memory buffer.
const std::string & flatbuffer_binary_schema_data() const
Get the FlatBuffer binary schema that you loaded with SetFlatbufferSchema().
Definition: entity_factory.h:368
virtual bool ReadEntityDefinition(const void *entity_definition, std::vector< const void * > *component_defs)=0
Handles reading an Entity definition and extracting the individual Component data definitions...
const std::unordered_map< std::string, const void * > & prototype_data() const
Get the map with all the current prototypes.
Definition: entity_factory.h:376
corgi::ComponentId max_component_id()
Get the maximum component ID.
Definition: entity_factory.h:230
uint16_t ComponentId
This represents the ID of a Component.
Definition: entity_common.h:36