Files
GladiatorIdle/Mono/Arena/FighterView.cs

364 lines
17 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using System;
using System.Collections.Generic;
using GladiatorIdle.Gfx;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Clip = GladiatorIdle.Gfx.ClipCache.Clip;
namespace GladiatorIdle.Arena
{
internal static class Stage
{
/// Größe der Kämpfer (1 = früher 560 px hoch); Abstände im Kampf: <see cref="Duel.SCALE"/>.
public const float Scale = (float)Duel.SCALE;
public const float FloorY = 890, FigH = 560 * Scale;
/// Sprites: 768er-Leinwand, Füße bei y = 742, Kopf bei ~26 (716 px Figur).
public const float Canvas = 768, Feet = 742, Body = 716;
}
/// <summary>
/// Darstellung eines Duell-Kämpfers: Pose je Zustand (weich überblendet) auf einem Gitter,
/// das auf der GPU verformt wird — Atmen, Ausholen (Stauchen), Schlag (Strecken, Vorstoß),
/// Treffer (Rückstoß, Wackeln, weißer Blitz), Lehnen beim Laufen. Füße bleiben am Boden.
/// </summary>
internal sealed partial class FighterView
{
const int NX = 6, NY = 14;
const float BLEND = 0.08f;
readonly string _art;
readonly Dictionary<string, Texture2D?> _poses = new();
readonly VertexPositionColorTexture[] _verts = new VertexPositionColorTexture[(NX + 1) * (NY + 1)];
static readonly short[] Indices = BuildIndices();
Texture2D? _cur, _prev;
string _poseKey = "";
float _blend = 1;
DuelState _state = (DuelState)(-1);
double _t;
float _phase;
// Federn: Strecken/Stauchen, Lehnen, Versatz entlang der Blickrichtung, Wackeln
float _sq, _sqV, _lean, _leanV, _off, _offV, _wob;
float _flash, _tint;
Color _tintColor = Color.Red;
float _lastX, _stepDust;
public float X { get; private set; }
public int Facing { get; private set; } = 1;
public Vector2 Chest => new(X + _off * Facing * Stage.Scale, Stage.FloorY - Stage.FigH * 0.58f);
public Vector2 Front => new(X + (150 + _off) * Facing * Stage.Scale, Stage.FloorY - Stage.FigH * 0.55f);
readonly int _color;
/// <param name="color">Teamfarbe (Index in <see cref="Appearance.Colors"/>), −1 = Bild unverändert.</param>
/// <param name="modelColor">Teamfarbe der 3D-Figur (−1: wie <paramref name="color"/>).</param>
public FighterView(string art, float phase, int color = -1, int modelColor = -1)
{
_art = art;
_phase = phase;
_color = color;
InitModel(modelColor >= 0 ? modelColor : color);
}
Texture2D? Pose(string key)
{
if (!_poses.TryGetValue(key, out var t))
{
t = Assets.Sprite(key == "" ? _art : _art + "_" + key);
if (t != null && _color >= 0) t = Assets.Paint(t, _color);
_poses[key] = t;
}
return t;
}
/// Erste vorhandene Pose aus der Liste (sonst das Grundbild).
string Pick(params string[] keys)
{
foreach (var k in keys) if (Pose(k) != null) return k;
return "";
}
// ── Video-Clips (Wan): Bildfolgen je Aktion, auf die Zustände der Kampflogik gelegt ──
Clip? _clip;
string? _clipName, _lastClipName;
float _clipPos;
bool _freePlay; // Clip läuft nach Ende des Zustands weiter (Erholung nach Schlag/Treffer)
const float FPS = 16;
/// Clip aus dem gemeinsamen Zwischenspeicher (null, solange er im Hintergrund lädt).
Clip? GetClip(string action) => ClipCache.Get(_art, action, _color);
/// Vorab laden (Duell-Beginn), damit es mitten im Kampf nicht ruckelt.
public void Preload()
{
if (_model != null) return;
foreach (var a in new[] { "idle", "walk", "attack", "heavy", "block", "hit", "dodge", "victory", "ko" }) GetClip(a);
}
/// Bild des passenden Clips zum Zustand (null = Posen verwenden).
Texture2D? ClipFrame(DuelFighter f, float dt)
{
string name; float pos;
double K(double total) => total > 0 ? Math.Clamp(f.StateTime / total, 0, 1) : 1;
bool heavy = f.Heavy && GetClip("heavy") != null;
Clip? atk = GetClip(heavy ? "heavy" : "attack");
switch (f.State)
{
case DuelState.Windup when atk != null:
name = heavy ? "heavy" : "attack";
pos = (float)(K(heavy ? Duel.HEAVY_WINDUP : f.Windup) * (atk.Impact - 3));
_freePlay = false; break;
case DuelState.Strike when atk != null:
name = heavy ? "heavy" : "attack";
pos = atk.Impact - 3 + (float)K(f.StrikeTime) * 3; break;
case DuelState.Recover when atk != null:
name = heavy ? "heavy" : "attack";
pos = atk.Impact + (float)K(f.RecoverTime) * (atk.Last - atk.Impact) * 0.55f;
_freePlay = true; break;
case DuelState.Block or DuelState.Wall when GetClip("block") is Clip b:
name = "block"; pos = MathF.Min(b.Last, (float)f.StateTime * FPS * 3); _freePlay = false; break;
case DuelState.Hit when GetClip("hit") is Clip h:
name = "hit"; pos = (float)K(0.25) * h.Impact; _freePlay = true; break;
case DuelState.Dodge when GetClip("dodge") is Clip d:
name = "dodge"; pos = (float)K(0.32) * d.Last; _freePlay = false; break;
case DuelState.Down when GetClip("ko") is Clip ko:
name = "ko"; pos = MathF.Min(ko.Last, (float)f.StateTime * FPS); break;
case DuelState.Victory when GetClip("victory") is Clip v:
name = "victory"; pos = MathF.Min(v.Last, (float)f.StateTime * FPS); break;
case DuelState.Ready:
{
float vx = dt > 0 ? (X - _lastX) / dt : 0;
bool moving = MathF.Abs(vx) > 40;
// nach Schlag/Treffer: Clip zu Ende spielen, außer man bewegt sich
if (_freePlay && _clip != null && !moving && _clipPos < _clip.Last)
{
_clipPos += dt * FPS * 1.6f;
return _clip.Frames[(int)MathF.Min(_clip.Last, _clipPos)];
}
_freePlay = false;
if (moving && GetClip("walk") is Clip w)
{
name = "walk";
float dir = MathF.Sign(vx) * Facing; // rückwärts gehen: Clip rückwärts
pos = _clipName == "walk" ? _clipPos + dt * FPS * 1.3f * dir : 0;
pos = ((pos % w.Last) + w.Last) % w.Last;
break;
}
if (GetClip("idle") is Clip idle)
{
name = "idle";
pos = _clipName == "idle" ? (_clipPos + dt * FPS) % idle.Last : 0;
break;
}
_clip = null; _clipName = null; return null;
}
default:
_clip = null; _clipName = null; _freePlay = false; return null;
}
_clip = GetClip(name); _clipName = name; _clipPos = pos;
return _clip!.Frames[Math.Clamp((int)pos, 0, _clip.Last)];
}
static short[] BuildIndices()
{
var idx = new List<short>();
for (int j = 0; j < NY; j++)
for (int i = 0; i < NX; i++)
{
short a = (short)(j * (NX + 1) + i), b = (short)(a + 1), c = (short)(a + NX + 1), d = (short)(c + 1);
idx.AddRange(new[] { a, b, c, b, d, c });
}
return idx.ToArray();
}
// ── Anstöße von außen (Ereignisse) ──────────────────────────
public void Hurt(bool crit)
{
_flash = crit ? 1f : 0.85f;
_tint = 0.55f; _tintColor = new Color(255, 80, 60);
_offV -= crit ? 900 : 600;
_sqV -= crit ? 2.6f : 1.8f;
_wob = crit ? 1f : 0.7f;
}
public void Blocked() { _offV -= 320; _sqV -= 1.0f; _flash = 0.35f; }
public void Parried() { _flash = 1f; _tint = 0.5f; _tintColor = new Color(160, 210, 255); _sqV += 1.5f; }
public void Heal() { _tint = 0.7f; _tintColor = new Color(120, 255, 140); _sqV += 1.2f; }
// ── Takt ────────────────────────────────────────────────────
/// Zustand übernehmen; liefert true bei einem Fußtritt (Staub).
public bool Update(float dt, float realDt, DuelFighter f, float displayX, bool calm)
{
_t += dt;
X = displayX;
Facing = f.Facing;
if (f.State != _state) Enter(f);
_state = f.State;
string key = f.State switch
{
DuelState.Windup => Pick("windup"),
DuelState.Strike => Pick("attack"),
DuelState.Recover => f.StateTime < f.RecoverTime * 0.35 ? Pick("attack") : Pick("recover", ""),
DuelState.Block or DuelState.Wall => Pick("block"),
DuelState.Dodge => Pick("dodge", ""),
DuelState.Hit => Pick("hit"),
DuelState.Stunned => Pick("stagger", "hit"),
DuelState.Down => Pick("ko", "hit"),
DuelState.Victory => Pick("victory"),
DuelState.Leap => Pick("attack"),
DuelState.Super => f.StateTime < Duel.SUPER_CHARGE ? Pick("windup") : Pick("attack"),
_ => Math.Sin(_t * 0.9 + _phase) > 0.55 ? Pick("idle2", "") : ""
};
var clipFrame = _model != null ? null : ClipFrame(f, dt);
if (_model != null) UpdateModel(f, dt, realDt);
else if (clipFrame != null)
{
// Video-Clip: neues Bild ohne Überblendung, nur beim Wechsel des Clips weich
if (_clipName != _lastClipName) { _prev = _cur; _blend = _prev == null ? 1 : 0; _lastClipName = _clipName; }
_cur = clipFrame; _poseKey = "";
}
else
{
if (_lastClipName != null) { _prev = _cur; _blend = 0; _lastClipName = null; _poseKey = "-"; }
if (key != _poseKey) { _prev = _cur; _cur = Pose(key); _poseKey = key; _blend = _prev == null ? 1 : 0; }
}
_cur ??= Pose("");
_blend = MathF.Min(1, _blend + realDt / BLEND); // in Echtzeit, auch im Trefferstopp
// Federn
Spring(ref _sq, ref _sqV, 0, 260, 14, dt);
Spring(ref _off, ref _offV, 0, 140, 16, dt);
float vx = dt > 0 ? (X - _lastX) / dt : 0;
float leanTarget = f.State == DuelState.Ready ? MathHelper.Clamp(vx * Facing / 440f, -1, 1) * 0.07f : 0;
if (f.State == DuelState.Windup) leanTarget = -0.06f;
if (f.State == DuelState.Strike) leanTarget = 0.09f;
if (f.State == DuelState.Stunned) leanTarget = 0.05f * MathF.Sin((float)_t * 6);
Spring(ref _lean, ref _leanV, leanTarget, 180, 18, dt);
// Blitz und Färbung in Echtzeit (auch während Trefferstopp und Zeitlupe)
_wob = MathF.Max(0, _wob - dt * 3.5f);
_flash = MathF.Max(0, _flash - realDt * 7);
_tint = MathF.Max(0, _tint - realDt * 2.5f);
if (calm) { _sq *= 0.3f; _wob = 0; }
bool step = false;
if (MathF.Abs(vx) > 60 && f.State == DuelState.Ready)
{
_stepDust -= dt;
if (_stepDust <= 0) { _stepDust = 0.22f; step = true; }
}
_lastX = X;
return step;
}
void Enter(DuelFighter f)
{
switch (f.State)
{
case DuelState.Windup: _sqV -= f.Heavy ? 2.2f : 1.2f; _offV -= 200; break; // Ausholen: zusammenziehen
case DuelState.Strike: _sqV += f.Heavy ? 3.5f : 2.4f; _offV += f.Heavy ? 1300 : 900; break; // Vorstoß
case DuelState.Leap: _sqV -= 1.8f; break;
case DuelState.Super: _sqV -= 2.0f; break;
case DuelState.Dodge: _sqV -= 1.5f; break;
case DuelState.Victory: _sqV += 2f; break;
case DuelState.Down: _sqV -= 2.5f; break;
}
}
static void Spring(ref float x, ref float v, float target, float k, float c, float dt)
{
v += (-(x - target) * k - v * c) * dt;
x += v * dt;
}
// ── Zeichnen ────────────────────────────────────────────────
/// Bodenschatten (SpriteBatch, Kamera-Matrix).
public void DrawShadow(SpriteBatch sb)
{
float w = 270 * Stage.Scale * (1 + _sq * 0.4f);
Draw.Glow(sb, new Vector2(X + _off * Facing * 0.5f * Stage.Scale, Stage.FloorY + 4), new Vector2(w, 58 * Stage.Scale), Color.Black * 0.38f);
}
/// Leuchten hinter der Figur (Silhouette, leicht vergrößert): angekündigter Schlag, Schildwall.
public void DrawAura(GraphicsDevice gd, BasicEffect fx, Color c, float grow)
{
if (_model != null) { if (c.A > 0) DrawModel(gd, fx, c, grow); return; }
if (_cur == null || c.A == 0) return;
Quad(gd, fx, Assets.White(_cur), 1 + grow, c, 0);
}
public void DrawBody(GraphicsDevice gd, BasicEffect fx, DuelFighter f, bool calm)
{
if (_model != null) { DrawModel(gd, fx, Color.Transparent, 0); return; }
if (_cur == null) return;
float alpha = f.State == DuelState.Down ? MathF.Max(0.55f, 1 - (float)f.StateTime * 0.3f) : 1f;
// Ausweichen: Nachbilder
if (f.State == DuelState.Dodge && !calm)
for (int k = 1; k <= 2; k++)
{
Quad(gd, fx, _cur, 1, Color.White * (0.22f / k), -Facing * 70 * k);
}
if (_prev != null && _blend < 1)
{
Quad(gd, fx, _prev, 1, Color.White * (alpha * (1 - _blend)), 0);
}
// neue Pose sofort voll deckend; nur die alte blendet darunter aus (sonst scheint
// die Figur beim Wechsel durch)
Quad(gd, fx, _cur, 1, Color.White * alpha, 0);
if (_tint > 0) { Quad(gd, fx, Assets.White(_cur), 1, _tintColor * (_tint * 0.5f), 0); }
if (_flash > 0) { Quad(gd, fx, Assets.White(_cur), 1, Color.White * (_flash * 0.55f), 0); }
}
/// Gitter aufbauen: Füße fest am Boden, Verformung wächst mit der Höhe.
void Quad(GraphicsDevice gd, BasicEffect fx, Texture2D tex, float scale, Color color, float shiftX)
{
// Clip-Bilder sind auf den Bewegungsbereich zugeschnitten: Lage in der vollen Leinwand
if (ClipCache.CropOf(tex) is (Vector4 crop, float aspect))
Build(scale, color, shiftX, Stage.Canvas * aspect, crop);
else
Build(scale, color, shiftX, Stage.Canvas * tex.Width / tex.Height, new Vector4(0, 0, 1, 1));
Mesh(gd, fx, tex);
}
void Build(float scale, Color color, float shiftX, float canvasW, Vector4 crop)
{
float s = Stage.FigH / Stage.Body * scale;
float breathe = _clip != null ? 0 : 0.012f * MathF.Sin((float)_t * 2.3f + _phase);
float sy = 1 + _sq * 0.10f + breathe, sx = 1 - _sq * 0.06f - breathe * 0.5f;
float baseX = X + (_off * Facing * Stage.Scale) * (_clip != null ? 0.35f : 1) + shiftX * Stage.Scale;
for (int j = 0; j <= NY; j++)
{
float v = (float)j / NY, cv = crop.Y + v * crop.W;
float py = (cv * Stage.Canvas - Stage.Feet) * s; // < 0 über den Füßen
float h = MathHelper.Clamp(-py / (Stage.Body * s), 0, 1.1f); // 0 Füße … 1 Kopf
float lean = _lean * h * Stage.FigH * Facing;
float wob = _wob * 22 * MathF.Sin(h * 5.5f - (float)_t * 38) * h;
for (int i = 0; i <= NX; i++)
{
float u = (float)i / NX, cu = crop.X + u * crop.Z;
float px = (cu - 0.5f) * canvasW * s * sx * Facing;
var pos = new Vector3(baseX + px + lean + wob, Stage.FloorY + py * sy, 0);
_verts[j * (NX + 1) + i] = new VertexPositionColorTexture(pos, color, new Vector2(u, v));
}
}
}
void Mesh(GraphicsDevice gd, BasicEffect fx, Texture2D tex)
{
fx.Texture = tex;
foreach (var pass in fx.CurrentTechnique.Passes)
{
pass.Apply();
gd.DrawUserIndexedPrimitives(PrimitiveType.TriangleList, _verts, 0, _verts.Length, Indices, 0, Indices.Length / 3);
}
}
}
}