# ---------------------------------------------------------------------------
# demo-scene-switching — game-mode demo for actuator-driven scene switching
# ---------------------------------------------------------------------------
# Separate executable built from the same sources as editSceneEditor (minus
# main.cpp).  It runs in game mode and loads demo_scene_a.json, which
# contains a flat colored floor plane (procedurally created in demo_main.cpp
# as "DemoFloorPlaneA"; scene B uses the blue "DemoFloorPlaneB") with a
# static box collider, a character spawner ("s1", character registry ID 2,
# same player character setup as town8.json), a PlayerControllerComponent
# targeting it and an actuator ("portal_a", marked by a colored pillar mesh
# also created in demo_main.cpp).  The actuator's "goto_scene_b" action
# (defined in the scene's top-level actionDatabase block) runs a behavior
# tree whose "switchScene" node queues EditorApp::switchScene() to
# demo_scene_b.json with a "@arrival_b" teleport target; demo_scene_b.json
# mirrors this with its own "portal_b" actuator and "goto_scene_a" action,
# so the player can travel back and forth endlessly.  Both scenes carry
# their own player controller, so each switch is a clean takeover.  The
# mouse is grabbed while Playing (built-in game-mode behaviour) and the
# initial window is larger than the default (1920x1080, see DemoApp in
# demo_main.cpp).
#
# The executable is self-contained in this build directory: a POST_BUILD
# step (stage_runtime.cmake) copies resources.cfg, the
# character prefab (prefabs/char_2.json, written by a previous editor/game
# run) and any runtime config JSONs here, and symlinks both demo scenes
# (demo_scene_a.json / demo_scene_b.json, so scene edits in the source tree
# are visible without a rebuild) plus the big pre-staged
# runtime directories (resources/, characters/, lua-scripts/) from the
# editScene binary directory.  Run it from here:
#   cd <build>/src/features/editScene/demos/demo-scene-switching
#   ./demoSceneSwitching
# Controls: mouse = look, W/A/S/D = move, Shift = run, E = use portal,
# Escape = pause menu (frees the cursor).

get_filename_component(EDITSCENE_SOURCE_DIR
	"${CMAKE_CURRENT_SOURCE_DIR}/../.." ABSOLUTE)
get_filename_component(EDITSCENE_BINARY_DIR
	"${CMAKE_CURRENT_BINARY_DIR}/../.." ABSOLUTE)

# Reuse the editScene sources, swapping main.cpp for demo_main.cpp.
set(DEMO_SOURCES ${EDITSCENE_SOURCES})
list(REMOVE_ITEM DEMO_SOURCES main.cpp)
list(TRANSFORM DEMO_SOURCES PREPEND "${EDITSCENE_SOURCE_DIR}/")

add_executable(demoSceneSwitching
	demo_main.cpp
	${DEMO_SOURCES}
)

add_dependencies(demoSceneSwitching morph)

# Define JPH_DEBUG_RENDERER for physics debug drawing (same as editor)
target_compile_definitions(demoSceneSwitching PRIVATE JPH_DEBUG_RENDERER)

target_link_libraries(demoSceneSwitching
	OgreMain
	OgreBites
	OgreOverlay
	OgreMeshLodGenerator
	OgrePaging
	OgreTerrain
	flecs::flecs_static
	nlohmann_json::nlohmann_json
	Jolt::Jolt
	OgreProcedural::OgreProcedural
	RecastNavigation::Recast
	RecastNavigation::Detour
	RecastNavigation::DetourTileCache
	RecastNavigation::DetourCrowd
	RecastNavigation::DebugUtils
	PackageArchive
	RoadGeometryLib
	lua
	SDL2::SDL2
)

target_include_directories(demoSceneSwitching PRIVATE
	${EDITSCENE_SOURCE_DIR}
	${EDITSCENE_SOURCE_DIR}/recastnavigation/Recast/Include
	${EDITSCENE_SOURCE_DIR}/recastnavigation/Detour/Include
	${EDITSCENE_SOURCE_DIR}/recastnavigation/DetourTileCache/Include
	${EDITSCENE_SOURCE_DIR}/recastnavigation/DetourCrowd/Include
	${EDITSCENE_SOURCE_DIR}/recastnavigation/DebugUtils/Include
	${CMAKE_SOURCE_DIR}/src/FastNoiseLite
	${CMAKE_SOURCE_DIR}/src/lua/lua-5.4.8/src
	${CMAKE_SOURCE_DIR}/src/lua/lpeg-1.1.0
)

# Stage the standalone runtime next to the executable (see header comment).
add_custom_command(TARGET demoSceneSwitching POST_BUILD
	COMMAND ${CMAKE_COMMAND}
		-DDEMO_DIR=${CMAKE_CURRENT_BINARY_DIR}
		-DEDITSCENE_BIN=${EDITSCENE_BINARY_DIR}
		-DEDITSCENE_SRC=${EDITSCENE_SOURCE_DIR}
		-DSRC_DIR=${CMAKE_CURRENT_SOURCE_DIR}
		-P "${CMAKE_CURRENT_SOURCE_DIR}/stage_runtime.cmake"
	COMMENT "Staging demo-scene-switching standalone runtime"
)
