using System;
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
{
///
/// Die Freiheit: der Kaiser überreicht die Rudis (Wan-Clip, Erzählstimme, Musik), danach die
/// Bilanz des Lebens mit dem jubelnden Gladiator (3D) und die Opfer an die Götter. Am Ende
/// : der nächste Gladiator wird erstellt (Profil, Legenden und Götter bleiben).
///
internal sealed class FreedomScene
{
enum Phase { Clip, Summary, Gods }
const float CLIP_MIN = 9;
readonly GameState _game;
readonly Legend _legend;
readonly int _favor;
readonly Ui _ui = new();
readonly Particles _fx = new();
readonly Texture2D? _still = Assets.Sprite("freedom_rudis", "jpg");
readonly FighterView _hero;
readonly DuelFighter _dummy = new() { IsPlayer = true, Facing = 1, MaxHp = 1, Hp = 1, State = DuelState.Victory };
readonly float _line2At, _clipTime;
Phase _phase;
float _t, _phaseT;
bool _line1, _line2;
static bool Voiced => SoundManager.Enabled && AppSettings.Narration;
public bool Done { get; private set; }
public FreedomScene(GameState game, Legend legend, int favor)
{
_game = game;
_legend = legend;
_favor = favor;
string hero = Art.Hero(game.Class, game.Look.Preset);
_hero = Assets.Has(hero) ? new FighterView(hero, 0, game.Look.Color) : new FighterView(Art.Fighter(game.Class), 0, -1, game.Look.Color);
SoundManager.PreloadVoice(new[] { "freedom.line1", "freedom.line2" });
float v1 = Voiced ? SoundManager.VoiceSeconds("freedom.line1") : 3.5f;
float v2 = Voiced ? SoundManager.VoiceSeconds("freedom.line2") : 3.5f;
_line2At = 0.8f + v1 + 0.6f;
_clipTime = MathF.Max(CLIP_MIN, _line2At + v2 + 1.2f);
ClipCache.Video("freedom_rudis"); // Geschichte, kein Effekt: auch bei "Effekte reduzieren"
SoundManager.StopAll();
SoundManager.PlayMusic("music_freedom", loop: true);
_fx.Calm = AppSettings.ReduceMotion;
}
void Next(Phase p)
{
if (p != Phase.Clip) SoundManager.StopVoice();
_phase = p; _phaseT = 0;
SoundManager.PlayClick();
if (p == Phase.Summary && !AppSettings.ReduceMotion)
{
// Goldregen zum Auftakt der Bilanz
for (int i = 0; i < 6; i++)
_fx.Sparks(new Vector2(Camera.W * (0.15f + 0.14f * i), -20), 18, D.Gold, MathF.PI / 2, 0.5f, 500);
}
}
/// Testläufe: Phase direkt setzen (0 Clip, 1 Bilanz, 2 Götter) mit Zeit in der Phase.
public void DevShow(int phase, float time) { _phase = (Phase)phase; _phaseT = time; }
public void DevDone() { SoundManager.StopAll(); Done = true; }
public void Update(float dt, Input input, Matrix ui)
{
_t += dt; _phaseT += dt;
_ui.Begin(input, ui);
_dummy.StateTime += dt;
_hero.Update(dt, dt, _dummy, 0, AppSettings.ReduceMotion);
_fx.Motes(AppSettings.ReduceMotion ? 0 : 40, Camera.W, Camera.H);
_fx.Update(dt);
if (_phase == Phase.Clip)
{
if (!_line1 && _phaseT >= 0.8f) { _line1 = true; if (Voiced) SoundManager.PlayVoice("freedom.line1"); }
if (!_line2 && _phaseT >= _line2At) { _line2 = true; if (Voiced) SoundManager.PlayVoice("freedom.line2"); }
if (_phaseT >= _clipTime || input.Pressed(Keys.Space) || input.Pressed(Keys.Enter) || input.Pressed(Keys.Escape) || input.LeftClick)
Next(Phase.Summary);
}
}
// ── Zeichnen ────────────────────────────────────────────────
public void Draw(GraphicsDevice gd, SpriteBatch sb, BasicEffect fx, float scale, Vector2 offset)
{
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 (_phase == Phase.Clip) DrawClip(sb);
else DrawBackdrop(sb);
sb.End();
if (_phase == Phase.Summary)
{
// der freie Gladiator, jubelnd, links in der Bilanz
var view = Matrix.CreateTranslation(0, -Stage.FloorY, 0) * Matrix.CreateScale(1.7f) * Matrix.CreateTranslation(560, 830, 0) * ui;
sb.Begin(SpriteSortMode.Deferred, BlendState.Additive, SamplerState.LinearClamp, null, null, null, view);
D.Glow(sb, new Vector2(0, Stage.FloorY - 200), new Vector2(700, 800), new Color(255, 200, 120) * 0.25f);
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;
gd.Clear(ClearOptions.DepthBuffer, Color.Transparent, 1, 0);
_hero.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);
if (_phase == Phase.Clip) DrawLines(sb);
else if (_phase == Phase.Summary) DrawSummary(sb);
else DrawGods(sb);
sb.End();
}
void DrawClip(SpriteBatch sb)
{
var frames = ClipCache.Video("freedom_rudis");
float p = Math.Clamp(_phaseT / _clipTime, 0, 1);
var tex = frames != null ? frames[Math.Min(frames.Length - 1, (int)(p * (frames.Length - 1)))] : _still;
if (tex == null) return;
float k = (1.04f + 0.06f * p) * MathF.Max(Camera.W / tex.Width, Camera.H / tex.Height); // langsam heran
sb.Draw(tex, new Vector2(Camera.W / 2, Camera.H / 2), null, Color.White * Math.Clamp(_phaseT / 0.8f, 0, 1), 0,
new Vector2(tex.Width / 2f, tex.Height / 2f), k, SpriteEffects.None, 0);
float bar = Camera.H * 0.1f;
D.Rect(sb, new RectangleF(0, 0, Camera.W, bar), Color.Black);
D.Rect(sb, new RectangleF(0, Camera.H - bar, Camera.W, bar), Color.Black);
}
void DrawBackdrop(SpriteBatch sb)
{
if (_still != null)
{
float k = 1.08f * MathF.Max(Camera.W / _still.Width, Camera.H / _still.Height);
sb.Draw(_still, new Vector2(Camera.W / 2, Camera.H / 2), null, new Color(0.32f, 0.28f, 0.24f), 0,
new Vector2(_still.Width / 2f, _still.Height / 2f), k, SpriteEffects.None, 0);
}
D.Glow(sb, new Vector2(Camera.W / 2, 160), new Vector2(1800, 500), D.Gold * 0.08f);
}
/// Untertitel der Erzählstimme und Hinweis zum Fortfahren.
void DrawLines(SpriteBatch sb)
{
bool second = _phaseT >= _line2At;
float start = second ? _line2At - 0.2f : 0.6f;
float a = Math.Clamp((_phaseT - start) / 0.6f, 0, 1);
D.Glow(sb, new Vector2(Camera.W / 2, Camera.H - 170), new Vector2(Camera.W * 1.4f, 360), Color.Black * 0.8f);
DrawCentered(sb, L.t(second ? "freedom.line2" : "freedom.line1"), Camera.H - 175, 40, new Color(240, 214, 150) * a);
D.Text(sb, Assets.Sans, 18, L.t("freedom.skip"), new Vector2(Camera.W - 40, Camera.H - 40), D.Sand * 0.7f, Align.Right);
}
static void DrawCentered(SpriteBatch sb, string text, float y, float px, Color c)
{
float max = Camera.W * 0.8f;
if (D.Measure(Assets.Serif, px, text).X <= max) { D.Text(sb, Assets.Serif, px, text, new Vector2(Camera.W / 2, y), c, Align.Center); return; }
var words = text.Split(' ');
int split = words.Length / 2;
for (int i = 1; i < words.Length; i++)
if (D.Measure(Assets.Serif, px, string.Join(' ', words[..i])).X > max * 0.55f) { split = i; break; }
D.Text(sb, Assets.Serif, px, string.Join(' ', words[..split]), new Vector2(Camera.W / 2, y - px * 0.6f), c, Align.Center);
D.Text(sb, Assets.Serif, px, string.Join(' ', words[split..]), new Vector2(Camera.W / 2, y + px * 0.6f), c, Align.Center);
}
void DrawSummary(SpriteBatch sb)
{
float a = Math.Clamp(_phaseT / 0.6f, 0, 1);
D.Text(sb, Assets.Serif, 96, L.t("freedom.title").ToUpperInvariant(), new Vector2(Camera.W / 2, 120), D.Gold * a, Align.Center, 1.2f - 0.2f * a);
D.Text(sb, Assets.Serif, 28, string.Format(L.t("freedom.subtitle"), _legend.Name, L.t(ClassDef.Of(_legend.Class).Key)),
new Vector2(Camera.W / 2, 200), D.Sand * a, Align.Center);
var box = new RectangleF(900, 270, 860, 560);
Ui.Panel(sb, box, D.Gold, 0.92f);
float x = box.X + 50, w = box.Width - 100, y = box.Y + 50;
void Row(string label, string value)
{
D.Text(sb, Assets.Sans, 24, label, new Vector2(x, y), D.Sand);
D.Text(sb, Assets.SansBold, 28, value, new Vector2(x + w, y), D.Gold, Align.Right);
D.Rect(sb, new RectangleF(x, y + 26, w, 1), D.Border * 0.6f);
y += 58;
}
Row(L.t("freedom.number"), "#" + _legend.Number.ToString(NumberFormat.Culture)); // i18n-ok
Row(L.t("freedom.level"), _legend.Level.ToString(NumberFormat.Culture));
Row(L.t("freedom.wins"), NumberFormat.Format(_legend.Wins));
Row(L.t("freedom.fame"), NumberFormat.Format(_legend.Fame));
Row(L.t("freedom.time"), NumberFormat.Duration(_legend.RunSeconds));
Row(L.t("freedom.power"), NumberFormat.Format(_legend.Power));
y += 20;
D.Text(sb, Assets.Serif, 36, string.Format(L.t("freedom.favor"), _favor), new Vector2(x, y), D.Purple);
D.TextWrap(sb, Assets.Sans, 19, L.t("freedom.hall"), new RectangleF(x, y + 50, w, 70), D.Dim);
if (_ui.Button(sb, new RectangleF(Camera.W / 2 - 230, 900, 460, 80), L.t("freedom.togods"), size: 28, selected: true))
Next(Phase.Gods);
}
void DrawGods(SpriteBatch sb)
{
D.Text(sb, Assets.Serif, 60, L.t("freedom.gods").ToUpperInvariant(), new Vector2(Camera.W / 2, 90), D.Gold, Align.Center);
D.Text(sb, Assets.Sans, 22, L.t("freedom.gods.intro"), new Vector2(Camera.W / 2, 150), D.Sand, Align.Center);
D.Text(sb, Assets.SansBold, 30, string.Format(L.t("gods.favor"), _game.GodFavor), new Vector2(Camera.W / 2, 200), D.Purple, Align.Center);
GodCards(sb, _ui, _game, new Vector2(Camera.W / 2, 260));
if (_ui.Button(sb, new RectangleF(Camera.W / 2 - 260, 900, 520, 80), L.t("freedom.next"), size: 28, selected: true))
{
SoundManager.StopAll();
Done = true;
}
}
/// Götter-Karten (3 × 2) mit Stufe, Wirkung und Opfer-Knopf — auch in der Ehrenhalle genutzt.
public static void GodCards(SpriteBatch sb, Ui ui, GameState game, Vector2 topCenter, float cw = 560, float ch = 250)
{
const float GX = 30, GY = 26;
var gods = Enum.GetValues();
float x0 = topCenter.X - (3 * cw + 2 * GX) / 2, y0 = topCenter.Y;
foreach (var god in gods)
{
int i = (int)god;
var r = new RectangleF(x0 + i % 3 * (cw + GX), y0 + i / 3 * (ch + GY), cw, ch);
Ui.Panel(sb, r, D.Border, 0.92f);
float tx = r.X + 20, iconSize = MathF.Min(110, ch * 0.48f);
if (Assets.Sprite(Art.God(god)) is Texture2D icon)
{
float s = iconSize / icon.Width;
sb.Draw(icon, new Vector2(r.X + 16, r.Y + 16), null, Color.White, 0, Vector2.Zero, s, SpriteEffects.None, 0);
tx = r.X + iconSize + 32;
}
D.Text(sb, Assets.Serif, 28, L.t(Gods.Key(god)), new Vector2(tx, r.Y + 34), D.Gold);
D.Text(sb, Assets.SansBold, 20, string.Format(L.t("ludus.level"), game.GodLevel(god)), new Vector2(r.Right - 20, r.Y + 34), D.Sand, Align.Right);
D.TextWrap(sb, Assets.Sans, ch < 200 ? 15 : 18, L.t(Gods.Key(god) + ".desc"), new RectangleF(tx, r.Y + 58, r.Right - 20 - tx, ch - 58 - 74), D.Dim);
int cost = Gods.Cost(game.GodLevel(god));
if (ui.Button(sb, new RectangleF(r.Right - 216, r.Bottom - 62, 196, 46), string.Format(L.t("gods.cost"), cost),
game.GodFavor >= cost, size: 20, accent: D.Purple))
if (game.BuyGod(god)) SoundManager.PlayLevelUp();
}
}
}
}