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