2021-12-22 16:18:06 +01:00
|
|
|
#include "player.h"
|
|
|
|
#include "conf.h"
|
|
|
|
#include "draw.h"
|
|
|
|
#include "input.h"
|
2021-12-23 17:43:44 +01:00
|
|
|
#include "level.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);
|
2021-12-23 17:43:44 +01:00
|
|
|
static int player_collide_pixel(int x, int y, tile_t id);
|
|
|
|
static int player_collide(struct Vec2 pos, tile_t id, int h);
|
2021-12-22 16:18:06 +01:00
|
|
|
|
|
|
|
void
|
|
|
|
player_init(void)
|
|
|
|
{
|
2021-12-23 17:43:44 +01:00
|
|
|
player.pos = (struct Vec2){160, 100};
|
2021-12-22 16:18:06 +01:00
|
|
|
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);
|
2021-12-23 17:43:44 +01:00
|
|
|
dprint(10, 10, C_BLACK, "%d", player_collide(player.pos, 1, 0));
|
2021-12-22 16:18:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
2021-12-23 17:43:44 +01:00
|
|
|
if (player_collide(player.pos, 1, 0)) {
|
2021-12-23 10:00:55 +01:00
|
|
|
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;
|
2021-12-23 17:43:44 +01:00
|
|
|
if (player_collide(player.pos, 1, 0)) {
|
2021-12-23 10:00:55 +01:00
|
|
|
player.spd.x = 0.0f;
|
2021-12-23 17:43:44 +01:00
|
|
|
while (player_collide(player.pos, 1, 0)) {
|
2021-12-23 10:00:55 +01:00
|
|
|
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;
|
2021-12-23 17:43:44 +01:00
|
|
|
if (player_collide(player.pos, 1, 0)) {
|
2021-12-23 10:00:55 +01:00
|
|
|
player.spd.y = 0.0f;
|
2021-12-23 17:43:44 +01:00
|
|
|
while (player_collide(player.pos, 1, 0)) {
|
2021-12-23 10:00:55 +01:00
|
|
|
player.pos.y -= sign_y;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2021-12-23 17:43:44 +01:00
|
|
|
player_collide_pixel(int x, int y, tile_t id)
|
2021-12-22 16:18:06 +01:00
|
|
|
{
|
2021-12-23 17:43:44 +01:00
|
|
|
const tile_t tile = level_get_px(x, y);
|
|
|
|
|
|
|
|
if (!tile) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (id == 1) {
|
|
|
|
return (tile - 1) % TILESET_W < 8;
|
|
|
|
}
|
|
|
|
|
|
|
|
return tile == id;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
player_collide(struct Vec2 pos, tile_t id, int h)
|
|
|
|
{
|
|
|
|
|
|
|
|
const struct Vec2 pos_tl = (struct Vec2){pos.x + h, pos.y + h};
|
|
|
|
const struct Vec2 pos_br =
|
|
|
|
(struct Vec2){pos.x + PLAYER_S - h - 1, pos.y + PLAYER_S - h - 1};
|
|
|
|
const struct Vec2 middle =
|
|
|
|
(struct Vec2){pos.x + PLAYER_S / 2, pos.y + PLAYER_S / 2};
|
|
|
|
|
|
|
|
if (player_collide_pixel(pos_tl.x, pos_tl.y, id) ||
|
|
|
|
player_collide_pixel(pos_br.x, pos_br.y, id) ||
|
|
|
|
player_collide_pixel(pos_tl.x, pos_br.y, id) ||
|
|
|
|
player_collide_pixel(pos_br.x, pos_tl.y, id) ||
|
|
|
|
player_collide_pixel(middle.x, pos_tl.y, id) ||
|
|
|
|
player_collide_pixel(middle.x, pos_br.y, id) ||
|
|
|
|
player_collide_pixel(pos_tl.x, middle.y, id) ||
|
|
|
|
player_collide_pixel(pos_br.x, middle.y, id)) {
|
|
|
|
return 1;
|
|
|
|
}
|
2021-12-23 10:00:55 +01:00
|
|
|
return 0;
|
2021-12-22 16:18:06 +01:00
|
|
|
}
|