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