257 lines
14 KiB
C#
257 lines
14 KiB
C#
using System;
|
||
using GladiatorIdle.Gfx;
|
||
using Microsoft.Xna.Framework;
|
||
using Microsoft.Xna.Framework.Graphics;
|
||
|
||
namespace GladiatorIdle.Arena
|
||
{
|
||
/// 3D-Darstellung eines Kämpfers (Mixamo-Bewegungen, siehe <see cref="Model3D"/>): gibt es für das
|
||
/// Bild ein Modell (Assets/Models/<art>.gim), sonst das Standardmodell, zeichnet die Figur in 3D
|
||
/// statt der Sprites. Zustände der Kampflogik werden auf Clips gelegt, der Treffer fällt auf den
|
||
/// Moment, in dem der Schwertarm am weitesten vorn ist.
|
||
internal sealed partial class FighterView
|
||
{
|
||
public const string DefaultModel = "hero_murmillo_1";
|
||
|
||
Model3D? _model;
|
||
BasicEffect? _mfx;
|
||
Matrix _fit;
|
||
string _mClip = "";
|
||
float _mTime, _mBlend = 1;
|
||
bool _mFree;
|
||
float _lift; // Höhe über dem Boden (Sprungangriff)
|
||
|
||
public bool Is3D => _model != null;
|
||
|
||
/// Teamfarbe der 3D-Figur nachträglich ändern (Vorschaubilder der Erstellung).
|
||
public void SetModelColor(int color) => _model?.SetColor(color);
|
||
|
||
void InitModel(int color)
|
||
{
|
||
var sw = System.Diagnostics.Stopwatch.StartNew();
|
||
_model = Model3D.Load(_art);
|
||
double tLoad = sw.Elapsed.TotalMilliseconds;
|
||
bool own = _model != null;
|
||
foreach (var alt in Fallbacks(_art)) _model ??= Model3D.Load(alt);
|
||
_model ??= Model3D.Load(DefaultModel);
|
||
// Vorlage mit dunkler Haut, aber noch ohne eigenes Modell: Haut des Ersatzmodells abdunkeln
|
||
if (!own && _model != null && DarkSkin.Contains(_art)) _model.SetSkin(1);
|
||
else if (!own && _model != null && _art.StartsWith("hero_") && _art.EndsWith("_3")) _model.SetSkin(-1); // Vorlage 3: helle Haut
|
||
if (_model == null || _model.Clips.Count == 0) { _model = null; return; }
|
||
double tSkin = sw.Elapsed.TotalMilliseconds;
|
||
_model.Arm(_art);
|
||
double tArm = sw.Elapsed.TotalMilliseconds;
|
||
_model.SetColor(color);
|
||
var idle = Clip3("idle");
|
||
_model.Pose(idle, 0, true);
|
||
double tColor = sw.Elapsed.TotalMilliseconds;
|
||
// Einpassen hängt nur von der Figur ab (sortiert alle Punkte): einmal je Bild-Name rechnen
|
||
if (!_fitCache.TryGetValue(_art, out _fit)) _fitCache[_art] = _fit = _model.FitNow();
|
||
double tFit = sw.Elapsed.TotalMilliseconds;
|
||
foreach (var c in new[] { "slash", "slash2", "slash3", "attack", "attack2" }) _model.ImpactTime(Clip3(c));
|
||
if (DevShot.Active) DevShot.Log($"timing: fighter {_art}: load {tLoad:0} skin {tSkin - tLoad:0} arm {tArm - tSkin:0} color {tColor - tArm:0} fit {tFit - tColor:0} impact {sw.Elapsed.TotalMilliseconds - tFit:0} ms");
|
||
_mfx = new BasicEffect(Assets.Device) { VertexColorEnabled = true, LightingEnabled = true, PreferPerPixelLighting = true };
|
||
// Kamera-Raum der Arena: x rechts, y nach unten, z zum Betrachter
|
||
_mfx.AmbientLightColor = new Vector3(0.45f, 0.40f, 0.37f);
|
||
_mfx.DirectionalLight0.Enabled = true; // warmes Licht von oben links vorn
|
||
_mfx.DirectionalLight0.Direction = Vector3.Normalize(new Vector3(0.45f, 0.75f, -0.5f));
|
||
_mfx.DirectionalLight0.DiffuseColor = new Vector3(0.95f, 0.84f, 0.70f);
|
||
_mfx.DirectionalLight0.SpecularColor = new Vector3(0.22f, 0.2f, 0.18f);
|
||
_mfx.DirectionalLight1.Enabled = true; // kühles Gegenlicht
|
||
_mfx.DirectionalLight1.Direction = Vector3.Normalize(new Vector3(-0.7f, 0.1f, 0.7f));
|
||
_mfx.DirectionalLight1.DiffuseColor = new Vector3(0.22f, 0.27f, 0.36f);
|
||
_mfx.DirectionalLight2.Enabled = false;
|
||
_mfx.SpecularPower = 24;
|
||
}
|
||
|
||
/// Aussehen-Vorlagen mit dunkler Haut (Bilder hero_<klasse>_2).
|
||
static readonly System.Collections.Generic.HashSet<string> DarkSkin = new()
|
||
{ "hero_murmillo_2", "hero_secutor_2", "hero_thraex_2", "hero_retiarius_2" };
|
||
|
||
/// Figuren ohne eigenes Modell: eine ähnliche vorhandene (in eigener Teamfarbe), bis es eines gibt.
|
||
static System.Collections.Generic.IEnumerable<string> Fallbacks(string art)
|
||
{
|
||
if (art.StartsWith("hero_") && art.LastIndexOf('_') is int u && u > 5) yield return art[..u] + "_1"; // Vorlage 1 der Klasse
|
||
if (art.StartsWith("champ_")) { yield return "champ_doctore"; yield break; }
|
||
if (art.StartsWith("fig_")) yield return "hero_" + art[4..] + "_1"; // Legenden, Klassenbild
|
||
switch (art)
|
||
{
|
||
case "foe_recruit": yield return "foe_slave"; break;
|
||
case "foe_provocator" or "foe_crupellarius" or "foe_praetorian" or "foe_eques":
|
||
yield return "foe_hoplomachus"; break;
|
||
// der Raufbold kämpft mit Fäusten – Schwertkämpfer ohne eigene Figur nehmen Gladiatoren-Figuren
|
||
case "foe_dimachaerus" or "foe_essedarius":
|
||
yield return "hero_secutor_1"; break;
|
||
case "foe_laquearius" or "foe_sagittarius":
|
||
yield return "hero_thraex_1"; break;
|
||
}
|
||
yield return "foe_hoplomachus";
|
||
}
|
||
|
||
static readonly System.Collections.Generic.Dictionary<string, Matrix> _fitCache = new();
|
||
|
||
Model3D.Clip Clip3(string name) => _model!.Find(name) ?? _model!.Clips[0];
|
||
|
||
/// Clip und Zeit zum Zustand der Kampflogik.
|
||
void UpdateModel(DuelFighter f, float dt, float realDt)
|
||
{
|
||
var m = _model!;
|
||
double K(double total) => total > 0 ? Math.Clamp(f.StateTime / total, 0, 1) : 1;
|
||
string name; float t; bool loop = false;
|
||
var atk = Clip3(f.Heavy ? "slash2" : "slash");
|
||
float impact = m.ImpactTime(atk), lead = MathF.Min(0.12f, impact);
|
||
switch (f.State)
|
||
{
|
||
case DuelState.Windup:
|
||
name = atk.Name; t = (float)K(f.Heavy ? Duel.HEAVY_WINDUP : f.Windup) * (impact - lead); _mFree = false; break;
|
||
case DuelState.Strike:
|
||
name = atk.Name; t = impact - lead + (float)K(f.StrikeTime) * lead; break;
|
||
case DuelState.Recover:
|
||
name = atk.Name; t = impact + (float)K(f.RecoverTime) * (atk.Length - impact) * 0.5f; _mFree = true; break;
|
||
case DuelState.Block or DuelState.Wall:
|
||
name = "block"; t = (float)f.StateTime * 1.5f; _mFree = false; break;
|
||
case DuelState.Hit:
|
||
name = "impact"; t = (float)f.StateTime * 1.4f; _mFree = true; break;
|
||
case DuelState.Stunned:
|
||
name = "impact2"; t = (float)f.StateTime * 0.6f; loop = true; _mFree = false; break;
|
||
case DuelState.Dodge:
|
||
name = "crouchblock"; t = (float)K(0.32) * Clip3("crouchblock").Length; _mFree = false; break;
|
||
case DuelState.Down:
|
||
name = "death"; t = (float)f.StateTime; _mFree = false; break;
|
||
case DuelState.Victory:
|
||
name = "victory"; t = (float)f.StateTime; _mFree = false; break;
|
||
case DuelState.Leap:
|
||
{
|
||
// Sprungangriff: der Hieb des Clips fällt auf den Treffer der Kampflogik
|
||
var leap = Clip3("attack");
|
||
float li = m.ImpactTime(leap), st = (float)f.StateTime, hit = (float)Duel.LEAP_HIT;
|
||
name = leap.Name;
|
||
t = st < hit ? st / hit * li : li + (st - hit);
|
||
_mFree = true; break;
|
||
}
|
||
case DuelState.Super:
|
||
{
|
||
// Aufladen, dann drei Hiebe; jeder Clip trifft genau zu seinem Zeitpunkt
|
||
float st = (float)f.StateTime;
|
||
if (st < Duel.SUPER_CHARGE) { name = "powerup"; t = st * 1.6f; _mFree = false; break; }
|
||
string[] combo = { "slash", "slash3", "attack2" };
|
||
int k = 0;
|
||
while (k < combo.Length - 1 && st > Duel.SUPER_HITS[k] + 0.08) k++;
|
||
var c = Clip3(combo[k]);
|
||
name = c.Name;
|
||
t = MathF.Max(0, m.ImpactTime(c) + (st - (float)Duel.SUPER_HITS[k]) * 1.2f);
|
||
_mFree = true; break;
|
||
}
|
||
default:
|
||
{
|
||
float vx = dt > 0 ? (X - _lastX) / dt : 0;
|
||
bool moving = MathF.Abs(vx) > 40;
|
||
// nach Schlag/Treffer den Clip auslaufen lassen, außer man bewegt sich
|
||
if (_mFree && !moving && _mTime < Clip3(_mClip).Length)
|
||
{
|
||
name = _mClip; t = _mTime + dt * 1.4f; break;
|
||
}
|
||
_mFree = false;
|
||
if (moving)
|
||
{
|
||
name = "walk"; loop = true;
|
||
float dir = MathF.Sign(vx) * Facing; // rückwärts gehen: Clip rückwärts
|
||
t = (_mClip == "walk" ? _mTime : 0) + dt * 1.25f * dir;
|
||
}
|
||
else { name = "idle"; loop = true; t = (_mClip == "idle" ? _mTime : 0) + dt; }
|
||
break;
|
||
}
|
||
}
|
||
if (name != _mClip) { m.BeginBlend(); _mBlend = 0; _mClip = name; }
|
||
_mBlend = MathF.Min(1, _mBlend + realDt / 0.12f);
|
||
_mTime = t;
|
||
m.Pose(Clip3(name), t, loop, _mBlend, rootY: f.State == DuelState.Leap ? 0.15f : 1);
|
||
// Sprungangriff: eigene Flugkurve (der Clip springt kaum und passt nicht zur kurzen Sprungzeit)
|
||
float air = (float)Duel.LEAP_HIT + 0.08f, leapT = (float)f.StateTime;
|
||
float lift = f.State == DuelState.Leap && leapT < air ? MathF.Sin(MathF.PI * leapT / air) * 0.55f * Stage.FigH : 0;
|
||
_lift += (lift - _lift) * MathF.Min(1, realDt * 30);
|
||
}
|
||
|
||
Matrix ModelWorld(float grow)
|
||
{
|
||
float s = Stage.FigH / 180f * (1 + grow);
|
||
float yaw = Facing * MathHelper.ToRadians(72); // zum Gegner, leicht zum Betrachter gedreht
|
||
return _fit * Matrix.CreateRotationY(yaw)
|
||
* Matrix.CreateScale(s * (1 - _sq * 0.04f), -s * (1 + _sq * 0.06f), s) // y nach unten
|
||
* Matrix.CreateRotationZ(_lean * Facing)
|
||
* Matrix.CreateTranslation(X + _off * Facing * 0.35f * Stage.Scale, Stage.FloorY - _lift, 0);
|
||
}
|
||
|
||
/// Perspektive der Duell-Kulisse (sonst wie bisher orthografisch über die 2D-Ansicht).
|
||
Matrix? _view3, _proj3;
|
||
public void UseCamera3D(Matrix view3, Matrix proj3) { _view3 = view3; _proj3 = proj3; }
|
||
|
||
/// Licht der Arena übernehmen (Sonne, Umgebung, kühles Gegenlicht für die Kontur).
|
||
public void SetLighting(ArenaLook look)
|
||
{
|
||
if (_mfx == null) return;
|
||
_mfx.AmbientLightColor = look.Ambient * 0.95f + new Vector3(0.04f);
|
||
_mfx.DirectionalLight0.Direction = Vector3.Normalize(look.SunDir);
|
||
_mfx.DirectionalLight0.DiffuseColor = look.Sun;
|
||
_mfx.DirectionalLight1.Direction = Vector3.Normalize(new Vector3(-look.SunDir.X, 0.1f, 0.8f));
|
||
_mfx.DirectionalLight1.DiffuseColor = Vector3.Lerp(new Vector3(0.22f, 0.27f, 0.36f), look.Fog * 0.4f, 0.4f);
|
||
_mfx.FogEnabled = false;
|
||
}
|
||
|
||
// Schatten: Stencil, damit sich überlappende Teile nicht doppelt abdunkeln
|
||
static readonly DepthStencilState ShadowStencil = new()
|
||
{
|
||
DepthBufferEnable = true, DepthBufferWriteEnable = false,
|
||
StencilEnable = true, StencilFunction = CompareFunction.Equal, ReferenceStencil = 0,
|
||
StencilPass = StencilOperation.Increment, StencilFail = StencilOperation.Keep, StencilDepthBufferFail = StencilOperation.Keep,
|
||
};
|
||
|
||
/// Figur flach auf den Sand geworfen (Richtung der Sonne), halbdurchsichtig dunkel.
|
||
public void DrawCastShadow(GraphicsDevice gd, Vector3 sunDir, float strength)
|
||
{
|
||
if (_model == null || _mfx == null || _view3 == null) return;
|
||
var fx = _mfx;
|
||
fx.View = _view3.Value; fx.Projection = _proj3!.Value;
|
||
// Licht fällt flach: Schatten nicht endlos lang
|
||
var dir = Vector3.Normalize(new Vector3(sunDir.X, MathF.Max(0.45f, sunDir.Y), sunDir.Z));
|
||
var shadow = Matrix.CreateShadow(-dir, new Plane(new Vector3(0, -1, 0), Stage.FloorY - 1));
|
||
fx.World = ModelWorld(0) * shadow;
|
||
fx.LightingEnabled = false;
|
||
fx.DiffuseColor = Vector3.Zero;
|
||
fx.EmissiveColor = Vector3.Zero;
|
||
fx.Alpha = strength;
|
||
_model.Draw(gd, fx, depth: ShadowStencil, blend: BlendState.AlphaBlend);
|
||
fx.Alpha = 1;
|
||
fx.LightingEnabled = true;
|
||
fx.DiffuseColor = Vector3.One;
|
||
gd.DepthStencilState = DepthStencilState.None;
|
||
gd.BlendState = BlendState.AlphaBlend;
|
||
}
|
||
|
||
void DrawModel(GraphicsDevice gd, BasicEffect scene, Color aura, float grow)
|
||
{
|
||
var fx = _mfx!;
|
||
var vp = gd.Viewport;
|
||
fx.View = _view3 ?? scene.View;
|
||
fx.Projection = _proj3 ?? Matrix.CreateOrthographicOffCenter(0, vp.Width, vp.Height, 0, -100000, 100000);
|
||
fx.World = ModelWorld(grow);
|
||
if (aura.A > 0)
|
||
{
|
||
fx.LightingEnabled = false;
|
||
fx.DiffuseColor = aura.ToVector3() * (aura.A / 255f);
|
||
fx.EmissiveColor = Vector3.Zero;
|
||
_model!.Draw(gd, fx, aura: true);
|
||
fx.LightingEnabled = true;
|
||
fx.DiffuseColor = Vector3.One;
|
||
}
|
||
else
|
||
{
|
||
fx.EmissiveColor = Color.White.ToVector3() * (_flash * 0.55f) + _tintColor.ToVector3() * (_tint * 0.5f);
|
||
_model!.Draw(gd, fx);
|
||
}
|
||
gd.DepthStencilState = DepthStencilState.None;
|
||
gd.BlendState = BlendState.AlphaBlend;
|
||
}
|
||
}
|
||
}
|