Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 9323942

Browse files
committed
Refactor advance simulation method
1 parent b0f2152 commit 9323942

File tree

2 files changed

+32
-38
lines changed

2 files changed

+32
-38
lines changed

‎godot/CellularAutomata/02.CavernGenerator/CellularCavernGenerator.gd‎

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ func generate_new_dungeon() -> void:
4545
if _step_time > 0:
4646
_paint_map()
4747
yield(get_tree().create_timer(_step_time), "timeout")
48-
_map = _advance_simulation(_map)
48+
_map = _advance_simulation()
4949

5050
_paint_map()
5151
emit_signal("dungeon_generation_completed")
@@ -61,22 +61,23 @@ func _generate_random_map() -> Dictionary:
6161

6262

6363
## Advances the cellular automata simulation by one step
64-
func _advance_simulation(input_map: Dictionary) -> Dictionary:
65-
var map := {}
66-
for cell in input_map:
64+
func _advance_simulation() -> Dictionary:
65+
var new_map := {}
66+
for cell in _map:
6767
var floor_neighbor_count = _count_floor_neighbors(cell)
68-
if input_map[cell] == CellType.FLOOR:
69-
if floor_neighbor_count < _wall_conversion:
70-
map[cell] = CellType.WALL
71-
else:
72-
map[cell] = input_map[cell]
68+
if _map[cell] == CellType.FLOOR:
69+
new_map[cell] = (
70+
CellType.WALL
71+
if floor_neighbor_count < _wall_conversion
72+
else CellType.FLOOR
73+
)
7374
else:
74-
map[cell] = (
75+
new_map[cell] = (
7576
CellType.FLOOR
7677
if floor_neighbor_count > _floor_conversion
7778
else CellType.WALL
7879
)
79-
return map
80+
return new_map
8081

8182

8283
## Draws tiles on the tilemap.

‎godot/CellularAutomata/03.DungeonGenerator/CellularDungeonGenerator.gd‎

Lines changed: 20 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ func generate_new_dungeon() -> void:
5454
_paint_map()
5555
yield(get_tree().create_timer(_step_time), "timeout")
5656

57-
_map = _step()
57+
_map = _advance_simulation()
5858

5959
_remove_small_caverns()
6060
_paint_map()
@@ -69,24 +69,23 @@ func _initialize_map() -> void:
6969
_map[Vector2(x, y)] = CellType.WALL if randf() < _wall_chance else CellType.FLOOR
7070

7171

72-
func _step() -> Dictionary:
73-
var _new_map := {}
74-
72+
func _advance_simulation() -> Dictionary:
73+
var new_map := {}
7574
for cell in _map:
7675
var floor_neighbor_count = _count_floor_neighbors(cell)
7776
if _map[cell] == CellType.FLOOR:
78-
if floor_neighbor_count < _wall_conversion:
79-
_new_map[cell] = CellType.WALL
80-
else:
81-
_new_map[cell] = _map[cell]
77+
new_map[cell] = (
78+
CellType.WALL
79+
if floor_neighbor_count < _wall_conversion
80+
else CellType.FLOOR
81+
)
8282
else:
83-
_new_map[cell] = (
83+
new_map[cell] = (
8484
CellType.FLOOR
8585
if floor_neighbor_count > _floor_conversion
8686
else CellType.WALL
8787
)
88-
89-
return _new_map
88+
return new_map
9089

9190

9291
func _remove_small_caverns():
@@ -153,12 +152,12 @@ func _add_start_and_exit() -> void:
153152

154153
floor_cells.shuffle()
155154
var player_cell := Vector2.ZERO
156-
155+
157156
for floor_cell in floor_cells:
158157
if _count_floor_neighbors(floor_cell) > 7:
159158
player_cell = floor_cell
160159
break
161-
160+
162161
_player.position = player_cell * CELL_SIZE
163162

164163
var exit_cell := Vector2.ZERO
@@ -174,25 +173,20 @@ func _add_start_and_exit() -> void:
174173
func _add_treasure() -> void:
175174
var floor_cells = _tilemap.get_used_cells_by_id(CellType.FLOOR)
176175
var treasures_placed := 0
177-
178-
var corner_subtiles := [
179-
Vector2(0, 0),
180-
Vector2(0, 2),
181-
Vector2(2, 0),
182-
Vector2(2, 2)
183-
]
184-
176+
177+
var corner_subtiles := [Vector2(0, 0), Vector2(0, 2), Vector2(2, 0), Vector2(2, 2)]
178+
185179
floor_cells.shuffle()
186180

187181
while treasures_placed < _maximum_treasure and floor_cells:
188182
var cell = floor_cells.pop_back()
189-
183+
190184
var subtile = _tilemap.get_cell_autotile_coord(cell.x, cell.y)
191-
185+
192186
if corner_subtiles.has(subtile):
193187
var treasure = treasure_scene.instance()
194188
# Offset the treasure based on which corner subtile the treasure appears in. This is based on the subtiles' position in relation to each other in the tileset.
195-
treasure.position = cell * CELL_SIZE + (subtile - Vector2(1, 1)) * -CELL_SIZE/2
189+
treasure.position = cell * CELL_SIZE + (subtile - Vector2(1, 1)) * -CELL_SIZE/2
196190
add_child(treasure)
197191
treasures_placed += 1
198192

@@ -212,20 +206,19 @@ func _count_floor_neighbors(location: Vector2) -> int:
212206

213207
func remove_walls(global_positions: Array) -> void:
214208
for pos in global_positions:
215-
216209
var cell = _tilemap.world_to_map(pos)
217210

218211
if _tilemap.get_cellv(cell) == CellType.FLOOR:
219212
continue
220-
213+
221214
_tilemap.set_cellv(cell, CellType.FLOOR)
222215
_tilemap.update_bitmask_area(cell)
223216
# Subtiles (0, 3) and (1, 3) correspond to different versions of the floor tile.
224217
# We have this line to prevent them from appearing while digging.
225218
for n in CELL_NEIGHBORS:
226219
var variants := [Vector2(0, 3), Vector2(1, 3)]
227220
var subtile = _tilemap.get_cell_autotile_coord(cell.x + n.x, cell.y + n.y)
228-
221+
229222
if variants.has(subtile):
230223
_tilemap.set_cell(
231224
cell.x + n.x, cell.y + n.y, CellType.FLOOR, false, false, false, Vector2(1, 1)

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /