diff options
author | Elias Fleckenstein <eliasfleckenstein@web.de> | 2021-06-11 21:11:37 +0200 |
---|---|---|
committer | Elias Fleckenstein <eliasfleckenstein@web.de> | 2021-06-11 21:11:37 +0200 |
commit | 30c560e271499cb461372a6798e53bf122a7495e (patch) | |
tree | 8d3022047c8b83d5a95e14c1fa488b0c8702be52 /plugins/cherry/cherry.c | |
parent | 14d315712d372932b27a118171865118a6b415c7 (diff) | |
download | dungeon_game-30c560e271499cb461372a6798e53bf122a7495e.tar.xz |
Add inventory and cherries (collectable food)
Diffstat (limited to 'plugins/cherry/cherry.c')
-rw-r--r-- | plugins/cherry/cherry.c | 71 |
1 files changed, 71 insertions, 0 deletions
diff --git a/plugins/cherry/cherry.c b/plugins/cherry/cherry.c new file mode 100644 index 0000000..072aec0 --- /dev/null +++ b/plugins/cherry/cherry.c @@ -0,0 +1,71 @@ +#include <stddef.h> +#include <stdlib.h> +#include "../game/game.h" +#include "../score/score.h" +#include "../inventory/inventory.h" + +static struct entity cherry_entity; + +static bool use_cherry(struct itemstack *stack) +{ + (void) stack; + + add_health(&player, 2); + return true; +} + +static struct item cherry_item = { + .name = "Cherry", + .stackable = true, + + .on_use = &use_cherry, + .on_destroy = NULL, +}; + +static void cherry_step(struct entity *self, struct entity_step_data stepdata) +{ + if (stepdata.dx == 0 && stepdata.dy == 0) { + add_score(2); + inventory_add(&player_inventory, (struct itemstack) { + .item = &cherry_item, + .count = 1, + .meta = NULL, + }); + self->remove = true; + } +} + +static void spawn_cherry(int x, int y) +{ + spawn(cherry_entity, x, y, NULL); +} + +__attribute__((constructor)) static void init() +{ + cherry_entity = (struct entity) { + .name = "cherry", + .x = 0, + .y = 0, + .color = get_color("#FF2A53"), + .texture = "🍒", + .remove = false, + .meta = NULL, + .health = 1, + .max_health = 1, + .collide_with_entities = false, + + .on_step = &cherry_step, + .on_collide = NULL, + .on_collide_with_entity = NULL, + .on_spawn = NULL, + .on_remove = NULL, + .on_death = NULL, + .on_damage = NULL, + }; + + register_air_function((struct generator_function) { + .chance = 100, + .callback = &spawn_cherry, + }); +} + |