mirror of
https://github.com/0x7c13/Pal3.Unity.git
synced 2026-11-26 09:28:07 +08:00
* 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.
246 lines
8.8 KiB
C#
246 lines
8.8 KiB
C#
// ------------------------------------------------------------------------
|
|
// Copyright (c) 2026, Jiaqi (0x7c13) Liu. All rights reserved.
|
|
// See LICENSE file in the project root for license information.
|
|
// ------------------------------------------------------------------------
|
|
|
|
namespace Pal3.Game.Effect
|
|
{
|
|
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using Runtime;
|
|
using Core.FileSystem;
|
|
using Core.Command.SceCommands;
|
|
using Engine.Core.Abstraction;
|
|
using Engine.Core.Implementation;
|
|
using Engine.Extensions;
|
|
using Engine.Logging;
|
|
using UnityEngine;
|
|
|
|
internal sealed class EffectRuntime : IDisposable
|
|
{
|
|
private readonly EffectResources _resources;
|
|
private readonly HashSet<int> _failures = new();
|
|
private readonly List<IGameEntity> _playedEffects = new();
|
|
private readonly List<PersistentEffect> _persistentEffects = new();
|
|
private int _preloadGeneration;
|
|
private bool _disposed;
|
|
|
|
internal EffectResources Resources => _resources;
|
|
|
|
internal EffectRuntime(ICpkFileSystem fileSystem, int codepage)
|
|
{
|
|
_resources = new EffectResources(fileSystem, codepage);
|
|
}
|
|
|
|
internal IEnumerator PreloadAsync(int groupId)
|
|
{
|
|
if (_disposed)
|
|
yield break;
|
|
int generation = _preloadGeneration;
|
|
EffectOverrides edits;
|
|
try
|
|
{
|
|
edits = UnityEngine.Resources.Load<EffectOverrides>(_resources.OverridesPath);
|
|
string[] issues = _resources.Diagnose(groupId, edits);
|
|
if (issues.Length != 0)
|
|
{
|
|
Report(groupId, string.Join("\n", issues));
|
|
yield break;
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Report(groupId, ex.Message);
|
|
yield break;
|
|
}
|
|
using var preload = _resources.Preload(groupId, edits).GetEnumerator();
|
|
while (!_disposed && generation == _preloadGeneration)
|
|
{
|
|
bool next;
|
|
try
|
|
{
|
|
next = preload.MoveNext();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Report(groupId, ex.Message);
|
|
break;
|
|
}
|
|
if (!next)
|
|
break;
|
|
yield return null;
|
|
}
|
|
}
|
|
|
|
private void Report(int groupId, string message)
|
|
{
|
|
if (_failures.Add(groupId))
|
|
EngineLogger.LogWarning($"Effect {groupId}: {message}");
|
|
}
|
|
|
|
private IGameEntity Create(int groupId, IGameEntity parent, uint portalType = 0)
|
|
{
|
|
if (_disposed || parent.IsNullOrDisposed())
|
|
return null;
|
|
GameObject root = null;
|
|
try
|
|
{
|
|
var edits = UnityEngine.Resources.Load<EffectOverrides>(_resources.OverridesPath);
|
|
string[] issues = groupId == -1 ? Array.Empty<string>() : _resources.Diagnose(groupId, edits);
|
|
if (issues.Length != 0)
|
|
{
|
|
Report(groupId, string.Join("\n", issues));
|
|
return null;
|
|
}
|
|
root = new GameObject(groupId == -1 ? "Effect transport" : $"Effect {groupId}");
|
|
root.transform.SetParent((Transform)parent.Transform.NativeObject, false);
|
|
if (groupId == -1)
|
|
root.AddComponent<PortalEffectPlayer>().Initialize(_resources, portalType);
|
|
else
|
|
{
|
|
var player = root.AddComponent<EffectPlayer>();
|
|
player.Initialize(_resources, groupId, (uint)UnityEngine.Random.Range(1, int.MaxValue), overrides: edits,
|
|
sound: name => Pal3.Instance.Execute(new PlaySfxCommand(name, 1)));
|
|
player.DestroyOnComplete = true;
|
|
if (player.Error != null)
|
|
throw new InvalidOperationException(player.Error);
|
|
}
|
|
return new GameEntity(root);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
if (root != null)
|
|
{
|
|
Stop(new GameEntity(root));
|
|
}
|
|
Report(groupId, ex.Message);
|
|
return null;
|
|
}
|
|
}
|
|
|
|
internal bool Play(int groupId, IGameEntity parent, Vector3 localPosition)
|
|
{
|
|
if (Create(groupId, parent) is not { } instance)
|
|
return false;
|
|
instance.Transform.LocalPosition += localPosition;
|
|
_playedEffects.RemoveAll(effect => effect.IsNativeObjectDisposed);
|
|
_playedEffects.Add(instance);
|
|
return true;
|
|
}
|
|
|
|
internal IDisposable AttachPortal(IGameEntity parent, uint type = 0) => Attach(-1, parent, type);
|
|
|
|
internal IDisposable Attach(int groupId, IGameEntity parent, uint portalType = 0)
|
|
{
|
|
if (_disposed || parent.IsNullOrDisposed())
|
|
return null;
|
|
PruneDeadAttachments();
|
|
var effect = new PersistentEffect(this, groupId, parent, portalType);
|
|
_persistentEffects.Add(effect);
|
|
return effect;
|
|
}
|
|
|
|
internal void ClearPlayedEffects()
|
|
{
|
|
foreach (IGameEntity effect in _playedEffects)
|
|
Stop(effect);
|
|
_playedEffects.Clear();
|
|
InvalidatePreloads();
|
|
_failures.Clear();
|
|
if (_disposed)
|
|
return;
|
|
if (Application.isPlaying && Pal3.Instance != null)
|
|
Pal3.Instance.StartCoroutine(TrimSceneRenderAssetsAfterFrame());
|
|
else
|
|
_resources.TrimUnusedRenderAssets(LiveRoots());
|
|
}
|
|
|
|
private IEnumerator TrimSceneRenderAssetsAfterFrame()
|
|
{
|
|
// Scene objects are destroyed at the end of the leaving frame.
|
|
yield return null;
|
|
if (_disposed)
|
|
yield break;
|
|
PruneDeadAttachments();
|
|
_resources.TrimUnusedRenderAssets(LiveRoots());
|
|
}
|
|
|
|
private IEnumerable<GameObject> LiveRoots()
|
|
{
|
|
foreach (IGameEntity effect in _playedEffects)
|
|
if (!effect.IsNullOrDisposed())
|
|
yield return (GameObject)effect.NativeObject;
|
|
foreach (PersistentEffect effect in _persistentEffects)
|
|
if (effect.Root != null)
|
|
yield return effect.Root;
|
|
}
|
|
|
|
private static void Stop(IGameEntity effect)
|
|
{
|
|
if (effect.IsNullOrDisposed())
|
|
return;
|
|
var root = (GameObject)effect.NativeObject;
|
|
root.GetComponent<EffectPlayer>()?.Dispose();
|
|
root.GetComponent<PortalEffectPlayer>()?.Dispose();
|
|
root.SetActive(false);
|
|
effect.Destroy();
|
|
}
|
|
|
|
private void PruneDeadAttachments()
|
|
{
|
|
for (int i = _persistentEffects.Count - 1; i >= 0; i--)
|
|
if (_persistentEffects[i].ParentIsDisposed)
|
|
_persistentEffects[i].Dispose();
|
|
}
|
|
|
|
internal void InvalidatePreloads()
|
|
{
|
|
PruneDeadAttachments();
|
|
_preloadGeneration++;
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
if (_disposed)
|
|
return;
|
|
_disposed = true;
|
|
ClearPlayedEffects();
|
|
foreach (PersistentEffect effect in _persistentEffects.ToArray())
|
|
effect.Dispose();
|
|
_resources.Dispose();
|
|
}
|
|
|
|
private sealed class PersistentEffect : IDisposable
|
|
{
|
|
private readonly EffectRuntime _owner;
|
|
private readonly IGameEntity _parent;
|
|
private IGameEntity _instance;
|
|
|
|
internal bool ParentIsDisposed => _parent.IsNullOrDisposed();
|
|
|
|
internal GameObject Root => _instance.IsNullOrDisposed() ? null : (GameObject)_instance.NativeObject;
|
|
|
|
internal PersistentEffect(EffectRuntime owner, int groupId, IGameEntity parent, uint portalType)
|
|
{
|
|
_owner = owner;
|
|
_parent = parent;
|
|
_instance = owner.Create(groupId, parent, portalType);
|
|
if (Root != null && Root.GetComponent<EffectPlayer>() is { } player)
|
|
{
|
|
player.DestroyOnComplete = false;
|
|
player.Loop = true;
|
|
player.RepeatInterval = owner._resources.GameVariant == "PAL3" && groupId == 275 ? 9.5f : 0;
|
|
}
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
_owner._persistentEffects.Remove(this);
|
|
Stop(_instance);
|
|
_instance = null;
|
|
}
|
|
}
|
|
}
|
|
}
|