You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
Gridmap/scripts/WorldGeneration.gd

68 lines
1.6 KiB

extends Control
@export var world_width = 512
@export var world_height = 512
func _ready():
Global.world.width = world_width
Global.world.height = world_height
Global.world.heightMap = generate_heightmap(Global.world.width, Global.world.height)
Global.world.blocs = set_blocs(Global.world.width, Global.world.height, Global.world.heightMap)
get_tree().change_scene_to_file("scenes/Game.tscn")
func generate_heightmap(width, height):
var heightmap = []
randomize()
var noise = FastNoiseLite.new()
noise.seed = randi()
noise.frequency = 0.005
# var wavelength = 2.5
# var terraces = 20
# var elevation = noise.get_noise_2d(x / wavelength, y / wavelength)
var image = noise.get_seamless_image(width, height)
for x in width:
heightmap.append([])
for y in width:
var terraces = 20
var elevation = image.get_pixel(x, y).r
elevation = remap(elevation, 0, 1, -1, 1)
# var border = border_width + rng.randf_range(-20.0, 20.0)
# var elevation = noise.get_noise_2d(x / wavelength, y / wavelength)
#
# var nx = 2 * x / width - 1
# var ny = 2 * y / height - 1
elevation = max(elevation, -1)
#
if elevation > 0.1:
elevation = max(pow((elevation) * 1.2, 1.5), 0.1)
elevation = max(elevation, 0)
## elevation = min(elevation, 1)
#
elevation = (elevation * terraces)
#
## print(elevation)
heightmap[x].append(floor(elevation))
return heightmap
func set_blocs(width, height, heightmap):
var blocs = []
for x in width:
blocs.append([])
for y in height:
var bloc = {
"type": 0,
"entity": -1
}
if heightmap[x][y] > 0:
bloc.type = 1
blocs[x].append(bloc)
return blocs