TowerGame/scripts/import/tiles.gd

73 lines
2.2 KiB
GDScript3
Raw Normal View History

2025-05-12 11:23:40 +01:00
@tool
extends EditorScenePostImport
# Output structure
# {{Tile Name}} : MeshInstance3D
# - StaticBody3D : StaticBody3D
# - {{Tile Name}} Collision Mesh : CollisionShape3D
# - NavigationRegion3D : NavigationRegion3D
# - {{Tile Name}} Nav Mesh : MeshInstance3D
func _post_import(scene: Node) -> Object:
print_rich("[b]Starting Import![/b]")
var tiles_to_edit: Array[MeshInstance3D] = get_visual_tiles(scene)
edit_scene(scene, tiles_to_edit)
var timestamp_node: Node = Node.new()
timestamp_node.name = Time.get_time_string_from_system().replacen(":", "-")
scene.add_child(timestamp_node)
timestamp_node.set_owner(scene)
return scene
func get_visual_tiles(scene: Node) -> Array[MeshInstance3D]:
var tiles: Array[MeshInstance3D] = []
# Avoids editing tree while parsing
for child in scene.get_children():
if child.name.ends_with("Nav") || child.name.ends_with("Collision"): continue
tiles.push_back(child as MeshInstance3D)
return tiles
func edit_scene(scene: Node, tiles: Array[MeshInstance3D]) -> void:
# Assumes specific names for Colliison / Nav meshes
for tile in tiles:
print("Editing %s" % [tile.name])
var collision = scene.find_child("%s Collision" % tile.name)
var nav = scene.find_child("%s Nav" % tile.name)
# Helpful? warnings
if collision == null:
push_warning("Missing '%s Collision'" % tile.name)
if nav == null:
push_warning("Missing '%s Nav'" % tile.name)
collision.set_owner(null)
collision.reparent(tile)
collision.set_owner(scene)
#print("- Adding NavigationMesh")
var nav_mesh: NavigationMesh = NavigationMesh.new()
nav_mesh.sample_partition_type = NavigationMesh.SAMPLE_PARTITION_LAYERS
nav_mesh.agent_radius = 0.2
nav_mesh.region_min_size = 1.0
nav_mesh.agent_max_slope = 46.0
nav_mesh.filter_walkable_low_height_spans = true
#print("- Adding NavigationRegion3D")
var nav_region: NavigationRegion3D = NavigationRegion3D.new()
nav_region.name = "NavigationRegion3D"
tile.add_child(nav_region)
nav_region.set_owner(scene)
nav_region.navigation_mesh = nav_mesh
nav_region.bake_navigation_mesh(false)
#print("- Reparenting Navigation MeshInstance3D")
nav.set_owner(null)
nav.reparent(nav_region)
nav.set_owner(scene)
#endfor
print_rich("[b]Done![/b]")