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

127 lines
5.4 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.Runtime
{
using Core.DataReader.Data;
using Core.DataReader.Par;
using UnityEngine;
internal sealed class SparNode : EffectNode
{
private readonly SparFile _file;
private readonly SparEmitter[] _emitters;
private readonly EffectMeshBuffer[] _meshes;
private bool _running;
private float _dt;
public SparNode(GameObject root, EffectDefinition d, EffectTask[] tasks, Vector3 origin, float birth,
EffectResources resources, uint seed) : base(root, d, tasks, origin, birth)
{
_file = resources.Spar(d.ParticleFileName);
_emitters = new SparEmitter[_file.Groups.Length];
_meshes = new EffectMeshBuffer[_emitters.Length];
try
{
for (int i = 0; i < _emitters.Length; i++)
{
SparGroup group = _file.Groups[i];
_emitters[i] = new SparEmitter(group, seed + (uint)i);
var child = new GameObject(group.Name) { hideFlags = HideFlags.DontSave, layer = root.layer };
child.transform.SetParent(root.transform, false);
_meshes[i] = new EffectMeshBuffer(child, resources.SparMaterial(EffectResources.RelativeTo(d.ParticleFileName, group.Texture), _file.Shader), reverseWinding: true);
}
}
catch
{
foreach (EffectMeshBuffer mesh in _meshes)
mesh?.Dispose();
throw;
}
}
public override int QuadCount
{
get
{
int count = 0;
foreach (EffectMeshBuffer mesh in _meshes)
count += mesh.QuadCount;
return count;
}
}
public override void Step(float time, float dt)
{
// CEffSParticle updates the system before processing effect tasks.
_dt = dt;
if (_running)
foreach (SparEmitter emitter in _emitters)
emitter.Step(dt);
base.Step(time, dt);
}
protected override void Apply(EffectTask task, EffectTaskClock clock, float dt)
{
if (clock.StartedNow)
{
foreach (SparEmitter emitter in _emitters)
emitter.Start();
_running = true;
}
// SPar's adapter uses the complete frame delta, ignores stretch,
// task rotation/color and InitSize, and applies translation at draw.
Position += new Vector3(task.SpeedX, task.SpeedY, task.SpeedZ) * _dt;
Orbit = OrbitPosition(new Vector3(task.CircleAxisX, task.CircleAxisY, task.CircleAxisZ),
(LocalTime - clock.StartedAt) * task.CircleSpeed, task.CircleRadius);
if (clock.Finished)
{
_running = false; // native Stop clears the pool immediately
foreach (SparEmitter emitter in _emitters)
emitter.Stop();
}
}
public override void Render(Camera camera)
{
Vector3 right = camera == null ? Vector3.right : Root.transform.InverseTransformVector(camera.transform.right);
Vector3 up = camera == null ? Vector3.up : Root.transform.InverseTransformVector(camera.transform.up);
Quaternion rotation = RawRotation(Rotation);
for (int i = 0; i < _emitters.Length; i++)
{
EffectMeshBuffer mesh = _meshes[i];
mesh.Clear();
if (Visible && _running)
{
SparEmitter emitter = _emitters[i];
for (int j = 0; j < emitter.Count; j++)
{
SparEmitter.Unit u = emitter.Units[j];
Vector3 center = UnityPosition(Position + Orbit + rotation * u.Position);
float sin = Mathf.Sin(u.Rotation), cos = Mathf.Cos(u.Rotation);
// Native SPar builds the quad directly in view space:
// positive radians, top-left (su,sv), unlike sgpar.
Vector3 x = (right * cos + up * sin) * (u.Size / 20);
Vector3 y = (-right * sin + up * cos) * (u.Size / 20);
float[] uv = _file.Groups[i].TextureFrames[u.Frame];
var color = new Color32((byte)(Mathf.Clamp01(u.Color.x) * 255), (byte)(Mathf.Clamp01(u.Color.y) * 255),
(byte)(Mathf.Clamp01(u.Color.z) * 255), (byte)(Mathf.Clamp01(u.Alpha) * 255));
mesh.Quad(center - x - y, center + x - y, center + x + y, center - x + y,
new Vector2(uv[0], 1 - uv[3]), new Vector2(uv[2], 1 - uv[1]), color);
}
}
mesh.Upload();
}
}
public override void Dispose()
{
foreach (EffectMeshBuffer mesh in _meshes)
mesh?.Dispose();
base.Dispose();
}
}
}