]> git.lizzy.rs Git - dungeon_game.git/blob - plugins/fireball/fireball.c
Shoot fireballs into last movement direction
[dungeon_game.git] / plugins / fireball / fireball.c
1 #include <stdlib.h>
2 #include <stddef.h>
3 #include "../game/game.h"
4 #include "../movement/movement.h"
5
6 static struct entity fireball;
7
8 struct fireball_data
9 {
10         double timer;
11         int vx;
12         int vy;
13 };
14
15 static void fireball_spawn(struct entity *self, void *data)
16 {
17         self->meta = malloc(sizeof(struct fireball_data));
18         *((struct fireball_data *) self->meta) = *((struct fireball_data *) data);
19
20         self->color.r = clamp(self->color.r + rand() % 65 - 32, 0, 255);
21         self->color.g = clamp(self->color.g + rand() % 65 - 32, 0, 255);
22         self->color.b = clamp(self->color.b + rand() % 65 - 32, 0, 255);
23 }
24
25 static void fireball_step(struct entity *self, struct entity_step_data stepdata)
26 {
27         struct fireball_data *data = self->meta;
28
29         if (stepdata.visible && (data->timer -= stepdata.dtime) <= 0.0) {
30                 data->timer = 0.1;
31                 move(self, data->vx, data->vy);
32         }
33 }
34
35 static void fireball_collide(struct entity *self, int x, int y)
36 {
37         (void) x, y;
38
39         self->remove = true;
40 }
41
42 static void fireball_collide_with_entity(struct entity *self, struct entity *other)
43 {
44         add_health(other, -(1 + rand() % 3));
45         self->remove = true;
46 }
47
48 static void shoot_fireball()
49 {
50         int vx, vy;
51         vx = vy = 0;
52
53         dir_to_xy(last_player_move, &vx, &vy);
54
55         spawn(fireball, player.x + vx, player.y + vy, & (struct fireball_data) {
56                 .timer = 0.1,
57                 .vx = vx,
58                 .vy = vy,
59         });
60 }
61
62 __attribute__((constructor)) static void init()
63 {
64         fireball = (struct entity) {
65                 .name = "fireball",
66                 .x = 0,
67                 .y = 0,
68                 .color = get_color("#FF6611"),
69                 .texture = "⬤ ",
70                 .remove = false,
71                 .meta = NULL,
72                 .health = 1,
73                 .max_health = 1,
74                 .collide_with_entities = true,
75
76                 .on_step = &fireball_step,
77                 .on_collide = &fireball_collide,
78                 .on_collide_with_entity = &fireball_collide_with_entity,
79                 .on_spawn = &fireball_spawn,
80                 .on_remove = NULL,
81                 .on_death = NULL,
82                 .on_damage = NULL,
83         };
84
85         register_input_handler(' ', (struct input_handler) {
86                 .run_if_dead = false,
87                 .callback = &shoot_fireball,
88         });
89 }