Techneck/code/source-c/entity.c

110 lines
3.2 KiB
C
Raw Normal View History

2023-10-14 13:04:52 +00:00
#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_from_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_from_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);
}
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);
}