diff --git a/CMakeLists.txt b/CMakeLists.txt index 955f4ab..3628410 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -298,7 +298,7 @@ add_executable(my_rpg include/components/attack_component.h src/systems/combat_methods.c src/combat/attacks.c - src/player_utilities.c include/player_utilities.h src/systems/inventory.c include/systems/inventory.h src/systems/game_over.c include/systems/game_over.h src/npc/mia.c lib/gamacon/src/scene/scene_constructor.c src/npc/fisherman.c src/npc/lumberjack.c src/npc/smith.c) + src/player_utilities.c include/player_utilities.h src/systems/inventory.c include/systems/inventory.h src/systems/game_over.c include/systems/game_over.h src/npc/mia.c lib/gamacon/src/scene/scene_constructor.c src/npc/fisherman.c src/npc/lumberjack.c src/npc/smith.c src/components/particule_component.c include/components/particule_component.h src/systems/particule_system.c include/systems/particule_system.h) add_compile_options(-W -Wall -Wextra -Wshadow) diff --git a/include/components/particule_component.h b/include/components/particule_component.h new file mode 100644 index 0000000..ce5c646 --- /dev/null +++ b/include/components/particule_component.h @@ -0,0 +1,35 @@ +/* +** EPITECH PROJECT, 2020 +** my_rpg +** File description: +** particule_component.c +*/ + +#ifndef MY_RPG_PARTICULE_COMPONENT_H +#define MY_RPG_PARTICULE_COMPONENT_H + +#include +#include "sprite.h" +#include "component.h" + +struct particule +{ + unsigned int lifetime; + gc_sprite *sprite; +}; + +struct particule_component +{ + gc_component base; + int type; + unsigned int lifetime; + int nb_max_particules; + void *texture; + struct particule *particules; +}; + + + +extern const struct particule_component particule_component; + +#endif //MY_RPG_PARTICULE_COMPONENT_H diff --git a/include/systems/game_manager_system.h b/include/systems/game_manager_system.h index 0197068..7dc1eb9 100644 --- a/include/systems/game_manager_system.h +++ b/include/systems/game_manager_system.h @@ -16,6 +16,6 @@ struct game_manager_system { bool has_message; }; -const struct game_manager_system game_manager_system; +extern const struct game_manager_system game_manager_system; #endif //_MAP_MOVEMENT_SYSTEM_H__ diff --git a/include/systems/particule_system.h b/include/systems/particule_system.h new file mode 100644 index 0000000..9be9768 --- /dev/null +++ b/include/systems/particule_system.h @@ -0,0 +1,18 @@ +/* +** EPITECH PROJECT, 2019 +** MUL_my_runner_2019 +** File description: +** particule_system +*/ + +#ifndef MY_RPG_PARTICULE_SYSTEM_H +#define MY_RPG_PARTICULE_SYSTEM_H + +#include "engine.h" + +void particule_update_entity(gc_engine *engine, void *system, \ +gc_entity *entity, float dt); + +extern const struct gc_system particule_system; + +#endif //MY_RPG_PARTICULE_SYSTEM_H diff --git a/prefabs/player.gcprefab b/prefabs/player.gcprefab index 2519cde..4ca9592 100644 --- a/prefabs/player.gcprefab +++ b/prefabs/player.gcprefab @@ -39,5 +39,6 @@ + diff --git a/src/components/particule_component.c b/src/components/particule_component.c new file mode 100644 index 0000000..dd14d8d --- /dev/null +++ b/src/components/particule_component.c @@ -0,0 +1,95 @@ +/* +** EPITECH PROJECT, 2020 +** my_rpg +** File description: +** particule_component.c +*/ + +#include "xml.h" +#include "components/particule_component.h" +#include "components/map_linker.h" +#include "component.h" +#include +#include "utility.h" +#include "tile.h" + +static void ctr(void *component, va_list args) +{ + struct particule_component *cmp = (struct particule_component *)\ +component; + gc_sprite *sprites; + + cmp->type = va_arg(args, int); + cmp->nb_max_particules = va_arg(args, int); + cmp->lifetime = va_arg(args, int); + cmp->texture = va_arg(args, void *); + cmp->particules = malloc(sizeof(struct particule_component) * \ +(cmp->nb_max_particules + 1)); + sprites = malloc(sizeof(gc_sprite) * (cmp->nb_max_particules)); + if (!cmp->particules || !sprites) + return; + for (int i = 0; i < cmp->nb_max_particules; i++) { + cmp->particules[i].sprite = &sprites[i]; + cmp->particules[i].lifetime = 0; + } +} + +static void fdctr(gc_entity *entity, gc_scene *scene, \ +void *component, node *n) +{ + struct particule_component *cmp = (struct particule_component *)\ +component; + struct map_linker *ml = GETCMP(entity, map_linker); + gc_sprite *sprites; + + if (!ml) { + my_printf("No linker\n"); + return; + } + cmp->type = xml_getintprop(n, "type"); + cmp->nb_max_particules = xml_getintprop(n, "nb_particules_max"); + cmp->texture = ml->tile->texture; + cmp->lifetime = xml_getintprop(n, "lifetime"); + cmp->particules = malloc(sizeof(struct particule_component) * \ +(cmp->nb_max_particules + 1)); + sprites = malloc(sizeof(gc_sprite) * (cmp->nb_max_particules)); + if (!cmp->particules || !sprites) + return; + for (int i = 0; i < cmp->nb_max_particules; i++) { + cmp->particules[i].sprite = &sprites[i]; + cmp->particules[i].lifetime = 0; + } +} + +static void dtr(void *component) +{ + struct particule_component *cmp = (struct particule_component *)\ +component; + if (!cmp->particules) + return; + free(cmp->particules[0].sprite); + free(cmp->particules); +} + +static char *serialize(void *component) +{ + (void)component; + return (NULL); +} + +const struct particule_component particule_component = { + base: { + name: "particule_component", + size: sizeof(struct particule_component), + dependencies: (char *[]){ + "transform_component", + "map_linker", + NULL + }, + ctr: &ctr, + fdctr: &fdctr, + dtr: &dtr, + serialize: &serialize, + destroy: &component_destroy + } +}; \ No newline at end of file diff --git a/src/game_loader.c b/src/game_loader.c index 13217b6..d4af3bd 100644 --- a/src/game_loader.c +++ b/src/game_loader.c @@ -27,6 +27,8 @@ #include "map_editor.h" #include "components/xp_component.h" #include "systems/game_over.h" +#include "systems/particule_system.h" +#include "components/particule_component.h" #include #include #include @@ -144,6 +146,8 @@ int register_customcmps(gc_engine *engine, bool map_editor) engine->add_component(engine, &combat_holder); engine->add_system(engine, new_system(&combat_manager, engine)); engine->add_component(engine, &health_component); + engine->add_system(engine, &particule_system); + engine->add_component(engine, &particule_component); engine->finish_physics(engine); engine->add_dataloader(engine, "enemies", &enemies_dataloader); for (int i = 0; callbacks[i].func; i++) diff --git a/src/systems/particule_system.c b/src/systems/particule_system.c new file mode 100644 index 0000000..bedfc02 --- /dev/null +++ b/src/systems/particule_system.c @@ -0,0 +1,74 @@ +/* +** EPITECH PROJECT, 2019 +** MUL_my_runner_2019 +** File description: +** particule_system +*/ + +#include "engine.h" +#include "renderer.h" +#include "sprite.h" +#include "systems/sfml_renderer_system.h" +#include "sfml_renderer.h" +#include "components/particule_component.h" +#include "system.h" + +void create_particule(struct particule *particule, int lifetime, \ +void *texture, gc_vector2 pos) +{ + if (!particule || !particule->sprite) + return; + particule->lifetime = lifetime; + particule->sprite->texture = texture; + particule->sprite->pos = pos; + particule->sprite->rect = (gc_int_rect){10, 10,0,0}; + particule->sprite->scale = (gc_vector2){0.5, 0.5}; +} + +void particule_draw(gc_engine *engine, void *system, \ +gc_entity *entity, float dt) +{ + struct particule_component *pm = GETCMP(entity, particule_component); + + if (!pm || !entity) + return; + for (int i = 0; i < pm->nb_max_particules; i++) { + if (!pm->particules[i].sprite) + continue; + sfmlrenderer_draw_texture(engine, entity, pm->particules[i].sprite, dt); + } +} + +void particule_update_entity(gc_engine *engine, void *system, gc_entity *entity, \ +float dtime) +{ + struct map_linker *ml = GETCMP(entity, map_linker); + struct particule_component *cmp = GETCMP(entity, particule_component); + struct transform_component *tc = GETCMP(entity, transform_component); + + if (!ml || !tc || !cmp) + return; + for (int i = 0; i < cmp->nb_max_particules; i++) + cmp->particules[i].lifetime -= (cmp->particules[i].lifetime) ? 1 : 0; + for (int i = 0; i < cmp->nb_max_particules; i++) { + if (!cmp->particules[i].lifetime) + create_particule(&cmp->particules[i], cmp->lifetime, cmp->texture, tc->position); + } + particule_draw(engine, system, entity, dtime); +} + +void particule_destroy(void *system, gc_engine *engine) +{ + (void)system; +} + +const struct gc_system particule_system = { + name: "particule_system", + component_name: "particule_component", + size: sizeof(gc_system), + ctr: NULL, + dtr: NULL, + check_dependencies: &system_check_dependencies, + update_entity: &particule_update_entity, + destroy: &particule_destroy +}; \ No newline at end of file