Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | edwardjoe214 |
extends Node3D
var dungeon_size = Vector2(30, 30)
var cell_size = 2.0
var wall_material = preload(“res://materials/Wall.tres”)
var floor_material = preload(“res://materials/Floor.tres”)
var door_material = preload(“res://materials/Door.tres”)
var floor_mesh_data
var wall_mesh_data
func generate_dungeon(size, fill_probability, smoothing_passes, smoothing_threshold):
var dungeon =
var count
var nx
var ny
for x in range(size):
dungeon.append()
for y in range(size):
dungeon.append(randf() < fill_probability)
for i in range(smoothing_passes):
for x in range(size):
for y in range(size):
count = 0
for dx in range(-1, 2):
for dy in range(-1, 2):
nx = x + dx
ny = y + dy
if nx >= 0 and nx < size and ny >= 0 and ny < size:
count += int(dungeon[nx][ny])
dungeon[x][y] = count >= smoothing_threshold
return dungeon
func smooth_dungeon(dungeon):
var new_dungeon =
for x in range(dungeon_size.x):
new_dungeon.append()
for y in range(dungeon_size.y):
var wall_count = 0
for dx in range(-1, 2):
for dy in range(-1, 2):
var nx = x + dx
var ny = y + dy
if nx >= 0 and nx < dungeon_size.x and ny >= 0 and ny < dungeon_size.y and not dungeon[nx][ny]:
wall_count += 1
if wall_count > 4:
new_dungeon.append(0)
else:
new_dungeon.append(1)
return new_dungeon
func create_dungeon_meshes(dungeon):
for x in range(dungeon_size.x):
for y in range(dungeon_size.y):
if dungeon[y] == 0:
var wall_mesh = BoxMesh.new()
wall_mesh.scale = Vector3(cell_size, cell_size, cell_size)
var wall = MeshInstance3D.new()
wall.mesh = wall_mesh
wall.material = wall_material
wall.translation = Vector3(x * cell_size, 0, y * cell_size)
add_child(wall)
else:
var floor_mesh = PlaneMesh.new()
floor_mesh.scale = Vector3(cell_size, cell_size, cell_size)
var floor = MeshInstance3D.new()
floor.mesh = floor_mesh
floor.material = floor_material
floor.translation = Vector3(x * cell_size, -cell_size, y * cell_size)
add_child(floor)
if x > 0 and dungeon[x-1][y] == 0:
var door_mesh = BoxMesh.new()
door_mesh.scale = Vector3(cell_size, cell_size/2, 0.1)
var door = MeshInstance3D.new()
door.mesh = door_mesh
door.material = door_material
door.translation = Vector3(x * cell_size - cell_size/2, -cell_size/2, y * cell_size)
add_child(door)
if y > 0 and dungeon[x][y-1] == 0:
var door_mesh = BoxMesh.new()
door_mesh.scale = Vector3(0.1, cell_size/2, cell_size)
var door = MeshInstance3D.new()
door.mesh = door_mesh
door.material = door_material
door.translation = Vector3(x * cell_size, -cell_size/2, y * cell_size - cell_size/2)
add_child(door)
func _ready():
# Generate the dungeon map
var dungeon_map = generate_dungeon(50,0.45,2,5)
# Smooth out the dungeon map
dungeon_map = smooth_dungeon(dungeon_map)
# Create the floor
var floor_mesh = MeshInstance3D.new()
floor_mesh.mesh = floor_mesh_data # assuming you have loaded the mesh data elsewhere
var floor_material_copy = floor_material.duplicate()
floor_mesh.material_override = floor_material_copy
add_child(floor_mesh)
# Create the wall meshes
var wall_mesh = MeshInstance3D.new()
wall_mesh.mesh = wall_mesh_data # assuming you have loaded the mesh data elsewhere
var wall_material_copy = wall_material.duplicate()
wall_mesh.material_override = wall_material_copy
for x in range(len(dungeon_map)):
for y in range(len(dungeon_map[x])):
if dungeon_map[x][y]:
var wall_instance = wall_mesh.duplicate()
wall_instance.set_position(Vector3(x, 0, y))
add_child(wall_instance)
why the dungeon not appear ?, for material .tres is still untouched