Moving out the gravity system

This commit is contained in:
AnonymusRaccoon
2020-01-03 12:06:14 +01:00
parent aede02ed98
commit 20ffadd223
8 changed files with 12 additions and 160 deletions
-2
View File
@@ -18,7 +18,6 @@ SRC = src/engine/engine.c \
src/components/renderer.c \
src/components/parallax_component.c \
src/components/controllable_component.c \
src/components/gravity_component.c \
src/components/friction_component.c \
src/components/controllers/keyboard_controller.c \
src/scene/scene.c \
@@ -32,7 +31,6 @@ SRC = src/engine/engine.c \
src/systems/movable_system.c \
src/systems/collision_system.c \
src/systems/parallax_system.c \
src/systems/gravity_system.c \
src/systems/controllers/keyboard_controller_system.c \
src/systems/friction_system.c \
src/engine/engine_system_builder.c \
-19
View File
@@ -1,19 +0,0 @@
/*
** 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;
-12
View File
@@ -1,12 +0,0 @@
/*
** EPITECH PROJECT, 2019
** MUL_my_runner_2019
** File description:
** texture_renderer_system
*/
#pragma once
#include "system.h"
extern const gc_system gravity_system;
-58
View File
@@ -1,58 +0,0 @@
/*
** 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
@@ -12,7 +12,6 @@
#include "components/renderer.h"
#include "components/transform_component.h"
#include "components/controllable_component.h"
#include "components/gravity_component.h"
#include "components/controllers/keyboard_controller.h"
#include "components/friction_component.h"
#include "components/collision_component.h"
@@ -34,7 +33,6 @@ void engine_add_buildin_components(gc_engine *engine)
engine->add_component(engine, &parallax_component);
engine->add_component(engine, &controllable_component);
engine->add_component(engine, &keyboard_controller);
engine->add_component(engine, &gravity_component);
engine->add_component(engine, &friction_component);
engine->add_component(engine, &collision_component);
}
-2
View File
@@ -10,7 +10,6 @@
#include "systems/sfml_renderer_system.h"
#include "systems/movable_system.h"
#include "systems/parallax_system.h"
#include "systems/gravity_system.h"
#include "systems/controllers/keyboard_controller_system.h"
#include "systems/friction_system.h"
#include "systems/collision_system.h"
@@ -36,7 +35,6 @@ void engine_add_buildin_systems(gc_engine *engine)
engine->add_system(engine, &keyboard_controller_system);
engine->add_system(engine, &friction_system);
engine->add_system(engine, new_system(&collision_system, engine->scene));
engine->add_system(engine, &gravity_system);
}
int engine_use_sfml(gc_engine *engine, const char *title, int framerate)
-50
View File
@@ -1,50 +0,0 @@
/*
** 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/renderer.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);
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;
}
(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
};
+12 -15
View File
@@ -17,8 +17,10 @@
#include "systems/movable_system.h"
#include <stddef.h>
void move_entity(gc_entity *entity, struct movable_component *mov, float dtime)
static void update_entity(gc_engine *engine __attribute__((unused)), \
void *system __attribute__((unused)), gc_entity *entity, float dtime)
{
struct movable_component *mov = GETCMP(movable_component);
struct transform_component *pos = GETCMP(transform_component);
struct collision_component *col = GETCMP(collision_component);
@@ -28,32 +30,27 @@ void move_entity(gc_entity *entity, struct movable_component *mov, float dtime)
pos->position.x += MIN(mov->velocity.x * dtime, col->distance_right);
if (mov->velocity.y < 0)
pos->position.y -= MIN(mov->velocity.y * -dtime, col->distance_down);
else
else {
// if (mov->velocity.y != 0)
// printf("Velocity y: %4.0f, Moving up of %4.0f\n", mov->velocity.y, mov->velocity.y * dtime);
pos->position.y += MIN(mov->velocity.y * dtime, col->distance_top);
}
if (col->distance_left == 0 || col->distance_right == 0)
mov->velocity.x = 0;
if (col->distance_down == 0 || col->distance_top == 0)
mov->velocity.y = 0;
}
void movable_update_entity(gc_engine *engine __attribute__((unused)), \
void *system __attribute__((unused)), gc_entity *entity, float dtime)
{
struct movable_component *mov = GETCMP(movable_component);
move_entity(entity, mov, dtime);
mov->velocity.x += mov->acceleration.x * dtime;
mov->velocity.y += mov->acceleration.y * dtime;
mov->acceleration.x = 0;
}
void movable_ctr(void *system, va_list args)
static void ctr(void *system, va_list args)
{
(void)system;
(void)args;
}
void movable_dtr(void *system)
static void dtr(void *system)
{
(void)system;
}
@@ -62,9 +59,9 @@ const gc_system movable_system = {
name: "MovableSystem",
component_name: "movable_component",
size: sizeof(gc_system),
ctr: &movable_ctr,
dtr: &movable_dtr,
ctr: &ctr,
dtr: &dtr,
check_dependencies: &system_check_dependencies,
update_entity: &movable_update_entity,
update_entity: &update_entity,
destroy: &system_destroy
};