Starting to add interact component.

This commit is contained in:
Ulysse Cura 2026-08-08 20:52:38 +02:00
parent a636553e39
commit 1878f02972
9 changed files with 114 additions and 6 deletions

View File

@ -2,9 +2,10 @@
SOURCES := \ SOURCES := \
game.c \ game.c \
components/transform_component.c \ components/transform_component.c \
components/sprite_component.c \
components/hitbox_component.c \ components/hitbox_component.c \
components/physics_system.c \ components/physics_system.c \
components/sprite_component.c \ components/interact_component.c \
components/animation_system.c \ components/animation_system.c \
components/player_system.c components/player_system.c

View File

@ -5,9 +5,11 @@
#if DEBUG >= 2 #if DEBUG >= 2
COMPONENT(TRANSFORM_COMPONENT, transform_component, DRAW) COMPONENT(TRANSFORM_COMPONENT, transform_component, DRAW)
COMPONENT(HITBOX_COMPONENT, hitbox_component, DRAW) COMPONENT(HITBOX_COMPONENT, hitbox_component, DRAW)
COMPONENT(INTERACT_COMPONENT, interact_component, DRAW)
#else #else
COMPONENT(TRANSFORM_COMPONENT, transform_component, NO_DRAW) COMPONENT(TRANSFORM_COMPONENT, transform_component, NO_DRAW)
COMPONENT(HITBOX_COMPONENT, hitbox_component, NO_DRAW) COMPONENT(HITBOX_COMPONENT, hitbox_component, NO_DRAW)
COMPONENT(INTERACT_COMPONENT, interct_component, NO_DRAW)
#endif // DEBUG >= 2 #endif // DEBUG >= 2
COMPONENT(PHYSICS_SYSTEM, physics_system, NO_DRAW) COMPONENT(PHYSICS_SYSTEM, physics_system, NO_DRAW)

View File

@ -6,6 +6,7 @@
#include "animation_system.h" #include "animation_system.h"
#include "hitbox_component.h" #include "hitbox_component.h"
#include "physics_system.h" #include "physics_system.h"
#include "interact_component.h"
#include "player_system.h" #include "player_system.h"
// Add new components header file here // Add new components header file here

View File

@ -0,0 +1,26 @@
#ifndef INTERACT_COMPONENT_H
#define INTERACT_COMPONENT_H
#include "ecs.h"
#include "transform_component.h"
#include "vector2d.h"
#include "errors.h"
typedef struct interact_component_data_t {
transform_component_data_t *transform_component_data;
fvector2d_t position;
fvector2d_t scale;
frect_t bounds;
} interact_component_data_t;
int interact_component_init(component_t *component);
int interact_component_update(component_t *component);
#if DEBUG >= 2
int interact_component_draw(const component_t *component);
#endif // DEBUG >= 2
int interact_component_destroy(component_t *component);
#endif // TRANSFORM_COMPONENT_H

View File

@ -5,7 +5,7 @@
#include "memory_alloc.h" #include "memory_alloc.h"
#include "errors.h" #include "errors.h"
int hitbox_component_init(component_t *component) inline int hitbox_component_init(component_t *component)
{ {
hitbox_component_data_t *component_data = component->data; hitbox_component_data_t *component_data = component->data;
@ -32,7 +32,7 @@ int hitbox_component_init(component_t *component)
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }
int hitbox_component_update(component_t *component) inline int hitbox_component_update(component_t *component)
{ {
hitbox_component_data_t *component_data = component->data; hitbox_component_data_t *component_data = component->data;
const transform_component_data_t *transform_component_data = component_data->transform_component_data; const transform_component_data_t *transform_component_data = component_data->transform_component_data;
@ -48,7 +48,7 @@ int hitbox_component_update(component_t *component)
#if DEBUG >= 2 #if DEBUG >= 2
#include "display.h" #include "display.h"
int hitbox_component_draw(const component_t *component) inline int hitbox_component_draw(const component_t *component)
{ {
const hitbox_component_data_t *component_data = component->data; const hitbox_component_data_t *component_data = component->data;

View File

@ -0,0 +1,65 @@
#include "interact_component.h"
#include "game.h"
#include "memory_alloc.h"
#include "errors.h"
inline int interact_component_init(component_t *component)
{
interact_component_data_t *component_data = component->data;
component_t *transform_component = entity_get_component(component->entity, TRANSFORM_COMPONENT);
if(!transform_component)
{
error_printf("Hitbox component require a transform component.");
return EXIT_FAILURE;
}
component_data->transform_component_data = transform_component->data;
component_data->position = (fvector2d_t) {
.x = 0.0f,
.y = 0.0f
};
component_data->scale = (fvector2d_t) {
.x = 1.0f,
.y = 1.0f
};
return EXIT_SUCCESS;
}
inline int interact_component_update(component_t *component __attribute__((unused)))
{
interact_component_data_t *component_data = component->data;
const transform_component_data_t *transform_component_data = component_data->transform_component_data;
component_data->bounds.x = transform_component_data->bounds.x + (transform_component_data->bounds.w - component_data->bounds.w) * component_data->position.x;
component_data->bounds.y = transform_component_data->bounds.y + (transform_component_data->bounds.h - component_data->bounds.h) * component_data->position.y;
component_data->bounds.w = transform_component_data->bounds.w * component_data->scale.x;
component_data->bounds.h = transform_component_data->bounds.h * component_data->scale.y;
return EXIT_SUCCESS;
}
#if DEBUG >= 2
#include "display.h"
inline int interact_component_draw(const component_t *component)
{
const interact_component_data_t *component_data = component->data;
if(display_draw_frect(&component_data->bounds, C_GREEN, 128)) return EXIT_FAILURE;
return EXIT_SUCCESS;
}
#endif // DEBUG >= 2
inline int interact_component_destroy(component_t *component)
{
interact_component_data_t *component_data = component->data;
free(component_data);
return EXIT_SUCCESS;
}

View File

@ -6,7 +6,7 @@
#include "memory_alloc.h" #include "memory_alloc.h"
#include "errors.h" #include "errors.h"
int physics_system_init(component_t *component) inline int physics_system_init(component_t *component)
{ {
physics_system_data_t *system_data = component->data; physics_system_data_t *system_data = component->data;

View File

@ -3,7 +3,7 @@
#include "game.h" #include "game.h"
#include "memory_alloc.h" #include "memory_alloc.h"
int transform_component_init(component_t *component) inline int transform_component_init(component_t *component)
{ {
transform_component_data_t *component_data = component->data; transform_component_data_t *component_data = component->data;

View File

@ -89,6 +89,19 @@ int game_init(void)
.y = 1.0f / 3.0f .y = 1.0f / 3.0f
}; };
component = entity_new_component(lever_entity, INTERACT_COMPONENT);
if(!component) return EXIT_FAILURE;
((interact_component_data_t *)component->data)->position = (fvector2d_t) {
.x = 0.5f,
.y = 4.0f / 3.0f
};
((interact_component_data_t *)component->data)->scale = (fvector2d_t) {
.x = 2.0f / 3.0f,
.y = 0.7f
};
/* ======== Door ======== */ /* ======== Door ======== */
entity_t *door_entity = entity_manager_new_entity(&game.entity_manager, 2); entity_t *door_entity = entity_manager_new_entity(&game.entity_manager, 2);
if(!door_entity) return EXIT_FAILURE; if(!door_entity) return EXIT_FAILURE;