From a9d88e50e71ed30570c72f3587be8f8899ca697f Mon Sep 17 00:00:00 2001 From: Ulysse Cura Date: Sun, 16 Aug 2026 14:25:19 +0200 Subject: [PATCH] Loading entities from maps ! --- src/headers/map.h | 2 +- src/map.c | 176 +++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 176 insertions(+), 2 deletions(-) diff --git a/src/headers/map.h b/src/headers/map.h index f8f22be..9d29df0 100644 --- a/src/headers/map.h +++ b/src/headers/map.h @@ -29,6 +29,6 @@ map_t *load_map(void); void destroy_map(map_t *map); -void map_load_entities(map_t *map); +int map_load_entities(map_t *map); #endif // MAP_H \ No newline at end of file diff --git a/src/map.c b/src/map.c index 24bcc13..0aac524 100644 --- a/src/map.c +++ b/src/map.c @@ -3,6 +3,7 @@ #include "asset_manager.h" #include "game.h" #include "ecs.h" +#include "components.h" #include "vector2d.h" #include "memory_alloc.h" #include "errors.h" @@ -202,7 +203,180 @@ void destroy_map(map_t *map) if(map->backgroundlayer1) free(map->backgroundlayer1); } -void map_load_entities(map_t *map) +int map_load_entities(map_t *map) { + for(uint32_t i = 0; i < map->entities_nb; i++) + { + map_entity_def_t *entity_def = &map->entities_defs[i]; + entity_t *entity = entity_manager_new_entity(entity_def->id); + if(!entity) 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]; + + component_t *component = entity_new_component(entity, component_def->component_type); + + switch(component_def->component_type) + { + case TRANSFORM_COMPONENT: + { + union { + void *raw; + struct { + frect_t bounds; + } *soft; + } data; + data.raw = component_def->attributes; + + transform_component_data_t *component_data = component->data; + + component_data->bounds = data.soft->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); + if(!texture_asset) return_failure_int; + + component_data->texture = texture_asset->data; + + break; + } + + 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; + + 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; + + animation_system_apply_changes(component); + + break; + } + + case HITBOX_COMPONENT: + { + union { + void *raw; + struct { + fvector2d_t position; + fvector2d_t scale; + } *soft; + } data; + data.raw = component_def->attributes; + + hitbox_component_data_t *component_data = component->data; + + component_data->position = data.soft->position; + component_data->scale = data.soft->scale; + + break; + } + + case DOOR_COMPONENT: + { + union { + void *raw; + struct { + int topic_id; + bool state; + } *soft; + } data; + data.raw = 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; + + //if(door_component_subscribe(component)) return_failure_int; + + break; + } + + 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; + + interact_component_data_t *component_data = component->data; + + component_data->type = data.soft->type; + component_data->state = data.soft->state; + + if(interact_component_modify_topic_id(component, data.soft->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; + + lever_component_data_t *component_data = component->data; + + component_data->topic_id = data.soft->topic_id; + component_data->state = data.soft->state; + + if(lever_component_subscribe(component)) return_failure_int; + + break; + } + + default: + error_printf("Component type not supported for loading : %s (%d)", component_type_to_name(component_def->component_type), component_def->component_type); + return EXIT_FAILURE; + } + } + } + + return EXIT_SUCCESS; }