using System;
using System.Collections.Generic;
using System.Linq;
using GladiatorIdle.Gfx;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using D = GladiatorIdle.Gfx.Draw;
namespace GladiatorIdle.Arena
{
///
/// Eigenen Gladiator zusammenstellen: Klasse, Vorlage, Teamfarbe, Wappen, Startpunkte,
/// Startfähigkeit und Name. Links eine große Vorschau, die ab und zu zuschlägt, blockt und jubelt.
///
internal sealed class CreatorScene
{
public GladiatorClass Class { get; private set; } = GladiatorClass.Murmillo;
public int Preset { get; private set; } = 1;
public int Tint { get; private set; }
public int Emblem { get; private set; }
public int[] Bonus { get; } = new int[4];
public string? StartSkill { get; private set; }
public string Name { get; private set; }
public bool Done { get; private set; }
readonly Ui _ui = new();
readonly Particles _fx = new();
readonly Texture2D? _backdrop = Assets.Sprite("arena_ludus", "jpg");
FighterView _preview = null!;
// Vorschaubilder der Vorlagen: aus den 3D-Figuren gerendert (je Klasse einmal geladen)
const int THUMB_W = 296, THUMB_H = 340;
readonly System.Collections.Generic.Dictionary _thumbFigs = new();
readonly RenderTarget2D?[] _thumbs = new RenderTarget2D?[Appearance.PRESETS];
bool _thumbsDirty = true;
int _thumbTint = -1;
GladiatorClass _thumbClass = (GladiatorClass)(-1);
readonly DuelFighter _dummy = new() { IsPlayer = true, X = 0, Facing = 1, MaxHp = 1, Hp = 1 };
double _t, _showAt = 2.5;
int _show;
string? _hoverSkill;
public Appearance Look => new(Preset, Tint, Emblem);
int Left => GameState.CREATE_POINTS - Bonus.Sum();
public CreatorScene(string name)
{
Name = name;
SetClass(Class);
SoundManager.StartAmbient("music_menu");
}
void SetClass(GladiatorClass c)
{
Class = c;
if (StartSkill == null || !GameState.StartSkills(c).Any(s => s.Id == StartSkill))
StartSkill = GameState.StartSkills(c).First().Id;
Rebuild();
}
void Rebuild() => _preview = new FighterView(Art.Hero(Class, Preset), 0, Tint);
/// Getippte Zeichen (vom Fenster) für den Namen.
public void OnText(char c, Keys key)
{
if (key == Keys.Back) { if (Name.Length > 0) Name = Name[..^1]; return; }
if (key == Keys.Enter) return;
if (!char.IsControl(c) && Name.Length < 18) Name += c;
}
// ── Takt ────────────────────────────────────────────────────
public void Update(float dt, Input input, Matrix ui)
{
_t += dt;
_ui.Begin(input, ui);
ShowcaseStep(dt);
_preview.Update(dt, dt, _dummy, 0, AppSettings.ReduceMotion);
_fx.Motes(AppSettings.ReduceMotion ? 0 : 50, Camera.W, Camera.H);
_fx.Update(dt);
}
/// Vorschau: meist ruhig, dazwischen Schlag, Block oder Siegerpose.
void ShowcaseStep(float dt)
{
_dummy.StateTime += dt;
if (_dummy.State == DuelState.Ready && _t >= _showAt)
{
_show++;
_dummy.Set(_show % 3 == 0 ? DuelState.Victory : _show % 3 == 1 ? DuelState.Windup : DuelState.Block);
}
switch (_dummy.State)
{
case DuelState.Windup when _dummy.StateTime > 0.35: _dummy.Set(DuelState.Strike); break;
case DuelState.Strike when _dummy.StateTime > 0.15: _dummy.Set(DuelState.Recover); break;
case DuelState.Recover when _dummy.StateTime > 0.5:
case DuelState.Block when _dummy.StateTime > 1.2:
case DuelState.Victory when _dummy.StateTime > 1.6:
_dummy.Set(DuelState.Ready); _showAt = _t + 2.8; break;
}
}
// ── Zeichnen ────────────────────────────────────────────────
public void Draw(GraphicsDevice gd, SpriteBatch sb, BasicEffect fx, float scale, Vector2 offset)
{
RenderThumbs(gd, fx);
D.UiScale = scale;
var ui = Matrix.CreateScale(scale) * Matrix.CreateTranslation(offset.X, offset.Y, 0);
sb.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, SamplerState.LinearClamp, null, null, null, ui);
if (_backdrop != null)
{
float k = 1.1f * MathF.Max(Camera.W / _backdrop.Width, Camera.H / _backdrop.Height);
sb.Draw(_backdrop, new Vector2(Camera.W / 2, Camera.H / 2), null, new Color(0.42f, 0.38f, 0.34f), 0,
new Vector2(_backdrop.Width / 2f, _backdrop.Height / 2f), k, SpriteEffects.None, 0);
}
D.Glow(sb, new Vector2(-200, Camera.H / 2), new Vector2(900, 2000), Color.Black * 0.7f);
D.Glow(sb, new Vector2(Camera.W + 200, Camera.H / 2), new Vector2(900, 2000), Color.Black * 0.7f);
D.Text(sb, Assets.Serif, 56, L.t("create.title").ToUpperInvariant(), new Vector2(110, 78), D.Gold);
sb.End();
// Vorschau: eigene Kamera (größer), Lichtkegel, Schatten, Figur
var view = Matrix.CreateTranslation(0, -Stage.FloorY, 0) * Matrix.CreateScale(1.25f) * Matrix.CreateTranslation(470, 905, 0) * ui;
sb.Begin(SpriteSortMode.Deferred, BlendState.Additive, SamplerState.LinearClamp, null, null, null, view);
D.Glow(sb, new Vector2(0, Stage.FloorY - 300), new Vector2(900, 1100), new Color(255, 190, 120) * 0.16f);
D.Glow(sb, new Vector2(0, Stage.FloorY + 6), new Vector2(700, 120), new Color(255, 180, 110) * 0.25f);
sb.End();
sb.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, SamplerState.LinearClamp, null, null, null, view);
_preview.DrawShadow(sb);
sb.End();
fx.View = view;
fx.Projection = Matrix.CreateOrthographicOffCenter(0, gd.Viewport.Width, gd.Viewport.Height, 0, 0, 1);
gd.BlendState = BlendState.AlphaBlend; gd.SamplerStates[0] = SamplerState.LinearClamp;
gd.RasterizerState = RasterizerState.CullNone; gd.DepthStencilState = DepthStencilState.None;
_preview.DrawBody(gd, fx, _dummy, AppSettings.ReduceMotion);
sb.Begin(SpriteSortMode.Deferred, BlendState.Additive, SamplerState.LinearClamp, null, null, null, ui);
_fx.DrawAdd(sb);
sb.End();
sb.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, SamplerState.LinearClamp, null, null, null, ui);
DrawNameField(sb);
DrawOptions(sb);
sb.End();
}
/// Vorschaubilder neu rendern, wenn Klasse oder Farbe gewechselt hat (in eigene Bildflächen,
/// danach zurück auf das bisherige Ziel; das wird dabei geleert, darum vor allem anderen).
void RenderThumbs(GraphicsDevice gd, BasicEffect fx)
{
if (_thumbClass != Class || _thumbTint != Tint) _thumbsDirty = true;
if (!_thumbsDirty) return;
if (!_thumbFigs.TryGetValue(Class, out var figs))
{
figs = new FighterView[Appearance.PRESETS];
for (int p = 1; p <= Appearance.PRESETS; p++) figs[p - 1] = new FighterView(Art.Hero(Class, p), 0, Tint);
_thumbFigs[Class] = figs;
}
var idle = new DuelFighter { IsPlayer = true, Facing = 1, MaxHp = 1, Hp = 1 };
var previous = gd.GetRenderTargets();
for (int i = 0; i < figs.Length; i++)
{
if (!figs[i].Is3D) { _thumbs[i] = null; continue; }
figs[i].SetModelColor(Tint);
figs[i].Update(1 / 60f, 1 / 60f, idle, 0, true);
_thumbs[i] ??= new RenderTarget2D(gd, THUMB_W, THUMB_H, false, SurfaceFormat.Color, DepthFormat.Depth24);
gd.SetRenderTarget(_thumbs[i]);
gd.Clear(Color.Transparent);
fx.View = Matrix.CreateTranslation(0, -Stage.FloorY, 0) * Matrix.CreateScale(0.95f) * Matrix.CreateTranslation(THUMB_W / 2f, THUMB_H - 8, 0);
fx.Projection = Matrix.CreateOrthographicOffCenter(0, THUMB_W, THUMB_H, 0, 0, 1);
figs[i].DrawBody(gd, fx, idle, true);
}
gd.SetRenderTargets(previous);
gd.Clear(Color.Black);
_thumbClass = Class; _thumbTint = Tint; _thumbsDirty = false;
}
void DrawNameField(SpriteBatch sb)
{
var r = new RectangleF(170, 968, 600, 68);
D.Text(sb, Assets.SansBold, 20, L.t("create.name"), new Vector2(r.X, r.Y - 22), D.Sand);
Ui.Panel(sb, r, D.Gold);
// Wappen vor dem Namen
if (Assets.Sprite(Look.EmblemArt) is Texture2D em)
sb.Draw(em, new Vector2(r.X + 36, r.Center.Y), null, Color.White, 0, new Vector2(em.Width / 2f, em.Height / 2f), 54f / em.Width, SpriteEffects.None, 0);
bool caret = _t % 1 < 0.55;
D.Text(sb, Assets.Serif, 34, Name + (caret ? "|" : " "), new Vector2(r.X + 76, r.Center.Y), Color.White); // i18n-ok
}
static void Header(SpriteBatch sb, string text, float y, float x = 930) =>
D.Text(sb, Assets.Serif, 26, text, new Vector2(x, y), D.Gold);
void DrawOptions(SpriteBatch sb)
{
Ui.Panel(sb, new RectangleF(900, 40, 940, 900), null, 0.78f);
float x0 = 930;
// Klasse
Header(sb, L.t("create.class"), 76);
for (int i = 0; i < 4; i++)
{
var c = (GladiatorClass)i;
if (_ui.Button(sb, new RectangleF(x0 + i * 222, 100, 210, 58), L.t(ClassDef.Of(c).Key), selected: Class == c, size: 22))
SetClass(c);
}
// Aussehen: drei Vorlagen in der gewählten Farbe
Header(sb, L.t("create.look"), 192);
for (int p = 1; p <= Appearance.PRESETS; p++)
{
var r = new RectangleF(x0 + (p - 1) * 160, 214, 148, 170);
bool sel = Preset == p, hover = _ui.Hover(r);
D.Rect(sb, r, new Color(30, 22, 15) * 0.9f);
if (_thumbs[p - 1] is RenderTarget2D th && !th.IsDisposed)
sb.Draw(th, new Rectangle((int)r.X + 2, (int)r.Y + 2, (int)r.Width - 4, (int)r.Height - 4), Color.White);
else if (Assets.Sprite(Art.Hero(Class, p)) is Texture2D t)
{
var painted = Assets.Paint(t, Tint);
float k = r.Height * 1.02f / t.Height;
sb.Draw(painted, new Vector2(r.Center.X, r.Bottom - 2), null, Color.White, 0,
new Vector2(t.Width / 2f, Stage.Feet), k, SpriteEffects.None, 0);
}
D.Frame(sb, r, sel ? D.Gold : hover ? D.Sand : D.Border, sel ? 3 : 2);
if (_ui.Hit(r)) { Preset = p; Rebuild(); }
}
// Klassenbeschreibung neben den Vorlagen
float dy = DrawWrapped(sb, L.t(ClassDef.Of(Class).Key + ".desc"), new RectangleF(x0 + 490, 226, 400, 120), 19, Align.Left);
D.Text(sb, Assets.SansBold, 19, string.Format(L.t("select.special"), L.t(TextKeys.Of(ClassDef.Of(Class).Special))),
new Vector2(x0 + 490, dy + 18), D.Blue);
// Farbe
Header(sb, L.t("create.color"), 418);
for (int i = 0; i < Appearance.Colors.Length; i++)
{
var tc = Appearance.Colors[i];
var center = new Vector2(x0 + 30 + i * 74, 468);
var r = new RectangleF(center.X - 30, center.Y - 30, 60, 60);
bool sel = Tint == i, hover = _ui.Hover(r);
var (cr, cg, cb) = Rgb(tc);
Disc(sb, center, sel ? 64 : 56, sel ? D.Gold : hover ? D.Sand : D.Border);
Disc(sb, center, sel ? 54 : 48, new Color(cr, cg, cb));
if (_ui.Hit(r)) { Tint = i; Rebuild(); }
}
// Wappen
Header(sb, L.t("create.emblem"), 530);
for (int i = 0; i < Appearance.Emblems.Length; i++)
{
var center = new Vector2(x0 + 36 + i * 74, 590);
var r = new RectangleF(center.X - 34, center.Y - 34, 68, 68);
bool sel = Emblem == i, hover = _ui.Hover(r);
if (sel) D.Glow(sb, center, new Vector2(110), D.Gold * 0.5f);
if (Assets.Sprite("emblem_" + Appearance.Emblems[i]) is Texture2D em)
sb.Draw(em, center, null, sel || hover ? Color.White : new Color(160, 150, 140), 0,
new Vector2(em.Width / 2f, em.Height / 2f), (sel ? 70f : 62f) / em.Width, SpriteEffects.None, 0);
else Disc(sb, center, 56, sel ? D.Gold : D.Border); // Bild fehlt noch
if (_ui.Hit(r)) Emblem = i;
}
if (Emblem >= 0) D.Text(sb, Assets.Sans, 18, L.t(Look.EmblemKey), new Vector2(x0 + 600, 530), D.Sand, Align.Right);
// Startpunkte
Header(sb, string.Format(L.t("create.points"), Left), 662);
var start = ClassDef.Of(Class).StartLevels;
for (int s = 0; s < 4; s++)
{
float y = 700 + s * 46;
D.Text(sb, Assets.Sans, 21, L.t(TextKeys.Of((Stat)s)), new Vector2(x0, y), D.Sand);
D.Text(sb, Assets.SansBold, 24, (start[s] + Bonus[s]).ToString(NumberFormat.Culture), new Vector2(x0 + 260, y),
Bonus[s] > 0 ? D.Gold : Color.White, Align.Right);
int ss = s;
if (_ui.Button(sb, new RectangleF(x0 + 290, y - 18, 44, 36), "−", Bonus[s] > 0, size: 22)) Bonus[ss]--; // i18n-ok
if (_ui.Button(sb, new RectangleF(x0 + 342, y - 18, 44, 36), "+", Left > 0, size: 22)) Bonus[ss]++; // i18n-ok
}
// Startfähigkeit
Header(sb, L.t("create.skill"), 662, x0 + 450);
_hoverSkill = null;
var skills = GameState.StartSkills(Class).ToList();
for (int i = 0; i < skills.Count; i++)
{
var sk = skills[i];
var center = new Vector2(x0 + 520 + i * 120, 730);
var r = new RectangleF(center.X - 46, center.Y - 46, 92, 92);
bool sel = StartSkill == sk.Id, hover = _ui.Hover(r);
if (hover) _hoverSkill = sk.Id;
if (sel) D.Glow(sb, center, new Vector2(150), D.Gold * 0.55f);
if (Assets.Sprite(sk.Icon) is Texture2D icon)
sb.Draw(icon, center, null, sel || hover ? Color.White : new Color(150, 140, 125), 0,
new Vector2(icon.Width / 2f, icon.Height / 2f), (sel ? 92f : 82f) / icon.Width, SpriteEffects.None, 0);
if (_ui.Hit(r)) StartSkill = sk.Id;
}
string shown = _hoverSkill ?? StartSkill ?? skills[0].Id;
var def = SkillDef.Get(shown);
D.Text(sb, Assets.SansBold, 21, L.t(def.Key), new Vector2(x0 + 640, 806), D.Gold, Align.Center);
DrawWrapped(sb, L.t(def.Desc), new RectangleF(x0 + 450, 826, 380, 90), 17);
// Los
if (_ui.Button(sb, new RectangleF(1420, 962, 420, 80), L.t("create.start"), Name.Trim().Length > 0, size: 32))
Done = true;
}
void Disc(SpriteBatch sb, Vector2 c, float size, Color color) =>
sb.Draw(Assets.Disc, c, null, color, 0, new Vector2(64), size / 128f, SpriteEffects.None, 0);
/// Einfacher Zeilenumbruch nach Wörtern.
/// Liefert die y-Position unter der letzten Zeile.
static float DrawWrapped(SpriteBatch sb, string text, RectangleF r, float px, Align align = Align.Center)
{
float x = align == Align.Left ? r.X : r.Center.X;
var words = text.Split(' ');
string line = "";
float y = r.Y + px * 0.7f;
foreach (var w in words)
{
string test = line.Length == 0 ? w : line + " " + w;
if (D.Measure(Assets.Sans, px, test).X > r.Width && line.Length > 0)
{
D.Text(sb, Assets.Sans, px, line, new Vector2(x, y), D.Sand, align);
y += px * 1.35f; line = w;
}
else line = test;
}
if (line.Length > 0) D.Text(sb, Assets.Sans, px, line, new Vector2(x, y), D.Sand, align);
return y + px * 0.7f;
}
/// Anzeigefarbe einer Teamfarbe (wie ein kräftiges Magenta nach dem Umfärben).
static (float, float, float) Rgb(TeamColor c)
{
float s = MathHelper.Clamp(0.85f * c.Sat, 0, 1), v = MathHelper.Clamp(0.85f * c.Val, 0, 1);
float h = c.Hue, ch = v * s, x = ch * (1 - MathF.Abs(h / 60 % 2 - 1)), m = v - ch;
var (r, g, b) = h < 60 ? (ch, x, 0f) : h < 120 ? (x, ch, 0f) : h < 180 ? (0f, ch, x) : h < 240 ? (0f, x, ch) : h < 300 ? (x, 0f, ch) : (ch, 0f, x);
return (r + m, g + m, b + m);
}
// ── Testläufe ───────────────────────────────────────────────
/// Testlauf: eine bestimmte Zusammenstellung wählen.
public void DevPick(GladiatorClass c, int preset, int color, int emblem)
{
SetClass(c); Preset = preset; Tint = color; Emblem = emblem;
Bonus[0] = 2; Bonus[2] = 3;
StartSkill = GameState.StartSkills(c).Last().Id;
Rebuild();
}
public void Finish() => Done = true;
}
}