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