summaryrefslogtreecommitdiffstats
path: root/World.gd
blob: 6ea8452bf6ec5bdf7687d8589f72aafb2ed0d213 (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
60
61
62
63
64
65
66
67
68
extends RigidBody

export var acceleration = 10.0
export var generation_distance = 500.0
export var generation_subdivision = 5.0

var gen_coords = Vector2.ZERO
const init_distance = 10

onready var Rock = preload("res://Rock.tscn")
onready var noise = OpenSimplexNoise.new()

func _on_WorldGenCull_body_exited(body:Node):
    body.queue_free()

func make_rock(xform:Transform):
    var rock = Rock.instance()
    rock.transform = xform
    self.add_child(rock)

func generate_location(coords:Vector2):
    if coords.x < -init_distance || coords.x > init_distance ||  coords.y < -init_distance || coords.y > init_distance:
        if noise.get_noise_2d(coords.x,coords.y) > 0.6:
            coords *= generation_subdivision
            make_rock(Transform(Basis(), Vector3(coords.x,0.0,coords.y)))

func generate_world():
    var new_coords = -(self.transform.origin / generation_subdivision).floor()
    new_coords = Vector2(new_coords.x,new_coords.z)
    var direction = new_coords - gen_coords
    var gen_max = generation_distance / generation_subdivision
    var gen_min = -gen_max
    if gen_coords.x != new_coords.x:
        for i in range(new_coords.y+gen_min,new_coords.y+gen_max):
            generate_location(Vector2(new_coords.x+(gen_min if direction.x < 0  else gen_max),i))
    if gen_coords.y != new_coords.y:
        for i in range(new_coords.x+gen_min,new_coords.x+gen_max):
            generate_location(Vector2(i,new_coords.y+(gen_min if direction.y < 0 else gen_max)))
    gen_coords = new_coords

func _ready():
    randomize()
    noise.seed = randi()
    noise.octaves = 4
    noise.period = 16
    noise.persistence = 0.001
    noise.lacunarity = 2.0

    var gen_max = generation_distance / generation_subdivision
    var gen_min = -gen_max
    for i in range(gen_min,gen_max):
        for j in range(gen_min,gen_max):
            generate_location(Vector2(i,j))

func travel(direction:Vector3):
    var boat = $"/root/Main/ShipRigidBody"
    var boat_direction = boat.global_transform.basis
    boat_direction = Basis(Vector3.UP*boat_direction.get_euler().y)
    direction = -direction
    direction = boat_direction * direction
    direction = direction.normalized()
    self.add_central_force(direction*acceleration)

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