]> git.lizzy.rs Git - dungeon_game.git/blob - plugins/fireball/fireball.c
fcb1ac19a2a62e26f4f6d238aa80b4ce826f33a1
[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 #include "../inventory/inventory.h"
6
7 struct fireball_data
8 {
9         double timer;
10         int vx;
11         int vy;
12 };
13
14 static void fireball_spawn(struct entity *self, void *data)
15 {
16         self->meta = malloc(sizeof(struct fireball_data));
17         *((struct fireball_data *) self->meta) = *((struct fireball_data *) data);
18
19         self->color.r = clamp(self->color.r + rand() % 65 - 32, 0, 255);
20         self->color.g = clamp(self->color.g + rand() % 65 - 32, 0, 255);
21         self->color.b = clamp(self->color.b + rand() % 65 - 32, 0, 255);
22 }
23
24 static void fireball_step(struct entity *self, struct entity_step_data stepdata)
25 {
26         struct fireball_data *data = self->meta;
27
28         if (stepdata.visible && (data->timer -= stepdata.dtime) <= 0.0) {
29                 data->timer = 0.1;
30                 move(self, data->vx, data->vy);
31         }
32 }
33
34 static void fireball_collide(struct entity *self, int x, int y)
35 {
36         (void) x, y;
37
38         self->remove = true;
39 }
40
41 static void fireball_collide_with_entity(struct entity *self, struct entity *other)
42 {
43         add_health(other, -(3 + rand() % 3));
44         self->remove = true;
45 }
46
47 static struct entity fireball_entity = {
48         .name = "fireball",
49         .x = 0,
50         .y = 0,
51         .color = {0},
52         .use_color = true,
53         .texture = "⬤ ",
54         .remove = false,
55         .meta = NULL,
56         .health = 1,
57         .max_health = 1,
58         .collide_with_entities = true,
59
60         .on_step = &fireball_step,
61         .on_collide = &fireball_collide,
62         .on_collide_with_entity = &fireball_collide_with_entity,
63         .on_spawn = &fireball_spawn,
64         .on_remove = NULL,
65         .on_death = NULL,
66         .on_damage = NULL,
67 };
68
69 static void shoot_fireball()
70 {
71         int vx, vy;
72         vx = vy = 0;
73
74         dir_to_xy(last_player_move, &vx, &vy);
75
76         spawn(fireball_entity, player.x + vx, player.y + vy, & (struct fireball_data) {
77                 .timer = 0.1,
78                 .vx = vx,
79                 .vy = vy,
80         });
81 }
82
83 static bool shoot_fireball_item(struct itemstack *stack)
84 {
85         (void) stack;
86
87         shoot_fireball();
88         return true;
89 }
90
91 static struct item fireball_item = {
92         .name = "Fireball",
93         .stackable = true,
94
95         .on_use = &shoot_fireball_item,
96         .on_destroy = NULL,
97 };
98
99 static void shoot_if_has_fireball()
100 {
101         if (inventory_remove(&player_inventory, &fireball_item))
102                 shoot_fireball();
103 }
104
105 __attribute__((constructor)) static void init()
106 {
107         fireball_entity.color = get_color("#FF6611");
108
109         register_input_handler(' ', (struct input_handler) {
110                 .run_if_dead = false,
111                 .callback = &shoot_if_has_fireball,
112         });
113
114         inventory_add(&player_inventory, (struct itemstack) {
115                 .item = &fireball_item,
116                 .count = 7,
117                 .meta = NULL,
118         });
119 }