diff --git a/Makefile b/Makefile index a968969..531bcaa 100644 --- a/Makefile +++ b/Makefile @@ -7,8 +7,10 @@ SRC = main.c \ src/game_loader.c \ + src/components/gravity_component.c \ src/components/walk_component.c \ src/components/jump_component.c \ + src/systems/gravity_system.c \ src/systems/walk_system.c \ src/systems/jump_system.c diff --git a/include/components/gravity_component.h b/include/components/gravity_component.h new file mode 100644 index 0000000..932126a --- /dev/null +++ b/include/components/gravity_component.h @@ -0,0 +1,19 @@ +/* +** EPITECH PROJECT, 2019 +** Gamacon +** File description: +** gravity_component +*/ + +#pragma once + +#include "component.h" + +struct gravity_component +{ + gc_component base; + int gravity_speed; + int max_speed; +}; + +extern const struct gravity_component gravity_component; \ No newline at end of file diff --git a/include/components/jump_action.h b/include/components/jump_action.h index 30486b3..603709b 100644 --- a/include/components/jump_action.h +++ b/include/components/jump_action.h @@ -14,6 +14,7 @@ struct jump_action { gc_component base; int acceleration; + int max_acceleration; }; extern const struct jump_action jump_action; \ No newline at end of file diff --git a/include/systems/gravity_system.h b/include/systems/gravity_system.h new file mode 100644 index 0000000..78ef038 --- /dev/null +++ b/include/systems/gravity_system.h @@ -0,0 +1,12 @@ +/* +** EPITECH PROJECT, 2019 +** MUL_my_runner_2019 +** File description: +** texture_renderer_system +*/ + +#pragma once + +#include "system.h" + +extern const gc_system gravity_system; \ No newline at end of file diff --git a/lib/gamacon b/lib/gamacon index aede02e..20ffadd 160000 --- a/lib/gamacon +++ b/lib/gamacon @@ -1 +1 @@ -Subproject commit aede02ed980e49abcadebc2685816d2675fbaf46 +Subproject commit 20ffadd22339798433c90f18d63e531b21f867fd diff --git a/prefabs/player.gcprefab b/prefabs/player.gcprefab index 733992c..f657da2 100644 --- a/prefabs/player.gcprefab +++ b/prefabs/player.gcprefab @@ -12,7 +12,7 @@ - + diff --git a/src/components/gravity_component.c b/src/components/gravity_component.c new file mode 100644 index 0000000..7e298e1 --- /dev/null +++ b/src/components/gravity_component.c @@ -0,0 +1,58 @@ +/* +** EPITECH PROJECT, 2019 +** MUL_my_runner_2019 +** File description: +** gravity_component +*/ + +#include "xml.h" +#include "component.h" +#include "components/gravity_component.h" +#include "utility.h" +#include + +static void gravity_ctr(void *component, va_list args) +{ + struct gravity_component *cmp = (struct gravity_component *)component; + + cmp->gravity_speed = va_arg(args, int); + cmp->max_speed = va_arg(args, int); +} + +static void gravity_fdctr(gc_scene *scene, void *component, node *n) +{ + struct gravity_component *cmp = (struct gravity_component *)component; + + cmp->gravity_speed = xml_getintprop(n, "speed"); + cmp->max_speed = xml_getintprop(n, "max_speed"); + (void)scene; +} + +static void gravity_dtr(void *component) +{ + (void)component; +} + +static char *gravity_serialize(void *component) +{ + (void)component; + return (NULL); +} + +const struct gravity_component gravity_component = { + base: { + name: "gravity_component", + size: sizeof(struct gravity_component), + dependencies: (char *[]){ + "movable_component", + "transform_component", + NULL + }, + ctr: &gravity_ctr, + fdctr: &gravity_fdctr, + dtr: &gravity_dtr, + serialize: &gravity_serialize, + destroy: &component_destroy + }, + gravity_speed: 10 +}; \ No newline at end of file diff --git a/src/components/jump_component.c b/src/components/jump_component.c index 8975423..1f77e23 100644 --- a/src/components/jump_component.c +++ b/src/components/jump_component.c @@ -24,6 +24,7 @@ static void fdctr(gc_scene *scene, void *component, node *n) struct jump_action *cmp = (struct jump_action *)component; cmp->acceleration = xml_getintprop(n, "acceleration"); + cmp->max_acceleration = xml_getintprop(n, "max_acceleration"); (void)scene; } @@ -44,6 +45,7 @@ const struct jump_action jump_action = { size: sizeof(struct jump_action), dependencies: (char *[]){ "controllable_component", + "collision_component", "movable_component", "transform_component", NULL diff --git a/src/game_loader.c b/src/game_loader.c index 2e113eb..7c1bfd6 100644 --- a/src/game_loader.c +++ b/src/game_loader.c @@ -8,15 +8,19 @@ #include "engine.h" #include "runner.h" #include "prefab.h" +#include "components/gravity_component.h" #include "components/walk_action.h" #include "components/jump_action.h" +#include "systems/gravity_system.h" #include "systems/walk_system.h" #include "systems/jump_system.h" int register_customcmps(gc_engine *engine) { + engine->add_component(engine, &gravity_component); engine->add_component(engine, &walk_action); engine->add_component(engine, &jump_action); + engine->add_system(engine, &gravity_system); engine->add_system(engine, &walk_system); engine->add_system(engine, &jump_system); engine->finish_physics(engine); diff --git a/src/systems/gravity_system.c b/src/systems/gravity_system.c new file mode 100644 index 0000000..35cb558 --- /dev/null +++ b/src/systems/gravity_system.c @@ -0,0 +1,53 @@ +/* +** EPITECH PROJECT, 2019 +** MUL_my_runner_2019 +** File description: +** gravity_system +*/ + + +#include "entity.h" +#include "system.h" +#include "texture.h" +#include "vector2.h" +#include "utility.h" +#include "components/movable_component.h" +#include "components/gravity_component.h" +#include "components/collision_component.h" +#include + +void gravity_update_entity(gc_engine *engine, void *system, \ +gc_entity *entity, float dtime) +{ + struct gravity_component *grav = GETCMP(gravity_component); + struct movable_component *mov = GETCMP(movable_component); + struct collision_component *col = GETCMP(collision_component); + + if (mov->acceleration.y > -grav->gravity_speed) { + if (mov->acceleration.y > 0) + mov->acceleration.y -= grav->gravity_speed; + else + mov->acceleration.y = -grav->gravity_speed; + } + if (mov->acceleration.y < 0 && col->distance_down == 0) + mov->acceleration.y = 0; + (void)system; + (void)dtime; + (void)engine; +} + +void gravity_destroy(void *system) +{ + (void)system; +} + +const gc_system gravity_system = { + name: "GravitySystem", + component_name: "gravity_component", + size: sizeof(gc_system), + ctr: NULL, + dtr: NULL, + check_dependencies: &system_check_dependencies, + update_entity: &gravity_update_entity, + destroy: &gravity_destroy +}; \ No newline at end of file diff --git a/src/systems/jump_system.c b/src/systems/jump_system.c index c202550..2cfe383 100644 --- a/src/systems/jump_system.c +++ b/src/systems/jump_system.c @@ -10,26 +10,31 @@ #include "texture.h" #include "vector2.h" #include "component.h" +#include "components/collision_component.h" #include "components/movable_component.h" #include "components/controllable_component.h" #include "components/jump_action.h" #include "utility.h" #include -void update_entity(gc_engine *engine, void *system, \ +static void update_entity(gc_engine *engine, void *system, gc_entity *entity, float dtime) { + struct collision_component *col = GETCMP(collision_component); struct controllable_component *con = GETCMP(controllable_component); struct movable_component *mov = GETCMP(movable_component); struct jump_action *jump = GETCMP(jump_action); - mov->acceleration.y += con->jumping * jump->acceleration; + if (con->jumping) + printf("Acceleartion: %4.0f, Velocity: %4.0f Distance down: %d\n", mov->acceleration.y, mov->velocity.y, col->distance_down); + if (col->distance_down == 0 && mov->acceleration.y < jump->max_acceleration) + mov->acceleration.y += con->jumping * jump->acceleration; (void)system; (void)dtime; (void)engine; } -void destroy(void *system) +static void destroy(void *system) { (void)system; }