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