From 56ce78f85014c217d84457928b41c7db9788b4d6 Mon Sep 17 00:00:00 2001 From: Tristan Roux Date: Mon, 2 Dec 2019 15:25:09 +0100 Subject: [PATCH] Moving position to a component --- include/component.h | 5 ++- .../movable_component.h | 2 +- include/components/position_component.h | 17 ++++++++++ include/entity.h | 2 +- include/system.h | 1 + include/utility.h | 9 ++++++ src/component.c | 4 ++- src/components/movable_component.c | 16 +++++----- src/components/position_component.c | 32 +++++++++++++++++++ 9 files changed, 76 insertions(+), 12 deletions(-) rename include/{Components => components}/movable_component.h (89%) create mode 100644 include/components/position_component.h create mode 100644 include/utility.h create mode 100644 src/components/position_component.c diff --git a/include/component.h b/include/component.h index e3fa3cd..a56fe45 100644 --- a/include/component.h +++ b/include/component.h @@ -17,6 +17,9 @@ typedef struct gc_component void (*ctr)(void *component, va_list); void (*dtr)(void *component); char *(*serialize)(void *component); -// privates: + + struct gc_component *next; + struct gc_component *prev; + char *tostr; } gc_component; \ No newline at end of file diff --git a/include/Components/movable_component.h b/include/components/movable_component.h similarity index 89% rename from include/Components/movable_component.h rename to include/components/movable_component.h index e240a58..c1b017e 100644 --- a/include/Components/movable_component.h +++ b/include/components/movable_component.h @@ -9,7 +9,7 @@ #include "component.h" -struct MovableComponent +struct movable_component { gc_component *base; int left_key; diff --git a/include/components/position_component.h b/include/components/position_component.h new file mode 100644 index 0000000..c63a1b0 --- /dev/null +++ b/include/components/position_component.h @@ -0,0 +1,17 @@ +/* +** EPITECH PROJECT, 2019 +** MUL_my_runner_2019 +** File description: +** position_component +*/ + +#pragma once + +#include "component.h" +#include "vector2.h" + +struct position_component +{ + gc_component *base; + vector2 position; +}; \ No newline at end of file diff --git a/include/entity.h b/include/entity.h index 3fe012c..307ed56 100644 --- a/include/entity.h +++ b/include/entity.h @@ -15,7 +15,7 @@ typedef struct gc_entity { int id; char *str; - gc_component *components; + gc_component components; void (*serialize)(); struct gc_entity *next; diff --git a/include/system.h b/include/system.h index 51a81ac..717c185 100644 --- a/include/system.h +++ b/include/system.h @@ -11,6 +11,7 @@ typedef struct gc_system { + char *name; unsigned size; void *(check_dependencies)(const gc_entity *entity); void *(update_entity)(const gc_entity *entity); diff --git a/include/utility.h b/include/utility.h new file mode 100644 index 0000000..ac8e2d8 --- /dev/null +++ b/include/utility.h @@ -0,0 +1,9 @@ +/* +** EPITECH PROJECT, 2019 +** MUL_my_runner_2019 +** File description: +** utility +*/ + +#pragma once + diff --git a/src/component.c b/src/component.c index d974a3b..8104bc8 100644 --- a/src/component.c +++ b/src/component.c @@ -11,9 +11,11 @@ void *new_component(void *component, ...) { gc_component *base = (gc_component *)component; - void *new_cmp = malloc(base->size); va_list args; + void *new_cmp = malloc(base->size); + if (!new_cmp) + return (NULL); *(gc_component *)new_cmp = *base; if (((gc_component *)new_cmp)->ctr) { va_start(args, component); diff --git a/src/components/movable_component.c b/src/components/movable_component.c index d3432c7..ae211cf 100644 --- a/src/components/movable_component.c +++ b/src/components/movable_component.c @@ -6,16 +6,16 @@ */ #include "component.h" -#include "Components/movable_component.h" +#include "components/movable_component.h" #include static void movable_ctr(void *component, va_list args) { - struct MovableComponent *movable = (struct MovableComponent *)component; + struct movable_component *cmp = (struct movable_component *)component; - movable->left_key = va_arg(args, int); - movable->right_key = va_arg(args, int); - movable->jump_key = va_arg(args, int); + cmp->left_key = va_arg(args, int); + cmp->right_key = va_arg(args, int); + cmp->jump_key = va_arg(args, int); } static void movable_dtr(void *component) @@ -33,9 +33,9 @@ static char *movable_serialize(void *component) return (NULL); } -const gc_component MovableComponent = { - name: "PositionComponent", - size: sizeof(MovableComponent), +const gc_component movable_component = { + name: "MovableComponent", + size: sizeof(struct movable_component), dependencies: NULL, ctr: &movable_ctr, dtr: &movable_dtr, diff --git a/src/components/position_component.c b/src/components/position_component.c new file mode 100644 index 0000000..600a6de --- /dev/null +++ b/src/components/position_component.c @@ -0,0 +1,32 @@ +/* +** EPITECH PROJECT, 2019 +** MUL_my_runner_2019 +** File description: +** position_component +*/ + +#include "component.h" +#include "components/position_component.h" +#include + +void position_ctr(void *component, va_list args) +{ + struct position_component *cmp = (struct position_component *)component; + + cmp->position = va_arg(args, vector2); +} + +char *position_serialize(void *component) +{ + return (NULL); +} + +const gc_component position_component = { + name: "PositionComponent", + size: sizeof(struct position_component), + dependencies: NULL, + ctr: &position_ctr, + dtr: NULL, + serialize: &position_serialize, + tostr: NULL +}; \ No newline at end of file