Added a map remove entities function and renamed component type to type in map_entity_component_def_t.

This commit is contained in:
Ulysse Cura 2026-08-16 20:47:19 +02:00
parent 71ae4eb05f
commit 009d463a65
2 changed files with 89 additions and 89 deletions

View File

@ -4,7 +4,7 @@
#include <stdint.h>
typedef struct map_entity_component_def_t {
uint32_t component_type;
uint32_t type;
void *attributes;
} map_entity_component_def_t;
@ -31,4 +31,6 @@ void destroy_map(map_t *map);
int map_load_entities(map_t *map);
int map_remove_entities(map_t *map);
#endif // MAP_H

174
src/map.c
View File

@ -89,13 +89,13 @@ map_t *load_map(void)
{
map_entity_component_def_t *component_def = &entity_def->components_defs[j];
if(fread(&component_def->component_type, sizeof(component_def->component_type), 1, game.asset_manager.asset_file) != 1)
if(fread(&component_def->type, sizeof(component_def->type), 1, game.asset_manager.asset_file) != 1)
{
error_printf("Failed to read in asset file.");
return NULL;
}
switch(component_def->component_type)
switch(component_def->type)
{
size_t data_size;
@ -162,7 +162,7 @@ map_t *load_map(void)
break;
default:
error_printf("Component type not supported for loading : %s (%d)", component_type_to_name(component_def->component_type), component_def->component_type);
error_printf("Component type not supported for loading : %s (%d)", component_type_to_name(component_def->type), component_def->type);
return NULL;
}
}
@ -181,7 +181,7 @@ void destroy_map(map_t *map)
if(entity_def->components_defs)
{
for(uint32_t j = 0; j < map->entities_defs->components_nb; j++)
for(uint32_t j = 0; j < entity_def->components_nb; j++)
{
map_entity_component_def_t *component_def = &entity_def->components_defs[j];
if(component_def->attributes) free(component_def->attributes);
@ -216,40 +216,28 @@ int map_load_entities(map_t *map)
{
map_entity_component_def_t *component_def = &entity_def->components_defs[j];
component_t *component = entity_new_component(entity, component_def->component_type);
component_t *component = entity_new_component(entity, component_def->type);
switch(component_def->component_type)
switch(component_def->type)
{
case TRANSFORM_COMPONENT:
{
union {
void *raw;
struct {
frect_t bounds;
} *soft;
} data;
data.raw = component_def->attributes;
struct {
frect_t bounds;
} *data = component_def->attributes;
transform_component_data_t *component_data = component->data;
component_data->bounds = data.soft->bounds;
component_data->bounds = data->bounds;
break;
}
case SPRITE_COMPONENT:
{
union {
void *raw;
struct {
char texture_name[];
} *soft;
} data;
data.raw = component_def->attributes;
sprite_component_data_t *component_data = component->data;
asset_t *texture_asset = asset_manager_load_asset(data.soft->texture_name, ASSET_TEXTURE);
asset_t *texture_asset = asset_manager_load_asset((char *)component_def->attributes, ASSET_TEXTURE);
if(!texture_asset) return_failure_int;
component_data->texture = texture_asset->data;
@ -259,27 +247,23 @@ int map_load_entities(map_t *map)
case ANIMATION_SYSTEM:
{
union {
void *raw;
struct {
int nb_frames;
int current_frame_nb;
float frame_delay_ms;
bool play;
bool loop;
bool reverse;
} *soft;
} data;
data.raw = component_def->attributes;
struct {
int nb_frames;
int current_frame_nb;
float frame_delay_ms;
bool play;
bool loop;
bool reverse;
} *data = component_def->attributes;
animation_system_data_t *system_data = component->data;
system_data->nb_frames = data.soft->nb_frames;
system_data->current_frame_nb = data.soft->current_frame_nb;
system_data->frame_delay_ms = data.soft->frame_delay_ms;
system_data->play = data.soft->play;
system_data->loop = data.soft->loop;
system_data->reverse = data.soft->reverse;
system_data->nb_frames = data->nb_frames;
system_data->current_frame_nb = data->current_frame_nb;
system_data->frame_delay_ms = data->frame_delay_ms;
system_data->play = data->play;
system_data->loop = data->loop;
system_data->reverse = data->reverse;
animation_system_apply_changes(component);
@ -288,38 +272,30 @@ int map_load_entities(map_t *map)
case HITBOX_COMPONENT:
{
union {
void *raw;
struct {
fvector2d_t position;
fvector2d_t scale;
} *soft;
} data;
data.raw = component_def->attributes;
struct {
fvector2d_t position;
fvector2d_t scale;
} *data = component_def->attributes;
hitbox_component_data_t *component_data = component->data;
component_data->position = data.soft->position;
component_data->scale = data.soft->scale;
component_data->position = data->position;
component_data->scale = data->scale;
break;
}
case DOOR_COMPONENT:
{
union {
void *raw;
struct {
int topic_id;
bool state;
} *soft;
} data;
data.raw = component_def->attributes;
struct {
int topic_id;
bool state;
} *data = component_def->attributes;
door_component_data_t *component_data = component->data;
component_data->topic_id = data.soft->topic_id;
component_data->state = data.soft->state;
component_data->topic_id = data->topic_id;
component_data->state = data->state;
if(door_component_subscribe(component)) return_failure_int;
@ -328,45 +304,37 @@ int map_load_entities(map_t *map)
case INTERACT_COMPONENT:
{
union {
void *raw;
struct {
fvector2d_t position;
fvector2d_t scale;
int topic_id;
int type;
int state;
} *soft;
} data;
data.raw = component_def->attributes;
struct {
fvector2d_t position;
fvector2d_t scale;
int topic_id;
int type;
int state;
} *data = component_def->attributes;
interact_component_data_t *component_data = component->data;
component_data->position = data.soft->position;
component_data->scale = data.soft->scale;
component_data->type = data.soft->type;
component_data->state = data.soft->state;
component_data->position = data->position;
component_data->scale = data->scale;
component_data->type = data->type;
component_data->state = data->state;
if(interact_component_modify_topic_id(component, data.soft->topic_id)) return_failure_int;
if(event_bus_new_topic(component_data->topic_id)) return_failure_int;
break;
}
case LEVER_COMPONENT:
{
union {
void *raw;
struct {
int topic_id;
bool state;
} *soft;
} data;
data.raw = component_def->attributes;
struct {
int topic_id;
bool state;
} *data = component_def->attributes;
lever_component_data_t *component_data = component->data;
component_data->topic_id = data.soft->topic_id;
component_data->state = data.soft->state;
component_data->topic_id = data->topic_id;
component_data->state = data->state;
if(lever_component_subscribe(component)) return_failure_int;
@ -374,7 +342,7 @@ int map_load_entities(map_t *map)
}
default:
error_printf("Component type not supported for loading : %s (%d)", component_type_to_name(component_def->component_type), component_def->component_type);
error_printf("Component type not supported for loading : %s (%d)", component_type_to_name(component_def->type), component_def->type);
return EXIT_FAILURE;
}
}
@ -382,3 +350,33 @@ int map_load_entities(map_t *map)
return EXIT_SUCCESS;
}
int map_remove_entities(map_t *map)
{
for(uint32_t i = 0; i < map->entities_nb; i++)
{
map_entity_def_t *entity_def = &map->entities_defs[i];
if(entity_manager_remove_entity(entity_def->id)) return_failure_int;
for(uint32_t j = 0; j < entity_def->components_nb; j++)
{
map_entity_component_def_t *component_def = &entity_def->components_defs[j];
switch(component_def->type)
{
case SPRITE_COMPONENT:
{
if(asset_manager_let_go_asset((char *)component_def->attributes)) return_failure_int;
break;
}
default:
break;
}
}
}
return EXIT_SUCCESS;
}