]> git.lizzy.rs Git - dungeon_game.git/blob - plugins/monster/monster.c
Fix plugin target
[dungeon_game.git] / plugins / monster / monster.c
1 #include <stdlib.h>
2 #include <stddef.h>
3 #include "../game/game.h"
4
5 static struct entity monster;
6
7 struct monster_data
8 {
9         double timer;
10 };
11
12 static void monster_spawn(struct entity *self)
13 {
14         self->meta = malloc(sizeof(struct monster_data));
15         ((struct monster_data *) self->meta)->timer = 0.5;
16 }
17
18 static void monster_step(struct entity *self, struct entity_step_data stepdata)
19 {
20         struct monster_data *data = self->meta;
21
22         if (stepdata.visible && (data->timer -= stepdata.dtime) <= 0.0) {
23                 data->timer = 0.5;
24
25                 (stepdata.dx && move(self, stepdata.dx > 0 ? -1 : 1, 0)) || (stepdata.dy && move(self, 0, stepdata.dy > 0 ? -1 : 1));
26         }
27 }
28
29 static void monster_on_collide_with_entity(struct entity *self, struct entity *other)
30 {
31         if (other == &player)
32                 add_health(other, -1);
33 }
34
35 static void monster_death(struct entity *self)
36 {
37         add_score(5);
38         self->remove = true;
39 }
40
41 static void spawn_monster(int x, int y)
42 {
43         spawn(monster, x, y);
44 }
45
46 __attribute__((constructor)) static void init()
47 {
48         monster = (struct entity) {
49                 .name = "monster",
50                 .x = 0,
51                 .y = 0,
52                 .color = get_color("#FF00F6"),
53                 .texture = "👾",
54                 .remove = false,
55                 .meta = NULL,
56                 .health = 5,
57                 .max_health = 5,
58                 .collide_with_entities = true,
59
60                 .on_step = &monster_step,
61                 .on_collide = NULL,
62                 .on_collide_with_entity = &monster_on_collide_with_entity,
63                 .on_spawn = &monster_spawn,
64                 .on_remove = NULL,
65                 .on_death = &monster_death,
66         };
67
68         register_air_function((struct generator_function) {
69                 .chance = 50,
70                 .callback = &spawn_monster,
71         });
72 }