]> git.lizzy.rs Git - dungeon_game.git/commitdiff
Nicer death handling
authorElias Fleckenstein <eliasfleckenstein@web.de>
Wed, 9 Jun 2021 15:44:02 +0000 (17:44 +0200)
committerElias Fleckenstein <eliasfleckenstein@web.de>
Wed, 9 Jun 2021 15:44:02 +0000 (17:44 +0200)
plugins/game/game.c
plugins/game/game.h

index 0e734a06c6281ab707ad9a6c90679d05649c4e36..ba5257ad07a10cd1c16bd12c432889ca0391dcb2 100644 (file)
@@ -126,6 +126,11 @@ void add_score(int s)
        score += s;
 }
 
+bool player_dead()
+{
+       return player.health <= 0;
+}
+
 struct list *add_element(struct list *list, void *element)
 {
        struct list **ptr;
@@ -152,8 +157,7 @@ void register_air_function(struct generator_function func)
 
 static void player_death(struct entity *self)
 {
-       (void) self;
-       quit();
+       self->texture = "☠ ";
 }
 
 /* Mapgen */
@@ -344,21 +348,23 @@ static void render(render_entity_list entity_list)
 
 static void handle_input(char c)
 {
+       bool dead = player_dead();
+
        switch (c) {
                case 'q':
                        quit();
                        break;
                case 'w':
-                       move(&player, 0, -1);
+                       dead || move(&player, 0, -1);
                        break;
                case 'a':
-                       move(&player, -1, 0);
+                       dead || move(&player, -1, 0);
                        break;
                case 's':
-                       move(&player, 0, 1);
+                       dead || move(&player, 0, 1);
                        break;
                case 'd':
-                       move(&player, 1, 0);
+                       dead || move(&player, 1, 0);
                        break;
        }
 }
@@ -460,6 +466,8 @@ void game()
                double dtime = (double) (ts.tv_sec - ts_old.tv_sec) + (double) (ts.tv_nsec - ts_old.tv_nsec) / 1000000000.0;
                ts_old = ts;
 
+               bool dead = player_dead();
+
                render_entity_list render_list = {{NULL}};
 
                for (struct list **ptr = &entities; *ptr != NULL; ) {
@@ -492,7 +500,7 @@ void game()
                        if (visible)
                                render_list[dx + LIGHT][dy + LIGHT] = entity;
 
-                       if (entity->on_step)
+                       if (! dead && entity->on_step)
                                entity->on_step(entity, (struct entity_step_data) {
                                        .dtime = dtime,
                                        .visible = visible,
index 9c6953f79a89321db80c813b78ec6fa9b55a52e5..0463086bccc022b915664e3a4efd523ff7f9da94 100644 (file)
@@ -37,7 +37,7 @@ struct entity
        const char *name;
        int x, y;
        struct color color;
-       char texture[8];
+       const char *texture;
        bool remove;
        void *meta;
        int health;
@@ -92,6 +92,7 @@ bool move(struct entity *entity, int xoff, int yoff);
 void spawn(struct entity def, int x, int y);
 void add_health(struct entity *entity, int health);
 void add_score(int s);
+bool player_dead();
 void set_color(struct color color, bool bg);
 struct color light_color(struct color color, double light);
 void register_air_function(struct generator_function func);