aboutsummaryrefslogtreecommitdiff
path: root/plugins/cherry/cherry.c
blob: 1f96aca63986a9910e7b5866d98e24036165c645 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <stddef.h>
#include <stdlib.h>
#include "../game/game.h"
#include "../score/score.h"
#include "../inventory/inventory.h"

static bool use_cherry(struct itemstack *stack)
{
	add_health(&player, 2);
	return true;
}

static struct item cherry_item = {
	.name = "Cherry",
	.stackable = true,

	.on_use = &use_cherry,
	.on_destroy = NULL,
	.on_create = 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 struct entity cherry_entity = {
	.name = "cherry",
	.x = 0,
	.y = 0,
	.color = {0},
	.use_color = false,
	.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,
};

static void spawn_cherry(int x, int y, enum mg_context ctx)
{
	spawn(cherry_entity, x, y, NULL);
}

__attribute__((constructor)) static void init()
{
	register_air_function((struct generator_function) {
		.chance = 100,
		.callback = &spawn_cherry,
	});
}