ChunkSystem.gd 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. extends Spatial
  2. var Chunk = preload("res://chunk.tscn")
  3. onready var Player = $"../Player"
  4. var resolution = 512
  5. var radius = 1737.4
  6. var renderDistance = 1
  7. var chunkIndex = {}
  8. # Called when the node enters the scene tree for the first time.
  9. func _ready():
  10. pass # Replace with function body.
  11. # Called every frame. 'delta' is the elapsed time since the previous frame.
  12. func _process(delta):
  13. var playerSpos = Player.getSpos()
  14. var playerRoundSpos = playerSpos.round()
  15. var playerChunkPos = Vector2(int(playerRoundSpos.x), int(playerRoundSpos.y))
  16. var playerInterChunkPos = Vector2(playerSpos.x - playerChunkPos.x, playerSpos.y - playerChunkPos.y)
  17. 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)]:
  18. var dist = Vector2(playerSpos.x, playerSpos.y).distance_to(playerChunkPos+i+Vector2(0.5,0.5))
  19. if dist < renderDistance:
  20. add_chunk(playerChunkPos+i)
  21. func add_chunk(chunkPos = Vector2()):
  22. var lat = int(chunkPos.x)
  23. var lon = int(chunkPos.y)
  24. if chunkIndex.has(lon) and chunkIndex[lon].has(lat):
  25. return
  26. var chunk = Chunk.instance()
  27. chunk.setParams(lat,lat+1,lon,lon+1,resolution,radius)
  28. #moontile.scale=Vector3(1000,1000,1000)
  29. .add_child(chunk)
  30. if not chunkIndex.has(lon):
  31. chunkIndex[lon]={}
  32. chunkIndex[lon][lat] = chunk
  33. func remove_chunk(chunkPos = Vector2()):
  34. var lat = int(chunkPos.x)
  35. var lon = int(chunkPos.y)
  36. if chunkIndex.has(lon) and chunkIndex[lon].has(lat):
  37. chunkIndex[lon][lat].queue_free()
  38. chunkIndex[lon].delete(lat)