3 #include "../game/game.h"
4 #include "../movement/movement.h"
5 #include "../inventory/inventory.h"
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, -(3 + rand() % 3));
47 static struct entity fireball_entity = {
58 .collide_with_entities = true,
60 .on_step = &fireball_step,
61 .on_collide = &fireball_collide,
62 .on_collide_with_entity = &fireball_collide_with_entity,
63 .on_spawn = &fireball_spawn,
69 static void shoot_fireball()
74 dir_to_xy(last_player_move, &vx, &vy);
76 spawn(fireball_entity, player.x + vx, player.y + vy, & (struct fireball_data) {
83 static bool shoot_fireball_item(struct itemstack *stack)
91 static struct item fireball_item = {
95 .on_use = &shoot_fireball_item,
99 static void shoot_if_has_fireball()
101 if (inventory_remove(&player_inventory, &fireball_item))
105 __attribute__((constructor)) static void init()
107 fireball_entity.color = get_color("#FF6611");
109 register_input_handler(' ', (struct input_handler) {
110 .run_if_dead = false,
111 .callback = &shoot_if_has_fireball,
114 inventory_add(&player_inventory, (struct itemstack) {
115 .item = &fireball_item,