summaryrefslogtreecommitdiffstats
path: root/world/chunk/ChunkGen.gd
blob: 5e99be871b55a708d6fb5b9b691f40ace72bec8f (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
extends Node

var chunk_half_size setget _set_chunk_size
var chunk_size

func _set_chunk_size(val):
    chunk_half_size = val
    chunk_size = val * 2.0

onready var noise = OpenSimplexNoise.new()

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

func gen_chunk(chunk):
    var gen_tree = Spatial.new()
    gen_tree.name = "gen_tree"
    chunk.add_child(gen_tree)
    iterate_chunk(chunk, Vector2(10.0,10.0), funcref(self,"gen_rocks"))

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

func v3_coords(coords:Vector2):
    return Vector3(coords.x,0.0,coords.y)

func iterate_chunk(chunk,step:Vector2,cb:FuncRef):
    var chunk_size_rounded = Vector2(chunk_half_size,chunk_half_size).snapped(step)
    for x in range(-chunk_size_rounded.x,chunk_size_rounded.x,step.x):
        for y in range(-chunk_size_rounded.y,chunk_size_rounded.y,step.y):
            cb.call_func(chunk,Vector2(x,y))

onready var Rock = preload("res://world/obstacles/Rock.tscn")
const rock_size = 5.0
func make_rock(chunk,xform:Transform):
    var rock = Rock.instance()
    rock.transform = xform
    chunk.get_node("gen_tree").add_child(rock)

func gen_rocks(chunk,chunk_coords:Vector2):
    var world_coords = chunk_coords + v2_coords(chunk.transform.origin)
    var noise_val = self.noise.get_noise_2dv(world_coords)
    if noise_val > 0.4:
        make_rock(chunk,Transform(Basis().scaled(Vector3(2.0,2.0,2.0)),v3_coords(chunk_coords)))