nyooom
This commit is contained in:
parent
c0dcb5b226
commit
da77b7d1ea
|
@ -4,7 +4,9 @@
|
|||
struct Player {
|
||||
struct Vec2 pos;
|
||||
struct FVec2 spd;
|
||||
struct FVec2 rem;
|
||||
struct Vec2 spawn;
|
||||
int jumping, airbreak;
|
||||
};
|
||||
|
||||
void player_init(void);
|
||||
|
|
88
src/player.c
88
src/player.c
|
@ -5,10 +5,11 @@
|
|||
#include "level.h"
|
||||
#include "tools.h"
|
||||
|
||||
static int k_up, k_down, k_left, k_right, k_jump;
|
||||
static int k_up, k_down, k_left, k_right, k_jump, k_jumpdown;
|
||||
static struct Player player;
|
||||
static void player_get_input(void);
|
||||
static void player_move(struct Vec2 mov);
|
||||
static void player_move(struct Vec2 spd);
|
||||
static struct Vec2 player_update_rem();
|
||||
static int player_collide_pixel(int x, int y, tile_t id);
|
||||
static int player_collide(struct Vec2 pos, tile_t id, int h);
|
||||
|
||||
|
@ -17,15 +18,37 @@ player_init(void)
|
|||
{
|
||||
player.pos = (struct Vec2){160, 100};
|
||||
player.spd = (struct FVec2){0.0f, 0.0f};
|
||||
player.rem = (struct FVec2){0.0f, 0.0f};
|
||||
player.jumping = 1;
|
||||
player.airbreak = 1;
|
||||
}
|
||||
|
||||
void
|
||||
player_update(void)
|
||||
{
|
||||
player_get_input();
|
||||
const int dir = k_right - k_left;
|
||||
|
||||
const struct Vec2 mov = (struct Vec2){k_right - k_left, k_down - k_up};
|
||||
player_move(mov);
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
|
@ -43,42 +66,51 @@ player_get_input(void)
|
|||
k_left = input_down(K_LEFT);
|
||||
k_right = input_down(K_RIGHT);
|
||||
k_jump = input_pressed(K_A);
|
||||
k_jumpdown = input_down(K_A);
|
||||
}
|
||||
|
||||
static struct Vec2
|
||||
player_update_rem(void)
|
||||
{
|
||||
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};
|
||||
|
||||
player.rem.x = spdrem.x - (float)ispd.x;
|
||||
player.rem.y = spdrem.y - (float)ispd.y;
|
||||
return ispd;
|
||||
}
|
||||
|
||||
static void
|
||||
player_move(struct Vec2 mov)
|
||||
player_move(struct Vec2 spd)
|
||||
{
|
||||
if (player_collide(player.pos, 1, 0)) {
|
||||
return;
|
||||
}
|
||||
|
||||
player.spd.x *= (1 - PLAYER_FRIC);
|
||||
player.spd.x += mov.x * PLAYER_ACC;
|
||||
player_update_rem();
|
||||
|
||||
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.pos, 1, 0)) {
|
||||
player.spd.x = 0.0f;
|
||||
while (player_collide(player.pos, 1, 0)) {
|
||||
player.pos.x -= sign_x;
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const int sign_y = sign(player.spd.y);
|
||||
const int Ispd_y = player.spd.y;
|
||||
player.pos.y += Ispd_y;
|
||||
if (player_collide(player.pos, 1, 0)) {
|
||||
player.spd.y = 0.0f;
|
||||
while (player_collide(player.pos, 1, 0)) {
|
||||
player.pos.y -= sign_y;
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue