change chunk render distance based on zoom level

This commit is contained in:
Antti Hakkarainen 2023-02-13 16:53:44 +02:00
parent 0109934933
commit 675b451723

View file

@ -11,24 +11,25 @@ extends Node2D
var chunks:Dictionary = {} var chunks:Dictionary = {}
var unready_chunks:Dictionary = {} var unready_chunks:Dictionary = {}
var chunk_amount = 16
var window_width = DisplayServer.window_get_size(0).x var window_width = DisplayServer.window_get_size(0).x
var distance = abs((window_width/(Globals.CHUNK_SIZE.x*Globals.TILE_SIZE_X)) / 2 +1 ) var distance = abs((window_width/(Globals.CHUNK_SIZE.x*Globals.TILE_SIZE_X)) / 2 +1 )
func _init() -> void: func _init() -> void:
self.name = "ChunkHandler" self.name = "ChunkHandler"
func _process(_delta): func _process(_delta):
update_chunks() update_chunks()
clean_up_chunks() clean_up_chunks()
reset_chunks() reset_chunks()
func _ready(): func _ready():
#thread = Thread.new() #thread = Thread.new()
#print(distance) #print(distance)
pass pass
func add_chunk(x:int, y:int) -> void: func add_chunk(x:int, y:int) -> void:
var key = str(y) + "," + str(x) var key = str(y) + "," + str(x)
@ -37,22 +38,34 @@ func add_chunk(x:int, y:int) -> void:
load_chunk(x, y, key) load_chunk(x, y, key)
func clean_up_chunks(): func clean_up_chunks():
for key in chunks: for key in chunks:
var chunk = chunks[key] var chunk = chunks[key]
if chunk.should_remove: if chunk.should_remove:
chunk.queue_free() chunk.queue_free()
chunks.erase(key) chunks.erase(key)
func clear_chunk(pos:Vector2i) -> void: func clear_chunk(pos:Vector2i) -> void:
self.chunks[pos.y][pos.x].clear() self.chunks[pos.y][pos.x].clear()
func correction_factor(distance) -> float:
if Globals.CAMERA_ZOOM_LEVEL < 0.6:
return distance * 2.0
elif Globals.CAMERA_ZOOM_LEVEL > 1.0:
return distance
else:
return distance * ( 1 + 2 * (1-Globals.CAMERA_ZOOM_LEVEL) )
func get_chunk(x:int, y:int): func get_chunk(x:int, y:int):
var key = str(y) + "," + str(x) var key = str(y) + "," + str(x)
if self.chunks.has(key): if self.chunks.has(key):
return chunks.get(key) return chunks.get(key)
return null return null
func load_chunk(x:int, y:int, key:String): func load_chunk(x:int, y:int, key:String):
var chunk = Chunk.new(x,y) var chunk = Chunk.new(x,y)
@ -65,14 +78,19 @@ func load_chunk(x:int, y:int, key:String):
func reset_chunks(): func reset_chunks():
for key in chunks: for key in chunks:
chunks[key].should_remove = true chunks[key].should_remove = true
func update_chunks(): func update_chunks():
var p_x = int(Globals.CAMERA_POSITION.x- Globals.CHUNK_SIZE.x) / Globals.TILE_SIZE_X / Globals.CHUNK_SIZE.x var p_x = int(Globals.CAMERA_POSITION.x- Globals.CHUNK_SIZE.x) / Globals.TILE_SIZE_X / Globals.CHUNK_SIZE.x
var p_y = int(Globals.CAMERA_POSITION.y- Globals.CHUNK_SIZE.y) / Globals.TILE_SIZE_Y / Globals.CHUNK_SIZE.y var p_y = int(Globals.CAMERA_POSITION.y- Globals.CHUNK_SIZE.y) / Globals.TILE_SIZE_Y / Globals.CHUNK_SIZE.y
# When updating chunks, adjust chunk rendering distance
# based on current zoom level.
var zoom_corrected = correction_factor(distance)
for y in Globals.map_size/Globals.CHUNK_SIZE.y: for y in Globals.map_size/Globals.CHUNK_SIZE.y:
for x in Globals.map_size/Globals.CHUNK_SIZE.x: for x in Globals.map_size/Globals.CHUNK_SIZE.x:
if (abs(x - p_x) <= distance && abs(y - p_y) <= distance): if (abs(x - p_x) <= zoom_corrected && abs(y - p_y) <= zoom_corrected):
add_chunk(x, y) add_chunk(x, y)
var chunk = get_chunk(x,y) var chunk = get_chunk(x,y)