diff options
Diffstat (limited to 'plugins/fireball/fireball.c')
-rw-r--r-- | plugins/fireball/fireball.c | 95 |
1 files changed, 55 insertions, 40 deletions
diff --git a/plugins/fireball/fireball.c b/plugins/fireball/fireball.c index 3da29a4..39e93fb 100644 --- a/plugins/fireball/fireball.c +++ b/plugins/fireball/fireball.c @@ -1,8 +1,8 @@ #include <stdlib.h> #include <stddef.h> #include "../game/game.h" - -static struct entity fireball; +#include "../movement/movement.h" +#include "../inventory/inventory.h" struct fireball_data { @@ -40,66 +40,81 @@ static void fireball_collide(struct entity *self, int x, int y) static void fireball_collide_with_entity(struct entity *self, struct entity *other) { - add_health(other, -(1 + rand() % 3)); + add_health(other, -(3 + rand() % 3)); self->remove = true; } -static bool try_shoot(int x, int y, int vx, int vy) +static struct entity fireball_entity = { + .name = "fireball", + .x = 0, + .y = 0, + .color = {0}, + .use_color = true, + .texture = "⬤ ", + .remove = false, + .meta = NULL, + .health = 1, + .max_health = 1, + .collide_with_entities = true, + + .on_step = &fireball_step, + .on_collide = &fireball_collide, + .on_collide_with_entity = &fireball_collide_with_entity, + .on_spawn = &fireball_spawn, + .on_remove = NULL, + .on_death = NULL, + .on_damage = NULL, +}; + +static void shoot_fireball() { - x += vx; - y += vy; + int vx, vy; + vx = vy = 0; - return spawn(fireball, x, y, & (struct fireball_data) { + dir_to_xy(last_player_move, &vx, &vy); + + spawn(fireball_entity, player.x + vx, player.y + vy, & (struct fireball_data) { .timer = 0.1, .vx = vx, .vy = vy, }); } -static void shoot_fireball() +static bool shoot_fireball_item(struct itemstack *stack) { - int x, y; + (void) stack; - x = player.x; - y = player.y; - - for (int tries = 10; tries > 0; tries--) { - int vx, vy; + shoot_fireball(); + return true; +} - vx = vy = 0; +static struct item fireball_item = { + .name = "Fireball", + .stackable = true, - dir_to_xy(rand() % 4, &vx, &vy); + .on_use = &shoot_fireball_item, + .on_destroy = NULL, + .on_create = NULL, +}; - if (try_shoot(x, y, vx, vy)) - return; - } +static void shoot_if_has_fireball() +{ + if (inventory_remove(&player_inventory, &fireball_item)) + shoot_fireball(); } __attribute__((constructor)) static void init() { - fireball = (struct entity) { - .name = "fireball", - .x = 0, - .y = 0, - .color = get_color("#FF6611"), - .texture = "⬤ ", - .remove = false, - .meta = NULL, - .health = 1, - .max_health = 1, - .collide_with_entities = true, - - .on_step = &fireball_step, - .on_collide = &fireball_collide, - .on_collide_with_entity = &fireball_collide_with_entity, - .on_spawn = &fireball_spawn, - .on_remove = NULL, - .on_death = NULL, - .on_damage = NULL, - }; + fireball_entity.color = get_color("#FF6611"); register_input_handler(' ', (struct input_handler) { .run_if_dead = false, - .callback = &shoot_fireball, + .callback = &shoot_if_has_fireball, + }); + + inventory_add(&player_inventory, (struct itemstack) { + .item = &fireball_item, + .count = 7, + .meta = NULL, }); } |