From 3548ef456fca642f6344e5c5431eea79f5ee04ef Mon Sep 17 00:00:00 2001 From: Ulysse Cura Date: Mon, 10 Aug 2026 12:38:55 +0200 Subject: [PATCH] Working event bus, and implemented in interact component. --- SDL3_Core | 2 +- src/components/headers/interact_component.h | 13 +++++++++++++ src/components/interact_component.c | 18 +++++++++++++++++- src/components/player_system.c | 13 ++++++++++--- src/game.c | 10 +++++----- 5 files changed, 46 insertions(+), 10 deletions(-) diff --git a/SDL3_Core b/SDL3_Core index 571bb96..2e180b6 160000 --- a/SDL3_Core +++ b/SDL3_Core @@ -1 +1 @@ -Subproject commit 571bb9699df2b281157a68217ea490482bccd729 +Subproject commit 2e180b6832978ebd7c211a863a9c2007f3a7c591 diff --git a/src/components/headers/interact_component.h b/src/components/headers/interact_component.h index d3466f5..9fffe85 100644 --- a/src/components/headers/interact_component.h +++ b/src/components/headers/interact_component.h @@ -6,11 +6,22 @@ #include "vector2d.h" #include "errors.h" +typedef enum interact_type_t { + INTERACT_BUTTON, + INTERACT_TOGGLE +} interact_type_t; + typedef struct interact_component_data_t { transform_component_data_t *transform_component_data; + fvector2d_t position; fvector2d_t scale; frect_t bounds; + + size_t topic_id; + interact_type_t type; + size_t event_payload; + bool activated; } interact_component_data_t; @@ -24,4 +35,6 @@ int interact_component_draw(const component_t *component); int interact_component_destroy(component_t *component); +inline int interact_component_set_topic(interact_component_data_t *component_data, size_t id); + #endif // TRANSFORM_COMPONENT_H \ No newline at end of file diff --git a/src/components/interact_component.c b/src/components/interact_component.c index 8fe8bc5..af86290 100644 --- a/src/components/interact_component.c +++ b/src/components/interact_component.c @@ -1,6 +1,7 @@ #include "interact_component.h" #include "game.h" +#include "event_bus.h" #include "memory_alloc.h" #include "errors.h" @@ -20,12 +21,18 @@ inline int interact_component_init(component_t *component) .x = 0.0f, .y = 0.0f }; - component_data->scale = (fvector2d_t) { .x = 1.0f, .y = 1.0f }; + component_data->topic_id = 0; + component_data->type = INTERACT_TOGGLE; + + component_data->event_payload = 0; + + if(event_bus_new_topic(&game.event_bus, component_data->topic_id, 1)) return EXIT_FAILURE; + component_data->activated = true; return EXIT_SUCCESS; @@ -65,3 +72,12 @@ inline int interact_component_destroy(component_t *component) return EXIT_SUCCESS; } + +inline int interact_component_set_topic(interact_component_data_t *component_data, size_t id) +{ + if(event_bus_change_topic_id(&game.event_bus, component_data->topic_id, id)) return EXIT_FAILURE; + + component_data->topic_id = id; + + return EXIT_SUCCESS; +} \ No newline at end of file diff --git a/src/components/player_system.c b/src/components/player_system.c index 5985797..47796b0 100644 --- a/src/components/player_system.c +++ b/src/components/player_system.c @@ -169,18 +169,21 @@ static inline int action_detect_activate_interact(elem_t *elem, va_list args) const component_t *target_interact_component = entity_get_component(target_entity, INTERACT_COMPONENT); if(!target_interact_component) return EXIT_SUCCESS; - if(!((interact_component_data_t *)target_interact_component->data)->activated) + const interact_component_data_t *target_interact_component_data = target_interact_component->data; + + if(!target_interact_component_data->activated) { return EXIT_SUCCESS; } const frect_t *hitbox_bounds = &system_data->physics_system_data->hitbox_component_data->bounds; - const frect_t *target_interact_bounds = &((interact_component_data_t *)target_interact_component->data)->bounds; + const frect_t *target_interact_bounds = &target_interact_component_data->bounds; if(fhas_intersection(hitbox_bounds, target_interact_bounds)) { static size_t i = 0; debug_printf("Interact n*%lu", i++); + if(event_bus_publish(&game.event_bus, target_interact_component_data->topic_id, (void *)&target_interact_component_data->event_payload)) return EXIT_FAILURE; } return EXIT_SUCCESS; @@ -188,11 +191,15 @@ static inline int action_detect_activate_interact(elem_t *elem, va_list args) static inline int player_system_process_interaction(player_system_data_t *system_data) { - if(game.events.keys[KEY_INTERACT]) + static bool last_state = false; + + if(game.events.keys[KEY_INTERACT] && !last_state) { if(linked_list_for_each(&game.entity_manager.entities, action_detect_activate_interact, system_data)) return EXIT_FAILURE; } + last_state = game.events.keys[KEY_INTERACT]; + return EXIT_SUCCESS; } diff --git a/src/game.c b/src/game.c index 081881c..4b2a4ba 100644 --- a/src/game.c +++ b/src/game.c @@ -4,6 +4,8 @@ #include "display.h" #include "asset_manager.h" #include "ecs.h" +#include "event_bus.h" +#include "inputs.h" #include "components.h" #include "errors.h" @@ -12,8 +14,9 @@ int game_init(void) game.is_running = false; if(display_init()) return EXIT_FAILURE; - if(asset_manager_init(&game.asset_manager)) return EXIT_FAILURE; + event_bus_init(&game.event_bus); + entity_manager_init(&game.entity_manager); if(asset_manager_load_asset(&game.asset_manager, "dummy_sheet", ASSET_TEXTURE)) return EXIT_FAILURE; if(asset_manager_load_asset(&game.asset_manager, "player_idle_sheet", ASSET_TEXTURE)) return EXIT_FAILURE; @@ -23,8 +26,6 @@ int game_init(void) if(asset_manager_load_asset(&game.asset_manager, "barred_door_sheet", ASSET_TEXTURE)) return EXIT_FAILURE; if(asset_manager_load_asset(&game.asset_manager, "tileset", ASSET_TEXTURE)) return EXIT_FAILURE; - entity_manager_init(&game.entity_manager); - /* ======== Player ======== */ entity_t *player_entity = entity_manager_new_entity(&game.entity_manager, 0); if(!player_entity) return EXIT_FAILURE; @@ -188,9 +189,8 @@ int game_render(void) int game_exit(void) { entity_manager_exit(&game.entity_manager); - + event_bus_exit(&game.event_bus); if(asset_manager_exit(&game.asset_manager)) return EXIT_FAILURE; - display_exit(); return EXIT_SUCCESS;