why are the tiles still 24bytes...
This commit is contained in:
@@ -3,10 +3,13 @@ package game
|
||||
import rl "vendor:raylib"
|
||||
import "core:fmt"
|
||||
|
||||
CHUNK_UNLOAD_DISTANCE :: 3
|
||||
|
||||
Player :: struct {
|
||||
position : rl.Vector2,
|
||||
move_timer: f32,
|
||||
mode: InteractMode
|
||||
mode: InteractMode,
|
||||
camera: rl.Camera2D
|
||||
}
|
||||
|
||||
InteractMode :: enum {
|
||||
@@ -14,8 +17,46 @@ InteractMode :: enum {
|
||||
ATTACK,
|
||||
}
|
||||
|
||||
handle_player_input :: proc(p : ^Player, w: ^World) {
|
||||
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()
|
||||
@@ -31,33 +72,41 @@ handle_player_input :: proc(p : ^Player, w: ^World) {
|
||||
if p.move_timer <= 0 {
|
||||
if rl.IsKeyDown(.D) {
|
||||
target_pos.x += 1
|
||||
if !will_collide(target_pos, w) {
|
||||
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(target_pos, w) {
|
||||
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(target_pos, w) {
|
||||
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(target_pos, w) {
|
||||
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))
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -74,12 +123,13 @@ draw_player :: proc(player:^Player) {
|
||||
draw_tile({27,0}, player.position, rl.DARKBLUE)
|
||||
}
|
||||
|
||||
will_collide :: proc(pos:rl.Vector2, w:^World) -> bool {
|
||||
if pos.y > WORLD_SIZE * CELL_SIZE || pos.x > WORLD_SIZE * CELL_SIZE {
|
||||
return false
|
||||
}
|
||||
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)
|
||||
|
||||
tile := get_grid_tile(w, vec2_to_vec2i(pos))
|
||||
chunk := get_chunk(w, chunk_pos)
|
||||
tile := get_chunk_tile(chunk, local_pos)
|
||||
|
||||
#partial switch tile.type {
|
||||
case .WALL:
|
||||
|
||||
Reference in New Issue
Block a user