141 lines
3.7 KiB
Odin
141 lines
3.7 KiB
Odin
package game
|
|
|
|
import rl "vendor:raylib"
|
|
import "core:fmt"
|
|
|
|
CHUNK_UNLOAD_DISTANCE :: 3
|
|
|
|
Player :: struct {
|
|
position : rl.Vector2,
|
|
move_timer: f32,
|
|
mode: InteractMode,
|
|
camera: rl.Camera2D
|
|
}
|
|
|
|
InteractMode :: enum {
|
|
INTERACT,
|
|
ATTACK,
|
|
}
|
|
|
|
handle_player_camera :: proc(p:^Player) {
|
|
p.camera.target = {p.position.x + (CELL_SIZE / 2), p.position.y + (CELL_SIZE / 2)}
|
|
|
|
if rl.IsWindowResized() {
|
|
p.camera.offset = {f32(rl.GetScreenWidth()) / 2, f32(rl.GetScreenHeight()) / 2}
|
|
}
|
|
}
|
|
|
|
player_update :: proc(p : ^Player, w: ^World) {
|
|
handle_player_input(p,w)
|
|
handle_player_camera(p)
|
|
|
|
|
|
}
|
|
|
|
load_nearby_chunks :: proc(w:^World, player_pos:rl.Vector2) {
|
|
player_chunk_pos := world_pos_to_chunk_pos(player_pos)
|
|
|
|
chunk_radius := 2 // Adjust based on the camera size
|
|
|
|
for x := -chunk_radius; x <= chunk_radius; x += 1 {
|
|
for y := -chunk_radius; y <= chunk_radius; y += 1 {
|
|
chunk_pos := Vec2i{player_chunk_pos.x + x, player_chunk_pos.y + y}
|
|
get_chunk(w, chunk_pos) // Ensures chunk is loaded or generated
|
|
}
|
|
}
|
|
}
|
|
|
|
unload_far_chunks :: proc(w: ^World, player_pos: Vec2i) {
|
|
for chunk_pos in w.chunks {
|
|
dist_x := abs(chunk_pos.x - player_pos.x)
|
|
dist_y := abs(chunk_pos.y - player_pos.y)
|
|
|
|
if dist_x > CHUNK_UNLOAD_DISTANCE || dist_y > CHUNK_UNLOAD_DISTANCE {
|
|
unload_chunk(chunk_pos, w)
|
|
}
|
|
}
|
|
}
|
|
|
|
handle_player_input :: proc(p:^Player, w:^World) {
|
|
target_pos := get_player_grid_position(p)
|
|
|
|
dt := rl.GetFrameTime()
|
|
move_delay : f32 = 0.15
|
|
|
|
if p.move_timer > 0 {
|
|
p.move_timer -= dt
|
|
}
|
|
|
|
|
|
// fmt.printfln("MOVING TO: %v : %v", target_pos, get_grid_tile(w, vec2_to_vec2i(target_pos)).type)
|
|
|
|
if p.move_timer <= 0 {
|
|
if rl.IsKeyDown(.D) {
|
|
target_pos.x += 1
|
|
if !will_collide(w, target_pos) {
|
|
player.position.x += CELL_SIZE
|
|
p.move_timer = move_delay
|
|
load_nearby_chunks(w, p.position)
|
|
unload_far_chunks(w, vec2_to_vec2i(p.position))
|
|
}
|
|
}
|
|
|
|
if rl.IsKeyDown(.A) {
|
|
target_pos.x -= 1
|
|
if !will_collide(w, target_pos) {
|
|
player.position.x -= CELL_SIZE
|
|
p.move_timer = move_delay
|
|
load_nearby_chunks(w, p.position)
|
|
unload_far_chunks(w, vec2_to_vec2i(p.position))
|
|
}
|
|
}
|
|
|
|
if rl.IsKeyDown(.W) {
|
|
target_pos.y -= 1
|
|
if !will_collide(w, target_pos) {
|
|
player.position.y -= CELL_SIZE
|
|
p.move_timer = move_delay
|
|
load_nearby_chunks(w, p.position)
|
|
unload_far_chunks(w, vec2_to_vec2i(p.position))
|
|
}
|
|
}
|
|
|
|
if rl.IsKeyDown(.S) {
|
|
target_pos.y += 1
|
|
if !will_collide(w, target_pos) {
|
|
p.move_timer = move_delay
|
|
player.position.y += CELL_SIZE
|
|
load_nearby_chunks(w, p.position)
|
|
unload_far_chunks(w, vec2_to_vec2i(p.position))
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
get_player_grid_position :: proc(player:^Player) -> rl.Vector2 {
|
|
grid_pos_x := player.position.x / CELL_SIZE
|
|
grid_pos_y := player.position.y / CELL_SIZE
|
|
|
|
return {grid_pos_x, grid_pos_y}
|
|
}
|
|
|
|
draw_player :: proc(player:^Player) {
|
|
draw_tile({27,0}, player.position, rl.DARKBLUE)
|
|
}
|
|
|
|
will_collide :: proc(w:^World, pos:rl.Vector2) -> bool {
|
|
world_grid_pos := vec2_to_vec2i(pos)
|
|
chunk_pos := world_pos_to_chunk_pos(pos)
|
|
local_pos := get_local_chunk_pos(world_grid_pos)
|
|
|
|
chunk := get_chunk(w, chunk_pos)
|
|
tile := get_chunk_tile(chunk, local_pos)
|
|
|
|
#partial switch tile.type {
|
|
case .WALL:
|
|
return true
|
|
}
|
|
|
|
return false
|
|
}
|