diff --git a/include/components/game_display.h b/include/components/game_display.h index 1953764..813f295 100644 --- a/include/components/game_display.h +++ b/include/components/game_display.h @@ -10,7 +10,9 @@ typedef enum display_type { SELECT_TILE_DISPLAY, - XP_DISPLAY + XP_DISPLAY, + HEALTH_DISPLAY, + HEALTH_DISPLAY_ENNEMY } display_type_enum; struct game_display diff --git a/include/components/health_component.h b/include/components/health_component.h index e8b5dbd..632c1e7 100644 --- a/include/components/health_component.h +++ b/include/components/health_component.h @@ -14,6 +14,7 @@ struct health_component { gc_component base; + int health_max; int health; bool dead; }; diff --git a/include/components/xp_component.h b/include/components/xp_component.h index 9398d31..80b46a8 100644 --- a/include/components/xp_component.h +++ b/include/components/xp_component.h @@ -5,8 +5,8 @@ ** xp_component */ -#ifndef MY_RPG_HEALTH_COMPONENT_H -#define MY_RPG_HEALTH_COMPONENT_H +#ifndef MY_RPG_XP_COMPONENT_H +#define MY_RPG_XP_COMPONENT_H #include #include "component.h" @@ -25,4 +25,4 @@ unsigned int amount); extern const struct xp_component xp_component; -#endif //MY_RPG_HEALTH_COMPONENT_H +#endif //MY_RPG_XP_COMPONENT_H diff --git a/prefabs/game.gcprefab b/prefabs/game.gcprefab index e55d2bf..b70fbe4 100644 --- a/prefabs/game.gcprefab +++ b/prefabs/game.gcprefab @@ -168,8 +168,10 @@ - - + + + + diff --git a/prefabs/player.gcprefab b/prefabs/player.gcprefab index 00e7262..d0a5361 100644 --- a/prefabs/player.gcprefab +++ b/prefabs/player.gcprefab @@ -37,5 +37,6 @@ + diff --git a/src/components/game_display.c b/src/components/game_display.c index bd631f5..d155e82 100644 --- a/src/components/game_display.c +++ b/src/components/game_display.c @@ -32,11 +32,15 @@ static void fdctr(gc_entity *entity, gc_scene *scene, void *component, node *n) } if (!my_strcmp(display_type, "selected_tile")) { cmp->type = SELECT_TILE_DISPLAY; - return; } if (!my_strcmp(display_type, "xp")) { cmp->type = XP_DISPLAY; - return; + } + if (!my_strcmp(display_type, "health")) { + cmp->type = HEALTH_DISPLAY; + } + if (!my_strcmp(display_type, "health_ennemy")) { + cmp->type = HEALTH_DISPLAY_ENNEMY; } } diff --git a/src/components/health_component.c b/src/components/health_component.c index 2c0c5a2..d2647af 100644 --- a/src/components/health_component.c +++ b/src/components/health_component.c @@ -13,7 +13,8 @@ static void ctr(void *component, va_list args) { struct health_component *cmp = (struct health_component *)\ component; - cmp->health = va_arg(args, int); + cmp->health_max = va_arg(args, int); + cmp->health = cmp->health_max; cmp->dead = false; } @@ -23,7 +24,10 @@ void *component, node *n) struct health_component *cmp = (struct health_component *)\ component; - cmp->health = xml_getintprop(n, "health"); + cmp->health_max = xml_getintprop(n, "max_health"); + if (cmp->health_max <= 0) + cmp->health_max = 42; + cmp->health = cmp->health_max; cmp->dead = false; } diff --git a/src/components/health_methods.c b/src/components/health_methods.c index 911d6a7..de3a9cd 100644 --- a/src/components/health_methods.c +++ b/src/components/health_methods.c @@ -25,5 +25,7 @@ unsigned int amount) if (this->dead) return; this->health += amount; + if (this->health > this->health_max) + this->health = this->health_max; engine->trigger_event(engine, "add_health", this->health, amount); } \ No newline at end of file diff --git a/src/game_loader.c b/src/game_loader.c index 7325e2b..d80f199 100644 --- a/src/game_loader.c +++ b/src/game_loader.c @@ -21,6 +21,7 @@ #include "my.h" #include "map_editor.h" #include "components/xp_component.h" +#include "components/health_component.h" const struct callback callbacks[] = { {"start_button", &start_button}, @@ -66,6 +67,7 @@ int register_customcmps(gc_engine *engine, bool map_editor) engine->add_component(engine, &game_display); engine->add_system(engine, &game_display_system); engine->add_component(engine, &xp_component); + engine->add_component(engine, &health_component); engine->finish_physics(engine); for (int i = 0; callbacks[i].func; i++) engine->add_callback(engine, my_strdup(callbacks[i].name), \ diff --git a/src/systems/game_display_system.c b/src/systems/game_display_system.c index d72c237..4f11b6b 100644 --- a/src/systems/game_display_system.c +++ b/src/systems/game_display_system.c @@ -15,6 +15,7 @@ #include "text.h" #include "components/renderer.h" #include +#include "components/health_component.h" #include "sprite.h" void display_current_texture(gc_scene *scene, struct renderer *rend) @@ -28,6 +29,25 @@ void display_current_texture(gc_scene *scene, struct renderer *rend) ((gc_sprite *)rend->data)->texture = map->selected_texture; } +void display_current_health(gc_scene *scene, struct renderer *rend, bool is_player) +{ + gc_list *entities = scene->get_entity_by_cmp(scene, "health_component"); + gc_entity *entity = NULL; + struct health_component *health_cmp = NULL; + static char str[10]; + + if (!entities) + return; + for (; entities->next || !entity; entities = entities->next ) { + entity = entities->data; + if ((entity->id == 50 && is_player) || (entity->id != 50 && !is_player)) + break; + } + health_cmp = GETCMP(entity, health_component); + snprintf(str, 10, "%d/%d", health_cmp->health, health_cmp->health_max); + ((gc_text *)rend->data)->text = str; +} + void display_current_xp(gc_scene *scene, struct renderer *rend) { gc_entity *player = scene->get_entity(scene, 50); @@ -50,7 +70,7 @@ float dtime) struct renderer *rend = GETCMP(entity, renderer); gc_scene *scene = engine->scene; - if (disp->type == SELECT_TILE_DISPLAY && rend->type == GC_TEXTUREREND){ + if (disp->type == SELECT_TILE_DISPLAY && rend->type == GC_TEXTUREREND) { display_current_texture(scene, rend); return; } @@ -58,6 +78,14 @@ float dtime) display_current_xp(scene, rend); return; } + if (disp->type == HEALTH_DISPLAY && rend->type == GC_TXTREND) { + display_current_health(scene, rend, true); + return; + } + if (disp->type == HEALTH_DISPLAY_ENNEMY && rend->type == GC_TXTREND) { + display_current_health(scene, rend, false); + return; + } } static void destroy(void *system, gc_engine *engine)