#include "game.h"
bool running = true;
+double damage_overlay = 0.0;
int score = 0;
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)
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)
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)
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(" ");
}
}
.on_spawn = NULL,
.on_remove = NULL,
.on_death = &player_death,
+ .on_damage = &player_damage,
};
entity_collision_map[player.x][player.y] = &player;
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; ) {
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
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);