-
-
Save rbleattler/69e0f2d7e904845c62b5fc48c6b52996 to your computer and use it in GitHub Desktop.
Procedural Generation in GDScript https://web.archive.org/web/20250212131706/https://chikorito.land/articles/2024-02-25-procgen-godot
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# world.gd | |
extends Node2D | |
class_name World | |
const TILE_SIZE = 32 | |
const SEA_LEVEL = 0.0 | |
const RENDER_DISTANCE = 16.0 | |
@export var altitude_noise: FastNoiseLite | |
@export var tile: PackedScene | |
func _ready(): | |
for n in RENDER_DISTANCE: | |
# We divide by two so that half the tiles | |
# generate left/above center and half right/below | |
var x = n - RENDER_DISTANCE / 2.0 | |
for m in RENDER_DISTANCE: | |
var y = m - RENDER_DISTANCE / 2.0 | |
generate_terrain_tile(x, y) | |
func generate_terrain_tile(x: int, y: int): | |
var tile = tile.instantiate() | |
tile.tile_type = altitude_value(x, y) | |
tile.position = Vector2(x, y) * TILE_SIZE | |
add_child(tile) | |
func altitude_value(x: int, y: int) -> Tile.TileType: | |
var value = altitude_noise.get_noise_2d(x, y) | |
if value >= SEA_LEVEL: | |
return Tile.TileType.LAND | |
return Tile.TileType.SEA |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment