summaryrefslogblamecommitdiffstats
path: root/World.gd
blob: 97f0a0952959ee08c70bfb788cfbec41e8a2a269 (plain) (tree)
1
2
3
4
5
6
7
8
9
10
               
 

                                          
                              
 
                                                                              
 

                                                               
 


                                                   
 
                                                    
                     

                               

                                                     
                          
                                   
                                      
                                                       
 
                              
                   

                                                     
                                          


















                                                                                       
extends Spatial

export(NodePath) var traveler = null
onready var _traveler = get_node(traveler)
export var acceleration = 10.0

onready var chunk_render_distance = $"ChunkRenderDistance/Radius".shape.radius

onready var chunk_half_size = $"Chunks/Chunk/Size".shape.radius
onready var chunk_size = chunk_half_size * 2.0

func _ready():
    ChunkLoader.world = self
    ChunkGen.chunk_half_size = self.chunk_half_size

func _on_ChunkRenderDistance_area_exited(area:Area):
    area.queue_free()

func travel(direction:Vector3):
    var heading = _traveler.global_transform.basis
    heading = Basis(Vector3.UP*heading.get_euler().y)
    direction = -direction
    direction = heading * direction
    direction = direction.normalized()
    $"Chunks".add_central_force(direction*acceleration)

func _physics_process(_state):
    chunk_process()
    var stick = Input.get_axis("ship_down","ship_up")
    if stick != 0.0:
        self.travel(Vector3.FORWARD*stick)

func v2_coords(coords:Vector3):
    return Vector2(coords.x,coords.z)

func chunk_coords(coords:Vector2):
    return coords.snapped(Vector2(chunk_size,chunk_size))

func chunk_process():
    var gen_center = -v2_coords($"Chunks".transform.origin)
    var gen_radius = chunk_render_distance
    var gen_radius_rounded = stepify(gen_radius, chunk_size)
    for x in range(-gen_radius_rounded, gen_radius_rounded+chunk_size, chunk_size):
        for y in range(-gen_radius_rounded, gen_radius_rounded+chunk_size, chunk_size):
            var gen_coords = Vector2(x,y)
            if gen_coords.length() > gen_radius:
                continue
            var coords = chunk_coords(gen_coords + gen_center)
            ChunkLoader.add_chunk(coords)
    ChunkLoader.chunk_update()