#include <stdlib.h>
#include <stddef.h>
#include "../game/game.h"
+#include "../movement/movement.h"
static struct entity fireball;
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) {
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;
}
}
/* 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;
}
}
- 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 {
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);
void (*callback)();
};
+enum direction
+{
+ UP,
+ LEFT,
+ DOWN,
+ RIGHT,
+};
+
extern int score;
extern struct color black;
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);
#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()
--- /dev/null
+#ifndef _MOVEMENT_H_
+#define _MOVEMENT_H_
+#include "../game/game.h"
+
+extern enum direction last_player_move;
+void move_player(enum direction dir);
+
+#endif