]> git.lizzy.rs Git - dungeon_game.git/commitdiff
Only use entity color if use_color = true
authorElias Fleckenstein <eliasfleckenstein@web.de>
Fri, 11 Jun 2021 19:21:53 +0000 (21:21 +0200)
committerElias Fleckenstein <eliasfleckenstein@web.de>
Fri, 11 Jun 2021 19:21:53 +0000 (21:21 +0200)
plugins/apple/apple.c
plugins/cherry/cherry.c
plugins/fireball/fireball.c
plugins/game/game.c
plugins/game/game.h
plugins/monster/monster.c

index 19e2e00404105e3c80dc7b657cfd5c90dd06e7db..795800d48ae47c5edd88a0879c77f6b9fb0f7723 100644 (file)
@@ -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,
index 072aec077b6592121c6ff64e9bde665dab38d6c4..436fcf941bfd8036e024aee3dbe92ab6fc7bd378 100644 (file)
@@ -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,
index f2e84799d2967637b5512ade10b78cd35e4eace1..30ea754c018a5f723a1ddf23b2b4fee1c5e319ca 100644 (file)
@@ -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,
index 8dacf4ad088e6f54eebea0beb2e342952bb4c8cd..9fcf7ea6c0191237fec9683d19161f2edd6a6cf6 100644 (file)
@@ -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++)
index 11d39ba3107e885c5f1bd1637b9d6a8182a633ae..cdca0b4e6eb12cb44aa06ba60933aba5d29b4a43 100644 (file)
@@ -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;
index 890e2bd1fbe11d292efb6c1db676ecd5adc762c0..02957da9abd6d52e2bbe5afd1c4524ff6b449c8a 100644 (file)
@@ -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,