3 #include "../game/game.h"
5 static struct entity fireball;
14 static void fireball_spawn(struct entity *self, void *data)
16 self->meta = malloc(sizeof(struct fireball_data));
17 *((struct fireball_data *) self->meta) = *((struct fireball_data *) data);
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);
24 static void fireball_step(struct entity *self, struct entity_step_data stepdata)
26 struct fireball_data *data = self->meta;
28 if (stepdata.visible && (data->timer -= stepdata.dtime) <= 0.0) {
30 move(self, data->vx, data->vy);
34 static void fireball_collide(struct entity *self, int x, int y)
41 static void fireball_collide_with_entity(struct entity *self, struct entity *other)
43 add_health(other, -(1 + rand() % 3));
47 static bool try_shoot(int x, int y, int vx, int vy)
52 return spawn(fireball, x, y, & (struct fireball_data) {
59 static void shoot_fireball()
66 for (int tries = 10; tries > 0; tries--) {
71 dir_to_xy(rand() % 4, &vx, &vy);
73 if (try_shoot(x, y, vx, vy))
78 __attribute__((constructor)) static void init()
80 fireball = (struct entity) {
84 .color = get_color("#FF6611"),
90 .collide_with_entities = true,
92 .on_step = &fireball_step,
93 .on_collide = &fireball_collide,
94 .on_collide_with_entity = &fireball_collide_with_entity,
95 .on_spawn = &fireball_spawn,
101 register_input_handler(' ', (struct input_handler) {
102 .run_if_dead = false,
103 .callback = &shoot_fireball,