can no longer go outside the grid, widened collision box
This commit is contained in:
@@ -3,8 +3,8 @@ package main
|
||||
import "core:math"
|
||||
import "vendor:raylib"
|
||||
|
||||
PLAYER_SPRINT_SPEED :: 2.5
|
||||
PLAYER_SPEED :: 1.5
|
||||
PLAYER_SPRINT_SPEED :: 80
|
||||
PLAYER_SPEED :: 50
|
||||
PLAYER_WIDTH :: 32
|
||||
PLAYER_HEIGHT :: 32
|
||||
PLAYER_SPRITE_PATH :: "assets/player/player.png"
|
||||
@@ -49,42 +49,42 @@ handle_player_camera :: proc(p: ^Player, delta: f32) {
|
||||
@(private = "file")
|
||||
handle_player_input :: proc(p: ^Player, delta: f32) {
|
||||
dir: raylib.Vector2 = {0, 0}
|
||||
|
||||
if raylib.IsKeyDown(.W) do dir.y -= 1
|
||||
if raylib.IsKeyDown(.S) do dir.y += 1
|
||||
if raylib.IsKeyDown(.A) do dir.x -= 1
|
||||
if raylib.IsKeyDown(.D) do dir.x += 1
|
||||
|
||||
is_moving := dir.x != 0 || dir.y != 0
|
||||
|
||||
if is_moving {
|
||||
if dir.x != 0 || dir.y != 0 {
|
||||
is_sprinting := raylib.IsKeyDown(.LEFT_SHIFT)
|
||||
dir = raylib.Vector2Normalize(dir)
|
||||
|
||||
speed: f32 = is_sprinting ? PLAYER_SPRINT_SPEED : PLAYER_SPEED
|
||||
speed_val := f32(is_sprinting ? PLAYER_SPRINT_SPEED : PLAYER_SPEED)
|
||||
p.animator.anim.fps = is_sprinting ? 11 : 6
|
||||
|
||||
velocity := dir * speed
|
||||
velocity := dir * speed_val * delta
|
||||
|
||||
new_pos_x := p.position
|
||||
new_pos_x.x += velocity.x
|
||||
foot_y := p.position.y + f32(p.sprite.height)
|
||||
box_half_w: f32 = 4.0
|
||||
center_offset_x := f32(p.sprite.width) * 0.5
|
||||
|
||||
foot_offset_y := f32(p.sprite.height)
|
||||
foot_offset_x := f32(p.sprite.width) * 0.5
|
||||
|
||||
if !is_wall_at({new_pos_x.x + foot_offset_x, new_pos_x.y + foot_offset_y}) {
|
||||
p.position.x = new_pos_x.x
|
||||
next_x := p.position.x + velocity.x
|
||||
if !is_wall_at({next_x + center_offset_x - box_half_w, foot_y}) &&
|
||||
!is_wall_at({next_x + center_offset_x + box_half_w, foot_y}) {
|
||||
p.position.x = next_x
|
||||
}
|
||||
|
||||
new_pos_y := p.position
|
||||
new_pos_y.y += velocity.y
|
||||
if !is_wall_at({p.position.x + foot_offset_x, new_pos_y.y + foot_offset_y}) {
|
||||
p.position.y = new_pos_y.y
|
||||
next_y := p.position.y + velocity.y
|
||||
if !is_wall_at(
|
||||
{p.position.x + center_offset_x - box_half_w, next_y + f32(p.sprite.height)},
|
||||
) &&
|
||||
!is_wall_at(
|
||||
{p.position.x + center_offset_x + box_half_w, next_y + f32(p.sprite.height)},
|
||||
) {
|
||||
p.position.y = next_y
|
||||
}
|
||||
|
||||
if dir.x < 0 do p.facing_left = true
|
||||
if dir.x > 0 do p.facing_left = false
|
||||
|
||||
p.state = .WALKING
|
||||
} else {
|
||||
p.state = .IDLE
|
||||
@@ -142,8 +142,11 @@ is_wall_at :: proc(world_pos: raylib.Vector2) -> bool {
|
||||
gx := int(math.floor(world_pos.x / tile_size))
|
||||
gy := int(math.floor(world_pos.y / tile_size))
|
||||
|
||||
tile := get_tile(&ground_layer_grid, gx, gy)
|
||||
if gx < 0 || gx >= ground_layer_grid.width || gy < 0 || gy >= ground_layer_grid.height {
|
||||
return true
|
||||
}
|
||||
|
||||
tile := get_tile(&ground_layer_grid, gx, gy)
|
||||
return tile != nil && tile.type == .WALL
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user