write a terraria like 2d map generation by random noise lite in godot 4
- User
write a terraria like 2d map generation by random noise lite in godot 4
- Assistant
write a terraria like 2d map generation by random noise lite in godot 4
Sure! Below is an example of how you can create a simple Terraria-like 2D map generation using random noise in Godot 4. This script generates terrain with varying heights and includes basic cave generation.
Firstly, ensure you have a TileSet resource with your tiles (e.g., ground tile, background tile). For this example, we'll assume you have a TileSet named TileSet.tres with at least one tile (tile ID 0 for ground).
Here's the GDScript code:
extends Node2D
# Map dimensions
var map_width: int = 512 # Width of the map in tiles
var map_height: int = 256 # Height of the map in tiles
# Noise scales
var terrain_noise_scale: float = 0.05 # Scale for terrain height noise
var cave_noise_scale: float = 0.1 # Scale for cave noise
# Noise instances
var terrain_noise: OpenSimplexNoise
var cave_noise: OpenSimplexNoise
# TileMap for rendering tiles
var tilemap: TileMap
func _ready():
# Initialize the TileMap
tilemap = TileMap.new()
tilemap.tile_set = preload("res://TileSet.tres") # Replace with your TileSet path
add_child(tilemap)
# Initialize terrain noise
terrain_noise = OpenSimplexNoise.new()
terrain_noise.seed = randi()
terrain_noise.octaves = 4
terrain_noise.period = 200.0
terrain_noise.persistence = 0.5
# Initialize cave noise
cave_noise = OpenSimplexNoise.new()
cave_noise.seed = randi()
cave_noise.octaves = 3
cave_noise.period = 100.0
cave_noise.persistence = 0.5
# Generate the map
generate_map()
func generate_map():
# Baseline ground level (e.g., half the map height)
var ground_level: int = map_height / 2
for x in range(map_width):
# Get terrain height using noise
var noise_value: float = terrain_noise.get_noise_1d(x * terrain_noise_scale)
var terrain_height: int = ground_level + int(noise_value * 20) # Adjust 20 for height variation
for y in range(map_height):
if y >= terrain_height:
# Determine if we should carve out a cave at this position
var cave_value: float = cave_noise.get_noise_2d(x * cave_noise_scale, y * cave_noise_scale)
if cave_value > -0.2:
# Place a ground tile (tile ID 0)
tilemap.set_cell(0, Vector2i(x, y), 0)
else:
# Leave empty for cave (air)
pass
else:
# Above ground; optionally place background tiles or leave empty
pass
Explanation:
Map Dimensions: We define map_width and map_height to set the size of our map in tiles.
Noise Initialization:
terrain_noise is used to generate the varying heights of the terrain.cave_noise is used to create caves within the terrain.Map Generation Logic:
x (column) in the map.terrain_height using terrain_noise.get_noise_1d(). This gives us a floating-point value between -1 and 1, which we scale to get reasonable terrain heights.y (row) for the current x.
y is greater than or equal to terrain_height, we're at or below the terrain's surface.
cave_noise.get_noise_2d() to decide whether to carve out a cave.cave_value is greater than -0.2, we place a ground tile. Otherwise, we leave it empty to represent a cave.y is above terrain_height, we leave it empty (air). You can place background tiles here if desired.Tile Placement:
tilemap.set_cell(0, Vector2i(x, y), 0) to set a tile at layer 0, position (x, y), with tile ID 0 (ground tile).Notes:
TileSet Resource: Replace "res://TileSet.tres" with the actual path to your TileSet resource. Ensure your TileSet has the necessary tiles.
Adjustments:
terrain_noise_scale, cave_noise_scale, period, and persistence to get different terrain and cave formations.int(noise_value * 20) affects the amplitude of terrain height variation.if cave_value > -0.2 controls how frequent and large the caves are.Visualization:
Example TileSet Setup:
TileSet resource in Godot.0 for ground).TileSet as TileSet.tres in your project directory.This script provides a basic foundation for generating a Terraria-like map. You can expand upon it by adding more tile types (ores, different soil types), refining cave generation, adding background layers, and implementing additional features like placing trees, water bodies, or biomes.
Feel free to modify and enhance the script to suit your game's needs!
Not yet included in a published release.