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