summaryrefslogtreecommitdiffstats
path: root/World.gd
blob: 6aba10360a7f5ea05565c92ddc765cdff664032f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
extends Spatial

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

onready var Chunk = preload("res://Chunk.tscn")
onready var chunk_size = $"WorldGen/Chunk/Size".shape.radius*2.0
var chunks = {}

func v2_coords(xform:Transform):
    var coords = xform.origin
    coords = Vector2(coords.x,coords.z)
    return coords

func chunk_coords(coords:Vector2):
    coords = (coords / chunk_size).floor()
    return coords

func world_coords(coords:Vector2):
    coords *= chunk_size
    return Vector3(coords.x,0.0,coords.y)

func gen_chunk(xform:Transform):
    var chunk = Chunk.instance()
    chunk.transform = xform
    $"WorldGen".add_child(chunk)
    chunks[chunk_coords(v2_coords(xform))] = chunk

func gen_chunks():
    var gen_center = -v2_coords($"WorldGen".transform)
    var gen_radius = $"WorldGenCull/Radius".shape.radius-chunk_size
    for x in range(-gen_radius, gen_radius, chunk_size):
        for y in range(-gen_radius, gen_radius, chunk_size):
            var gen_coords = Vector2(x,y)
            var coords = chunk_coords(gen_coords + gen_center)
            if gen_coords.length() <= gen_radius && !chunks.has(coords):
                gen_chunk(Transform(Basis(), world_coords(coords)))

func _on_WorldGenCull_area_exited(area:Area):
    gen_chunks()
    area.queue_free()
    chunks.erase(chunk_coords(v2_coords(area.transform)))

func _ready():
    gen_chunks()

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()
    $"WorldGen".add_central_force(direction*acceleration)

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