]> git.lizzy.rs Git - dungeon_game.git/commitdiff
Add read damage overlay
authorElias Fleckenstein <eliasfleckenstein@web.de>
Wed, 9 Jun 2021 16:03:40 +0000 (18:03 +0200)
committerElias Fleckenstein <eliasfleckenstein@web.de>
Wed, 9 Jun 2021 16:03:40 +0000 (18:03 +0200)
plugins/apple/apple.c
plugins/game/game.c
plugins/game/game.h
plugins/monster/monster.c

index dd8a559edc19cabc2a13703ea4276a517c096368..d8e7375a7e9f81672825eecddfc0d676e86120a2 100644 (file)
@@ -38,6 +38,7 @@ __attribute__((constructor)) static void init()
                .on_spawn = NULL,
                .on_remove = NULL,
                .on_death = NULL,
+               .on_damage = NULL,
        };
 
        register_air_function((struct generator_function) {
index ba5257ad07a10cd1c16bd12c432889ca0391dcb2..c77a53c1ffdec9753f5df345436dcf95cbf38444 100644 (file)
@@ -14,6 +14,7 @@
 #include "game.h"
 
 bool running = true;
+double damage_overlay = 0.0;
 
 int score = 0;
 
@@ -115,6 +116,9 @@ void add_health(struct entity *entity, int health)
 
        entity->health += health;
 
+       if (health < 0 && entity->on_damage)
+               entity->on_damage(entity, -health);
+
        if (entity->health > entity->max_health)
                entity->health = entity->max_health;
        else if (entity->health <= 0 && was_alive && entity->on_death)
@@ -160,6 +164,11 @@ static void player_death(struct entity *self)
        self->texture = "☠ ";
 }
 
+static void player_damage(struct entity *self, int damage)
+{
+       damage_overlay = (double) damage * 0.5;
+}
+
 /* Mapgen */
 
 static bool check_direction(int x, int y, int dir)
@@ -242,13 +251,34 @@ void set_color(struct color color, bool bg)
        printf("\e[%u;2;%u;%u;%um", bg ? 48 : 38, color.r, color.g, color.b);
 }
 
-struct color light_color(struct color color, double light)
+void light_color(struct color *color, double light)
 {
-       return (struct color) {
-               color.r * light,
-               color.g * light,
-               color.b * light,
-       };
+       color->r *= light;
+       color->g *= light;
+       color->b *= light;
+}
+
+void mix_color(struct color *color, struct color other, double ratio)
+{
+       double ratio_total = ratio + 1;
+
+       color->r = (color->r + other.r * ratio) / ratio_total;
+       color->g = (color->g + other.g * ratio) / ratio_total;
+       color->b = (color->b + other.b * ratio) / ratio_total;
+}
+
+void render_color(struct color color, double light, bool bg)
+{
+       if (light <= 0.0) {
+               set_color(black, bg);
+       } else {
+               if (damage_overlay > 0.0)
+                       mix_color(&color, get_color("#F20000"), damage_overlay * 2.0);
+
+               light_color(&color, light);
+
+               set_color(color, bg);
+       }
 }
 
 static void render(render_entity_list entity_list)
@@ -285,18 +315,14 @@ static void render(render_entity_list entity_list)
                        double dist = sqrt(x * x + y * y);
                        double light = 1.0 - (double) dist / (double) LIGHT;
 
-                       if (light <= 0)
-                               goto empty;
-
-                       set_color(light_color(node.material->color, light), true);
+                       render_color(node.material->color, light, true);
 
                        struct entity *entity = entity_list[x + LIGHT][y + LIGHT];
 
                        if (entity) {
-                               set_color(entity->color, false);
+                               render_color(entity->color, light, false);
                                printf("%s", entity->texture);
                        } else {
-                               empty:
                                printf("  ");
                        }
                }
@@ -425,6 +451,7 @@ __attribute__ ((constructor)) static void init()
                .on_spawn = NULL,
                .on_remove = NULL,
                .on_death = &player_death,
+               .on_damage = &player_damage,
        };
 
        entity_collision_map[player.x][player.y] = &player;
@@ -468,6 +495,9 @@ void game()
 
                bool dead = player_dead();
 
+               if (! dead && damage_overlay > 0.0)
+                       damage_overlay -= dtime;
+
                render_entity_list render_list = {{NULL}};
 
                for (struct list **ptr = &entities; *ptr != NULL; ) {
index 0463086bccc022b915664e3a4efd523ff7f9da94..d5457a9eb9e7973e50cfcd6a13267817c5ebed53 100644 (file)
@@ -50,6 +50,7 @@ struct entity
        void (*on_spawn)(struct entity *self);
        void (*on_remove)(struct entity *self);
        void (*on_death)(struct entity *self);
+       void (*on_damage)(struct entity *self, int damage);
 };
 
 struct list
@@ -94,7 +95,8 @@ 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 light_color(struct color *color, double light);
+void mix_color(struct color *color, struct color other, double ratio);
 void register_air_function(struct generator_function func);
 struct list *add_element(struct list *list, void *element);
 
index 6cb14ffd49de7ec60e72df098db0bb0801235b30..7124c76fe3c6768157a168bfa436b72147e3b2b9 100644 (file)
@@ -63,6 +63,7 @@ __attribute__((constructor)) static void init()
                .on_spawn = &monster_spawn,
                .on_remove = NULL,
                .on_death = &monster_death,
+               .on_damage = NULL,
        };
 
        register_air_function((struct generator_function) {