Skip to content

Commit

Permalink
Merge pull request #3 from jules1univ/main
Browse files Browse the repository at this point in the history
update player jump
  • Loading branch information
julesgrc0 authored Feb 13, 2024
2 parents 9850c8b + a0b03b8 commit 48c1740
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 24 deletions.
38 changes: 21 additions & 17 deletions src/entities/player.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ void animate_player(w_player *player, bool should_walk) {
player->animation += PHYSICS_TICK;

if (should_walk || abs(player->velocity.x) > 0) {
if (player->animation > PHYSICS_TICK * 4) {
if (player->animation > PHYSICS_TICK * PLAYER_ANIMATION_WALK) {
player->animation = 0;
player->state = (player->state == P_WALK_1) ? P_WALK_2 : P_WALK_1;
}
} else {
if (player->animation > PHYSICS_TICK * 24) {
if (player->animation > PHYSICS_TICK * PLAYER_ANIMATION_IDLE) {
player->animation = 0;
player->state = (player->state == P_IDLE_1) ? P_IDLE_2 : P_IDLE_1;
}
Expand All @@ -44,33 +44,41 @@ void animate_player(w_player *player, bool should_walk) {
void update_player_input(w_player *player, w_keyboard *keyboard) {
if (player->delay > 0) {
player->delay--;
player->is_jumping = player->delay > 0;

player->velocity.y -=
(MAX_PLAYER_VELOCITY_Y * player->delay) / (PHYSICS_TICK / 2);
}
if (keyboard->jump && player->delay <= 0) {
player->velocity.y -= PLAYER_JUMP;
player->delay = (1 / PHYSICS_TICK);
if (keyboard->jump && !player->is_jumping && player->on_ground &&
player->delay <= 0) {
player->delay = (1 / PHYSICS_TICK) / 3;
player->is_jumping = true;
player->on_ground = false;
}

if (keyboard->left) {

if (player->src.width > 0) {
player->src.width = -player->src.width;
}
player->velocity.x -= 1;

player->velocity.x -= MAX_PLAYER_VELOCITY_X;
} else if (keyboard->right) {

if (player->src.width < 0) {
player->src.width = -player->src.width;
}
player->velocity.x += 1;
player->velocity.x += MAX_PLAYER_VELOCITY_X;
}
}

void update_player_velocity(w_player *player) {
player->velocity.y += 1 / 2.f;
if (!player->is_jumping) {
player->velocity.y += MAX_PLAYER_VELOCITY_Y / 2;
}

player->velocity.x =
Clamp(player->velocity.x, -MAX_PLAYER_VELOCITY_X, MAX_PLAYER_VELOCITY_X);

player->velocity.y =
Clamp(player->velocity.y, -PLAYER_JUMP, MAX_PLAYER_VELOCITY_Y);

Expand Down Expand Up @@ -112,8 +120,6 @@ void check_player_collision_vel(w_player *player, w_chunkview *view) {
.height = player->dst.height};

bool col_x = false;
bool col_y = false;

for (size_t i = 0; i < view->textures_len; i++) {
Rectangle block = view->blocks[i].dst;
if (!col_x && CheckCollisionRecs(block, next_velx)) {
Expand All @@ -124,19 +130,17 @@ void check_player_collision_vel(w_player *player, w_chunkview *view) {
if (adjust_x > player->position.x) {
player->position.x = adjust_x;
}

if (col_y) {
break;
}
}

if (!col_y && CheckCollisionRecs(block, next_vely)) {
if (CheckCollisionRecs(block, next_vely)) {

if (!player->is_jumping) {
player->on_ground = true;
}
player->velocity.y = 0;

float adjust_y = GetCollisionRec(block, next_vely).y - player->dst.height;
if (adjust_y > player->position.y) {

player->position.y = adjust_y;
}
}
Expand Down
18 changes: 14 additions & 4 deletions src/entities/player.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,21 @@
#include "../stdafx.h"
#include "../terrain/chunk_view.h"

#define PLAYER_SPEED 1000.f
#define PLAYER_JUMP 10.f
#define PLAYER_ANIMTATION_TIME(x) ((x * (1 / PHYSICS_TICK)) / 30)
#define PLAYER_ANIMATION_IDLE PLAYER_ANIMTATION_TIME(12)
#define PLAYER_ANIMATION_WALK PLAYER_ANIMTATION_TIME(2)

// 30 12
// 120

#define PLAYER_SPEED 250.f
#define PLAYER_JUMP 3.f
#define PLAYER_FRICTION 0.8f
#define MAX_PLAYER_VELOCITY_X 1.f

#define MAX_PLAYER_VELOCITY_X 2.f
#define MAX_PLAYER_VELOCITY_Y 5.f
#define MIN_PLAYER_VELOCITY 0.1f
#define MAX_OVERLAP_LEN 12

#define PLAYER_SRC_RECT \
(Rectangle) { 0, 0, 8, 16 }

Expand All @@ -32,6 +40,8 @@ typedef struct w_player {
Vector2 position;
Vector2 velocity;

bool on_ground;
bool is_jumping;
w_playerstate state;
} w_player;

Expand Down
9 changes: 7 additions & 2 deletions src/screen/game.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,14 @@ void game_screen(w_state *state) {
td->force_update = true;
}

EndMode2D();
DrawTexturePro(player_textures[td->player->state], td->player->src,
td->player->dst, VEC_ZERO, 0, WHITE);
(Rectangle){.x = td->player->position.x,
.y = td->player->position.y,
.width = td->player->dst.width,
.height = td->player->dst.height},
VEC_ZERO, 0, WHITE);

EndMode2D();
EndTextureMode();

BeginDrawing();
Expand Down
2 changes: 1 addition & 1 deletion src/stdafx.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
if (x) \
free(x);

#define PHYSICS_TICK (1.0f / 60.0f)
#define PHYSICS_TICK (1.0f / 120.0f)

#define RENDER_CUBE_COUNT 20
#define RENDER_CUBE_GAP 2
Expand Down

0 comments on commit 48c1740

Please sign in to comment.