]> git.lizzy.rs Git - dungeon_game.git/blob - plugins/cherry/cherry.c
Add on_create callback for items
[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         .on_create = NULL,
22 };
23
24 static void cherry_step(struct entity *self, struct entity_step_data stepdata)
25 {
26         if (stepdata.dx == 0 && stepdata.dy == 0) {
27                 add_score(2);
28                 inventory_add(&player_inventory, (struct itemstack) {
29                         .item = &cherry_item,
30                         .count = 1,
31                         .meta = NULL,
32                 });
33                 self->remove = true;
34         }
35 }
36
37 static struct entity cherry_entity = {
38         .name = "cherry",
39         .x = 0,
40         .y = 0,
41         .color = {0},
42         .use_color = false,
43         .texture = "🍒",
44         .remove = false,
45         .meta = NULL,
46         .health = 1,
47         .max_health = 1,
48         .collide_with_entities = false,
49
50         .on_step = &cherry_step,
51         .on_collide = NULL,
52         .on_collide_with_entity = NULL,
53         .on_spawn = NULL,
54         .on_remove = NULL,
55         .on_death = NULL,
56         .on_damage = NULL,
57 };
58
59 static void spawn_cherry(int x, int y)
60 {
61         spawn(cherry_entity, x, y, NULL);
62 }
63
64 __attribute__((constructor)) static void init()
65 {
66         register_air_function((struct generator_function) {
67                 .chance = 100,
68                 .callback = &spawn_cherry,
69         });
70 }
71