using System;
using System.Collections.Generic;
using GladiatorIdle.Gfx;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
namespace GladiatorIdle.Arena
{
///
/// Publikum im Vordergrund: zwei Reihen dunkler Zuschauer am unteren Rand (Gegenlicht). Jeder
/// lebt für sich — atmet, wippt, dreht sich, jubelt mal spontan. Mit der Stimmung springen sie,
/// recken die Fäuste (Wechsel ruhig/jubelnd im eigenen Takt) und schwanken; bei großen Momenten
/// läuft eine Welle (La Ola) durch die Reihen und Blütenblätter fliegen in die Arena.
/// Liegt vor der Kampfebene (Parallaxe > 1).
///
internal sealed class Crowd
{
sealed class Fan
{
public Texture2D Calm = null!;
public Texture2D? Cheer;
public float X, H, Threshold, Phase, Shade, Pump, Temper;
public bool Mirror, Cheering;
public float Y, Vy, Tilt, Solo; // Sprunghöhe, Tempo nach oben, Neigung, spontaner Jubel (s)
}
sealed class Petal { public Vector2 Pos, Vel; public float Spin, Angle, Life, Size; public Color Color; }
readonly List _fans = new();
readonly List _petals = new();
readonly Random _rng;
float _excite;
double _t;
float _wave = -1; // Fortschritt der Welle (−1: keine)
public float Excitement => _excite;
public Crowd(int seed)
{
_rng = new Random(seed);
var kinds = new List<(Texture2D, Texture2D?)>();
for (int i = 1; i <= 6; i++)
if (Assets.Sprite($"spectator_{i}") is Texture2D calm)
kinds.Add((calm, Assets.Sprite($"spectator_{i}_cheer")));
if (kinds.Count == 0) return;
// zwei Reihen: hinten kleiner und heller, vorne größer und dunkler
for (int row = 0; row < 2; row++)
for (float x = -260 + _rng.Next(80); x < Camera.W + 260; x += row == 0 ? 95 : 130)
{
var (calm, cheer) = kinds[_rng.Next(kinds.Count)];
_fans.Add(new Fan
{
Calm = calm, Cheer = cheer, X = x + _rng.Next(-30, 30), H = row == 0 ? 230 : 300,
Threshold = 0.12f + (float)_rng.NextDouble() * 0.7f, Phase = (float)_rng.NextDouble() * 6.28f,
Shade = row == 0 ? 0.13f : 0.07f, Mirror = _rng.Next(2) == 0,
Pump = 2.2f + (float)_rng.NextDouble() * 2.4f, // Fäuste recken: eigener Takt
Temper = 0.6f + (float)_rng.NextDouble() * 0.8f // wie wild gesprungen wird
});
}
}
/// Stimmung steigern (0..1).
public void Excite(float amount) => _excite = MathF.Min(1, _excite + amount);
/// La-Ola-Welle durch beide Reihen und Blütenblätter (Raserei, Sieg).
public void Wave(bool petals = true)
{
_wave = 0;
Excite(1);
if (!petals) return;
for (int i = 0; i < 70; i++)
{
var f = _fans.Count > 0 ? _fans[_rng.Next(_fans.Count)] : null;
float x = f?.X ?? (float)_rng.NextDouble() * Camera.W;
_petals.Add(new Petal
{
Pos = new Vector2(x, Camera.H + 40), Vel = new Vector2(((float)_rng.NextDouble() - 0.5f) * 500, -700 - (float)_rng.NextDouble() * 650),
Spin = ((float)_rng.NextDouble() - 0.5f) * 14, Life = 2.6f + (float)_rng.NextDouble() * 1.4f,
Size = 9 + (float)_rng.NextDouble() * 9,
Color = _rng.Next(3) switch { 0 => new Color(220, 40, 50), 1 => new Color(255, 235, 220), _ => new Color(240, 170, 40) }
});
}
}
public void Update(float dt)
{
_t += dt;
_excite = MathF.Max(0.05f, _excite - dt * 0.18f);
float waveX = _wave < 0 ? -9999 : -300 + _wave * 2200;
if (_wave >= 0 && (_wave += dt) > 1.4f) _wave = -1;
foreach (var f in _fans)
{
// spontaner Jubel einzelner, auch wenn es ruhig ist
if (f.Solo > 0) f.Solo -= dt;
else if (_rng.NextDouble() < dt * 0.035) f.Solo = 0.8f + (float)_rng.NextDouble() * 1.2f;
bool inWave = MathF.Abs(f.X - waveX) < 170;
f.Cheering = inWave || f.Solo > 0 || (f.Cheering ? _excite > f.Threshold - 0.08f : _excite > f.Threshold);
// Sprünge mit Schwerkraft: jubelnd öfter und höher
f.Vy -= 1900 * dt;
f.Y = MathF.Max(0, f.Y + f.Vy * dt);
if (f.Y <= 0)
{
f.Vy = 0;
float chance = inWave ? 1 : f.Cheering ? 2.2f * f.Temper * dt : 0;
if (_rng.NextDouble() < chance)
f.Vy = (inWave ? 520 : 260 + 260 * _excite) * (0.7f + 0.5f * (float)_rng.NextDouble());
}
float tiltTarget = f.Cheering ? MathF.Sin((float)_t * 3.1f + f.Phase) * 0.09f * f.Temper
: MathF.Sin((float)_t * 0.7f + f.Phase) * 0.025f;
f.Tilt += (tiltTarget - f.Tilt) * MathF.Min(1, dt * 6);
}
for (int i = _petals.Count - 1; i >= 0; i--)
{
var p = _petals[i];
p.Vel.Y += 520 * dt;
p.Vel *= MathF.Exp(-dt * 0.9f);
p.Vel.X += MathF.Sin((float)_t * 2 + i) * 40 * dt; // flattern
p.Pos += p.Vel * dt;
p.Angle += p.Spin * dt;
if ((p.Life -= dt) <= 0) _petals.RemoveAt(i);
}
}
/// Zeichnen in der Vordergrund-Ebene (eigene Kamera-Matrix mit Parallaxe).
public void Draw(SpriteBatch sb, bool calm)
{
foreach (var f in _fans)
{
// jubelnd: Fäuste im eigenen Takt hoch und runter (Wechsel der Bilder)
bool armsUp = f.Cheering && f.Cheer != null && (calm || MathF.Sin((float)_t * f.Pump + f.Phase) > -0.35f);
var tex = armsUp ? f.Cheer! : f.Calm;
float breathe = 1 + MathF.Sin((float)_t * 1.3f + f.Phase) * 0.012f;
float bob = calm ? 0 : f.Cheering
? f.Y + MathF.Abs(MathF.Sin((float)_t * 7.5f + f.Phase)) * 10
: MathF.Sin((float)_t * 1.6f + f.Phase) * 3 + _excite * MathF.Abs(MathF.Sin((float)_t * 5 + f.Phase)) * 8;
// in der Luft gestreckt, bei der Landung gestaucht
float stretch = calm ? 1 : 1 + Math.Clamp(f.Vy / 4000f, -0.06f, 0.08f);
float scale = f.H / tex.Height;
float bottom = f.H > 260 ? Camera.H + 175 : Camera.H + 70;
var pos = new Vector2(f.X, bottom - bob);
var c = new Color(f.Shade * 1.1f, f.Shade * 0.9f, f.Shade * 0.75f);
sb.Draw(tex, pos, null, c, calm ? 0 : f.Tilt, new Vector2(tex.Width / 2f, tex.Height),
new Vector2(scale / MathF.Sqrt(stretch), scale * stretch * breathe),
f.Mirror ? SpriteEffects.FlipHorizontally : SpriteEffects.None, 0);
}
if (calm) return;
foreach (var p in _petals)
{
float a = MathF.Min(1, p.Life * 1.5f);
float flip = MathF.Abs(MathF.Cos(p.Angle * 1.7f)); // dreht sich in der Luft
sb.Draw(Assets.Disc, p.Pos, null, p.Color * a, p.Angle, new Vector2(64), new Vector2(p.Size / 128f, p.Size * (0.35f + 0.65f * flip) / 128f / 1.6f),
SpriteEffects.None, 0);
}
}
}
}