interference/src/player.c

156 lines
3.5 KiB
C
Raw Normal View History

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
2021-12-23 19:31:40 +01:00
static int k_up, k_down, k_left, k_right, k_jump, k_jumpdown;
2021-12-22 16:18:06 +01:00
static struct Player player;
static void player_get_input(void);
2021-12-23 19:31:40 +01:00
static void player_move(struct Vec2 spd);
static struct Vec2 player_update_rem();
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};
2021-12-23 19:31:40 +01:00
player.rem = (struct FVec2){0.0f, 0.0f};
player.jumping = 1;
player.airbreak = 1;
2021-12-22 16:18:06 +01:00
}
void
player_update(void)
{
player_get_input();
2021-12-23 19:31:40 +01:00
const int dir = k_right - k_left;
2021-12-22 16:18:06 +01:00
2021-12-23 19:31:40 +01:00
if (player.jumping && (!k_jumpdown || player.spd.y > 0)) {
player.airbreak = 1;
}
if (k_jump && !player.jumping) {
player.spd.y = -PLAYER_JUMP;
player.jumping = 1;
player.airbreak = 0;
}
player.spd.x *= (1 - PLAYER_FRIC);
player.spd.x += dir * PLAYER_ACC;
player.spd.y += GRAVITY + GRAVITY * player.airbreak;
player_move(player_update_rem());
if (player_collide((struct Vec2){player.pos.x, player.pos.y + 1}, 1,
0)) {
player.jumping = 0;
}
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);
2021-12-23 19:31:40 +01:00
k_jumpdown = input_down(K_A);
2021-12-22 16:18:06 +01:00
}
2021-12-23 19:31:40 +01:00
static struct Vec2
player_update_rem(void)
2021-12-23 10:00:55 +01:00
{
2021-12-23 19:31:40 +01:00
struct FVec2 spdrem = (struct FVec2){player.spd.x + player.rem.x,
player.spd.y + player.rem.y};
struct Vec2 ispd = (struct Vec2){spdrem.x, spdrem.y};
2021-12-23 10:00:55 +01:00
2021-12-23 19:31:40 +01:00
player.rem.x = spdrem.x - (float)ispd.x;
player.rem.y = spdrem.y - (float)ispd.y;
return ispd;
}
2021-12-23 10:00:55 +01:00
2021-12-23 19:31:40 +01:00
static void
player_move(struct Vec2 spd)
{
if (player_collide(player.pos, 1, 0)) {
2021-12-23 10:00:55 +01:00
return;
}
2021-12-23 19:31:40 +01:00
player_update_rem();
if (spd.x) {
const int sign_x = sign(spd.x);
player.pos.x += spd.x;
if (player_collide(player.pos, 1, 0)) {
player.spd.x = 0.0f;
player.rem.x = 0.0f;
while (player_collide(player.pos, 1, 0)) {
player.pos.x -= sign_x;
}
2021-12-23 10:00:55 +01:00
}
}
2021-12-23 19:31:40 +01:00
if (spd.y) {
const int sign_y = sign(spd.y);
player.pos.y += spd.y;
if (player_collide(player.pos, 1, 0)) {
player.spd.y = 0.0f;
player.rem.y = 0.0f;
while (player_collide(player.pos, 1, 0)) {
player.pos.y -= sign_y;
}
2021-12-23 10:00:55 +01:00
}
}
}
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
}