]> git.lizzy.rs Git - dungeon_game.git/blob - plugins/cherry/cherry.c
Separate air_function chances for rooms and corridors
[dungeon_game.git] / plugins / cherry / cherry.c
1 #include <stddef.h>
2 #include <stdlib.h>
3 #include "../game/game.h"
4 #include "../score/score.h"
5 #include "../inventory/inventory.h"
6
7 static bool use_cherry(struct itemstack *stack)
8 {
9         add_health(&player, 2);
10         return true;
11 }
12
13 static struct item cherry_item = {
14         .name = "Cherry",
15         .stackable = true,
16
17         .on_use = &use_cherry,
18         .on_destroy = NULL,
19         .on_create = NULL,
20 };
21
22 static void cherry_step(struct entity *self, struct entity_step_data stepdata)
23 {
24         if (stepdata.dx == 0 && stepdata.dy == 0) {
25                 add_score(2);
26                 inventory_add(&player_inventory, (struct itemstack) {
27                         .item = &cherry_item,
28                         .count = 1,
29                         .meta = NULL,
30                 });
31                 self->remove = true;
32         }
33 }
34
35 static struct entity cherry_entity = {
36         .name = "cherry",
37         .x = 0,
38         .y = 0,
39         .color = {0},
40         .use_color = false,
41         .texture = "🍒",
42         .remove = false,
43         .meta = NULL,
44         .health = 1,
45         .max_health = 1,
46         .collide_with_entities = false,
47
48         .on_step = &cherry_step,
49         .on_collide = NULL,
50         .on_collide_with_entity = NULL,
51         .on_spawn = NULL,
52         .on_remove = NULL,
53         .on_death = NULL,
54         .on_damage = NULL,
55 };
56
57 static void spawn_cherry(int x, int y, enum mg_context ctx)
58 {
59         spawn(cherry_entity, x, y, NULL);
60 }
61
62 __attribute__((constructor)) static void init()
63 {
64         register_air_function((struct generator_function) {
65                 .corridor_chance = 100,
66                 .room_chance = 100,
67                 .callback = &spawn_cherry,
68         });
69 }
70