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);
20 static void fireball_step(struct entity *self, struct entity_step_data stepdata)
22 struct fireball_data *data = self->meta;
24 if (stepdata.visible && (data->timer -= stepdata.dtime) <= 0.0) {
26 move(self, data->vx, data->vy);
30 static void fireball_collide(struct entity *self, int x, int y)
37 static void fireball_collide_with_entity(struct entity *self, struct entity *other)
39 add_health(other, -(1 + rand() % 3));
43 static bool try_shoot(int x, int y, int vx, int vy)
48 return spawn(fireball, x, y, & (struct fireball_data) {
55 static void shoot_fireball()
62 for (int tries = 10; tries > 0; tries--) {
67 dir_to_xy(rand() % 4, &vx, &vy);
69 if (try_shoot(x, y, vx, vy))
74 __attribute__((constructor)) static void init()
76 fireball = (struct entity) {
80 .color = get_color("#FF6611"),
86 .collide_with_entities = true,
88 .on_step = &fireball_step,
89 .on_collide = &fireball_collide,
90 .on_collide_with_entity = &fireball_collide_with_entity,
91 .on_spawn = &fireball_spawn,
97 register_input_handler(' ', (struct input_handler) {
99 .callback = &shoot_fireball,