41 lines
1.1 KiB
GDScript3
41 lines
1.1 KiB
GDScript3
|
class_name WorldGrid
|
||
|
extends GridMap
|
||
|
|
||
|
@export var moisture_noise: Noise
|
||
|
@export var temperature_noise: Noise
|
||
|
@export var altitude_noise: Noise
|
||
|
@export var grass: PackedScene
|
||
|
|
||
|
var width := 60
|
||
|
var depth := 60
|
||
|
var current_pos := Vector3i(0, 0, 0)
|
||
|
var old_pos := Vector3i(NAN, NAN, NAN)
|
||
|
|
||
|
func _process(_delta):
|
||
|
current_pos = local_to_map(Global.player.position)
|
||
|
|
||
|
if current_pos != old_pos:
|
||
|
generate_chunk()
|
||
|
old_pos = current_pos
|
||
|
|
||
|
func generate_chunk():
|
||
|
for x in range(-width, width):
|
||
|
for z in range(-depth, depth):
|
||
|
var tile_x := current_pos.x + x
|
||
|
var tile_z := current_pos.z + z
|
||
|
var pos := Vector3i(tile_x, 0, tile_z)
|
||
|
|
||
|
if get_cell_item(pos) != -1:
|
||
|
continue
|
||
|
|
||
|
var moisture := moisture_noise.get_noise_2d(tile_x, tile_z)
|
||
|
# var temperature = temperature_noise.get_noise_2d(tile_x, tile_z)
|
||
|
# var altitude = altitude_noise.get_noise_2d(tile_x, tile_z)
|
||
|
|
||
|
var index := floori((moisture + 1.0) * 2.0)
|
||
|
set_cell_item(pos, index)
|
||
|
|
||
|
if index == 1:
|
||
|
var node := grass.instantiate() as Node3D
|
||
|
node.position = (pos * 2) + Vector3i(1, 0, 1)
|
||
|
add_child(node)
|