From b98d12b27c65de55226318012a3805ba9a4b739f Mon Sep 17 00:00:00 2001 From: Anonymus Raccoon Date: Sat, 11 Jan 2020 19:22:27 +0100 Subject: [PATCH] Handling win and loose --- include/components/timer_component.h | 1 + prefabs/player.gcprefab | 2 +- src/components/live_component.c | 20 +++++++++++++++++++- src/components/timer_component.c | 2 ++ 4 files changed, 23 insertions(+), 2 deletions(-) diff --git a/include/components/timer_component.h b/include/components/timer_component.h index 8fd79c3..44e02b8 100644 --- a/include/components/timer_component.h +++ b/include/components/timer_component.h @@ -13,6 +13,7 @@ struct timer_component { gc_component base; float time_left; + int default_value; }; extern const struct timer_component timer_component; \ No newline at end of file diff --git a/prefabs/player.gcprefab b/prefabs/player.gcprefab index 62fa9af..46cfa6c 100644 --- a/prefabs/player.gcprefab +++ b/prefabs/player.gcprefab @@ -21,7 +21,7 @@ - + \ No newline at end of file diff --git a/src/components/live_component.c b/src/components/live_component.c index 8190b72..11a66a8 100644 --- a/src/components/live_component.c +++ b/src/components/live_component.c @@ -11,26 +11,44 @@ #include "components/collision_component.h" #include "components/transform_component.h" #include "components/win_component.h" +#include "components/controllable_component.h" +#include "components/timer_component.h" #include "utility.h" #include "prefab.h" #include +void reset_timer(gc_scene *scene) +{ + gc_entity *entity = scene->get_entity(scene, 50); + struct timer_component *timer; + + if (!entity) + return; + timer = GETCMP(timer_component); + timer->time_left = timer->default_value; +} + static void on_collide(gc_engine *engine, gc_entity *entity, int id) { struct live_component *cmp = GETCMP(live_component); struct transform_component *trans = GETCMP(transform_component); + struct controllable_component *con = GETCMP(controllable_component); if (GETCOLCMP(kill_component)) { cmp->live--; - if (cmp->live < 0) { + if (cmp->live == 0) { engine->should_close = true; } else { trans->position = cmp->spawn_position; + reset_timer(engine->scene); } } if (GETCOLCMP(win_component)) { prefab_load(engine, "prefabs/winscreen.gcprefab"); entity->remove_component(engine->scene, entity, "keyboard_controller"); + con->jumping = false; + con->moving_left = false; + con->moving_right = false; } } diff --git a/src/components/timer_component.c b/src/components/timer_component.c index 8070a1b..6b6dd19 100644 --- a/src/components/timer_component.c +++ b/src/components/timer_component.c @@ -15,6 +15,7 @@ static void ctr(void *component, va_list args) struct timer_component *cmp = (struct timer_component *)component; cmp->time_left = va_arg(args, double); + cmp->default_value = cmp->time_left; } static void fdctr(gc_entity *entity, gc_scene *scene, void *component, node *n) @@ -22,6 +23,7 @@ static void fdctr(gc_entity *entity, gc_scene *scene, void *component, node *n) struct timer_component *cmp = (struct timer_component *)component; cmp->time_left = xml_getintprop(n, "time"); + cmp->default_value = cmp->time_left; (void)entity; (void)scene; }