aboutsummaryrefslogtreecommitdiff
path: root/plugins/monster/monster.c
blob: 02957da9abd6d52e2bbe5afd1c4524ff6b449c8a (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
70
71
72
73
74
75
76
#include <stdlib.h>
#include <stddef.h>
#include "../game/game.h"
#include "../score/score.h"

struct monster_data
{
	double timer;
};

static void monster_spawn(struct entity *self, void *data)
{
	(void) data;

	self->meta = malloc(sizeof(struct monster_data));
	((struct monster_data *) self->meta)->timer = 0.5;
}

static void monster_step(struct entity *self, struct entity_step_data stepdata)
{
	struct monster_data *data = self->meta;

	if (stepdata.visible && (data->timer -= stepdata.dtime) <= 0.0) {
		data->timer = 0.5;

		(stepdata.dx && move(self, stepdata.dx > 0 ? -1 : 1, 0)) || (stepdata.dy && move(self, 0, stepdata.dy > 0 ? -1 : 1));
	}
}

static void monster_collide_with_entity(struct entity *self, struct entity *other)
{
	if (other == &player)
		add_health(other, -1);
}

static void monster_death(struct entity *self)
{
	add_score(5);
	self->remove = true;
}

static struct entity monster_entity = {
	.name = "monster",
	.x = 0,
	.y = 0,
	.color = {0},
	.use_color = false,
	.texture = "👾",
	.remove = false,
	.meta = NULL,
	.health = 5,
	.max_health = 5,
	.collide_with_entities = true,

	.on_step = &monster_step,
	.on_collide = NULL,
	.on_collide_with_entity = &monster_collide_with_entity,
	.on_spawn = &monster_spawn,
	.on_remove = NULL,
	.on_death = &monster_death,
	.on_damage = NULL,
};


static void spawn_monster(int x, int y)
{
	spawn(monster_entity, x, y, NULL);
}

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