skullball/godot/maps/meta/map_rebuilder.gd

61 lines
2.7 KiB
GDScript

@tool
extends Node
# This node and script are here to rebuild the .map file and do general updating stuff
# when invoked with the --rebuild-map user CLI argument. This is useful for being able
# to update the scene with the latest changes to the .map file automatically in a
# trenchbroom compile step. The way this works is that this script is a bit of a sleeper agent,
# doing nothing most of the time, but if the engine is running in Editor mode and the --rebuild-map
# user CLI argument is passed, this script runs the "Build Map" buttons' function for the func_godot
# map, and does some other misc. node setup (like hooking up spawn points to the Map), and then
# quits. With the headless argument, this allows rebuilding the Godot representation of the map, and
# doing menial maintenance work, all from a single CLI command. That TrenchBroom can also invoke,
# enabling a level design iteration loop without any (visible) Godot editor in the loop.
#
# E.g. to rebuild the Two Towers map from CLI, run this from repo root:
# godot --headless --editor --path godot res://maps/two_towers/two_towers.tscn -- --rebuild-map
#
# In TrenchBroom, the following should work as "Parameters" of a "Run Tool" compile step with Godot as the tool:
# --headless --editor --path godot res://maps/${MAP_BASE_NAME}/${MAP_BASE_NAME}.tscn -- --rebuild-map
# Another step might be useful for actually running the game.
func _ready() -> void:
var args: PackedStringArray = OS.get_cmdline_user_args()
if args.has("--rebuild-map"):
var success: bool = rebuild_map()
get_tree().quit(0 if success else 1)
func rebuild_map() -> bool:
var map: Map = get_parent()
# Rebuild func_godot_map
var func_godot_maps: Array = map.find_children("*", "FuncGodotMap")
assert(len(func_godot_maps) == 1)
var func_godot_map: FuncGodotMap = func_godot_maps[0]
print("Rebuilding ", func_godot_map.name, "...")
func_godot_map.build()
print(func_godot_map.name, " rebuilt. Registering spawners...")
# Hook up team and ball spawns
map.spawners = []
var spawner_count: int = 0
var spawn_entities: Array = map.find_children("*", "SpawnEntity")
for node in spawn_entities:
var spawn_entity: SpawnEntity = node
if spawn_entity.is_ball_spawn:
map.ball_spawner = spawn_entity
spawner_count += 1
if spawn_entity.team_index >= 0:
while len(map.spawners) <= spawn_entity.team_index:
map.spawners.push_back(null)
map.spawners[spawn_entity.team_index] = spawn_entity
spawner_count += 1
print(spawner_count, " spawners registered. Saving scene...")
var packed_scene = PackedScene.new()
packed_scene.pack(get_tree().edited_scene_root)
ResourceSaver.save(packed_scene, get_tree().edited_scene_root.scene_file_path)
print("Scene saved. All rebuilding steps complete!")
return true