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