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

129 lines
4.6 KiB
C#

// ------------------------------------------------------------------------
// Copyright (c) 2026, Jiaqi (0x7c13) Liu. All rights reserved.
// See LICENSE file in the project root for license information.
// ------------------------------------------------------------------------
namespace Pal3.BattleEditor
{
using System;
using Battle;
using ResourceBrowser;
using UnityEngine;
using Object = UnityEngine.Object;
public sealed class BattleTrailPreview : IResourcePreviewFrameEffect, IDisposable
{
private readonly BattleTrailState _state = new();
private readonly RenderTexture[] _history = new RenderTexture[BattleTrailState.Capacity];
private RenderTexture _output;
private Material _material;
private bool _disposed;
public bool Enabled
{
get; set;
}
public bool Suspended
{
get; set;
}
private bool Applies => Enabled && !Suspended;
public Rect Viewport => Applies ? new Rect(0, 0, 1, .75f) : new Rect(0, 0, 1, 1);
public Vector2Int TargetSize(int requestedWidth, int requestedHeight) => Applies ? new Vector2Int(256, 256) : new Vector2Int(requestedWidth, requestedHeight);
public void Advance(double scaledSeconds, double absoluteSeconds) => _state.Advance(scaledSeconds);
public RenderTexture Composite(RenderTexture scene, int requestedWidth, int requestedHeight)
{
if (_disposed)
throw new ObjectDisposedException(nameof(BattleTrailPreview));
if (!Applies)
return scene;
RenderTexture previous = RenderTexture.active;
try
{
EnsureResources(requestedWidth, requestedHeight);
Graphics.Blit(scene, _history[_state.CurrentTexture]);
RenderTexture.active = _output;
GL.Clear(false, true, Color.black);
for (int order = 0; order < BattleTrailState.Capacity; order++)
{
int alpha = BattleTrailState.AlphaAt(order);
_material.SetVector("_Diffuse", new Vector4((alpha / 2) / 255f, (alpha / 2) / 255f, (alpha / 2) / 255f, alpha / 255f));
Graphics.Blit(_history[_state.TextureAt(order)], _output, _material);
}
}
finally
{
RenderTexture.active = previous;
}
return _output;
}
private void EnsureResources(int width, int height)
{
if (_material == null)
{
Shader shader = Shader.Find("Hidden/Pal3/BattleTrail");
if (shader == null || !shader.isSupported)
throw new InvalidOperationException("The original battle trail shader is unavailable.");
_material = new Material(shader) { hideFlags = HideFlags.HideAndDontSave };
}
for (int index = 0; index < _history.Length; index++)
if (_history[index] == null)
{
_history[index] = Create(256, 256);
RenderTexture previous = RenderTexture.active;
RenderTexture.active = _history[index];
GL.Clear(false, true, Color.black);
RenderTexture.active = previous;
}
if (_output != null && _output.width == width && _output.height == height)
return;
Release(_output);
_output = Create(width, height);
}
private static RenderTexture Create(int width, int height)
{
var texture = new RenderTexture(width, height, 0, RenderTextureFormat.ARGB32)
{
hideFlags = HideFlags.HideAndDontSave,
filterMode = FilterMode.Bilinear,
wrapMode = TextureWrapMode.Clamp
};
texture.Create();
return texture;
}
private static void Release(RenderTexture texture)
{
if (texture == null)
return;
texture.Release();
Object.DestroyImmediate(texture);
}
public void Dispose()
{
if (_disposed)
return;
_disposed = true;
Release(_output);
_output = null;
for (int index = 0; index < _history.Length; index++)
{
Release(_history[index]);
_history[index] = null;
}
if (_material != null)
Object.DestroyImmediate(_material);
_material = null;
}
}
}