Compare commits

...

2 Commits

Author SHA1 Message Date
Ulysse Cura a9d88e50e7 Loading entities from maps ! 2026-08-16 14:25:19 +02:00
Ulysse Cura 11258031c2 Added error message in event bus subcription func. 2026-08-16 14:25:08 +02:00
3 changed files with 181 additions and 3 deletions

View File

@ -110,7 +110,11 @@ int event_bus_publish(size_t id, void *arg)
int event_bus_subscribe(size_t id, callback_t callback, void *data)
{
elem_t *topic_elem = linked_list_get_if(&game.event_bus.topics, condition_is_topic_id, &id);
if(!topic_elem) return_failure_int;
if(!topic_elem)
{
error_printf("Topic with id : %lu doesn't exists.", id);
return EXIT_FAILURE;
}
topic_t *topic = topic_elem->data;

View File

@ -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

176
src/map.c
View File

@ -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;
}