mirror of
https://github.com/zoriya/Gamacon.git
synced 2026-05-31 02:31:19 +00:00
Reworking a bit how movement is made
This commit is contained in:
@@ -17,6 +17,7 @@ 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/scene/scene.c \
|
||||
src/utility/arraylen.c \
|
||||
src/utility/list.c \
|
||||
|
||||
@@ -15,6 +15,7 @@ struct controllable_component
|
||||
int left_key;
|
||||
int right_key;
|
||||
int jump_key;
|
||||
int speed;
|
||||
};
|
||||
|
||||
extern const struct controllable_component controllable_component;
|
||||
@@ -0,0 +1,18 @@
|
||||
/*
|
||||
** EPITECH PROJECT, 2019
|
||||
** Gamacon
|
||||
** File description:
|
||||
** gravity_component
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "component.h"
|
||||
|
||||
struct gravity_component
|
||||
{
|
||||
gc_component base;
|
||||
int gravity_speed;
|
||||
};
|
||||
|
||||
extern const struct gravity_component gravity_component;
|
||||
@@ -13,9 +13,8 @@
|
||||
struct movable_component
|
||||
{
|
||||
gc_component base;
|
||||
bool moving_left;
|
||||
bool moving_right;
|
||||
int speed;
|
||||
int speed_x;
|
||||
int speed_y;
|
||||
};
|
||||
|
||||
extern const struct movable_component movable_component;
|
||||
+2
-1
@@ -47,4 +47,5 @@ int qt_add(quadtree *tree, qt_object obj);
|
||||
qt_collision collision_get_info(quadtree *tree, int entity_id);
|
||||
bool qt_collide(qt_intrect r1, qt_intrect r2);
|
||||
qt_object *qt_getobj(quadtree *tree, int id);
|
||||
int qt_update(quadtree *tree, qt_object obj);
|
||||
int qt_update(quadtree *tree, qt_object obj);
|
||||
void qt_destroy(quadtree *tree);
|
||||
@@ -19,6 +19,7 @@ component;
|
||||
cmp->left_key = va_arg(args, int);
|
||||
cmp->right_key = va_arg(args, int);
|
||||
cmp->jump_key = va_arg(args, int);
|
||||
cmp->speed = va_arg(args, int);
|
||||
}
|
||||
|
||||
static void controllable_fdctr(gc_engine *engine, void *component, node *n)
|
||||
@@ -29,6 +30,7 @@ component;
|
||||
cmp->left_key = xml_getintprop(n, "left");
|
||||
cmp->right_key = xml_getintprop(n, "right");
|
||||
cmp->jump_key = xml_getintprop(n, "jump");
|
||||
cmp->speed = xml_getintprop(n, "speed");
|
||||
(void)engine;
|
||||
}
|
||||
|
||||
@@ -61,4 +63,5 @@ const struct controllable_component controllable_component = {
|
||||
left_key: 16,
|
||||
right_key: 3,
|
||||
jump_key: ' ',
|
||||
speed: 700
|
||||
};
|
||||
@@ -0,0 +1,56 @@
|
||||
/*
|
||||
** 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);
|
||||
}
|
||||
|
||||
static void gravity_fdctr(gc_engine *engine, void *component, node *n)
|
||||
{
|
||||
struct gravity_component *cmp = (struct gravity_component *)component;
|
||||
|
||||
cmp->gravity_speed = xml_getintprop(n, "gravity_speed");
|
||||
(void)engine;
|
||||
}
|
||||
|
||||
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: "GravityComponent",
|
||||
size: sizeof(struct gravity_component),
|
||||
dependencies: (char *[]){
|
||||
"MovableComponent",
|
||||
"TransformComponent",
|
||||
NULL
|
||||
},
|
||||
ctr: &gravity_ctr,
|
||||
fdctr: &gravity_fdctr,
|
||||
dtr: &gravity_dtr,
|
||||
serialize: &gravity_serialize,
|
||||
destroy: &component_destroy
|
||||
},
|
||||
gravity_speed: 10
|
||||
};
|
||||
@@ -15,16 +15,16 @@ static void movable_ctr(void *component, va_list args)
|
||||
{
|
||||
struct movable_component *cmp = (struct movable_component *)component;
|
||||
|
||||
cmp->speed = va_arg(args, int);
|
||||
cmp->speed_x = va_arg(args, int);
|
||||
cmp->speed_y = va_arg(args, int);
|
||||
}
|
||||
|
||||
static void movable_fdctr(gc_engine *engine, void *component, node *n)
|
||||
{
|
||||
struct movable_component *cmp = (struct movable_component *)component;
|
||||
|
||||
cmp->speed = xml_getintprop(n, "speed");
|
||||
cmp->moving_left = false;
|
||||
cmp->moving_right = false;
|
||||
cmp->speed_x = xml_getintprop(n, "speedX");
|
||||
cmp->speed_y = xml_getintprop(n, "speedY");
|
||||
(void)engine;
|
||||
}
|
||||
|
||||
@@ -50,7 +50,6 @@ const struct movable_component movable_component = {
|
||||
serialize: &movable_serialize,
|
||||
destroy: &component_destroy
|
||||
},
|
||||
moving_left: false,
|
||||
moving_right: false,
|
||||
speed: 10
|
||||
speed_x: 0,
|
||||
speed_y: 0
|
||||
};
|
||||
@@ -36,7 +36,6 @@ static void rend_fdctr(gc_engine *engine, void *component, node *n)
|
||||
node *rect = xml_getnode(n, "Rect");
|
||||
sfVector2u size;
|
||||
|
||||
cmp->sprite = malloc(sizeof(gc_sprite));
|
||||
if (!cmp->sprite)
|
||||
return;
|
||||
cmp->sprite->texture = get_texture(engine, xml_getproperty(n, "src"));
|
||||
@@ -54,7 +53,9 @@ static void rend_fdctr(gc_engine *engine, void *component, node *n)
|
||||
|
||||
static void rend_dtr(void *component)
|
||||
{
|
||||
(void)component;
|
||||
struct renderer *cmp = (struct renderer *)component;
|
||||
|
||||
free(cmp->sprite);
|
||||
}
|
||||
|
||||
static char *rend_serialize(void *component)
|
||||
|
||||
@@ -47,6 +47,10 @@ void engine_destroy(gc_engine *engine)
|
||||
((gc_system *)system->data)->destroy(system->data);
|
||||
free(system);
|
||||
}
|
||||
for (gc_list *cmp = engine->components; cmp; cmp = next) {
|
||||
next = cmp->next;
|
||||
free(cmp);
|
||||
}
|
||||
free(engine);
|
||||
}
|
||||
|
||||
|
||||
+12
-1
@@ -39,13 +39,24 @@ int scene_load_textures(gc_scene *scene, const char **textures)
|
||||
void scene_destroy(gc_scene *scene)
|
||||
{
|
||||
gc_list *next = NULL;
|
||||
gc_tupple *tup = scene->entities_by_cmp;
|
||||
|
||||
for (gc_list *entity = scene->entities; entity; entity = next) {
|
||||
next = entity->next;
|
||||
((gc_entity *)entity->data)->destroy(entity->data);
|
||||
free(entity);
|
||||
}
|
||||
for (int i = 0; scene->textures[i]; i++) {
|
||||
for (int i = 0; scene->textures[i]; i++)
|
||||
scene->textures[i]->destroy(scene->textures[i]);
|
||||
free(scene->textures);
|
||||
for (gc_tupple *tupple = tup; tupple; tupple = tup) {
|
||||
tup = tupple->next;
|
||||
for (gc_list *li = tupple->entities; li; li = next) {
|
||||
next = li->next;
|
||||
free(li);
|
||||
}
|
||||
free(tupple->name);
|
||||
free(tupple);
|
||||
}
|
||||
free(scene);
|
||||
}
|
||||
|
||||
@@ -22,8 +22,9 @@ entity->get_component(entity, "ControllableComponent");
|
||||
struct movable_component *mov = (struct movable_component *)\
|
||||
entity->get_component(entity, "MovableComponent");
|
||||
|
||||
mov->moving_left = engine->is_keypressed(con->left_key);
|
||||
mov->moving_right = engine->is_keypressed(con->right_key);
|
||||
mov->speed_x = 0;
|
||||
mov->speed_x -= engine->is_keypressed(con->left_key) * con->speed;
|
||||
mov->speed_x += engine->is_keypressed(con->right_key) * con->speed;
|
||||
(void)system;
|
||||
(void)dtime;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,46 @@
|
||||
/*
|
||||
** EPITECH PROJECT, 2019
|
||||
** MUL_my_runner_2019
|
||||
** File description:
|
||||
** gravity_system
|
||||
*/
|
||||
|
||||
|
||||
#include "entity.h"
|
||||
#include "system.h"
|
||||
#include "texture.h"
|
||||
#include "vector2.h"
|
||||
#include "components/movable_component.h"
|
||||
#include "components/controllable_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 *con = (struct gravity_component *)\
|
||||
entity->get_component(entity, "GravityComponent");
|
||||
struct movable_component *mov = (struct movable_component *)\
|
||||
entity->get_component(entity, "MovableComponent");
|
||||
|
||||
mov->moving_left = engine->is_keypressed(con->left_key);
|
||||
mov->moving_right = engine->is_keypressed(con->right_key);
|
||||
(void)system;
|
||||
(void)dtime;
|
||||
}
|
||||
|
||||
void gravity_destroy(void *system)
|
||||
{
|
||||
(void)system;
|
||||
}
|
||||
|
||||
const gc_system gravity_system = {
|
||||
name: "GravitySystem",
|
||||
component_name: "GravityComponent",
|
||||
size: sizeof(gc_system),
|
||||
ctr: NULL,
|
||||
dtr: NULL,
|
||||
check_dependencies: &system_check_dependencies,
|
||||
update_entity: &gravoty_update_entity,
|
||||
destroy: &gravity_destroy
|
||||
};
|
||||
@@ -30,10 +30,14 @@ entity->get_component(entity, "TransformComponent");
|
||||
};
|
||||
qt_collision i = collision_get_info(sys->tree, obj.id);
|
||||
|
||||
if (mov->moving_left)
|
||||
pos->position.x -= MIN(mov->speed * dtime, i.distance_left);
|
||||
if (mov->moving_right)
|
||||
pos->position.x += MIN(mov->speed * dtime, i.distance_right);
|
||||
if (mov->speed_x < 0)
|
||||
pos->position.x -= MIN(mov->speed_x * -dtime, i.distance_left);
|
||||
else
|
||||
pos->position.x += MIN(mov->speed_x * dtime, i.distance_right);
|
||||
if (mov->speed_y < 0)
|
||||
pos->position.y -= MIN(mov->speed_y * -dtime, i.distance_down);
|
||||
else
|
||||
pos->position.y += MIN(mov->speed_y * dtime, i.distance_top);
|
||||
obj.rect.x = pos->position.x;
|
||||
qt_update(sys->tree, obj);
|
||||
(void)engine;
|
||||
@@ -50,7 +54,9 @@ void movable_ctr(void *system, va_list args)
|
||||
|
||||
void movable_dtr(void *system)
|
||||
{
|
||||
//SHOULD DESTROY THE QUADTREE HERE
|
||||
gc_movable_system *mov = (gc_movable_system *)system;
|
||||
|
||||
qt_destroy(mov->tree);
|
||||
(void)system;
|
||||
}
|
||||
|
||||
|
||||
+2
-2
@@ -19,9 +19,9 @@ Test(getentities, filter)
|
||||
engine->change_scene(engine, scene);
|
||||
prefab_load(engine, "tests/game.gcprefab");
|
||||
list = engine->scene->get_entity_by_cmp(engine->scene, "MovableComponent");
|
||||
while(list) {
|
||||
while (list) {
|
||||
count++;
|
||||
list = list->next;
|
||||
list = list->next;
|
||||
}
|
||||
cr_assert_eq(count, 3);
|
||||
}
|
||||
Reference in New Issue
Block a user