Sprengen funktioniert

This commit is contained in:
2024-12-03 20:16:17 +01:00
parent 92aaadc783
commit af0a145953
3 changed files with 302 additions and 32 deletions

52
bomb.gd
View File

@@ -93,39 +93,41 @@ func push_away_players():
body.apply_central_impulse(impulse)
func destroy_tiles_in_explosion_area():
var center = global_position # This is the center of the explosion in global coordinates
var radius = 30 # The radius of the explosion
# Convert center to map coordinates, taking scale into account
var center = global_position # Explosion center in global coordinates
var radius = 150 # Explosion radius in pixels
# Convert center to map coordinates
var map_center = tile_map_layer.local_to_map(center)
print("Center: ", map_center)
# Get the scale factor of the TileMapLayer
var scale = tile_map_layer.scale.x # Assuming uniform scale for both x and y
print("Explosion center in map coordinates:", center, map_center)
# Tile dimensions
var tile_width = 16
var tile_height = 16
# Get the bounding box for the explosion area in map coordinates
var min_x = int((map_center.x - radius * tile_width) / tile_width)
var max_x = int((map_center.x + radius * tile_width) / tile_width)
var min_y = int((map_center.y - radius * tile_height) / tile_height)
var max_y = int((map_center.y + radius * tile_height) / tile_height)
# Loop through all cells in the bounding box
# Calculate bounds of explosion in tile coordinates
var min_x = int((center.x - radius) / tile_width)
var max_x = int((center.x + radius) / tile_width)
var min_y = int((center.y - radius) / tile_height)
var max_y = int((center.y + radius) / tile_height)
print("Explosion bounds in tile coordinates: ", Vector2i(min_x, min_y), Vector2i(max_x, max_y))
# Loop through the affected tiles
for x in range(min_x, max_x + 1):
for y in range(min_y, max_y + 1):
# Calculate distance from the center of the explosion (map coordinates)
var dist = map_center.distance_to(Vector2(x, y))
# Check if the tile is within the explosion radius (using the distance check)
# Calculate the distance from the explosion center to the tile center
var tile_pos = tile_map_layer.map_to_local(Vector2i(x, y))
var dist = center.distance_to(tile_pos)
# Check if the tile is within the explosion radius
if dist <= radius:
# Print out some debug information
print("Checking tile:", Vector2i(x, y), "Distance:", dist)
# Check if the tile is within the bounds of the map and not empty
print("Checking tile at:", Vector2i(x, y), "Distance:", dist)
print(tile_map_layer.get_cell_source_id(Vector2i(x, y)))
# Erase the tile if it exists
if tile_map_layer.get_cell_source_id(Vector2i(x, y)) != -1:
# Erase the tile at coordinates (x, y) in map coordinates
tile_map_layer.erase_cell(Vector2i(x, y))
print("Erased tile at:", Vector2i(x, y))
# Function to check if the bomb is off-screen

281
main.tscn

File diff suppressed because one or more lines are too long

View File

@@ -20,6 +20,7 @@ config/icon="res://icon.svg"
throw_bomb={
"deadzone": 0.5,
"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":32,"key_label":0,"unicode":32,"location":0,"echo":false,"script":null)
, Object(InputEventJoypadButton,"resource_local_to_scene":false,"resource_name":"","device":-1,"button_index":0,"pressure":0.0,"pressed":false,"script":null)
]
}