|
@@ -0,0 +1,49 @@
|
|
|
+extends Spatial
|
|
|
+
|
|
|
+var Chunk = preload("res://chunk.tscn")
|
|
|
+onready var Player = $"../Player"
|
|
|
+
|
|
|
+var resolution = 512
|
|
|
+var radius = 1737.4
|
|
|
+var renderDistance = 1
|
|
|
+
|
|
|
+var chunkIndex = {}
|
|
|
+
|
|
|
+# Called when the node enters the scene tree for the first time.
|
|
|
+func _ready():
|
|
|
+ pass # Replace with function body.
|
|
|
+
|
|
|
+
|
|
|
+# Called every frame. 'delta' is the elapsed time since the previous frame.
|
|
|
+func _process(delta):
|
|
|
+ var playerSpos = Player.getSpos()
|
|
|
+ var playerRoundSpos = playerSpos.round()
|
|
|
+ var playerChunkPos = Vector2(int(playerRoundSpos.x), int(playerRoundSpos.y))
|
|
|
+ var playerInterChunkPos = Vector2(playerSpos.x - playerChunkPos.x, playerSpos.y - playerChunkPos.y)
|
|
|
+
|
|
|
+ for i in [Vector2(1,0),Vector2(1,1),Vector2(0,1),Vector2(-1,0),Vector2(0,-1),Vector2(-1,-1),Vector2(1,-1),Vector2(-1,1)]:
|
|
|
+ var dist = Vector2(playerSpos.x, playerSpos.y).distance_to(playerChunkPos+i+Vector2(0.5,0.5))
|
|
|
+ if dist < renderDistance:
|
|
|
+ add_chunk(playerChunkPos+i)
|
|
|
+
|
|
|
+
|
|
|
+func add_chunk(chunkPos = Vector2()):
|
|
|
+ var lat = int(chunkPos.x)
|
|
|
+ var lon = int(chunkPos.y)
|
|
|
+ if chunkIndex.has(lon) and chunkIndex[lon].has(lat):
|
|
|
+ return
|
|
|
+ var chunk = Chunk.instance()
|
|
|
+ chunk.setParams(lat,lat+1,lon,lon+1,resolution,radius)
|
|
|
+ #moontile.scale=Vector3(1000,1000,1000)
|
|
|
+ .add_child(chunk)
|
|
|
+ if not chunkIndex.has(lon):
|
|
|
+ chunkIndex[lon]={}
|
|
|
+ chunkIndex[lon][lat] = chunk
|
|
|
+
|
|
|
+func remove_chunk(chunkPos = Vector2()):
|
|
|
+ var lat = int(chunkPos.x)
|
|
|
+ var lon = int(chunkPos.y)
|
|
|
+ if chunkIndex.has(lon) and chunkIndex[lon].has(lat):
|
|
|
+ chunkIndex[lon][lat].queue_free()
|
|
|
+ chunkIndex[lon].delete(lat)
|
|
|
+
|