From c13ff04eecea50a907d2e43040188a3e90eeea35 Mon Sep 17 00:00:00 2001
From: Anonymus Raccoon
Date: Sun, 12 Jan 2020 17:14:30 +0100
Subject: [PATCH] Making the timer component reset the player (really ugly)
---
main.c | 2 +-
src/components/live_component.c | 22 +++++++++++-----------
src/systems/timer_system.c | 16 ++++++++++++----
3 files changed, 24 insertions(+), 16 deletions(-)
diff --git a/main.c b/main.c
index 2f0c203..d2cc33e 100644
--- a/main.c
+++ b/main.c
@@ -13,7 +13,7 @@ int usage(const char *bin)
my_printf("My runner:\n\n\tUsage: %s [path_to_map]. \
If no map are specified, the default one is loaded.\n\n\
Moves:\n\tQ or D to move to the left or right\n\t\
-SPACE to jump\n", bin);
+SPACE to jump (you can release the jump before the end to jump lower)\n", bin);
return (0);
}
diff --git a/src/components/live_component.c b/src/components/live_component.c
index 11a66a8..fe7dc82 100644
--- a/src/components/live_component.c
+++ b/src/components/live_component.c
@@ -28,13 +28,20 @@ void reset_timer(gc_scene *scene)
timer->time_left = timer->default_value;
}
-static void on_collide(gc_engine *engine, gc_entity *entity, int id)
+void live_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)) {
+ 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;
+ }
+ else if (GETCOLCMP(kill_component) || id == -1) {
cmp->live--;
if (cmp->live == 0) {
engine->should_close = true;
@@ -43,13 +50,6 @@ static void on_collide(gc_engine *engine, gc_entity *entity, int id)
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;
- }
}
static void ctr(void *component, va_list args)
@@ -63,7 +63,7 @@ static void ctr(void *component, va_list args)
col = GETCMP(collision_component);
trans = GETCMP(transform_component);
if (col)
- add_on_collide(col, &on_collide);
+ add_on_collide(col, &live_collide);
if (trans)
cmp->spawn_position = trans->position;
}
@@ -84,7 +84,7 @@ live component after the collision component.\n"));
return ((void)my_printf("Collision not yet setup, you should place the \
live component after the collision component.\n"));
cmp->spawn_position = trans->position;
- add_on_collide(col, &on_collide);
+ add_on_collide(col, &live_collide);
(void)scene;
}
diff --git a/src/systems/timer_system.c b/src/systems/timer_system.c
index d8d4b2c..a90c2c4 100644
--- a/src/systems/timer_system.c
+++ b/src/systems/timer_system.c
@@ -14,6 +14,8 @@
#include "text.h"
#include
+void live_collide(gc_engine *engine, gc_entity *entity, int id);
+
static void update_entity(gc_engine *engine, void *system, \
gc_entity *entity, float dtime)
{
@@ -23,11 +25,17 @@ gc_entity *entity, float dtime)
if (!rend || rend->type != GC_TXTREND)
return;
timer->time_left -= dtime;
- if (((gc_text *)rend->data)->text)
- free(((gc_text *)rend->data)->text);
- ((gc_text *)rend->data)->text = tostr((int)timer->time_left);
+ if (timer->time_left <= 0) {
+ entity = engine->scene->get_entity(engine->scene, 25);
+ if (!entity || !entity->get_component(entity, "keyboard_controller"))
+ return;
+ live_collide(engine, entity, -1);
+ } else {
+ if (((gc_text *)rend->data)->text)
+ free(((gc_text *)rend->data)->text);
+ ((gc_text *)rend->data)->text = tostr((int)timer->time_left);
+ }
(void)system;
- (void)engine;
}
static void destroy(void *system)