34 lines
1.1 KiB
C
34 lines
1.1 KiB
C
#include "player_controller.h"
|
|
#include "physics.h"
|
|
|
|
void initialize_player(PlayerState *player) {
|
|
player->position = (Vector3){0};
|
|
player->velocity = (Vector3){0};
|
|
player->acceleration = (Vector3){0};
|
|
player->yaw = 0.0f;
|
|
player->pitch = 0.0f;
|
|
player->isGrounded = false;
|
|
player->isSprinting = false;
|
|
player->isSliding = false;
|
|
}
|
|
|
|
void update_player_movement(PlayerState *player, InputState *input, float delta) {
|
|
Vector3 wishDir = calculate_wish_direction(input, player->yaw);
|
|
float wishSpeed = player->isSprinting ? PLAYER_SPRINT_SPEED : PLAYER_WALK_SPEED;
|
|
|
|
if (player->isGrounded) {
|
|
if (input->jump) {
|
|
player->velocity.y = PLAYER_JUMP_VELOCITY;
|
|
player->isGrounded = false;
|
|
}
|
|
player->velocity = ground_accelerate(player->velocity, wishDir, wishSpeed, delta);
|
|
if (input->crouch) player->isSliding = true;
|
|
} else {
|
|
player->velocity = air_accelerate(player->velocity, wishDir, wishSpeed, delta);
|
|
}
|
|
|
|
player->velocity = apply_friction(player->velocity, delta, player->isGrounded);
|
|
player->velocity = apply_gravity(player->velocity, delta);
|
|
player->position = Vector3Add(player->position, Vector3Scale(player->velocity, delta));
|
|
}
|