using System;
using System.Collections.Generic;
using System.Linq;
using GladiatorIdle.Gfx;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using D = GladiatorIdle.Gfx.Draw;
namespace GladiatorIdle.Arena
{
///
/// Arena zwischen den Kämpfen: der eigene Gladiator in der Arena, oben Stufe, Denare und Ruhm,
/// rechts die Gegner zur Wahl (leichter / ebenbürtig / stärker / Champion), unten Schubladen
/// für Gladiator und Ludus. Training und Einnahmen laufen weiter.
///
internal sealed partial class HubScene
{
enum Drawer { None, Gladiator, Ludus, Forum, Skills, Hall }
readonly GameState _game;
readonly Ui _ui = new();
readonly Particles _fx = new();
readonly Crowd _crowd = new(11);
readonly FighterView _hero;
readonly DuelFighter _dummy = new() { IsPlayer = true, X = 0, Facing = 1, MaxHp = 1, Hp = 1 };
Texture2D? _backdrop;
string? _loopName;
int _arenaShown = -1;
double _t;
Drawer _drawer;
float _drawerK; // 0 zu, 1 offen (gleitet)
// Meldung nach dem Kampf
string[] _toast = Array.Empty();
Color _toastColor;
double _toastAge = 99;
/// Gewählter Gegner (Kampf beginnt) — von außen abgeholt.
public Offer? Chosen { get; set; }
public HubScene(GameState game)
{
_game = game;
string hero = Art.Hero(game.Class, game.Look.Preset);
_hero = Assets.Has(hero) ? new FighterView(hero, 0.5f, game.Look.Color) : new FighterView(Art.Fighter(game.Class), 0.5f);
_hero.Preload();
_fx.Calm = AppSettings.ReduceMotion;
SoundManager.StartAmbient("music_ludus");
}
public void ShowResult(string[] lines, Color color)
{
_toast = lines; _toastColor = color; _toastAge = 0;
_crowd.Excite(color == D.Gold ? 0.8f : 0.1f);
}
void EnsureArena()
{
if (_arenaShown == _game.Arena) return;
_arenaShown = _game.Arena;
string art = Art.Arena(ArenaDef.All[_game.Arena]);
_backdrop = Assets.Sprite(art, "jpg");
_loopName = AppSettings.ReduceMotion ? null : art;
}
// ── Takt ────────────────────────────────────────────────────
public void Update(float dt, Input input, Matrix ui)
{
_t += dt;
_toastAge += dt;
_ui.Begin(input, ui);
_game.Tick(dt); // Training, Einnahmen, Erholung
_hero.Update(dt, dt, _dummy, 0, AppSettings.ReduceMotion);
_crowd.Update(dt);
_fx.Motes(AppSettings.ReduceMotion ? 0 : 50, Camera.W, Camera.H);
_fx.Update(dt);
float target = _drawer == Drawer.None ? 0 : 1;
_drawerK += (target - _drawerK) * (1 - MathF.Exp(-dt * 14));
if (input.Pressed(Microsoft.Xna.Framework.Input.Keys.Escape) && _drawer != Drawer.None) { _drawer = Drawer.None; input.PrevKeys = input.Keys; }
}
public bool DrawerOpen => _drawer != Drawer.None;
/// Testlauf: Schublade öffnen (0 zu, 1 Gladiator, 2 Ludus), sofort ganz offen.
public void DevOpen(int d)
{
_drawer = (Drawer)d; _drawerK = d == 0 ? 0 : 1;
if (d == 1 && _game.Inventory.Count > 0) _sel = _game.Inventory[0].Id;
}
// ── Zeichnen ────────────────────────────────────────────────
public void Draw(GraphicsDevice gd, SpriteBatch sb, BasicEffect fx, float scale, Vector2 offset)
{
EnsureArena();
D.UiScale = scale;
var ui = Matrix.CreateScale(scale) * Matrix.CreateTranslation(offset.X, offset.Y, 0);
// Arena im Hintergrund
sb.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, SamplerState.LinearClamp, null, null, null, ui);
var loop = _loopName != null ? ClipCache.Video(_loopName) : null; // Standbild, bis der Film geladen ist
var bg = loop != null ? loop[PingPong(_t * 8, loop.Length)] : _backdrop;
if (bg != null)
{
float k = 1.12f * MathF.Max(Camera.W / bg.Width, Camera.H / bg.Height);
sb.Draw(bg, new Vector2(Camera.W / 2, Camera.H / 2), null, new Color(0.55f, 0.51f, 0.47f), 0,
new Vector2(bg.Width / 2f, bg.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.6f);
D.Rect(sb, new RectangleF(0, 0, Camera.W, 120), Color.Black * 0.45f);
sb.End();
// Gladiator (links, in der Arena), Licht, Schatten
var view = Matrix.CreateTranslation(0, -Stage.FloorY, 0) * Matrix.CreateScale(1.1f) * Matrix.CreateTranslation(520, 930, 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(1000, 1100), new Color(255, 190, 120) * 0.14f);
D.Glow(sb, new Vector2(0, Stage.FloorY + 6), new Vector2(760, 130), new Color(255, 180, 110) * 0.22f);
sb.End();
sb.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, SamplerState.LinearClamp, null, null, null, view);
_hero.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;
_hero.DrawBody(gd, fx, _dummy, AppSettings.ReduceMotion);
sb.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, SamplerState.LinearClamp, null, null, null, ui * Matrix.Identity);
_crowd.Draw(sb, AppSettings.ReduceMotion);
sb.End();
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);
DrawTopBar(sb);
if (_drawerK < 0.98f) DrawOffers(sb);
DrawDrawerButtons(sb);
DrawToast(sb);
if (_drawerK > 0.01f) DrawDrawer(sb);
sb.End();
}
static int PingPong(double t, int n)
{
int period = 2 * (n - 1), i = (int)t % period;
return i < n ? i : period - i;
}
// ── Kopfleiste ──────────────────────────────────────────────
void DrawTopBar(SpriteBatch sb)
{
float x = 40;
if (Assets.Sprite(_game.Look.EmblemArt) is Texture2D em)
{
sb.Draw(em, new Vector2(x + 34, 60), null, Color.White, 0, new Vector2(em.Width / 2f, em.Height / 2f), 70f / em.Width, SpriteEffects.None, 0);
x += 80;
}
D.Text(sb, Assets.Serif, 32, $"{_game.Name} · {L.t(_game.ClassDef.Key)}", new Vector2(x, 42), D.Gold);
D.Text(sb, Assets.SansBold, 20, string.Format(L.t("char.level"), _game.CharLevel), new Vector2(x, 84), D.Sand);
D.Bar(sb, new RectangleF(x + 110, 74, 300, 20), (float)(_game.CharXp / _game.CharXpToNext), (float)(_game.CharXp / _game.CharXpToNext), D.Blue);
// Denare
float gx = 860;
if (Assets.Sprite("icon_denarius") is Texture2D coin)
sb.Draw(coin, new Vector2(gx, 60), null, Color.White, 0, new Vector2(coin.Width / 2f, coin.Height / 2f), 52f / coin.Width, SpriteEffects.None, 0);
D.Text(sb, Assets.SansBold, 30, NumberFormat.Format(_game.Gold), new Vector2(gx + 36, 52), Color.White);
if (_game.GoldPerSec > 0)
D.Text(sb, Assets.Sans, 17, string.Format(L.t("res.persec"), NumberFormat.Small(_game.GoldPerSec)), new Vector2(gx + 36, 86), D.Sand);
// Arena, Rang, Ruhm bis zum Champion
float rx = Camera.W - 40;
D.Text(sb, Assets.Serif, 28, L.t(ArenaDef.All[_game.Arena].Key), new Vector2(rx, 36), D.Gold, Align.Right);
D.Text(sb, Assets.Sans, 18, string.Format(L.t("arena.rank"), _game.Rank + 1, Balance.RANKS_PER_ARENA), new Vector2(rx - 440, 76), D.Sand);
D.Bar(sb, new RectangleF(rx - 300, 68, 300, 18), (float)(_game.FameRun / _game.FameRequired), (float)(_game.FameRun / _game.FameRequired), D.Gold);
D.Text(sb, Assets.Sans, 15, string.Format(L.t("arena.fame"), NumberFormat.Format(_game.FameRun), NumberFormat.Format(_game.FameRequired)),
new Vector2(rx - 150, 100), D.Dim, Align.Center);
}
// ── Gegner zur Wahl ─────────────────────────────────────────
void DrawOffers(SpriteBatch sb)
{
var offers = _game.Offers();
float w = offers.Count > 3 ? 220 : 270, gap = 20;
float total = offers.Count * w + (offers.Count - 1) * gap, x0 = Camera.W - 60 - total, y = 170, h = 650;
bool canFight = _game.CanFight;
for (int i = 0; i < offers.Count; i++)
{
var o = offers[i];
var r = new RectangleF(x0 + i * (w + gap), y, w, h);
bool champ = o.Kind == OfferKind.Champion, hover = canFight && _ui.Hover(r);
var accent = champ ? D.Gold : o.Kind == OfferKind.Hard ? D.Red : o.Kind == OfferKind.Easy ? D.Green : D.Sand;
Ui.Panel(sb, r, hover ? accent : champ ? D.Gold : D.Border, 0.84f);
if (champ) D.Glow(sb, r.Center, new Vector2(w * 1.6f, h * 1.2f), D.Gold * (0.08f + 0.04f * MathF.Sin((float)_t * 3)));
D.Text(sb, Assets.SansBold, 18, L.t(o.Key).ToUpperInvariant(), new Vector2(r.Center.X, r.Y + 24), accent, Align.Center);
// Gegner
string art = o.Enemy.Art ?? Art.Enemy(o.Enemy.NameKey);
if (Assets.Sprite(art) is Texture2D t)
{
float k = 300f / t.Height;
sb.Draw(t, new Vector2(r.Center.X, r.Y + 350), null, Color.White, 0, new Vector2(t.Width / 2f, Stage.Feet), k * 1.0f, SpriteEffects.FlipHorizontally, 0);
}
D.Text(sb, Assets.Serif, 24, o.Enemy.Name, new Vector2(r.Center.X, r.Y + 372), Color.White, Align.Center);
// Aussichten nach Kampfkraft
double ratio = _game.DuelRatio(o.Enemy);
var (odds, oc) = ratio >= 1.3 ? ("hub.odds.easy", D.Green) : ratio >= 0.85 ? ("hub.odds.even", D.Gold) : ("hub.odds.hard", D.Red);
D.Text(sb, Assets.SansBold, 17, L.t(odds), new Vector2(r.Center.X, r.Y + 404), oc, Align.Center);
// Lohn
var (gold, fame, xp) = _game.RewardPreview(o);
float ly = r.Y + 440;
Line(sb, r, ly, L.t("res.gold"), "+" + NumberFormat.Format(gold));
Line(sb, r, ly + 26, L.t("res.fame"), "+" + NumberFormat.Format(fame));
Line(sb, r, ly + 52, "XP", "+" + NumberFormat.Format(xp)); // i18n-ok
// Knöpfe
bool auto = _game.CanAutoWin(o);
var fb = new RectangleF(r.X + 16, r.Bottom - (auto ? 112 : 62), r.Width - 32, 48);
if (_ui.Button(sb, fb, L.t(champ ? "arena.challenge" : "hub.fight"), canFight, selected: champ, size: champ ? 18 : 22, accent: accent))
Chosen = o;
if (auto && _ui.Button(sb, new RectangleF(r.X + 16, r.Bottom - 58, r.Width - 32, 42), L.t("hub.auto"), canFight, size: 17))
{
var before = Snapshot();
_game.AutoWin(o);
ShowResult(ResultLines(before, true), D.Gold);
SoundManager.PlayCoins();
}
}
if (!canFight && _game.Recovery > 0)
{
var r = new RectangleF(x0, y + h / 2 - 50, total, 100);
D.Rect(sb, r, Color.Black * 0.7f);
D.Text(sb, Assets.Serif, 34, string.Format(L.t("arena.recover"), NumberFormat.Duration(_game.Recovery)), r.Center, D.Red, Align.Center);
}
}
static void Line(SpriteBatch sb, RectangleF r, float y, string label, string value)
{
D.Text(sb, Assets.Sans, 17, label, new Vector2(r.X + 20, y), D.Dim);
D.Text(sb, Assets.SansBold, 18, value, new Vector2(r.Right - 20, y), D.Sand, Align.Right);
}
// ── Ergebnis-Meldung ────────────────────────────────────────
/// Stand vor einem Kampf (für die Meldung danach).
public (double Gold, double Fame, double Xp, int Level, int Items) Snapshot() =>
(_game.Gold, _game.FameRun, _game.CharXp, _game.CharLevel, _game.Inventory.Count);
public string[] ResultLines((double Gold, double Fame, double Xp, int Level, int Items) before, bool won)
{
if (!won) return new[] { L.t("duel.lose"), L.t("hub.lost") };
var lines = new List
{
L.t("duel.win"),
string.Format(L.t("hub.won"), NumberFormat.Format(_game.Gold - before.Gold), NumberFormat.Format(_game.FameRun - before.Fame))
};
if (_game.CharLevel > before.Level) lines.Add(string.Format(L.t("ludus.levelup"), _game.CharLevel));
if (_game.Inventory.Count > before.Items && _game.Inventory.Count > 0)
lines.Add(string.Format(L.t("hub.loot"), ItemName(_game.Inventory[^1])));
return lines.ToArray();
}
void DrawToast(SpriteBatch sb)
{
if (_toastAge > 4.5 || _toast.Length == 0 || _drawerK > 0.05f) return;
float a = (float)Math.Clamp(Math.Min(_toastAge * 4, (4.5 - _toastAge) * 2), 0, 1);
float pop = 1 + 0.25f * MathF.Max(0, 1 - (float)_toastAge * 5);
var c = new Vector2(520, 220);
D.Glow(sb, c + new Vector2(0, 40), new Vector2(900, 320), Color.Black * (0.6f * a));
D.Text(sb, Assets.Serif, 64, _toast[0], c, _toastColor * a, Align.Center, pop);
for (int i = 1; i < _toast.Length; i++)
D.Text(sb, Assets.SansBold, 24, _toast[i], c + new Vector2(0, 40 + i * 34), Color.White * a, Align.Center);
}
// ── Schubladen ──────────────────────────────────────────────
void DrawDrawerButtons(SpriteBatch sb)
{
float y = Camera.H - 92;
string gl = _game.AttrPoints > 0 ? $"{L.t("ludus.gladiator")} ({_game.AttrPoints})" : L.t("ludus.gladiator");
if (_ui.Button(sb, new RectangleF(40, y, 230, 60), gl, selected: _drawer == Drawer.Gladiator, size: 22,
accent: _game.AttrPoints > 0 ? D.Gold : null))
_drawer = _drawer == Drawer.Gladiator ? Drawer.None : Drawer.Gladiator;
if (_ui.Button(sb, new RectangleF(286, y, 230, 60), L.t("main.ludus"), selected: _drawer == Drawer.Ludus, size: 22))
_drawer = _drawer == Drawer.Ludus ? Drawer.None : Drawer.Ludus;
if (_ui.Button(sb, new RectangleF(532, y, 230, 60), L.t("main.shop"), selected: _drawer == Drawer.Forum, size: 22))
_drawer = _drawer == Drawer.Forum ? Drawer.None : Drawer.Forum;
int sp = _game.LearnablePoints;
string sk = sp > 0 ? $"{L.t("char.skills")} ({sp})" : L.t("char.skills");
if (_ui.Button(sb, new RectangleF(778, y, 230, 60), sk, selected: _drawer == Drawer.Skills, size: 22, accent: sp > 0 ? D.Gold : null))
_drawer = _drawer == Drawer.Skills ? Drawer.None : Drawer.Skills;
int open = _game.Legends.Count(l => !_game.LegendsBeaten.Contains(l.Number));
if (_ui.Button(sb, new RectangleF(1024, y, 230, 60), string.Format(L.t("hall.button"), _game.Legends.Count), selected: _drawer == Drawer.Hall,
size: 22, accent: open > 0 || _game.GodFavor > 0 ? D.Purple : null))
_drawer = _drawer == Drawer.Hall ? Drawer.None : Drawer.Hall;
}
void DrawDrawer(SpriteBatch sb)
{
float w = _drawer == Drawer.Ludus ? 820 : 1300, x = -w + (w + 40) * Ease(_drawerK);
var r = new RectangleF(x, 140, w, Camera.H - 260);
Ui.Panel(sb, r, D.Gold, 0.94f);
if (_drawer == Drawer.Gladiator) DrawGladiator(sb, r);
else if (_drawer == Drawer.Ludus) DrawLudus(sb, r);
else if (_drawer == Drawer.Forum) DrawForum(sb, r);
else if (_drawer == Drawer.Skills) DrawSkills(sb, r);
else if (_drawer == Drawer.Hall) DrawHall(sb, r);
}
static float Ease(float k) => 1 - (1 - k) * (1 - k);
void DrawGladiator(SpriteBatch sb, RectangleF r)
{
float x = r.X + 30, y = r.Y + 40;
D.Text(sb, Assets.Serif, 30, $"{_game.Name} · {L.t(_game.ClassDef.Key)}", new Vector2(x, y), D.Gold);
D.Text(sb, Assets.SansBold, 20, string.Format(L.t("char.power"), NumberFormat.Format(_game.Power())), new Vector2(x + 700, y), D.Sand, Align.Right);
// Werte mit Attributpunkten
y += 50;
D.Text(sb, Assets.SansBold, 20, string.Format(L.t("char.points"), _game.AttrPoints), new Vector2(x, y), _game.AttrPoints > 0 ? D.Gold : D.Dim);
for (int i = 0; i < 4; i++)
{
var s = (Stat)i;
float ry = y + 44 + i * 44;
D.Text(sb, Assets.Sans, 21, L.t(TextKeys.Of(s)), new Vector2(x, ry), D.Sand);
D.Text(sb, Assets.SansBold, 22, _game.Level(s).ToString(NumberFormat.Culture), new Vector2(x + 300, ry), Color.White, Align.Right);
if (_ui.Button(sb, new RectangleF(x + 320, ry - 17, 44, 34), "+", _game.AttrPoints > 0, size: 22)) // i18n-ok
_game.SpendPoint(s);
}
// Kampfwerte
var c = _game.PlayerCombat();
float cx = x + 420;
Stat2(sb, cx, y + 44, L.t("ludus.hp"), NumberFormat.Format(c.MaxHp));
Stat2(sb, cx, y + 88, L.t("ludus.damage"), NumberFormat.Format(c.Damage));
Stat2(sb, cx, y + 132, L.t("ludus.crit"), NumberFormat.Percent(c.Crit));
// Ausrüstung
y += 240;
if (_ui.Button(sb, new RectangleF(x + 480, y - 22, 220, 44), L.t("char.equipbest"), size: 18))
{
if (_game.EquipBest() > 0) SoundManager.PlayPurchase();
}
for (int i = 0; i < 5; i++)
{
var slot = (ItemSlot)i;
var it = _game.Equipped[i];
float ry = y + 40 + i * 74;
var row = new RectangleF(x, ry, 700, 66);
D.Rect(sb, row, new Color(30, 22, 15) * 0.9f);
D.Frame(sb, row, it == null ? D.Border : RarityColor(it.Rarity), 2);
if (it != null && Assets.Sprite(it.Def.Icon) is Texture2D icon)
sb.Draw(icon, new Vector2(row.X + 36, row.Center.Y), null, Color.White, 0, new Vector2(icon.Width / 2f, icon.Height / 2f), 56f / icon.Width, SpriteEffects.None, 0);
D.Text(sb, Assets.Sans, 16, L.t("slot." + slot.ToString().ToLowerInvariant()), new Vector2(row.X + 76, row.Y + 18), D.Dim);
D.Text(sb, Assets.SansBold, 20, it == null ? "–" : ItemName(it), new Vector2(row.X + 76, row.Y + 44), it == null ? D.Dim : RarityColor(it.Rarity)); // i18n-ok
if (it != null && _sel == it.Id) D.Frame(sb, row, Color.White, 3);
if (it != null && _ui.Hit(row)) _sel = it.Id;
}
DrawInventory(sb, new RectangleF(r.X + 790, r.Y + 30, 430, r.Height - 60));
}
static void Stat2(SpriteBatch sb, float x, float y, string label, string value)
{
D.Text(sb, Assets.Sans, 19, label, new Vector2(x, y), D.Dim);
D.Text(sb, Assets.SansBold, 21, value, new Vector2(x + 320, y), Color.White, Align.Right);
}
void DrawLudus(SpriteBatch sb, RectangleF r)
{
float x = r.X + 30, y = r.Y + 40;
D.Text(sb, Assets.Serif, 30, L.t("main.ludus"), new Vector2(x, y), D.Gold);
y += 40;
foreach (var u in UpgradeDef.All)
{
var row = new RectangleF(x, y, r.Width - 60, 84);
D.Rect(sb, row, new Color(30, 22, 15) * 0.9f);
D.Frame(sb, row, D.Border, 2);
if (Assets.Sprite(Art.Upgrade(u)) is Texture2D icon)
sb.Draw(icon, new Vector2(row.X + 42, row.Center.Y), null, Color.White, 0, new Vector2(icon.Width / 2f, icon.Height / 2f), 64f / icon.Width, SpriteEffects.None, 0);
int level = _game.UpgradeLevel(u.Id);
D.Text(sb, Assets.SansBold, 21, L.t(u.Key), new Vector2(row.X + 86, row.Y + 24), Color.White);
D.Text(sb, Assets.Sans, 17, string.Format(L.t("ludus.level"), level), new Vector2(row.X + 86, row.Y + 56), D.Sand);
string desc = u.Stat is Stat s ? string.Format(L.t("upgrade.trainer.desc"), L.t(TextKeys.Of(s))) : L.t(u.Key + ".desc");
D.Text(sb, Assets.Sans, 15, desc.Length > 52 ? desc[..50] + "…" : desc, new Vector2(row.X + 200, row.Y + 56), D.Dim); // i18n-ok
double cost = _game.UpgradeCost(u.Id);
if (_ui.Button(sb, new RectangleF(row.Right - 200, row.Y + 16, 184, 52), NumberFormat.Format(cost), _game.Gold >= cost, size: 20))
if (_game.Buy(u.Id)) SoundManager.PlayPurchase();
y += 92;
}
}
public static string ItemName(Item it) => L.t(it.Def.Key) + (it.Level > 0 ? $" +{it.Level}" : ""); // i18n-ok
static Color RarityColor(Rarity r) => r switch
{
Rarity.Good => new Color(110, 200, 90), Rarity.Rare => new Color(90, 160, 240),
Rarity.Epic => new Color(185, 110, 235), Rarity.Legendary => new Color(250, 160, 40),
_ => new Color(190, 175, 150)
};
}
}