]> git.lizzy.rs Git - dungeon_game.git/blob - plugins/cherry/cherry.c
436fcf941bfd8036e024aee3dbe92ab6fc7bd378
[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         (void) stack;
10
11         add_health(&player, 2);
12         return true;
13 }
14
15 static struct item cherry_item = {
16         .name = "Cherry",
17         .stackable = true,
18
19         .on_use = &use_cherry,
20         .on_destroy = 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)
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                 .chance = 100,
67                 .callback = &spawn_cherry,
68         });
69 }
70