diff --git a/src/entities/player.c b/src/entities/player.c index e8945bb..0c46894 100644 --- a/src/entities/player.c +++ b/src/entities/player.c @@ -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; } @@ -44,10 +44,16 @@ 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) { @@ -55,22 +61,24 @@ void update_player_input(w_player *player, w_keyboard *keyboard) { 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); @@ -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)) { @@ -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; } } diff --git a/src/entities/player.h b/src/entities/player.h index 0ad5c46..08839f6 100644 --- a/src/entities/player.h +++ b/src/entities/player.h @@ -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 } @@ -32,6 +40,8 @@ typedef struct w_player { Vector2 position; Vector2 velocity; + bool on_ground; + bool is_jumping; w_playerstate state; } w_player; diff --git a/src/screen/game.c b/src/screen/game.c index c860a1f..e05c63a 100644 --- a/src/screen/game.c +++ b/src/screen/game.c @@ -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(); diff --git a/src/stdafx.h b/src/stdafx.h index 6c69fbb..a54a63a 100644 --- a/src/stdafx.h +++ b/src/stdafx.h @@ -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