From 149848dbfa136f828b09253f402de59c00a5a1cf Mon Sep 17 00:00:00 2001 From: Elias Fleckenstein Date: Wed, 9 Jun 2021 20:26:57 +0200 Subject: [PATCH] Shoot fireballs into last movement direction --- plugins/fireball/dependencies.txt | 1 + plugins/fireball/fireball.c | 30 +++++++------------------- plugins/game/game.c | 35 +++++++++++++++++-------------- plugins/game/game.h | 10 ++++++++- plugins/movement/movement.c | 21 +++++++++++++++---- plugins/movement/movement.h | 8 +++++++ 6 files changed, 61 insertions(+), 44 deletions(-) create mode 100644 plugins/movement/movement.h diff --git a/plugins/fireball/dependencies.txt b/plugins/fireball/dependencies.txt index dc22e61..facbb95 100644 --- a/plugins/fireball/dependencies.txt +++ b/plugins/fireball/dependencies.txt @@ -1 +1,2 @@ game +movement diff --git a/plugins/fireball/fireball.c b/plugins/fireball/fireball.c index 3da29a4..f2e8479 100644 --- a/plugins/fireball/fireball.c +++ b/plugins/fireball/fireball.c @@ -1,6 +1,7 @@ #include #include #include "../game/game.h" +#include "../movement/movement.h" static struct entity fireball; @@ -44,37 +45,20 @@ static void fireball_collide_with_entity(struct entity *self, struct entity *oth self->remove = true; } -static bool try_shoot(int x, int y, int vx, int vy) +static void shoot_fireball() { - x += vx; - y += vy; + int vx, vy; + vx = vy = 0; + + dir_to_xy(last_player_move, &vx, &vy); - return spawn(fireball, x, y, & (struct fireball_data) { + spawn(fireball, player.x + vx, player.y + vy, & (struct fireball_data) { .timer = 0.1, .vx = vx, .vy = vy, }); } -static void shoot_fireball() -{ - int x, y; - - x = player.x; - y = player.y; - - for (int tries = 10; tries > 0; tries--) { - int vx, vy; - - vx = vy = 0; - - dir_to_xy(rand() % 4, &vx, &vy); - - if (try_shoot(x, y, vx, vy)) - return; - } -} - __attribute__((constructor)) static void init() { fireball = (struct entity) { diff --git a/plugins/game/game.c b/plugins/game/game.c index 6f91ca2..4ba7d52 100644 --- a/plugins/game/game.c +++ b/plugins/game/game.c @@ -172,20 +172,20 @@ void register_input_handler(unsigned char c, struct input_handler handler) input_handlers[c] = buf; } -void dir_to_xy(int dir, int *x, int *y) +void dir_to_xy(enum direction dir, int *x, int *y) { switch (dir) { - case 0: - (*x)++; - break; - case 1: - (*y)++; + case UP: + (*y)--; break; - case 2: + case LEFT: (*x)--; break; - case 3: - (*y)--; + case DOWN: + (*y)++; + break; + case RIGHT: + (*x)++; break; } } @@ -209,15 +209,15 @@ static void player_damage(struct entity *self, int damage) /* Mapgen */ -static bool check_direction(int x, int y, int dir) +static bool check_direction(int x, int y, enum direction dir) { if (dir % 2 == 0) - return is_solid(x, y + 1) && is_solid(x, y - 1) && (is_solid(x + 1, y) || rand() % 3 > 1) && (is_solid(x - 1, y) || rand() % 3 > 1); - else return is_solid(x + 1, y) && is_solid(x - 1, y) && (is_solid(x, y + 1) || rand() % 3 > 1) && (is_solid(x, y - 1) || rand() % 3 > 1); + else + return is_solid(x, y + 1) && is_solid(x, y - 1) && (is_solid(x + 1, y) || rand() % 3 > 1) && (is_solid(x - 1, y) || rand() % 3 > 1); } -static void generate_corridor(int lx, int ly, int ldir, bool off) +static void generate_corridor(int lx, int ly, enum direction ldir, bool off) { if (is_outside(lx, ly)) return; @@ -237,8 +237,11 @@ static void generate_corridor(int lx, int ly, int ldir, bool off) } } - int x, y, dir; - int ret = (ldir + 2) % 4; + int x, y; + + enum direction dir; + enum direction ret = (ldir + 2) % 4; + int limit = 50; do { @@ -262,7 +265,7 @@ static void generate_corridor(int lx, int ly, int ldir, bool off) static void generate_corridor_random(int x, int y) { - int dir = rand() % 4; + enum direction dir = rand() % 4; generate_corridor(x, y, dir, false); generate_corridor(x, y, (dir + 2) % 4, false); diff --git a/plugins/game/game.h b/plugins/game/game.h index 6131402..d8555d2 100644 --- a/plugins/game/game.h +++ b/plugins/game/game.h @@ -73,6 +73,14 @@ struct input_handler void (*callback)(); }; +enum direction +{ + UP, + LEFT, + DOWN, + RIGHT, +}; + extern int score; extern struct color black; @@ -107,7 +115,7 @@ void light_color(struct color *color, double light); void mix_color(struct color *color, struct color other, double ratio); void register_air_function(struct generator_function func); void register_input_handler(unsigned char c, struct input_handler handler); -void dir_to_xy(int dir, int *x, int *y); +void dir_to_xy(enum direction dir, int *x, int *y); int clamp(int v, int max, int min); struct list *add_element(struct list *list, void *element); diff --git a/plugins/movement/movement.c b/plugins/movement/movement.c index 494c8b0..25c5f93 100644 --- a/plugins/movement/movement.c +++ b/plugins/movement/movement.c @@ -1,23 +1,36 @@ #include "../game/game.h" +enum direction last_player_move; + +void move_player(enum direction dir) +{ + int x, y; + x = y = 0; + + dir_to_xy(dir, &x, &y); + last_player_move = dir; + + move(&player, x, y); +} + static void move_up() { - move(&player, 0, -1); + move_player(UP); } static void move_left() { - move(&player, -1, 0); + move_player(LEFT); } static void move_down() { - move(&player, 0, 1); + move_player(DOWN); } static void move_right() { - move(&player, 1, 0); + move_player(RIGHT); } __attribute__((constructor)) static void init() diff --git a/plugins/movement/movement.h b/plugins/movement/movement.h new file mode 100644 index 0000000..c3afdac --- /dev/null +++ b/plugins/movement/movement.h @@ -0,0 +1,8 @@ +#ifndef _MOVEMENT_H_ +#define _MOVEMENT_H_ +#include "../game/game.h" + +extern enum direction last_player_move; +void move_player(enum direction dir); + +#endif -- 2.44.0