2021-12-22 16:18:06 +01:00
|
|
|
#include "player.h"
|
|
|
|
#include "conf.h"
|
|
|
|
#include "draw.h"
|
|
|
|
#include "input.h"
|
2021-12-23 10:00:55 +01:00
|
|
|
#include "tools.h"
|
2021-12-22 16:18:06 +01:00
|
|
|
|
|
|
|
static int k_up, k_down, k_left, k_right, k_jump;
|
|
|
|
static struct Player player;
|
|
|
|
static void player_get_input(void);
|
2021-12-23 10:00:55 +01:00
|
|
|
static void player_move(struct Vec2 mov);
|
|
|
|
static int player_collide(void);
|
2021-12-22 16:18:06 +01:00
|
|
|
|
|
|
|
void
|
|
|
|
player_init(void)
|
|
|
|
{
|
|
|
|
player.pos = (struct Vec2){0, 0};
|
|
|
|
player.spd = (struct FVec2){0.0f, 0.0f};
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
player_update(void)
|
|
|
|
{
|
|
|
|
player_get_input();
|
|
|
|
|
|
|
|
const struct Vec2 mov = (struct Vec2){k_right - k_left, k_down - k_up};
|
2021-12-23 10:00:55 +01:00
|
|
|
player_move(mov);
|
2021-12-22 16:18:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
player_draw(void)
|
|
|
|
{
|
|
|
|
draw_rectangle(player.pos.x, player.pos.y, PLAYER_S, PLAYER_S, C_BLACK);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
player_get_input(void)
|
|
|
|
{
|
|
|
|
k_up = input_down(K_UP);
|
|
|
|
k_down = input_down(K_DOWN);
|
|
|
|
k_left = input_down(K_LEFT);
|
|
|
|
k_right = input_down(K_RIGHT);
|
|
|
|
k_jump = input_pressed(K_A);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2021-12-23 10:00:55 +01:00
|
|
|
player_move(struct Vec2 mov)
|
|
|
|
{
|
|
|
|
if (player_collide()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
player.spd.x *= (1 - PLAYER_FRIC);
|
|
|
|
player.spd.x += mov.x * PLAYER_ACC;
|
|
|
|
|
|
|
|
player.spd.y *= (1 - PLAYER_FRIC);
|
|
|
|
player.spd.y += mov.y * PLAYER_ACC;
|
|
|
|
|
|
|
|
if (!player.spd.x && !player.spd.y) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const int sign_x = sign(player.spd.x);
|
|
|
|
const int Ispd_x = player.spd.x;
|
|
|
|
player.pos.x += Ispd_x;
|
|
|
|
if (player_collide()) {
|
|
|
|
player.spd.x = 0.0f;
|
|
|
|
while (player_collide()) {
|
|
|
|
player.pos.x -= sign_x;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const int sign_y = sign(player.spd.y);
|
|
|
|
const int Ispd_y = player.spd.y;
|
|
|
|
player.pos.y += Ispd_y;
|
|
|
|
if (player_collide()) {
|
|
|
|
player.spd.y = 0.0f;
|
|
|
|
while (player_collide()) {
|
|
|
|
player.pos.y -= sign_y;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
player_collide(void)
|
2021-12-22 16:18:06 +01:00
|
|
|
{
|
2021-12-23 10:00:55 +01:00
|
|
|
/* Je ne fais qu'acte de présence, rien de plus */
|
|
|
|
return 0;
|
2021-12-22 16:18:06 +01:00
|
|
|
}
|