Files
GladiatorIdle/Mono/Arena/LeaderboardScene.cs

218 lines
12 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 System.Linq;
using GladiatorIdle.Gfx;
using GladiatorIdle.Net;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using D = GladiatorIdle.Gfx.Draw;
namespace GladiatorIdle.Arena
{
/// <summary>
/// Bestenliste (aus dem Hauptmenü): Siegertreppchen für die drei Besten, darunter die Plätze 4–15.
/// Zwei Wertungen: stärkste Gladiatoren (beste Kampfkraft) und schnellste Freiheit. Der eigene
/// Eintrag leuchtet; steht er nicht unter den ersten 15, erscheint er unter der Liste.
/// </summary>
internal sealed class LeaderboardScene
{
enum Tab { Power, Fastest }
readonly Ui _ui = new();
readonly Particles _fx = new();
// Kolosseum in 3D als Bühne; die drei Besten stehen als Figuren ihrer Klasse auf dem Podest
readonly Stage3D _stage = new(5, AppSettings.ReduceMotion);
readonly Dictionary<string, FighterView> _podiumViews = new();
readonly DuelFighter _podiumDummy = new() { IsPlayer = true, Facing = 1, MaxHp = 1, Hp = 1, State = DuelState.Victory };
readonly RenderTarget2D?[] _podiumRt = new RenderTarget2D?[3];
BasicEffect? _figFx;
float _time;
const int FIG_W = 220, FIG_H = 300;
readonly string _me = Leaderboard.PlayerId;
List<LeaderEntry>? _all;
bool _loading = true, _offline, _dev;
Tab _tab;
double _t;
public bool Done { get; private set; }
public LeaderboardScene(GameState? game)
{
if (game != null) Leaderboard.Submit(game); // eigenen Stand zuerst aktualisieren
Reload();
}
void Reload()
{
_loading = true;
_ = Leaderboard.FetchAsync().ContinueWith(t =>
{
if (_dev) return;
_all = t.Result;
_offline = _all == null;
_loading = false;
});
}
/// Testläufe: Beispieleinträge statt der Datenbank (sendet und lädt nichts).
public void DevSample(GameState? game)
{
_dev = true;
var rnd = new Random(5);
string[] names = { "Spiculus", "Flamma", "Priscus", "Verus", "Carpophorus", "Tetraites", "Crixus", "Oenomaus", "Gannicus", "Varro", "Hilarus", "Celadus" };
_all = names.Select((n, i) => new LeaderEntry("dev" + i, n, ((GladiatorClass)(i % 4)).ToString(), 70 - i * 4, Math.Round(4e9 / Math.Pow(1.9, i)),
Math.Max(0, 6 - i / 2), 7200 + rnd.Next(9000), 7, "")).ToList();
if (game != null) _all.Add(Leaderboard.FromGame(game) with { Id = _me });
_loading = false; _offline = false;
}
public void Update(float dt, Input input, Matrix ui)
{
_t += dt;
_ui.Begin(input, ui);
_fx.Motes(AppSettings.ReduceMotion ? 0 : 40, Camera.W, Camera.H);
_fx.Update(dt);
if (input.Pressed(Keys.Escape)) Done = true;
}
List<LeaderEntry> Ranked() => _all == null ? new() : _tab == Tab.Power
? _all.Where(e => e.Power > 0).OrderByDescending(e => e.Power).ThenBy(e => e.Name).ToList()
: _all.Where(e => e.FastestFreedom > 0).OrderBy(e => e.FastestFreedom).ThenBy(e => e.Name).ToList();
string Value(LeaderEntry e) => _tab == Tab.Power ? NumberFormat.Format(e.Power) : NumberFormat.Duration(e.FastestFreedom);
string ClassName(LeaderEntry e) => Enum.TryParse<GladiatorClass>(e.Class, out var c) ? L.t(ClassDef.Of(c).Key) : e.Class;
// ── Zeichnen ────────────────────────────────────────────────
/// Siegerfiguren der Plätze 1–3 in ihre Bildflächen (vor allem anderen: Zielwechsel leert das Bild).
void RenderPodium(GraphicsDevice gd, List<LeaderEntry> list)
{
_figFx ??= new BasicEffect(gd) { VertexColorEnabled = true };
_podiumDummy.StateTime = 0.6f + (_time % 4f) * 0.5f; // Siegesjubel immer wieder
var previous = gd.GetRenderTargets();
for (int i = 0; i < 3; i++)
{
_podiumRt[i] = null;
if (i >= list.Count || !Enum.TryParse<GladiatorClass>(list[i].Class, out var cls)) continue;
string art = Art.Hero(cls, 1);
if (!_podiumViews.TryGetValue(art, out var v)) _podiumViews[art] = v = new FighterView(art, i * 0.7f, -1, i == 0 ? 4 : i == 1 ? 7 : 0);
v.Update(1 / 60f, 1 / 60f, _podiumDummy, 0, true);
if (!v.Is3D) continue;
var rt = _podiumRtCache[i] ??= new RenderTarget2D(gd, FIG_W, FIG_H, false, SurfaceFormat.Color, DepthFormat.Depth24);
gd.SetRenderTarget(rt);
gd.Clear(Color.Transparent);
_figFx.View = Matrix.CreateTranslation(0, -Stage.FloorY, 0) * Matrix.CreateScale(0.78f) * Matrix.CreateTranslation(FIG_W / 2f, FIG_H - 6, 0);
_figFx.Projection = Matrix.CreateOrthographicOffCenter(0, FIG_W, FIG_H, 0, 0, 1);
gd.BlendState = BlendState.AlphaBlend; gd.DepthStencilState = DepthStencilState.None; gd.RasterizerState = RasterizerState.CullNone;
v.DrawBody(gd, _figFx, _podiumDummy, true);
_podiumRt[i] = rt;
}
gd.SetRenderTargets(previous);
gd.Clear(Color.Black);
}
readonly RenderTarget2D?[] _podiumRtCache = new RenderTarget2D?[3];
public void Draw(SpriteBatch sb, float scale, Vector2 offset)
{
D.UiScale = scale;
var gd = sb.GraphicsDevice;
_time += 1 / 60f;
var ranked = !_loading && !_offline ? Ranked() : new List<LeaderEntry>();
RenderPodium(gd, ranked);
var ui = Matrix.CreateScale(scale) * Matrix.CreateTranslation(offset.X, offset.Y, 0);
_stage.Cam.Update(1 / 60f, Camera.W / 2 - 1800, Camera.W / 2 + 1800, AppSettings.ReduceMotion);
_stage.Prepare(gd, scale, offset);
_stage.DrawSet(gd, sb, _time, 0.8f);
sb.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, SamplerState.LinearClamp, null, null, null, ui);
D.Rect(sb, new RectangleF(0, 0, Camera.W, Camera.H), Color.Black * 0.45f); // Tafeln lesbar
sb.End();
sb.Begin(SpriteSortMode.Deferred, BlendState.Additive, SamplerState.LinearClamp, null, null, null, ui);
_fx.DrawAdd(sb);
D.Glow(sb, new Vector2(Camera.W / 2, 120), new Vector2(1600, 400), D.Gold * 0.1f);
sb.End();
sb.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, SamplerState.LinearClamp, null, null, null, ui);
D.Text(sb, Assets.Serif, 72, L.t("lb.title").ToUpperInvariant(), new Vector2(Camera.W / 2, 80), D.Gold, Align.Center);
float tw = 360;
if (_ui.Button(sb, new RectangleF(Camera.W / 2 - tw - 10, 140, tw, 56), L.t("lb.tab.power"), selected: _tab == Tab.Power, size: 22)) _tab = Tab.Power;
if (_ui.Button(sb, new RectangleF(Camera.W / 2 + 10, 140, tw, 56), L.t("lb.tab.fastest"), selected: _tab == Tab.Fastest, size: 22)) _tab = Tab.Fastest;
if (_loading) D.Text(sb, Assets.Serif, 30, L.t("lb.loading"), new Vector2(Camera.W / 2, 520), D.Sand, Align.Center);
else if (_offline)
{
D.Text(sb, Assets.Serif, 30, L.t("lb.offline"), new Vector2(Camera.W / 2, 500), D.Red, Align.Center);
if (_ui.Button(sb, new RectangleF(Camera.W / 2 - 160, 560, 320, 56), L.t("lb.retry"), size: 22)) Reload();
}
else
{
var list = Ranked();
if (list.Count == 0) D.Text(sb, Assets.Serif, 28, L.t(_tab == Tab.Power ? "lb.empty" : "lb.empty.fastest"), new Vector2(Camera.W / 2, 520), D.Sand, Align.Center);
else DrawRanking(sb, list);
}
if (_ui.Button(sb, new RectangleF(60, Camera.H - 100, 260, 60), L.t("lb.back"), size: 22)) Done = true;
sb.End();
}
void DrawRanking(SpriteBatch sb, List<LeaderEntry> list)
{
// Siegertreppchen: 2 – 1 – 3
Color[] medal = { D.Gold, new Color(205, 212, 225), new Color(205, 140, 90) };
int[] order = { 1, 0, 2 };
float[] heights = { 175, 215, 150 };
for (int s = 0; s < 3; s++)
{
int i = order[s];
if (i >= list.Count) continue;
var e = list[i];
float w = 360, x = Camera.W / 2 - 1.5f * w - 20 + s * (w + 20), h = heights[s], baseY = 480;
var r = new RectangleF(x, baseY - h, w, h);
D.Rect(sb, r, new Color(30, 22, 15) * 0.92f);
D.Frame(sb, r, e.Id == _me ? D.Purple : medal[i], e.Id == _me ? 4 : 3);
float tx = r.Center.X;
if (_podiumRt[i] is RenderTarget2D fig)
{
// Figur steht links auf der Tafel und ragt nach oben heraus
float k = (h + 60) / FIG_H;
sb.Draw(fig, new Vector2(r.X + 70, r.Bottom - 8), null, Color.White, 0, new Vector2(FIG_W / 2f, FIG_H - 6), k, SpriteEffects.None, 0);
tx = r.X + 230;
}
D.Text(sb, Assets.Serif, 44, (i + 1).ToString(NumberFormat.Culture), new Vector2(tx, r.Y + 30), medal[i], Align.Center);
D.Text(sb, Assets.Serif, e.Name.Length > 11 ? 22 : 28, e.Name, new Vector2(tx, r.Y + 72), Color.White, Align.Center);
if (h >= 170) D.Text(sb, Assets.Sans, 16, $"{ClassName(e)} · {string.Format(L.t("ludus.level"), e.Level)}", new Vector2(tx, r.Y + 104), D.Sand, Align.Center); // i18n-ok
D.Text(sb, Assets.SansBold, 24, Value(e), new Vector2(tx, r.Bottom - 26), medal[i], Align.Center);
}
// Plätze 4–15
float x0 = Camera.W / 2 - 560, y = 512, rowH = 36, wAll = 1120;
D.Text(sb, Assets.Sans, 15, L.t("lb.col.rank"), new Vector2(x0 + 20, y), D.Sand);
D.Text(sb, Assets.Sans, 15, L.t("lb.col.name"), new Vector2(x0 + 90, y), D.Sand);
D.Text(sb, Assets.Sans, 15, L.t("lb.col.class"), new Vector2(x0 + 470, y), D.Sand);
D.Text(sb, Assets.Sans, 15, L.t("lb.col.freedoms"), new Vector2(x0 + 760, y), D.Sand, Align.Center);
D.Text(sb, Assets.Sans, 15, L.t(_tab == Tab.Power ? "lb.tab.power" : "lb.tab.fastest"), new Vector2(x0 + wAll - 20, y), D.Sand, Align.Right);
y += 26;
for (int i = 3; i < Math.Min(15, list.Count); i++) { Row(sb, list[i], i, x0, y, wAll, rowH); y += rowH; }
int mine = list.FindIndex(e => e.Id == _me);
if (mine >= 15) { y += 10; Row(sb, list[mine], mine, x0, y, wAll, rowH); }
if (mine >= 0)
D.Text(sb, Assets.Sans, 17, string.Format(L.t("lb.yourrank"), mine + 1, list.Count), new Vector2(Camera.W - 60, Camera.H - 70), D.Purple, Align.Right);
}
void Row(SpriteBatch sb, LeaderEntry e, int i, float x0, float y, float w, float h)
{
bool me = e.Id == _me;
var r = new RectangleF(x0, y, w, h - 4);
D.Rect(sb, r, (me ? new Color(60, 40, 80) : new Color(30, 22, 15)) * 0.9f);
if (me) D.Frame(sb, r, D.Purple, 2);
float cy = r.Center.Y;
D.Text(sb, Assets.SansBold, 19, (i + 1).ToString(NumberFormat.Culture), new Vector2(x0 + 20, cy), D.Sand);
D.Text(sb, Assets.SansBold, 19, me ? $"{e.Name} ({L.t("lb.you")})" : e.Name, new Vector2(x0 + 90, cy), me ? D.Purple : Color.White); // i18n-ok
D.Text(sb, Assets.Sans, 17, $"{ClassName(e)} · {string.Format(L.t("ludus.level"), e.Level)}", new Vector2(x0 + 470, cy), D.Sand); // i18n-ok
D.Text(sb, Assets.Sans, 17, e.Freedoms.ToString(NumberFormat.Culture), new Vector2(x0 + 760, cy), D.Sand, Align.Center);
D.Text(sb, Assets.SansBold, 19, Value(e), new Vector2(x0 + w - 20, cy), D.Gold, Align.Right);
}
}
}