floating tile detection

This commit is contained in:
2024-12-04 16:17:31 +01:00
parent 32ccd05d9d
commit a3ef6f5328
6 changed files with 1636 additions and 14 deletions

View File

@@ -65,6 +65,8 @@ func trigger_explosion():
# Destroy tiles in the explosion radius # Destroy tiles in the explosion radius
destroy_tiles_in_explosion_area() destroy_tiles_in_explosion_area()
tile_map_layer.detect_and_make_tiles_fall()
# Start the timer to cleanup the explosion # Start the timer to cleanup the explosion
timer.start() timer.start()

View File

@@ -11,23 +11,31 @@ script = ExtResource("1_p5ww4")
[node name="PlayerList" type="VBoxContainer" parent="."] [node name="PlayerList" type="VBoxContainer" parent="."]
layout_mode = 0 layout_mode = 0
offset_left = 33.0 offset_left = 531.0
offset_top = 54.0 offset_top = 168.0
offset_right = 73.0 offset_right = 648.0
offset_bottom = 94.0 offset_bottom = 382.0
[node name="StartButton" type="Button" parent="."] [node name="StartButton" type="Button" parent="."]
layout_mode = 2 layout_mode = 2
offset_left = 370.0 offset_left = 516.0
offset_top = 165.0 offset_top = 464.0
offset_right = 416.0 offset_right = 666.0
offset_bottom = 196.0 offset_bottom = 511.0
text = "Start" text = "Start"
[node name="Label" type="Label" parent="."] [node name="Label" type="Label" parent="."]
layout_mode = 0 layout_mode = 0
offset_left = 155.0 offset_left = 514.0
offset_top = 31.0 offset_top = 88.0
offset_right = 292.0 offset_right = 651.0
offset_bottom = 54.0 offset_bottom = 111.0
text = "Bomber-Thorsten" text = "Bomber-Thorsten"
[node name="Label2" type="Label" parent="."]
layout_mode = 0
offset_left = 530.0
offset_top = 614.0
offset_right = 660.0
offset_bottom = 637.0
text = "www.it-thaler.de"

758
mai5410.tmp Normal file

File diff suppressed because one or more lines are too long

758
mai9FEE.tmp Normal file

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

94
tile_map_layer.gd Normal file
View File

@@ -0,0 +1,94 @@
extends TileMapLayer
# Detect and make tiles fall
func detect_and_make_tiles_fall():
var all_tiles = get_used_cells()
var grounded_tiles = get_grounded_tiles()
print("Grounded tiles:", grounded_tiles)
var floating_tiles = []
for tile in all_tiles:
if tile not in grounded_tiles:
floating_tiles.append(tile)
print("Floating tiles:", floating_tiles)
for tile_pos in floating_tiles:
make_tile_fall(tile_pos)
# Returns all the grounded tiles (tiles connected to the bottom-most tiles)
func get_grounded_tiles() -> Array:
var grounded_tiles = []
var bottom_y = get_bottom_y() # Identify the bottom-most y coordinate
print("Bottom y:", bottom_y)
# Get all tiles at the bottom-most y-coordinate
var bottom_tiles = []
for tile in get_used_cells():
if tile.y == bottom_y:
bottom_tiles.append(tile)
print("Bottom tiles:", bottom_tiles)
# Traverse all tiles connected to the bottom-most tiles (iteratively)
var visited = {} # Dictionary to track visited tiles
var queue = bottom_tiles.duplicate() # Initialize the queue with bottom-most tiles
while queue.size() > 0:
var current = queue.pop_front() # Get the next tile to process
# Skip if already visited
if visited.has(current):
continue
visited[current] = true # Mark as visited
grounded_tiles.append(current) # Add to grounded tiles
# Check neighboring tiles (up, down, left, right)
var neighbors = [
current + Vector2i(1, 0), # Right
current + Vector2i(-1, 0), # Left
current + Vector2i(0, 1), # Down (already checked for bottom-most)
current + Vector2i(0, -1) # Up
]
for neighbor in neighbors:
if has_tile(neighbor) and not visited.has(neighbor):
queue.append(neighbor) # Add unvisited neighboring tile to the queue
return grounded_tiles
# Function to get the lowest y-coordinate of tiles in the map
func get_bottom_y() -> int:
var bottom_y = -1
for tile in get_used_cells():
if tile.y > bottom_y:
bottom_y = tile.y # Keep track of the largest y value (lowest tile)
return bottom_y
# Check if a specific tile exists at the given coordinates
func has_tile(coords: Vector2i) -> bool:
return get_cell_source_id(coords) != -1
# Make a tile fall by erasing it and placing it below
func make_tile_fall(coords: Vector2i) -> void:
var source_id = get_cell_source_id(coords) # Get the source ID of the tile
var atlas_coords = get_cell_atlas_coords(coords) # Get the atlas coordinates
var below_coords = coords + Vector2i(0, 1) # Position where the tile will fall
# Erase the current tile
erase_cell(coords)
# Place the tile one step below
#set_cell(below_coords, source_id, atlas_coords)
func _ready():
# Automatically call the function to test it when the scene runs
detect_and_make_tiles_fall()