ChunkSystem.gd 1.9 KB

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