]> git.lizzy.rs Git - dungeon_game.git/commitdiff
Shoot fireballs into last movement direction
authorElias Fleckenstein <eliasfleckenstein@web.de>
Wed, 9 Jun 2021 18:26:57 +0000 (20:26 +0200)
committerElias Fleckenstein <eliasfleckenstein@web.de>
Wed, 9 Jun 2021 18:26:57 +0000 (20:26 +0200)
plugins/fireball/dependencies.txt
plugins/fireball/fireball.c
plugins/game/game.c
plugins/game/game.h
plugins/movement/movement.c
plugins/movement/movement.h [new file with mode: 0644]

index dc22e61c94aa75648e8d87ca4ebb5ad089bcc1ad..facbb959c3c9557b0e3c91b8153cdc918fdc603b 100644 (file)
@@ -1 +1,2 @@
 game
+movement
index 3da29a47d9e025a8efed27dbdbc27a74be46e147..f2e84799d2967637b5512ade10b78cd35e4eace1 100644 (file)
@@ -1,6 +1,7 @@
 #include <stdlib.h>
 #include <stddef.h>
 #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) {
index 6f91ca2dc3cbe3d559c13bffc4d26ae8c1bafda3..4ba7d524700e770ff2d62b34ca582eec176e082a 100644 (file)
@@ -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);
index 6131402d65ec0d31147a546747d0136dedf81a15..d8555d2292580d0ee48625cfd866c420ca312673 100644 (file)
@@ -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);
 
index 494c8b0a47d965f7b144d477ad8f83dfad15733a..25c5f939c35907a7e0bc29f7aaaa02c9c199eded 100644 (file)
@@ -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 (file)
index 0000000..c3afdac
--- /dev/null
@@ -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