diff --git a/CMakeLists.txt b/CMakeLists.txt index 5ba0600..f34ecdc 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -280,6 +280,8 @@ add_executable(my_rpg src/systems/game_display_system.c include/map_editor.h lib/gamacon/include/sfml_init.h + src/components/xp_component.c + src/components/xp_methods.c ) add_compile_options(-W -Wall -Wextra -Wshadow) diff --git a/Makefile b/Makefile index 4e01a6b..b5b652e 100644 --- a/Makefile +++ b/Makefile @@ -26,7 +26,9 @@ SRC = src/main.c \ src/map_editor/selectors.c \ src/map_editor/toolbar.c \ src/components/health_component.c \ - src/components/health_methods.c + src/components/health_methods.c \ + src/components/xp_component.c \ + src/components/xp_methods.c OBJ = $(SRC:%.c=%.o) diff --git a/include/components/game_display.h b/include/components/game_display.h index 90609ac..cab3569 100644 --- a/include/components/game_display.h +++ b/include/components/game_display.h @@ -9,7 +9,8 @@ typedef enum display_type { - SELECT_TILE_DISPLAY + SELECT_TILE_DISPLAY, + xp_display } display_type_enum; struct game_display diff --git a/include/components/xp_component.h b/include/components/xp_component.h new file mode 100644 index 0000000..9398d31 --- /dev/null +++ b/include/components/xp_component.h @@ -0,0 +1,28 @@ +/* +** EPITECH PROJECT, 2019 +** Gamacon +** File description: +** xp_component +*/ + +#ifndef MY_RPG_HEALTH_COMPONENT_H +#define MY_RPG_HEALTH_COMPONENT_H + +#include +#include "component.h" + +struct xp_component +{ + gc_component base; + int xp; + bool full; +}; + +void xp_add(struct xp_component *this, gc_engine *engine, \ +unsigned int amount); +void xp_rem(struct xp_component *this, gc_engine *engine, \ +unsigned int amount); + +extern const struct xp_component xp_component; + +#endif //MY_RPG_HEALTH_COMPONENT_H diff --git a/prefabs/game.gcprefab b/prefabs/game.gcprefab index 544e410..c12a01f 100644 --- a/prefabs/game.gcprefab +++ b/prefabs/game.gcprefab @@ -151,7 +151,9 @@ - - + + + + \ No newline at end of file diff --git a/prefabs/player.gcprefab b/prefabs/player.gcprefab index dbd417b..00e7262 100644 --- a/prefabs/player.gcprefab +++ b/prefabs/player.gcprefab @@ -18,24 +18,24 @@ - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file + + + + + + + + + + + + + + + + + + + + + diff --git a/src/components/game_display.c b/src/components/game_display.c index f2161e6..ffc3942 100644 --- a/src/components/game_display.c +++ b/src/components/game_display.c @@ -26,10 +26,18 @@ static void fdctr(gc_entity *entity, gc_scene *scene, void *component, node *n) struct renderer *rend = GETCMP(entity, renderer); char *display_type = xml_gettempprop(n, "stats"); + if (!display_type) { + my_printf("Please select properties\n"); + return; + } if (!my_strcmp(display_type, "selected_tile")) { cmp->type = SELECT_TILE_DISPLAY; return; } + if (!my_strcmp(display_type, "xp")) { + cmp->type = xp_display; + return; + } } static void dtr(void *component) diff --git a/src/components/xp_component.c b/src/components/xp_component.c new file mode 100644 index 0000000..a5b5687 --- /dev/null +++ b/src/components/xp_component.c @@ -0,0 +1,54 @@ +/* +** EPITECH PROJECT, 2020 +** My3D +** File description: +** xp component +*/ + +#include "xml.h" +#include "component.h" +#include "components/xp_component.h" + +static void ctr(void *component, va_list args) +{ + struct xp_component *cmp = (struct xp_component *)\ +component; + cmp->xp = va_arg(args, int); + cmp->full = false; +} + +static void fdctr(gc_entity *entity, gc_scene *scene, \ +void *component, node *n) +{ + struct xp_component *cmp = (struct xp_component *)\ +component; + + cmp->xp = xml_getintprop(n, "xp"); + cmp->full = false; +} + +static void dtr(void *component) +{ + (void)component; +} + +static char *serialize(void *component) +{ + (void)component; + return (NULL); +} + +const struct xp_component xp_component = { + base: { + name: "xp_component", + size: sizeof(struct xp_component), + dependencies: (char *[]){ + NULL + }, + ctr: &ctr, + fdctr: &fdctr, + dtr: &dtr, + serialize: &serialize, + destroy: &component_destroy + } +}; \ No newline at end of file diff --git a/src/components/xp_methods.c b/src/components/xp_methods.c new file mode 100644 index 0000000..b38ebf7 --- /dev/null +++ b/src/components/xp_methods.c @@ -0,0 +1,29 @@ +/* +** EPITECH PROJECT, 2020 +** My3D +** File description: +** xp methods +*/ + +#include "components/xp_component.h" +#include "engine.h" + +void xp_add(struct xp_component *this, gc_engine *engine, \ +unsigned int amount) +{ + if (this->full) + return; + this->xp += amount; + if (this->xp >= 100) { + this->full = true; + this->xp = 100; + } + engine->trigger_event(engine, "xp_add", this->xp, amount); +} + +void xp_rem(struct xp_component *this, gc_engine *engine, \ +unsigned int amount) +{ + this->xp -= amount; + engine->trigger_event(engine, "xp_rem", this->xp, amount); +} \ No newline at end of file diff --git a/src/game_loader.c b/src/game_loader.c index 7b72dee..7325e2b 100644 --- a/src/game_loader.c +++ b/src/game_loader.c @@ -20,6 +20,7 @@ #include "components/game_display.h" #include "my.h" #include "map_editor.h" +#include "components/xp_component.h" const struct callback callbacks[] = { {"start_button", &start_button}, @@ -64,6 +65,7 @@ int register_customcmps(gc_engine *engine, bool map_editor) engine->add_component(engine, &map_manager_component); engine->add_component(engine, &game_display); engine->add_system(engine, &game_display_system); + engine->add_component(engine, &xp_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 e9c72ba..f8c42cd 100644 --- a/src/systems/game_display_system.c +++ b/src/systems/game_display_system.c @@ -10,6 +10,7 @@ #include #include "components/game_display.h" #include "components/game_manager.h" +#include "components/xp_component.h" #include "map_editor.h" #include "text.h" #include "components/renderer.h" @@ -27,6 +28,19 @@ void display_current_texture(gc_scene *scene, struct renderer *rend) ((gc_sprite *)rend->data)->texture = map->selected_texture; } +void display_current_xp(gc_scene *scene, struct renderer *rend) +{ + gc_entity *player = scene->get_entity(scene, 50); + struct xp_component *xp; + static char str[10]; + + if (!player) + return; + xp = GETCMP(player, xp_component); + snprintf(str, 10, "%d/100", xp->xp); + ((gc_text *)rend->data)->text = str; +} + static void update_entity(gc_engine *engine, void *system, gc_entity *entity, \ float dtime) { @@ -38,6 +52,10 @@ float dtime) display_current_texture(scene, rend); return; } + if (disp->type == xp_display && rend->type == GC_TXTREND) { + display_current_xp(scene, rend); + return; + } } static void destroy(void *system, gc_engine *engine)