Working event bus, and implemented in interact component.

This commit is contained in:
Ulysse Cura 2026-08-10 12:38:55 +02:00
parent 06d2d6f8ad
commit 3548ef456f
5 changed files with 46 additions and 10 deletions

@ -1 +1 @@
Subproject commit 571bb9699df2b281157a68217ea490482bccd729
Subproject commit 2e180b6832978ebd7c211a863a9c2007f3a7c591

View File

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

View File

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

View File

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

View File

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