From 07b52bfe3f13319dac0d066ba82e127c1bb95616 Mon Sep 17 00:00:00 2001 From: Elias Fleckenstein Date: Fri, 11 Jun 2021 21:21:53 +0200 Subject: [PATCH] Only use entity color if use_color = true --- plugins/apple/apple.c | 47 +++++++++++++++---------------- plugins/cherry/cherry.c | 45 +++++++++++++++-------------- plugins/fireball/fireball.c | 47 ++++++++++++++++--------------- plugins/game/game.c | 56 +++++++++++++++++++------------------ plugins/game/game.h | 5 ++-- plugins/monster/monster.c | 48 +++++++++++++++---------------- 6 files changed, 125 insertions(+), 123 deletions(-) diff --git a/plugins/apple/apple.c b/plugins/apple/apple.c index 19e2e00..795800d 100644 --- a/plugins/apple/apple.c +++ b/plugins/apple/apple.c @@ -3,8 +3,6 @@ #include "../game/game.h" #include "../score/score.h" -static struct entity apple; - static void apple_step(struct entity *self, struct entity_step_data stepdata) { if (stepdata.dx == 0 && stepdata.dy == 0) { @@ -14,34 +12,35 @@ static void apple_step(struct entity *self, struct entity_step_data stepdata) } } +static struct entity apple_entity = { + .name = "apple", + .x = 0, + .y = 0, + .color = {0}, + .use_color = false, + .texture = "🍎", + .remove = false, + .meta = NULL, + .health = 1, + .max_health = 1, + .collide_with_entities = false, + + .on_step = &apple_step, + .on_collide = NULL, + .on_collide_with_entity = NULL, + .on_spawn = NULL, + .on_remove = NULL, + .on_death = NULL, + .on_damage = NULL, +}; + static void spawn_apple(int x, int y) { - spawn(apple, x, y, NULL); + spawn(apple_entity, x, y, NULL); } __attribute__((constructor)) static void init() { - apple = (struct entity) { - .name = "apple", - .x = 0, - .y = 0, - .color = get_color("#FF2A53"), - .texture = "🍎", - .remove = false, - .meta = NULL, - .health = 1, - .max_health = 1, - .collide_with_entities = false, - - .on_step = &apple_step, - .on_collide = NULL, - .on_collide_with_entity = NULL, - .on_spawn = NULL, - .on_remove = NULL, - .on_death = NULL, - .on_damage = NULL, - }; - register_air_function((struct generator_function) { .chance = 25, .callback = &spawn_apple, diff --git a/plugins/cherry/cherry.c b/plugins/cherry/cherry.c index 072aec0..436fcf9 100644 --- a/plugins/cherry/cherry.c +++ b/plugins/cherry/cherry.c @@ -4,8 +4,6 @@ #include "../score/score.h" #include "../inventory/inventory.h" -static struct entity cherry_entity; - static bool use_cherry(struct itemstack *stack) { (void) stack; @@ -35,6 +33,28 @@ static void cherry_step(struct entity *self, struct entity_step_data stepdata) } } +static struct entity cherry_entity = { + .name = "cherry", + .x = 0, + .y = 0, + .color = {0}, + .use_color = false, + .texture = "🍒", + .remove = false, + .meta = NULL, + .health = 1, + .max_health = 1, + .collide_with_entities = false, + + .on_step = &cherry_step, + .on_collide = NULL, + .on_collide_with_entity = NULL, + .on_spawn = NULL, + .on_remove = NULL, + .on_death = NULL, + .on_damage = NULL, +}; + static void spawn_cherry(int x, int y) { spawn(cherry_entity, x, y, NULL); @@ -42,27 +62,6 @@ static void spawn_cherry(int x, int y) __attribute__((constructor)) static void init() { - cherry_entity = (struct entity) { - .name = "cherry", - .x = 0, - .y = 0, - .color = get_color("#FF2A53"), - .texture = "🍒", - .remove = false, - .meta = NULL, - .health = 1, - .max_health = 1, - .collide_with_entities = false, - - .on_step = &cherry_step, - .on_collide = NULL, - .on_collide_with_entity = NULL, - .on_spawn = NULL, - .on_remove = NULL, - .on_death = NULL, - .on_damage = NULL, - }; - register_air_function((struct generator_function) { .chance = 100, .callback = &spawn_cherry, diff --git a/plugins/fireball/fireball.c b/plugins/fireball/fireball.c index f2e8479..30ea754 100644 --- a/plugins/fireball/fireball.c +++ b/plugins/fireball/fireball.c @@ -3,8 +3,6 @@ #include "../game/game.h" #include "../movement/movement.h" -static struct entity fireball; - struct fireball_data { double timer; @@ -45,6 +43,28 @@ static void fireball_collide_with_entity(struct entity *self, struct entity *oth self->remove = true; } +static struct entity fireball_entity = { + .name = "fireball", + .x = 0, + .y = 0, + .color = {0}, + .use_color = true, + .texture = "⬤ ", + .remove = false, + .meta = NULL, + .health = 1, + .max_health = 1, + .collide_with_entities = true, + + .on_step = &fireball_step, + .on_collide = &fireball_collide, + .on_collide_with_entity = &fireball_collide_with_entity, + .on_spawn = &fireball_spawn, + .on_remove = NULL, + .on_death = NULL, + .on_damage = NULL, +}; + static void shoot_fireball() { int vx, vy; @@ -52,7 +72,7 @@ static void shoot_fireball() dir_to_xy(last_player_move, &vx, &vy); - spawn(fireball, player.x + vx, player.y + vy, & (struct fireball_data) { + spawn(fireball_entity, player.x + vx, player.y + vy, & (struct fireball_data) { .timer = 0.1, .vx = vx, .vy = vy, @@ -61,26 +81,7 @@ static void shoot_fireball() __attribute__((constructor)) static void init() { - fireball = (struct entity) { - .name = "fireball", - .x = 0, - .y = 0, - .color = get_color("#FF6611"), - .texture = "⬤ ", - .remove = false, - .meta = NULL, - .health = 1, - .max_health = 1, - .collide_with_entities = true, - - .on_step = &fireball_step, - .on_collide = &fireball_collide, - .on_collide_with_entity = &fireball_collide_with_entity, - .on_spawn = &fireball_spawn, - .on_remove = NULL, - .on_death = NULL, - .on_damage = NULL, - }; + fireball_entity.color = get_color("#FF6611"); register_input_handler(' ', (struct input_handler) { .run_if_dead = false, diff --git a/plugins/game/game.c b/plugins/game/game.c index 8dacf4a..9fcf7ea 100644 --- a/plugins/game/game.c +++ b/plugins/game/game.c @@ -13,7 +13,7 @@ /* Shared variables */ -struct color black = {0, 0, 0}; +struct color black = {0}; struct material wall; struct material air; @@ -21,7 +21,6 @@ struct material outside; struct node map[MAP_WIDTH][MAP_HEIGHT]; -struct entity player; struct list *entities = & (struct list) { .element = &player, .next = NULL, @@ -252,6 +251,28 @@ static void player_damage(struct entity *self, int damage) damage_overlay += (double) damage * 0.5; } +struct entity player = { + .name = "player", + .x = MAP_WIDTH / 2, + .y = MAP_HEIGHT / 2, + .color = {0}, + .use_color = false, + .texture = "🙂", + .remove = false, + .meta = NULL, + .health = 10, + .max_health = 10, + .collide_with_entities = true, + + .on_step = NULL, + .on_collide = NULL, + .on_collide_with_entity = NULL, + .on_spawn = NULL, + .on_remove = NULL, + .on_death = &player_death, + .on_damage = &player_damage, +}; + /* Mapgen */ static void mapgen_set_air(int x, int y) @@ -350,12 +371,12 @@ static void generate_corridor_random(int x, int y) /* Rendering */ -static bool render_color(struct color color, double light, bool bg) +static bool render_color(struct color color, double light, bool bg, bool use_color) { if (light <= 0.0) { set_color(black, bg); return false; - } else { + } else if (use_color) { if (damage_overlay > 0.0) mix_color(&color, damage_overlay_color, damage_overlay * 2.0); @@ -363,6 +384,8 @@ static bool render_color(struct color color, double light, bool bg) set_color(color, bg); return true; + } else { + return true; } } @@ -393,12 +416,12 @@ static void render_map(struct winsize ws) double dist = sqrt(x * x + y * y); double light = 1.0 - (double) dist / (double) LIGHT; - render_color(node.material->color, light, true); + render_color(node.material->color, light, true, true); struct entity *entity = render_entities[x + LIGHT][y + LIGHT]; render_entities[x + LIGHT][y + LIGHT] = NULL; - if (entity && render_color(entity->color, light, false)) + if (entity && render_color(entity->color, light, false, entity->use_color)) printf("%s", entity->texture); else printf(" "); @@ -575,27 +598,6 @@ __attribute__ ((constructor)) static void init() .color = black, }; - player = (struct entity) { - .name = "player", - .x = MAP_WIDTH / 2, - .y = MAP_HEIGHT / 2, - .color = get_color("#00FFFF"), - .texture = "🙂", - .remove = false, - .meta = NULL, - .health = 10, - .max_health = 10, - .collide_with_entities = true, - - .on_step = NULL, - .on_collide = NULL, - .on_collide_with_entity = NULL, - .on_spawn = NULL, - .on_remove = NULL, - .on_death = &player_death, - .on_damage = &player_damage, - }; - entity_collision_map[player.x][player.y] = &player; for (int x = 0; x < MAP_WIDTH; x++) diff --git a/plugins/game/game.h b/plugins/game/game.h index 11d39ba..cdca0b4 100644 --- a/plugins/game/game.h +++ b/plugins/game/game.h @@ -36,10 +36,11 @@ struct entity_step_data struct entity { - const char *name; + char *name; int x, y; struct color color; - const char *texture; + bool use_color; + char *texture; bool remove; void *meta; int health; diff --git a/plugins/monster/monster.c b/plugins/monster/monster.c index 890e2bd..02957da 100644 --- a/plugins/monster/monster.c +++ b/plugins/monster/monster.c @@ -3,8 +3,6 @@ #include "../game/game.h" #include "../score/score.h" -static struct entity monster; - struct monster_data { double timer; @@ -41,34 +39,36 @@ static void monster_death(struct entity *self) self->remove = true; } +static struct entity monster_entity = { + .name = "monster", + .x = 0, + .y = 0, + .color = {0}, + .use_color = false, + .texture = "👾", + .remove = false, + .meta = NULL, + .health = 5, + .max_health = 5, + .collide_with_entities = true, + + .on_step = &monster_step, + .on_collide = NULL, + .on_collide_with_entity = &monster_collide_with_entity, + .on_spawn = &monster_spawn, + .on_remove = NULL, + .on_death = &monster_death, + .on_damage = NULL, +}; + + static void spawn_monster(int x, int y) { - spawn(monster, x, y, NULL); + spawn(monster_entity, x, y, NULL); } __attribute__((constructor)) static void init() { - monster = (struct entity) { - .name = "monster", - .x = 0, - .y = 0, - .color = get_color("#FF00F6"), - .texture = "👾", - .remove = false, - .meta = NULL, - .health = 5, - .max_health = 5, - .collide_with_entities = true, - - .on_step = &monster_step, - .on_collide = NULL, - .on_collide_with_entity = &monster_collide_with_entity, - .on_spawn = &monster_spawn, - .on_remove = NULL, - .on_death = &monster_death, - .on_damage = NULL, - }; - register_air_function((struct generator_function) { .chance = 50, .callback = &spawn_monster, -- 2.44.0