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