Files
Jiaqi Liu 5d0f564a00 Add a standalone battle editor and battle system for PAL3 and PAL3A (#99)
* build: migrate C# formatting and add offscreen editor test support

Host IMGUI tests on a detached panel without creating a native editor window.

* feat(core): decode native battle records, geometry and animation metadata

Read the two game variants’ battle tables and model data, with binary-reader and original-resource coverage.

* feat(battle): implement deterministic combat rules and action settlement

Implement actors, inventory, formation, action timing, status interruption, AI, stage ownership and deterministic replay. Keep the domain and its regression suite independent of Unity.

* feat(battle): project native HUD state and input commands

Add the presentation assembly and tests for command routing, lineup selection, floating numbers and stage messages.

* feat(rendering): add battle lighting, actor appearance and native shadows

Support baked and toon lighting, actor and weapon rendering, display refresh rate and resource ownership.

* feat(effects): drive battle effects through shared resource previews

Bind battle control nodes to action owners. Share preview lighting, geometry and effect resources with the existing Unity editors.

* refactor(game): isolate story battle targeting and actor lifetimes

Preserve scene ownership, bound story battle targeting and stop debug attacks when their actor leaves the scene.

* feat(editor): compile original game data into battle scenarios

Build per-variant animation and ability catalogs, encounter music, scene lighting profiles, random parties, equipment and test inventory from the original resources.

* feat(editor): play battle sessions with animated previews and synced media

Drive the preview scene, camera, effects, hit feedback, result choreography, music and movies from simulation snapshots and events.

* feat(editor): add the battle workbench, native HUD and input controls

Expose scenario authoring, replay, diagnostics, responsive command menus and mouse, touch and gamepad targeting. Include the editor workflow and HUD integration tests.
2026-11-21 22:28:47 +08:00

281 lines
10 KiB
C#

// ------------------------------------------------------------------------
// Copyright (c) 2021-2026, Jiaqi (0x7c13) Liu. All rights reserved.
// See LICENSE file in the project root for license information.
// ------------------------------------------------------------------------
namespace Pal3.Game.Scene
{
using System;
using System.Collections.Generic;
using System.Diagnostics;
using Actor.Controllers;
using Command;
using Command.Extensions;
using Core.Command;
using Core.Command.SceCommands;
using Core.Contract.Constants;
using Core.DataReader.Sce;
using Core.DataReader.Scn;
using Core.Utilities;
using Data;
using Engine.Core.Abstraction;
using Engine.Core.Implementation;
using Engine.Extensions;
using Engine.Logging;
using GameSystems.Battle.Scene;
using Newtonsoft.Json;
using Script;
using State;
using UnityEngine;
public sealed class SceneManager : IDisposable,
ICommandExecutor<SceneLoadCommand>,
ICommandExecutor<SceneObjectDoNotLoadFromSaveStateCommand>,
ICommandExecutor<ResetGameStateCommand>
{
private readonly GameResourceProvider _resourceProvider;
private readonly ISceneObjectFactory _sceneObjectFactory;
private readonly SceneStateManager _sceneStateManager;
private readonly ScriptManager _scriptManager;
private readonly IGameEntity _cameraEntity;
private IGameEntity _currentSceneRoot;
private IGameEntity _currentBattleSceneRoot;
private GameScene _currentGameScene;
private BattleScene _currentBattleScene;
private readonly HashSet<int> _sceneObjectIdsToNotLoadFromSaveState = new ();
public SceneManager(GameResourceProvider resourceProvider,
ISceneObjectFactory sceneObjectFactory,
SceneStateManager sceneStateManager,
ScriptManager scriptManager,
IGameEntity cameraEntity)
{
_resourceProvider = Requires.IsNotNull(resourceProvider, nameof(resourceProvider));
_sceneObjectFactory = Requires.IsNotNull(sceneObjectFactory, nameof(sceneObjectFactory));
_sceneStateManager = Requires.IsNotNull(sceneStateManager, nameof(sceneStateManager));
_scriptManager = Requires.IsNotNull(scriptManager, nameof(scriptManager));
_cameraEntity = Requires.IsNotNull(cameraEntity, nameof(cameraEntity));
CommandExecutorRegistry<ICommand>.Instance.Register(this);
}
public void Dispose()
{
DisposeCurrentScene();
CommandExecutorRegistry<ICommand>.Instance.UnRegister(this);
}
public GameScene GetCurrentScene()
{
return _currentGameScene;
}
public BattleScene GetCurrentBattleScene()
{
return _currentBattleScene;
}
public IGameEntity GetSceneRootGameEntity()
{
if (_currentBattleSceneRoot != null)
{
return _currentBattleSceneRoot;
}
else if (_currentSceneRoot != null)
{
return _currentSceneRoot;
}
return null;
}
public GameScene LoadScene(string sceneCityName, string sceneName)
{
Stopwatch timer = Stopwatch.StartNew();
DisposeCurrentScene();
ScnFile scnFile = _resourceProvider.GetGameResourceFile<ScnFile>(
FileConstants.GetScnFileVirtualPath(sceneCityName, sceneName));
Pal3.Instance.Execute(new ScenePreLoadingNotification(scnFile.SceneInfo));
EngineLogger.Log($"Loading scene: {JsonConvert.SerializeObject(scnFile.SceneInfo)}");
_currentSceneRoot = GameEntityFactory.Create($"Scene_{sceneCityName}_{sceneName}",
null, worldPositionStays: false);
_currentGameScene = _currentSceneRoot.AddComponent<GameScene>();
_currentGameScene.Init(_resourceProvider,
_sceneObjectFactory,
_sceneStateManager,
_resourceProvider.GetMaterialManager().UsesToonRendering,
_cameraEntity,
_sceneObjectIdsToNotLoadFromSaveState);
_currentGameScene.Load(scnFile, _currentSceneRoot);
// Must to clear the exclude list after loading the scene.
_sceneObjectIdsToNotLoadFromSaveState.Clear();
// Add scene script if exists.
SceFile sceFile = _resourceProvider.GetGameResourceFile<SceFile>(
FileConstants.GetSceneSceFileVirtualPath(sceneCityName));
Pal3.Instance.Execute(
_scriptManager.TryAddSceneScript(sceFile, $"_{sceneCityName}_{sceneName}", out var sceneScriptId)
? new ScenePostLoadingNotification(scnFile.SceneInfo, sceneScriptId)
: new ScenePostLoadingNotification(scnFile.SceneInfo, ScriptConstants.InvalidScriptId));
EngineLogger.Log($"Scene loaded in {timer.ElapsedMilliseconds} ms");
// Also a good time to collect garbage
System.GC.Collect();
return _currentGameScene;
}
public BattleScene LoadBattleScene(string combatSceneName)
{
Stopwatch timer = Stopwatch.StartNew();
if (_currentBattleSceneRoot != null)
UnloadBattleScene();
try
{
HideCurrentScene();
_currentBattleSceneRoot = GameEntityFactory.Create($"CombatScene_{combatSceneName}",
null, worldPositionStays: false);
_currentBattleScene = _currentBattleSceneRoot.AddComponent<BattleScene>();
_currentBattleScene.Init(_resourceProvider);
_currentBattleScene.Load(_currentBattleSceneRoot,
combatSceneName,
_resourceProvider.GetMaterialManager().UsesToonRendering);
}
catch
{
UnloadBattleScene();
throw;
}
EngineLogger.Log($"BattleScene loaded in {timer.ElapsedMilliseconds} ms");
// Also a good time to collect garbage
System.GC.Collect();
return _currentBattleScene;
}
public void UnloadBattleScene()
{
DisposeBattleScene();
ShowCurrentScene();
}
private void DisposeBattleScene()
{
if (_currentBattleSceneRoot?.NativeObject is GameObject root && root != null)
{
root.SetActive(false);
_currentBattleSceneRoot.Destroy();
}
_currentBattleSceneRoot = null;
_currentBattleScene = null;
}
private readonly IList<Renderer> _temporarilyDisabledRenderers = new List<Renderer>();
private readonly IList<GameObject> _temporarilyDisabledGameObjects = new List<GameObject>();
private void HideCurrentScene()
{
_currentGameScene?.SetLightingActive(false);
_temporarilyDisabledRenderers.Clear();
_temporarilyDisabledGameObjects.Clear();
foreach (ActorMovementController movementController in
_currentSceneRoot.GetComponentsInChildren<ActorMovementController>())
{
movementController.PauseMovement();
}
foreach (Renderer renderer in _currentSceneRoot.GetComponentsInChildren<Renderer>())
{
if (renderer.enabled)
{
renderer.enabled = false;
_temporarilyDisabledRenderers.Add(renderer);
}
}
foreach (ParticleSystem particleSystem in _currentSceneRoot.GetComponentsInChildren<ParticleSystem>())
{
GameObject particleGo = particleSystem.gameObject;
if (particleGo.activeInHierarchy)
{
particleGo.SetActive(false);
_temporarilyDisabledGameObjects.Add(particleGo);
}
}
// Move the scene root to a lower position just to be safe.
_currentSceneRoot.Transform.LocalPosition = Vector3.down * 100f;
}
private void ShowCurrentScene()
{
_currentGameScene?.SetLightingActive(true);
// Move the scene root back to the original position.
_currentSceneRoot.Transform.LocalPosition = Vector3.zero;
foreach (GameObject disabledGo in _temporarilyDisabledGameObjects)
{
if (disabledGo != null) disabledGo.SetActive(true);
}
foreach (Renderer renderer in _temporarilyDisabledRenderers)
{
if (renderer != null) renderer.enabled = true;
}
foreach (ActorMovementController movementController in
_currentSceneRoot.GetComponentsInChildren<ActorMovementController>())
{
movementController.ResumeMovement();
}
_temporarilyDisabledRenderers.Clear();
_temporarilyDisabledGameObjects.Clear();
}
private void DisposeCurrentScene()
{
_temporarilyDisabledRenderers.Clear();
_temporarilyDisabledGameObjects.Clear();
if (_currentGameScene != null)
{
Pal3.Instance.Execute(new SceneLeavingCurrentSceneNotification());
_currentGameScene.Destroy();
_currentGameScene = null;
}
DisposeBattleScene();
if (_currentSceneRoot != null)
{
_currentSceneRoot.Destroy();
_currentSceneRoot = null;
}
}
public void Execute(SceneLoadCommand command) => LoadScene(command.SceneCityName, command.SceneName);
public void Execute(ResetGameStateCommand command) => DisposeCurrentScene();
public void Execute(SceneObjectDoNotLoadFromSaveStateCommand command)
{
_sceneObjectIdsToNotLoadFromSaveState.Add(command.ObjectId);
}
}
}