221 lines
6.5 KiB
C
221 lines
6.5 KiB
C
#include "entity.h"
|
|
#include "state.h"
|
|
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
tc_entity_registry_s tc_init_entity_registry()
|
|
{
|
|
tc_entity_registry_s registry;
|
|
registry.types_capacity = 64;
|
|
registry.num_types = 0;
|
|
registry.types = calloc(sizeof(tc_entity_type_s), registry.types_capacity);
|
|
registry.entities_capacity = 512;
|
|
registry.num_entities = 0;
|
|
registry.entities = calloc(sizeof(tc_entity_s *), registry.entities_capacity);
|
|
|
|
return registry;
|
|
}
|
|
|
|
void tc_register_entity_type(tc_entity_type_s type)
|
|
{
|
|
if(tc_game_state_g.entity_registry.num_types
|
|
>=
|
|
tc_game_state_g.entity_registry.types_capacity
|
|
) {
|
|
tc_game_state_g.entity_registry.types_capacity *= 2;
|
|
tc_game_state_g.entity_registry.types =
|
|
realloc(
|
|
tc_game_state_g.entity_registry.types,
|
|
tc_game_state_g.entity_registry.types_capacity
|
|
);
|
|
}
|
|
|
|
tc_game_state_g.entity_registry.types[tc_game_state_g.entity_registry.num_types] = type;
|
|
|
|
++tc_game_state_g.entity_registry.num_types;
|
|
}
|
|
|
|
tc_entity_type_s * tc_get_entity_type_with_name(char *internal_name)
|
|
{
|
|
uint32_t num_types = tc_game_state_g.entity_registry.num_types;
|
|
tc_entity_type_s *types = tc_game_state_g.entity_registry.types;
|
|
|
|
uint32_t index = 0;
|
|
while(index < num_types)
|
|
{
|
|
if(!strcmp(types[index].internal_name, internal_name)) return &types[index];
|
|
++index;
|
|
}
|
|
return NULL;
|
|
}
|
|
|
|
tc_entity_s * tc_create_entity(char *type)
|
|
{
|
|
tc_entity_type_s *type_struct = tc_get_entity_type_with_name(type);
|
|
if(type_struct == NULL) return NULL;
|
|
|
|
return type_struct->functions.fn_create();
|
|
}
|
|
|
|
void tc_spawn_entity(tc_entity_s *entity, tc_location_s location)
|
|
{
|
|
entity->type->functions.fn_spawn(entity, location);
|
|
}
|
|
|
|
void tc_teleport_entity(tc_entity_s *entity, tc_location_s location)
|
|
{
|
|
entity->type->functions.fn_teleport(entity, location);
|
|
}
|
|
|
|
void tc_delete_entity(tc_entity_s *entity)
|
|
{
|
|
entity->type->functions.fn_delete(entity);
|
|
}
|
|
|
|
|
|
|
|
tc_entity_attribute_s * tc_get_entity_attribute_with_name(tc_entity_s *entity, char *name)
|
|
{
|
|
uint32_t attribute_index = 0;
|
|
while(attribute_index < entity->attributes_capacity)
|
|
{
|
|
tc_entity_attribute_s attribute = entity->attributes[attribute_index];
|
|
if(attribute.name == NULL)
|
|
{
|
|
++attribute_index;
|
|
continue;
|
|
}
|
|
|
|
if(!strcmp(attribute.name, name)) return &entity->attributes[attribute_index];
|
|
++attribute_index;
|
|
}
|
|
return NULL;
|
|
}
|
|
|
|
tc_entity_attribute_s * tc_allocate_entity_attribute(tc_entity_s *entity)
|
|
{
|
|
if(entity->num_attributes >= entity->attributes_capacity)
|
|
{
|
|
entity->attributes_capacity *= 2;
|
|
entity->attributes = realloc(entity->attributes, sizeof(tc_entity_s) * entity->attributes_capacity);
|
|
}
|
|
++entity->num_attributes;
|
|
return &entity->attributes[entity->num_attributes-1];
|
|
}
|
|
|
|
tc_entity_attribute_s * tc_get_or_allocate_entity_attribute_with_name(tc_entity_s *entity, char *name)
|
|
{
|
|
tc_entity_attribute_s *attribute;
|
|
if((attribute = tc_get_entity_attribute_with_name(entity, name)) == NULL)
|
|
{
|
|
attribute = tc_allocate_entity_attribute(entity);
|
|
attribute->name = name;
|
|
}
|
|
return attribute;
|
|
}
|
|
|
|
|
|
|
|
void tc_set_pointer_for_entity(tc_entity_s *entity, char *attr_name, void *pointer)
|
|
{
|
|
tc_get_or_allocate_entity_attribute_with_name(entity, attr_name)->value.pointer = pointer;
|
|
}
|
|
|
|
void tc_set_string_for_entity(tc_entity_s *entity, char *attr_name, char *string)
|
|
{
|
|
tc_get_or_allocate_entity_attribute_with_name(entity, attr_name)->value.string = string;
|
|
}
|
|
|
|
void tc_set_integer_for_entity(tc_entity_s *entity, char *attr_name, int64_t integer)
|
|
{
|
|
tc_get_or_allocate_entity_attribute_with_name(entity, attr_name)->value.integer = integer;
|
|
}
|
|
|
|
void tc_set_float_for_entity(tc_entity_s *entity, char *attr_name, double floating)
|
|
{
|
|
tc_get_or_allocate_entity_attribute_with_name(entity, attr_name)->value.floating = floating;
|
|
}
|
|
|
|
|
|
|
|
void * tc_get_pointer_from_entity(tc_entity_s *entity, char *attr_name)
|
|
{
|
|
return tc_get_or_allocate_entity_attribute_with_name(entity, attr_name)->value.pointer;
|
|
}
|
|
|
|
char * tc_get_string_from_entity(tc_entity_s *entity, char *attr_name)
|
|
{
|
|
return tc_get_or_allocate_entity_attribute_with_name(entity, attr_name)->value.string;
|
|
}
|
|
|
|
int64_t tc_get_integer_from_entity(tc_entity_s *entity, char *attr_name)
|
|
{
|
|
return tc_get_or_allocate_entity_attribute_with_name(entity, attr_name)->value.integer;
|
|
}
|
|
|
|
double tc_get_float_from_entity(tc_entity_s *entity, char *attr_name)
|
|
{
|
|
return tc_get_or_allocate_entity_attribute_with_name(entity, attr_name)->value.floating;
|
|
}
|
|
|
|
|
|
|
|
void tc_forget_entity_attribute(tc_entity_s *entity, char *attr_name)
|
|
{
|
|
uint32_t attribute_index = 0;
|
|
while(attribute_index < entity->attributes_capacity)
|
|
{
|
|
tc_entity_attribute_s *attribute = &entity->attributes[attribute_index];
|
|
if(attribute->name == NULL)
|
|
{
|
|
++attribute_index;
|
|
continue;
|
|
}
|
|
|
|
if(!strcmp(attribute->name, attr_name))
|
|
{
|
|
entity->attributes[attribute_index] = entity->attributes[entity->num_attributes];
|
|
--entity->num_attributes;
|
|
break;
|
|
}
|
|
++attribute_index;
|
|
}
|
|
return;
|
|
}
|
|
|
|
|
|
|
|
void tc_send_pointer_to_entity(tc_entity_s *entity, char *event_name, void *pointer)
|
|
{
|
|
tc_entity_event_s event;
|
|
event.event_type = event_name;
|
|
event.value.pointer = pointer;
|
|
entity->type->functions.fn_send_event(entity, event);
|
|
}
|
|
|
|
void tc_send_string_to_entity(tc_entity_s *entity, char *event_name, char *string)
|
|
{
|
|
tc_entity_event_s event;
|
|
event.event_type = event_name;
|
|
event.value.string = string;
|
|
entity->type->functions.fn_send_event(entity, event);
|
|
}
|
|
|
|
void tc_send_integer_to_entity(tc_entity_s *entity, char *event_name, int64_t integer)
|
|
{
|
|
tc_entity_event_s event;
|
|
event.event_type = event_name;
|
|
event.value.integer = integer;
|
|
entity->type->functions.fn_send_event(entity, event);
|
|
}
|
|
|
|
void tc_send_float_to_entity(tc_entity_s *entity, char *event_name, double floating)
|
|
{
|
|
tc_entity_event_s event;
|
|
event.event_type = event_name;
|
|
event.value.floating = floating;
|
|
entity->type->functions.fn_send_event(entity, event);
|
|
}
|
|
|