Working map generator

This commit is contained in:
Layla 2024-01-21 00:42:57 +01:00
parent cd80ab3357
commit 405b374b9a
17 changed files with 240 additions and 38 deletions

View File

@ -0,0 +1,7 @@
[plugin]
name="Glitch Tools"
description=""
author="Layla Manley"
version=""
script="plugin.gd"

View File

@ -0,0 +1,19 @@
@tool
extends EditorPlugin
func _enter_tree() -> void:
# This doesn't function in Godot4
# get_editor_interface().get_editor_data().connect("scene_saved", self, "_on_scene_saved")
pass
func _exit_tree() -> void:
# get_editor_interface().get_editor_data().disconnect("scene_saved", self, "_on_scene_saved")
pass
func _on_scene_saved(scene: PackedScene) -> void:
var instance: Node = scene.instantiate()
instance.queue_free.call_deferred()
for node in instance.get_children():
if node is StandardRoom:
node._validate()

View File

@ -26,6 +26,10 @@ MusicPlayer="*res://nodes/MusicPlayer.tscn"
window/stretch/mode="2d"
window/stretch/aspect="keep_width"
[editor_plugins]
enabled=PackedStringArray("res://addons/glitch_tools/plugin.cfg")
[input]
ui_accept={

View File

@ -1,18 +1,16 @@
[gd_scene load_steps=5 format=3 uid="uid://c7cvc4y0kpl3g"]
[gd_scene load_steps=4 format=3 uid="uid://c7cvc4y0kpl3g"]
[ext_resource type="Script" path="res://scripts/v2/world.gd" id="1_cgytt"]
[ext_resource type="Script" path="res://scripts/v2/worldgen/standard_generator.gd" id="2_3jlht"]
[ext_resource type="Resource" uid="uid://din7qhqccs2nr" path="res://scenes/v2/worldgen/generator_instances/standard_gen.tres" id="2_ag01j"]
[ext_resource type="TileSet" uid="uid://cluemfplimly4" path="res://assets/tiles.tres" id="3_5mj08"]
[sub_resource type="Resource" id="Resource_kbdfk"]
script = ExtResource("2_3jlht")
parts = Array[PackedScene]([])
[node name="World" type="Node2D" node_paths=PackedStringArray("map")]
script = ExtResource("1_cgytt")
generator = SubResource("Resource_kbdfk")
generator = ExtResource("2_ag01j")
map = NodePath("TileMap")
[node name="TileMap" type="TileMap" parent="."]
tile_set = ExtResource("3_5mj08")
format = 2
[node name="Camera2D" type="Camera2D" parent="."]

View File

@ -0,0 +1,16 @@
[gd_resource type="Resource" script_class="StandardWorldGenerator" load_steps=9 format=3 uid="uid://din7qhqccs2nr"]
[ext_resource type="Script" path="res://scripts/v2/worldgen/standard_generator.gd" id="1_dw1bv"]
[ext_resource type="PackedScene" uid="uid://csuo1u5ickjbt" path="res://scenes/v2/worldgen/roomsets/standard/blocking.tscn" id="1_oci60"]
[ext_resource type="PackedScene" uid="uid://c5ipf40bxdqc6" path="res://scenes/v2/worldgen/roomsets/standard/hallway.tscn" id="2_t3otv"]
[ext_resource type="PackedScene" uid="uid://c8vs23y1tdhx8" path="res://scenes/v2/worldgen/roomsets/standard/medium_room_up.tscn" id="3_xlr3c"]
[ext_resource type="PackedScene" uid="uid://7jnspqbabihc" path="res://scenes/v2/worldgen/roomsets/standard/medium_room_up_down.tscn" id="4_tc4ie"]
[ext_resource type="PackedScene" uid="uid://bc5n3rb5t8ua7" path="res://scenes/v2/worldgen/roomsets/standard/small_room_down_right.tscn" id="5_ft2s4"]
[ext_resource type="PackedScene" uid="uid://t511rr478gom" path="res://scenes/v2/worldgen/roomsets/standard/small_fork.tscn" id="6_ttx05"]
[ext_resource type="PackedScene" uid="uid://3vfg1g7uhjp2" path="res://scenes/v2/worldgen/roomsets/standard/spawn_room.tscn" id="7_pb65x"]
[resource]
script = ExtResource("1_dw1bv")
rooms = Array[PackedScene]([ExtResource("1_oci60"), ExtResource("2_t3otv"), ExtResource("3_xlr3c"), ExtResource("4_tc4ie"), ExtResource("5_ft2s4"), ExtResource("6_ttx05")])
max_room_path_length = 10
spawn_room = ExtResource("7_pb65x")

View File

@ -1,7 +1,10 @@
[gd_scene load_steps=2 format=3 uid="uid://csuo1u5ickjbt"]
[gd_scene load_steps=3 format=3 uid="uid://csuo1u5ickjbt"]
[ext_resource type="Script" path="res://scripts/v2/worldgen/standard/room.gd" id="1_kd8r8"]
[ext_resource type="TileSet" uid="uid://cluemfplimly4" path="res://assets/tiles.tres" id="1_vckf1"]
[node name="TileMap" type="TileMap"]
tile_set = ExtResource("1_vckf1")
format = 2
layer_0/tile_data = PackedInt32Array(0, 0, 0, 1, 0, 0, 65537, 0, 0, 65536, 0, 0, 2, 0, 0, 65538, 0, 0, 131074, 0, 0, 131073, 0, 0, 131072, 0, 0, 196608, 0, 0, 262144, 0, 0, 262145, 0, 0, 262146, 0, 0, 262147, 0, 0, 262148, 0, 0, 262149, 0, 0, 196613, 0, 0, 131077, 0, 0, 65541, 0, 0, 5, 0, 0, 4, 0, 0, 3, 0, 0, 65539, 0, 0, 131075, 0, 0, 65540, 0, 0, 131076, 0, 0, 196611, 0, 0, 196610, 0, 0, 196609, 0, 0, 196612, 0, 0)
script = ExtResource("1_kd8r8")

View File

@ -0,0 +1,13 @@
[gd_scene load_steps=3 format=3 uid="uid://c5ipf40bxdqc6"]
[ext_resource type="TileSet" uid="uid://cluemfplimly4" path="res://assets/tiles.tres" id="1_mnkww"]
[ext_resource type="Script" path="res://scripts/v2/worldgen/standard/room.gd" id="2_4hex2"]
[node name="Hallway" type="TileMap"]
position = Vector2(-2, -52)
tile_set = ExtResource("1_mnkww")
format = 2
layer_0/tile_data = PackedInt32Array(0, 0, 0, 1, 0, 0, 2, 0, 0, 262144, 0, 0, 262145, 0, 0, 262146, 0, 0, 262147, 0, 0, 3, 0, 0, 4, 0, 0, 5, 0, 0, 262148, 0, 0, 262149, 0, 0, 262150, 0, 0, 262151, 0, 0, 262152, 0, 0, 262153, 0, 0, 262154, 0, 0, 6, 0, 0, 7, 0, 0, 8, 0, 0, 9, 0, 0, 10, 0, 0)
script = ExtResource("2_4hex2")
left = true
right = true

View File

@ -0,0 +1,11 @@
[gd_scene load_steps=3 format=3 uid="uid://c8vs23y1tdhx8"]
[ext_resource type="TileSet" uid="uid://cluemfplimly4" path="res://assets/tiles.tres" id="1_ucjv8"]
[ext_resource type="Script" path="res://scripts/v2/worldgen/standard/room.gd" id="2_nekba"]
[node name="MediumRoomUp" type="TileMap"]
tile_set = ExtResource("1_ucjv8")
format = 2
layer_0/tile_data = PackedInt32Array(0, 0, 0, 1, 0, 0, 2, 0, 0, 3, 0, 0, 4, 0, 0, 5, 0, 0, 6, 0, 0, 10, 0, 0, 11, 0, 0, 12, 0, 0, 13, 0, 0, 14, 0, 0, 15, 0, 0, 16, 0, 0, 65536, 0, 0, 131072, 0, 0, 196608, 0, 0, 262144, 0, 0, 65552, 0, 0, 131088, 0, 0, 196624, 0, 0, 327680, 0, 0, 393216, 0, 0, 458752, 0, 0, 524288, 0, 0, 589824, 0, 0, 655360, 0, 0, 720896, 0, 0, 720897, 0, 0, 720898, 0, 0, 720899, 0, 0, 720900, 0, 0, 720901, 0, 0, 720902, 0, 0, 720909, 0, 0, 720910, 0, 0, 589840, 0, 0, 524304, 0, 0, 458768, 0, 0, 393232, 0, 0, 327696, 0, 0, 262160, 0, 0, 720912, 0, 0, 655376, 0, 0, 720911, 0, 0, 720907, 0, 0, 720906, 0, 0, 720905, 0, 0, 720904, 0, 0, 720903, 0, 0, 720908, 0, 0, 196614, 0, 0, 196615, 0, 0, 196616, 0, 0, 196617, 0, 0, 196618, 0, 0, 8, 1, 0, 65543, 1, 0, 65545, 1, 0, 196612, 0, 0, 196613, 0, 0, 393217, 0, 0, 393218, 0, 0, 393219, 0, 0, 393220, 0, 0, 393221, 0, 0, 393222, 0, 0, 393223, 0, 0, 393224, 0, 0, 196619, 0, 0, 196620, 0, 0, 262156, 0, 0, 327692, 0, 0, 393228, 0, 0, 458764, 0, 0, 589837, 1, 0, 589839, 1, 0, 524302, 1, 0, 458765, 1, 0, 458767, 1, 0, 393230, 1, 0, 327693, 1, 0, 327695, 1, 0, 262158, 1, 0, 196621, 1, 0, 196623, 1, 0, 458760, 0, 0, 458759, 0, 0, 458758, 0, 0, 458757, 0, 0, 458756, 0, 0, 458755, 0, 0, 458754, 0, 0, 458753, 0, 0)
script = ExtResource("2_nekba")
top = true

View File

@ -0,0 +1,12 @@
[gd_scene load_steps=3 format=3 uid="uid://7jnspqbabihc"]
[ext_resource type="TileSet" uid="uid://cluemfplimly4" path="res://assets/tiles.tres" id="1_0bmf2"]
[ext_resource type="Script" path="res://scripts/v2/worldgen/standard/room.gd" id="2_n0xwr"]
[node name="MediumRoomUpDown" type="TileMap"]
tile_set = ExtResource("1_0bmf2")
format = 2
layer_0/tile_data = PackedInt32Array(0, 0, 0, 65536, 0, 0, 131072, 0, 0, 196608, 0, 0, 262144, 0, 0, 327680, 0, 0, 393216, 0, 0, 458752, 0, 0, 524288, 0, 0, 589824, 0, 0, 655360, 0, 0, 720896, 0, 0, 1, 0, 0, 720897, 0, 0, 2, 0, 0, 720898, 0, 0, 3, 0, 0, 720899, 0, 0, 4, 0, 0, 720900, 0, 0, 5, 0, 0, 720901, 0, 0, 6, 0, 0, 720902, 0, 0, 10, 0, 0, 720906, 0, 0, 11, 0, 0, 720907, 0, 0, 720908, 0, 0, 720909, 0, 0, 720910, 0, 0, 720911, 0, 0, 196624, 0, 0, 262160, 0, 0, 327696, 0, 0, 393232, 0, 0, 458768, 0, 0, 524304, 0, 0, 589840, 0, 0, 655376, 0, 0, 720912, 0, 0, 720903, 1, 0, 720905, 1, 0, 8, 1, 0, 65543, 1, 0, 65545, 1, 0, 458759, 0, 0, 458758, 0, 0, 458760, 0, 0, 458761, 0, 0, 458757, 0, 0, 458756, 0, 0, 458762, 0, 0, 458763, 0, 0, 196612, 0, 0, 196613, 0, 0, 196614, 0, 0, 196615, 0, 0, 196616, 0, 0, 196617, 0, 0, 196618, 0, 0, 196619, 0, 0, 65546, 0, 0, 131082, 0, 0, 131083, 0, 0, 65547, 0, 0, 196620, 0, 0, 196621, 0, 0, 196622, 0, 0, 196623, 0, 0, 458764, 0, 0, 589837, 1, 0, 589839, 1, 0, 524302, 1, 0, 458765, 1, 0, 458767, 1, 0, 458755, 1, 0, 458753, 1, 0, 524290, 1, 0, 589825, 1, 0, 589827, 1, 0, 393218, 1, 0, 327681, 1, 0, 327683, 1, 0, 262146, 1, 0, 196609, 1, 0, 196611, 1, 0, 262156, 0, 0, 262157, 0, 0, 262158, 0, 0, 262159, 0, 0, 262148, 0, 0, 12, 0, 0, 13, 0, 0, 14, 0, 0, 15, 0, 0, 16, 0, 0, 65552, 0, 0, 131088, 0, 0, 131087, 0, 0, 131086, 0, 0, 131085, 0, 0, 131084, 0, 0, 65548, 0, 0, 65549, 0, 0, 65550, 0, 0, 65551, 0, 0)
script = ExtResource("2_n0xwr")
top = true
bottom = true

View File

@ -0,0 +1,14 @@
[gd_scene load_steps=3 format=3 uid="uid://t511rr478gom"]
[ext_resource type="TileSet" uid="uid://cluemfplimly4" path="res://assets/tiles.tres" id="1_eu7wp"]
[ext_resource type="Script" path="res://scripts/v2/worldgen/standard/room.gd" id="2_lsa1k"]
[node name="SmallFork" type="TileMap"]
tile_set = ExtResource("1_eu7wp")
format = 2
layer_0/tile_data = PackedInt32Array(65536, 0, 0, 131072, 0, 0, 458752, 0, 0, 524288, 0, 0, 131073, 0, 0, 524289, 0, 0, 65538, 0, 0, 131074, 0, 0, 458754, 0, 0, 524290, 0, 0, 65542, 0, 0, 131078, 0, 0, 458758, 0, 0, 524294, 0, 0, 131079, 0, 0, 524295, 0, 0, 65544, 0, 0, 131080, 0, 0, 458760, 0, 0, 524296, 0, 0, 0, 0, 0, 393216, 0, 0, 1, 0, 0, 393217, 0, 0, 2, 0, 0, 393218, 0, 0, 393219, 1, 0, 524291, 1, 0, 458756, 1, 0, 393221, 1, 0, 524293, 1, 0, 6, 0, 0, 393222, 0, 0, 7, 0, 0, 393223, 0, 0, 8, 0, 0, 393224, 0, 0, 4, 1, 0, 65539, 1, 0, 65541, 1, 0, 131076, 1, 0, 196611, 1, 0, 196613, 1, 0, 262148, 1, 0)
script = ExtResource("2_lsa1k")
left = true
right = true
top = true
bottom = true

View File

@ -0,0 +1,12 @@
[gd_scene load_steps=3 format=3 uid="uid://bc5n3rb5t8ua7"]
[ext_resource type="TileSet" uid="uid://cluemfplimly4" path="res://assets/tiles.tres" id="1_mja1s"]
[ext_resource type="Script" path="res://scripts/v2/worldgen/standard/room.gd" id="2_0e88q"]
[node name="SmallRoom1" type="TileMap"]
tile_set = ExtResource("1_mja1s")
format = 2
layer_0/tile_data = PackedInt32Array(65536, 0, 0, 0, 0, 0, 131072, 0, 0, 393216, 0, 0, 458752, 0, 0, 524288, 0, 0, 524289, 0, 0, 524290, 0, 0, 524295, 0, 0, 524296, 0, 0, 524297, 0, 0, 1, 0, 0, 2, 0, 0, 3, 0, 0, 4, 0, 0, 5, 0, 0, 6, 0, 0, 7, 0, 0, 8, 0, 0, 9, 0, 0, 524291, 0, 0, 524298, 0, 0, 458762, 0, 0, 393226, 0, 0, 327690, 0, 0, 262154, 0, 0, 196618, 0, 0, 131082, 0, 0, 65546, 0, 0, 10, 0, 0, 524292, 1, 0, 524294, 1, 0, 458755, 0, 0, 458754, 0, 0, 458753, 0, 0, 393217, 0, 0, 393218, 0, 0, 393219, 0, 0, 458759, 0, 0, 393223, 0, 0, 393224, 0, 0, 393225, 0, 0, 458761, 0, 0, 458760, 0, 0, 393222, 1, 0)
script = ExtResource("2_0e88q")
left = true
bottom = true

View File

@ -0,0 +1,12 @@
[gd_scene load_steps=3 format=3 uid="uid://3vfg1g7uhjp2"]
[ext_resource type="TileSet" uid="uid://cluemfplimly4" path="res://assets/tiles.tres" id="1_bu30l"]
[ext_resource type="Script" path="res://scripts/v2/worldgen/standard/room.gd" id="2_n3iuk"]
[node name="SpawnRoom" type="TileMap"]
tile_set = ExtResource("1_bu30l")
format = 2
layer_0/tile_data = PackedInt32Array(65536, 0, 0, 0, 0, 0, 327680, 0, 0, 327681, 0, 0, 327682, 0, 0, 327683, 0, 0, 1, 0, 0, 2, 0, 0, 3, 0, 0, 4, 0, 0, 327684, 0, 0, 327685, 0, 0, 5, 0, 0, 6, 0, 0, 65542, 0, 0, 327686, 0, 0, 262146, 0, 0, 262147, 0, 0, 262148, 0, 0, 393216, 0, 0, 393217, 0, 0, 393218, 0, 0, 393219, 0, 0, 393220, 0, 0, 393221, 0, 0, 393222, 0, 0)
script = ExtResource("2_n3iuk")
left = true
right = true

View File

@ -12,7 +12,7 @@ var _steps_count: int
func _init(step_count: int = 0) -> void:
_steps_count = step_count
self.substep_complete.connect(self, "_on_substep_complete")
#self.substep_complete.connect(self, "_on_substep_complete")
func _on_step_complete() -> void:
if _current_step_index == _steps_count:

View File

@ -8,3 +8,8 @@ var map: TileMap
func _ready() -> void:
self.generator.generate(map)
func _process(_delta: float) -> void:
if Input.is_action_just_pressed("jump"):
map.clear()
self.generator.generate(map)

View File

@ -9,11 +9,12 @@ const TILEMAP_LAYER = 0
@export var top: bool = false
@export var bottom: bool = false
@onready
var room_size: Vector2i = self.get_used_rect().size
@onready
var cell_size: Vector2i = self.tile_set.tile_size
var room_size: Vector2i
var cell_size: Vector2i
func _init() -> void:
self.room_size = self.get_used_rect().size
self.cell_size = self.tile_set.tile_size
func is_block() -> bool:
if self.left or self.right or self.top or self.bottom:
@ -44,19 +45,62 @@ func get_exit_pos(exit: Vector2i) -> Vector2i:
pos = Vector2i(self.room_size.x / 2, self.room_size.y - 1)
return pos
func copy_to_map(map: TileMap, pos: Vector2i) -> void:
for x in range(self.room_size.x):
for y in range(self.room_size.y):
func get_exit_pos_offset(edge_pos: Vector2i, exit: Vector2i) -> Vector2i:
match exit:
Vector2i.UP:
return edge_pos + Vector2i(self.room_size.x/-2, self.room_size.y * -1)
Vector2i.DOWN:
return edge_pos + Vector2i(self.room_size.x/-2, 1)
Vector2i.LEFT:
return edge_pos + Vector2i(-self.room_size.x, -self.room_size.y/2)
Vector2i.RIGHT:
return edge_pos + Vector2i(1, -self.room_size.y/2)
_:
return Vector2i.ZERO
func copy_to_map(map: TileMap) -> void:
for x in range(0, self.room_size.x):
for y in range(0, self.room_size.y):
var tile = self.get_cell_source_id(TILEMAP_LAYER, Vector2i(x, y))
if tile.tile_set:
map.set_cell(TILEMAP_LAYER, Vector2i(pos.x + x, pos.y + y), tile.tile_id)
if tile != -1:
map.set_cell(TILEMAP_LAYER, Vector2i(position.x + x, position.y + y), tile, Vector2i(0, 0))
else:
map.set_cell(TILEMAP_LAYER, Vector2i(pos.x + x, pos.y + y), -1)
map.set_cell(TILEMAP_LAYER, Vector2i(position.x + x, position.y + y), -1, Vector2i(0, 0))
func seal_exit(map: TileMap, pos: Vector2i, exit: Vector2i) -> void:
var exit_pos = get_exit_pos(exit)
var seal_tile_id = 0
match exit:
Vector2i.UP:
map.set_cell(TILEMAP_LAYER, exit_pos + Vector2i(pos.x - 1, pos.y), seal_tile_id, Vector2i(0, 0))
map.set_cell(TILEMAP_LAYER, exit_pos + Vector2i(pos.x, pos.y), seal_tile_id, Vector2i(0, 0))
map.set_cell(TILEMAP_LAYER, exit_pos + Vector2i(pos.x + 1, pos.y), seal_tile_id, Vector2i(0, 0))
Vector2i.DOWN:
map.set_cell(TILEMAP_LAYER, exit_pos + Vector2i(pos.x - 1, pos.y), seal_tile_id, Vector2i(0, 0))
map.set_cell(TILEMAP_LAYER, exit_pos + Vector2i(pos.x, pos.y), seal_tile_id, Vector2i(0, 0))
map.set_cell(TILEMAP_LAYER, exit_pos + Vector2i(pos.x + 1, pos.y), seal_tile_id, Vector2i(0, 0))
Vector2i.RIGHT:
map.set_cell(TILEMAP_LAYER, exit_pos + Vector2i(pos.x, pos.y - 1), seal_tile_id, Vector2i(0, 0))
map.set_cell(TILEMAP_LAYER, exit_pos + Vector2i(pos.x, pos.y), seal_tile_id, Vector2i(0, 0))
map.set_cell(TILEMAP_LAYER, exit_pos + Vector2i(pos.x, pos.y + 1), seal_tile_id, Vector2i(0, 0))
Vector2i.LEFT:
map.set_cell(TILEMAP_LAYER, exit_pos + Vector2i(pos.x, pos.y - 1), seal_tile_id, Vector2i(0, 0))
map.set_cell(TILEMAP_LAYER, exit_pos + Vector2i(pos.x, pos.y), seal_tile_id, Vector2i(0, 0))
map.set_cell(TILEMAP_LAYER, exit_pos + Vector2i(pos.x, pos.y + 1), seal_tile_id, Vector2i(0, 0))
func is_overlapping(map: TileMap) -> bool:
for x in range(0, self.room_size.x):
for y in range(0, self.room_size.y):
var tile = map.get_cell_source_id(TILEMAP_LAYER, Vector2i(self.position.x + x, self.position.y + y))
if tile != -1:
return true
return false
# Validate room by checking that the top left tile is at 0, 0 and all exits are valid
func _validate() -> bool:
# Check of tiles where x is negative
# Check for tiles where x is negative
pass
# Check for tiles where y is negative
@ -80,3 +124,19 @@ func _validate() -> bool:
return false
return true
func flip_vector(vector: Vector2i) -> Vector2i:
return Vector2i(vector.x * -1, vector.y * -1)
func disable_room_exit_opposite(exit: Vector2i) -> void:
self.disable_room_exit(self.flip_vector(exit))
func disable_room_exit(exit: Vector2i) -> void:
if exit == Vector2i.LEFT:
self.left = false
elif exit == Vector2i.RIGHT:
self.right = false
elif exit == Vector2i.UP:
self.top = false
elif exit == Vector2i.DOWN:
self.bottom = false

View File

@ -18,9 +18,10 @@ var _rooms_top: Array[PackedScene] = []
var _rooms_bottom: Array[PackedScene] = []
var _rooms_blocking: Array[PackedScene] = []
var _progress_tracker: ProgressTracker = ProgressTracker.new(1)
var _progress_tracker: ProgressTracker
func _generate(map: TileMap) -> void:
self._progress_tracker = ProgressTracker.new(1)
self._sort_rooms(_progress_tracker.next_step("Sorting Rooms"))
@ -30,8 +31,9 @@ func _generate(map: TileMap) -> void:
# Create rooms
self._create_rooms(map, init_room, self.max_room_path_length, _progress_tracker.next_step("Creating Rooms"))
map.update_internals()
func _sort_rooms(step_tracker: ProgressStepTracker, validate: bool = false) -> void:
func _sort_rooms(step_tracker: ProgressStepTracker) -> void:
step_tracker.complete.call_deferred()
step_tracker.set_substeps(len(self.rooms))
@ -67,12 +69,12 @@ func _create_initial_room(map: TileMap, room: PackedScene, step_tracker: Progres
step_tracker.complete.call_deferred()
# Instantiate spawn room
var spawn_room: StandardRoom = room.instance()
var init_room: StandardRoom = room.instantiate()
# Copy spawn room into map
spawn_room.copy_to_map(map, Vector2i(0, 0))
init_room.copy_to_map(map)
return spawn_room
return init_room
# Create rooms by randomly selecting from the available rooms in every direction with openings from the current room
@ -88,6 +90,8 @@ func _create_rooms(map: TileMap, parent_room: StandardRoom, max_path_length: int
# If the maximum path length has been exceeded, stop
if max_path_length <= 0:
for exit in exits:
parent_room.seal_exit(map, parent_room.position, exit)
return
# If there are no more exits, stop
@ -106,30 +110,42 @@ func _create_rooms(map: TileMap, parent_room: StandardRoom, max_path_length: int
var room_edge_pos: Vector2i = exit_pos + Vector2i(parent_room.position)
# If there is already a room at this position, skip it
if map.get_cellv(room_edge_pos) != -1:
continue
# Get the rooms that can be added in this direction
var rooms: Array[PackedScene] = self._get_rooms(exit)
var possible_rooms: Array[PackedScene] = self._get_rooms(parent_room.flip_vector(exit)).duplicate()
# If there are no rooms that can be added in this direction, skip it
if len(rooms) == 0:
if len(possible_rooms) == 0:
continue
# Get the room to add
var room: PackedScene = rooms[randi() % len(rooms)]
var room_instance: StandardRoom = null
while len(possible_rooms) != 0 and room_instance == null:
var i = randi() % len(possible_rooms)
var possible_room = possible_rooms.pop_at(i).instantiate()
var origin_pos: Vector2i = possible_room.get_exit_pos_offset(room_edge_pos, exit)
possible_room.position = origin_pos
# Instantiate the room
var room_instance: StandardRoom = room.instance()
room_instance.position = room_edge_pos + (room_instance.size / 2)
if possible_room.is_overlapping(map):
continue
room_instance = possible_room
if room_instance == null:
parent_room.seal_exit(map, parent_room.position, exit)
continue
room_instance.queue_free.call_deferred()
# Disable direction that the room was added from
# since the parent room will already be there
room_instance.disable_room_exit_opposite(exit)
# Copy the room into the map
room_instance.copy_to_map(map, Vector2i(room_instance.position))
room_instance.copy_to_map(map)
# Create rooms from the exits of the room that was just added
self._create_rooms(map, room_instance, max_path_length - 1, step_tracker.next_step("Creating Rooms"))
step_tracker.substep("Creating Rooms")
self._create_rooms(map, room_instance, max_path_length - 1, step_tracker)
# If there are no more exits, stop
return

View File

@ -2,7 +2,7 @@ class_name WorldGenerator
extends Resource
func generate(map: TileMap) -> void:
if self.has_method("_generate"):
if not self.has_method("_generate"):
push_error("Generator missing `_generate` method")
return