gai-godot-games/pathfinding-algorithms/scenes/setup_tilemap.gd

46 lines
1.7 KiB
GDScript

extends Node2D
func _ready():
var physics_tilemap = $Fence
var navigation_tilemap = $Grass
var tile_set = physics_tilemap.tile_set
if not tile_set:
print("Failed to find tile_set on physics_tilemap")
return
var tile_size = tile_set.get_tile_size()
var used_rect = physics_tilemap.get_used_rect()
print(used_rect)
for x in range(used_rect.position.x, used_rect.size.x):
for y in range(used_rect.position.y, used_rect.size.y):
var tile_id = physics_tilemap.get_cell(x, y)
if tile_id == -1:
continue
# Get the collision shapes for the current tile
var collision_shapes = tile_set.tile_get_shapes(tile_id)
if not collision_shapes:
continue
# Convert collision shapes into NavigationPolygon cut-outs
for shape in collision_shapes:
if shape.shape.is_class("RectangleShape2D"): # assuming rectangle shapes for simplicity
var rect = Rect2(
Vector2(x, y) * tile_size,
Vector2(shape.shape.extents) * 2
)
cut_navigation_polygon(navigation_tilemap, rect)
# Expand this if-else for other shapes like CircleShape2D, etc.
func cut_navigation_polygon(navigation_tilemap: TileMapLayer, rect: Rect2):
for nav_polygon in navigation_tilemap.get_children():
if nav_polygon.is_class("NavigationPolygonInstance"):
# This is a crude example - in practice, create new polygons to adjust the navigation mesh
var nav_rect = nav_polygon.get_item_rect()
if nav_rect.intersects(rect):
# Here, define logic to cut rect from nav_rect, adjusting navigation mesh
# Example: Remove affected navigation polygon and recreate it around `rect`
nav_polygon.remove() # remove or edit as per your exact cutout needs
# Note: Fine-tuning would involve more detailed polygon edits or re-creation.