diff --git a/CMakeLists.txt b/CMakeLists.txt index 3c53131..a28c027 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -263,7 +263,7 @@ add_executable(my_rpg lib/gamacon/src/systems/map_linker_system.c src/components/health_component.c include/components/health_component.h -) + src/components/health_methods.c) add_compile_options(-W -Wall -Wextra -Wshadow) diff --git a/include/components/health_component.h b/include/components/health_component.h index 1c850cd..e8b5dbd 100644 --- a/include/components/health_component.h +++ b/include/components/health_component.h @@ -8,14 +8,21 @@ #ifndef MY_RPG_HEALTH_COMPONENT_H #define MY_RPG_HEALTH_COMPONENT_H +#include #include "component.h" struct health_component { gc_component base; - unsigned int health; + int health; + bool dead; }; +void add_health(struct health_component *this, gc_engine *engine, \ +unsigned int amount); +void rem_health(struct health_component *this, gc_engine *engine, \ +unsigned int amount); + extern const struct health_component health_component; #endif //MY_RPG_HEALTH_COMPONENT_H diff --git a/src/components/health_component.c b/src/components/health_component.c index 9e6f7dc..a1999e5 100644 --- a/src/components/health_component.c +++ b/src/components/health_component.c @@ -11,16 +11,16 @@ static void health_ctr(void *component, va_list args) { - struct controllable_component *cmp = (struct controllable_component *)\ + struct health_component *cmp = (struct health_component *)\ component; - - (void)args; + cmp->health = va_arg(args, int); + cmp->dead = false; } static void health_fdctr(gc_entity *entity, gc_scene *scene, \ void *component, node *n) { - struct controllable_component *cmp = (struct controllable_component *)\ + struct health_component *cmp = (struct health_component *)\ component; (void)scene; diff --git a/src/components/health_methods.c b/src/components/health_methods.c new file mode 100644 index 0000000..911d6a7 --- /dev/null +++ b/src/components/health_methods.c @@ -0,0 +1,29 @@ +/* +** EPITECH PROJECT, 2020 +** My3D +** File description: +** health component +*/ + +#include "components/health_component.h" +#include "engine.h" + +void rem_health(struct health_component *this, gc_engine *engine, \ +unsigned int amount) +{ + if (this->dead) + return; + this->health -= amount; + engine->trigger_event(engine, "rem_health", this->health, amount); + if (this->health <= 0) + this->dead = true; +} + +void add_health(struct health_component *this, gc_engine *engine, \ +unsigned int amount) +{ + if (this->dead) + return; + this->health += amount; + engine->trigger_event(engine, "add_health", this->health, amount); +} \ No newline at end of file