Making a proper jump

This commit is contained in:
AnonymusRaccoon
2020-01-03 12:06:50 +01:00
parent 786855bbb4
commit 5c2e400d2d
11 changed files with 161 additions and 5 deletions
+2
View File
@@ -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
+19
View File
@@ -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;
+1
View File
@@ -14,6 +14,7 @@ struct jump_action
{
gc_component base;
int acceleration;
int max_acceleration;
};
extern const struct jump_action jump_action;
+12
View File
@@ -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;
+1 -1
View File
@@ -12,7 +12,7 @@
<gravity_component speed="500" />
<keyboard_controller left="16" right="3" jump="57" />
<walk_action acceleration="3000" max_acceleration="10000" />
<jump_action acceleration="600" />
<jump_action acceleration="2400" max_acceleration="3800"/>
<friction_component value=".7" />
<collision_component />
</gc_entity>
+58
View File
@@ -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 <stdlib.h>
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
};
+2
View File
@@ -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
+4
View File
@@ -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);
+53
View File
@@ -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 <stddef.h>
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
};
+8 -3
View File
@@ -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 <stddef.h>
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;
}