diff --git a/src/Makefile b/src/Makefile index 41a50ff..68a5363 100644 --- a/src/Makefile +++ b/src/Makefile @@ -2,9 +2,10 @@ SOURCES := \ game.c \ components/transform_component.c \ + components/sprite_component.c \ components/hitbox_component.c \ components/physics_system.c \ - components/sprite_component.c \ + components/interact_component.c \ components/animation_system.c \ components/player_system.c diff --git a/src/components/headers/components.def b/src/components/headers/components.def index c692dce..10d88d8 100644 --- a/src/components/headers/components.def +++ b/src/components/headers/components.def @@ -5,9 +5,11 @@ #if DEBUG >= 2 COMPONENT(TRANSFORM_COMPONENT, transform_component, DRAW) COMPONENT(HITBOX_COMPONENT, hitbox_component, DRAW) +COMPONENT(INTERACT_COMPONENT, interact_component, DRAW) #else COMPONENT(TRANSFORM_COMPONENT, transform_component, NO_DRAW) COMPONENT(HITBOX_COMPONENT, hitbox_component, NO_DRAW) +COMPONENT(INTERACT_COMPONENT, interct_component, NO_DRAW) #endif // DEBUG >= 2 COMPONENT(PHYSICS_SYSTEM, physics_system, NO_DRAW) diff --git a/src/components/headers/components.h b/src/components/headers/components.h index 0558970..e8f1787 100644 --- a/src/components/headers/components.h +++ b/src/components/headers/components.h @@ -6,6 +6,7 @@ #include "animation_system.h" #include "hitbox_component.h" #include "physics_system.h" +#include "interact_component.h" #include "player_system.h" // Add new components header file here diff --git a/src/components/headers/interact_component.h b/src/components/headers/interact_component.h new file mode 100644 index 0000000..9499dae --- /dev/null +++ b/src/components/headers/interact_component.h @@ -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 \ No newline at end of file diff --git a/src/components/hitbox_component.c b/src/components/hitbox_component.c index 3c20b84..83c0c61 100644 --- a/src/components/hitbox_component.c +++ b/src/components/hitbox_component.c @@ -5,7 +5,7 @@ #include "memory_alloc.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; @@ -32,7 +32,7 @@ int hitbox_component_init(component_t *component) 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; 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 #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; diff --git a/src/components/interact_component.c b/src/components/interact_component.c new file mode 100644 index 0000000..6ccd3c9 --- /dev/null +++ b/src/components/interact_component.c @@ -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; +} diff --git a/src/components/physics_system.c b/src/components/physics_system.c index ced372b..19f30c5 100644 --- a/src/components/physics_system.c +++ b/src/components/physics_system.c @@ -6,7 +6,7 @@ #include "memory_alloc.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; diff --git a/src/components/transform_component.c b/src/components/transform_component.c index 120596f..42cfa90 100644 --- a/src/components/transform_component.c +++ b/src/components/transform_component.c @@ -2,8 +2,9 @@ #include "game.h" #include "memory_alloc.h" +#include "errors.h" -int transform_component_init(component_t *component) +inline int transform_component_init(component_t *component) { transform_component_data_t *component_data = component->data; @@ -24,7 +25,6 @@ inline int transform_component_update(component_t *component __attribute__((unus #if DEBUG >= 2 #include "display.h" -#include "errors.h" inline int transform_component_draw(const component_t *component) { diff --git a/src/game.c b/src/game.c index 252f020..428fab8 100644 --- a/src/game.c +++ b/src/game.c @@ -89,6 +89,19 @@ int game_init(void) .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 ======== */ entity_t *door_entity = entity_manager_new_entity(&game.entity_manager, 2); if(!door_entity) return EXIT_FAILURE;