Shoot fireballs into last movement direction

master
Elias Fleckenstein 2021-06-09 20:26:57 +02:00
parent 98e09a9f51
commit 149848dbfa
6 changed files with 61 additions and 44 deletions

View File

@ -1 +1,2 @@
game
movement

View 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;
return spawn(fireball, x, y, & (struct fireball_data) {
dir_to_xy(last_player_move, &vx, &vy);
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) {

View 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)++;
case UP:
(*y)--;
break;
case 1:
(*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);

View 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);

View 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()

View File

@ -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