using System; using Microsoft.Xna.Framework; namespace GladiatorIdle.Arena { /// /// Kamera der Arena in Design-Pixeln: folgt den Kämpfern, zoomt bei Nähe heran, ruckt bei /// Treffern (Trauma² als Stärke) und stößt kurz in Richtung des Aufpralls (Punch). /// internal sealed class Camera { public const float W = 1920, H = 1080; public Vector2 Center = new(W / 2, H / 2); public float Zoom = 1f; float _trauma, _punch; Vector2 _punchAt; double _t; public void Shake(float amount) => _trauma = MathF.Min(1, _trauma + amount); /// Nach dem Kampf: langsam auf den Sieger zufahren (null = wieder beide zeigen). public float? FocusX; /// Reise-Ansicht: der Figur frei folgen (keine Begrenzung auf die Arena-Mitte). public float? FollowX; public float FollowZoom = 1f; public void Punch(float amount, Vector2 at) { _punch = MathF.Max(_punch, amount); _punchAt = at; } /// Ziel: Mitte zwischen den Kämpfern, je näher, desto enger. public void Update(float dt, float x1, float x2, bool calm) { _t += dt; float mid = (x1 + x2) / 2, dist = MathF.Abs(x2 - x1); float targetZoom = MathHelper.Clamp(1.2f - dist / 3000f, 1.0f, 1.12f); var target = new Vector2(MathHelper.Clamp(mid, W / 2 - 220, W / 2 + 220), H / 2 + 40); float k = 1 - MathF.Exp(-dt * 3.5f); if (FocusX is float fx) { targetZoom = 1.4f; target = new Vector2(fx, H / 2 + 30); k = 1 - MathF.Exp(-dt * 1.2f); } if (FollowX is float fw) { targetZoom = FollowZoom; target = new Vector2(fw, H / 2 + 40); k = 1 - MathF.Exp(-dt * 4f); } // Reise: ohne Begrenzung if (CineWeight > 0.01f) { target = Vector2.Lerp(target, new Vector2(CinePivot.X + CineFrameX, CinePivot.Y), CineWeight); targetZoom = MathHelper.Lerp(targetZoom, 1, CineWeight); } Center += (target - Center) * k; Zoom += (targetZoom - Zoom) * k; _trauma = MathF.Max(0, _trauma - dt * 1.7f); _punch *= MathF.Exp(-dt * 9f); if (calm) { _trauma = 0; _punch = 0; } UpdateYaw(dt, calm); UpdateCine(dt, calm); } // ── Kinokamera: Fahrten um einen Drehpunkt (360°), Neigung (Untersicht), Heranfahren ── /// Schlüsselbild einer Fahrt: Zeit (s), Drehung (Grad, beliebig, auch über 360), Neigung /// (Grad, negativ = von unten nach oben), Heranfahren (Design-Pixel näher). public readonly record struct Key(float T, float Yaw, float Pitch, float Dolly); /// Drehpunkt der Fahrt (Weltpunkt, meist Brust eines Kämpfers); setzt die Szene jedes Bild. public Vector3 CinePivot; /// Bildlage des Drehpunkts: + schiebt ihn nach links (Platz für Tafeln rechts). public float CineFrameX; public float CineYaw { get; private set; } public float CinePitch { get; private set; } public float CineDolly { get; private set; } /// 0 = normale Seitenansicht, 1 = ganz in der Fahrt (Ein- und Ausblenden weich). public float CineWeight { get; private set; } Key[]? _shot; float _shotT, _orbitSpeed; bool _orbit; Key _orbitKey; /// Fahrt aus Schlüsselbildern; danach weich zurück zur Seitenansicht. public void Shot(params Key[] keys) { if (keys.Length > 0) { _shot = keys; _shotT = 0; _orbit = false; } } /// Fortlaufende Umrundung (Grad/s) mit fester Neigung und Nähe, bis . public void Orbit(float degPerSec, float pitch, float dolly) { _orbit = true; _shot = null; _orbitSpeed = degPerSec; _orbitKey = new Key(0, CineWeight > 0.01f ? CineYaw : 0, pitch, dolly); } public void StopCine() { _shot = null; _orbit = false; } public bool CineActive => _shot != null || _orbit; void UpdateCine(float dt, bool calm) { if (calm) { _shot = null; _orbit = false; CineWeight = 0; return; } float target = 0; if (_orbit) { _orbitKey = _orbitKey with { Yaw = _orbitKey.Yaw + _orbitSpeed * dt }; Blend(_orbitKey, dt); target = 1; } else if (_shot is { } keys) { _shotT += dt; int i = 0; while (i < keys.Length - 1 && _shotT > keys[i + 1].T) i++; Key a = keys[i], b = keys[Math.Min(i + 1, keys.Length - 1)]; float k = b.T > a.T ? MathHelper.Clamp((_shotT - a.T) / (b.T - a.T), 0, 1) : 1; k = k * k * (3 - 2 * k); CineYaw = MathHelper.Lerp(a.Yaw, b.Yaw, k); CinePitch = MathHelper.Lerp(a.Pitch, b.Pitch, k); CineDolly = MathHelper.Lerp(a.Dolly, b.Dolly, k); if (_shotT > keys[^1].T) _shot = null; target = 1; CineWeight = 1; } if (target == 0) { // zurück: Winkel auf den nächsten Vollkreis, Neigung und Nähe auf 0 float home = MathF.Round(CineYaw / 360) * 360; float k = 1 - MathF.Exp(-dt * 3.2f); CineYaw += (home - CineYaw) * k; CinePitch -= CinePitch * k; CineDolly -= CineDolly * k; if (MathF.Abs(CineYaw - home) < 0.3f && MathF.Abs(CinePitch) < 0.2f && MathF.Abs(CineDolly) < 2) { CineYaw = 0; CinePitch = 0; CineDolly = 0; CineWeight = 0; } else CineWeight = 1; } } void Blend(Key to, float dt) { float k = CineWeight < 1 ? 1 - MathF.Exp(-dt * 3f) : 1; CineYaw += (to.Yaw - CineYaw) * (_orbit && CineWeight >= 1 ? 1 : k); CinePitch += (to.Pitch - CinePitch) * k; CineDolly += (to.Dolly - CineDolly) * k; CineWeight = 1; } /// Wie sichtbar die Kampfebene (2D-Effekte) gerade ist: 1 von vorn, 0 von der Seite. public float PlaneAlpha => MathHelper.Clamp((MathF.Abs(MathF.Cos(MathHelper.ToRadians(Yaw + CineYaw))) - 0.35f) / 0.4f, 0, 1); /// Publikum im Vordergrund (Bildschirm-Ebene) nur in der Seitenansicht. public float FrontAlpha => 1 - CineWeight; // während einer Fahrt tritt das Publikum im Vordergrund ganz zurück static float WrapDeg(float d) { d %= 360; return d > 180 ? d - 360 : d < -180 ? d + 360 : d; } /// Kameraverschiebung durch Rütteln (Design-Pixel). Vector2 ShakeOffset() { float s = _trauma * _trauma * 26; return new Vector2( s * (MathF.Sin((float)_t * 71) + 0.6f * MathF.Sin((float)_t * 133 + 1.3f)) / 1.6f, s * (MathF.Cos((float)_t * 89) + 0.6f * MathF.Sin((float)_t * 151 + 0.7f)) / 1.6f); } // ── 3D: Perspektive, die die Kampfebene z = 0 genau wie die 2D-Ansicht abbildet ── // Raum in Design-Pixeln: x rechts, y nach unten, z zum Betrachter; Kämpfer bei z = 0. /// Augenhöhe (y) und Abstand der Kamera zur Kampfebene; Nah- und Fernebene. public const float EYE_Y = 330, DIST = 2600, NEAR = 150, FAR = 16000; /// Augenhöhe jetzt (atmet leicht, steigt beim Sprung mit). public float EyeY { get; private set; } = EYE_Y; float _lift, _liftTarget; /// Kamera hebt sich kurz (Sprungangriff): Ziel in Design-Pixeln nach oben. public void Lift(float amount) => _liftTarget = amount; /// Drehung um die Bildmitte (Grad): Kinomomente. Basis gleitet zum Ziel, Puls schwingt hin und zurück. public float Yaw { get; private set; } float _yawBase, _yawTarget, _pulseAmp, _pulseDur, _pulseT = -1; /// Kurzer Schwenk um die Kämpfer und zurück (Raserei, Treffer zum Sieg). public void Pulse(float degrees, float seconds) { // ein laufender, größerer Schwenk wird nicht von einem kleinen unterbrochen if (_pulseT >= 0 && MathF.Abs(_pulseAmp) > MathF.Abs(degrees) && _pulseT < _pulseDur * 0.7f) return; _pulseAmp = degrees; _pulseDur = seconds; _pulseT = 0; } /// Langsame Fahrt auf einen festen Winkel (Sieg); 0 = zurück zur Seitenansicht. public void HoldYaw(float degrees) => _yawTarget = degrees; void UpdateYaw(float dt, bool calm) { _yawBase += (_yawTarget - _yawBase) * (1 - MathF.Exp(-dt * 0.9f)); float pulse = 0; if (_pulseT >= 0) { _pulseT += dt; float k = _pulseT / _pulseDur; if (k >= 1) _pulseT = -1; else pulse = _pulseAmp * MathF.Sin(MathF.PI * k); } // ständige leichte Bewegung: langsames Pendeln und Atmen der Höhe (wie eine Handkamera am Rand) float drift = 3.2f * MathF.Sin((float)_t * 0.21f) + 1.3f * MathF.Sin((float)_t * 0.53f + 1.1f); Yaw = calm ? 0 : _yawBase + pulse + drift; _lift += (_liftTarget - _lift) * (1 - MathF.Exp(-dt * 4)); _liftTarget *= MathF.Exp(-dt * 1.5f); EyeY = calm ? EYE_Y : EYE_Y + 22 * MathF.Sin((float)_t * 0.33f) - _lift; } /// Welt → Kameraraum (Kamera im Ursprung, Blick nach −z), mit Drehung um die Bildmitte. public Matrix View3() { var pivot = new Vector3(Center.X, 0, 0); var view = Matrix.CreateTranslation(-pivot) * Matrix.CreateRotationY(MathHelper.ToRadians(Yaw)) * Matrix.CreateTranslation(pivot); if (CineWeight > 0.001f) { // Kinofahrt: um den Drehpunkt drehen und neigen, dann heranfahren var cp = new Vector3(Center.X, CinePivot.Y, CinePivot.Z); view *= Matrix.CreateTranslation(-cp) * Matrix.CreateRotationY(MathHelper.ToRadians(CineYaw)) * Matrix.CreateRotationX(MathHelper.ToRadians(CinePitch)) * Matrix.CreateTranslation(cp) * Matrix.CreateTranslation(0, 0, CineDolly - FitPull); } return view * Matrix.CreateTranslation(-Center.X, -EyeNow, -DIST); } /// Augenhöhe mit Fahrt: wandert zum Drehpunkt, damit das Heranfahren auf ihn zielt. float EyeNow => MathHelper.Lerp(EyeY, CinePivot.Y, CineWeight * 0.85f); /// Sicherung: so weit fährt die Kamera gerade zurück, damit alle Figuren im Bild bleiben. public float FitPull { get; private set; } /// Figuren im Bild halten: ragt ein Punkt (Kopf, Füße) aus dem sicheren Bereich, zieht die /// Kamera schnell auf; ist genug Platz, kehrt sie langsam zur geplanten Nähe zurück. public void KeepInView(ReadOnlySpan points, float screenScale, Vector2 offset, int vpW, int vpH, float dt) { if (CineWeight < 0.01f && FitPull <= 0) return; var vp = View3() * Proj3(screenScale, offset, vpW, vpH); float worst = 0; foreach (var pt in points) { var c = Vector4.Transform(new Vector4(pt, 1), vp); if (c.W <= 1e-3f) { worst = MathF.Max(worst, 1); continue; } // hinter der Kamera float x = c.X / c.W, y = c.Y / c.W; worst = MathF.Max(worst, MathF.Max(MathF.Abs(x) - 0.78f, MathF.Abs(y) - 0.76f)); } if (worst > 0) FitPull += (900 + 6000 * worst) * dt; else if (worst < -0.08f) FitPull = MathF.Max(0, FitPull - 260 * dt); FitPull = MathF.Min(FitPull, CineDolly + 1400); // höchstens etwas weiter weg als die Seitenansicht // nie hinter die Arenamauer: steht die Kamera außerhalb des Rings, fährt sie wieder heran if (Ring is { } ring && CineWeight > 0.01f) { var eye = Matrix.Invert(View3()).Translation; float d = Vector2.Distance(new Vector2(eye.X, eye.Z), ring.Center); if (d > ring.R - 260) FitPull -= (d - (ring.R - 260)) + 1; } } /// Ring der Arena (Mitte x/z, Radius): die Kinokamera bleibt innen. public (Vector2 Center, float R)? Ring; /// Kameraraum → Bildschirm (Clip): erst perspektivisch zurück in Design-Pixel der Ebene z = 0, /// dann dieselbe 2D-Abbildung wie (Zoom, Wackeln, Stoß), dann Pixel → Clip. public Matrix Proj3(float screenScale, Vector2 offset, int vpW, int vpH) { float ex = Center.X, ey = EyeNow, d = DIST; float a = -FAR / (d * (FAR - NEAR)), b = -NEAR * FAR / (d * (FAR - NEAR)); // Zeilen-Vektor-Form: (x, y, z, 1) → (X, Y, Z, W) mit W = −z/d (z = 0 der Welt ⇒ W = 1) var p = new Matrix( 1, 0, 0, 0, 0, 1, 0, 0, -ex / d, -ey / d, a, -1 / d, 0, 0, b, 0); // Kameraraum hat den Ursprung bei (ex, ey, d): Design-Pixel = x_c + ex·W var v2 = View(screenScale, offset); // nur x/y benutzen (2D-affin) var a2 = new Matrix( v2.M11, v2.M12, 0, 0, v2.M21, v2.M22, 0, 0, 0, 0, 1, 0, v2.M41, v2.M42, 0, 1); var clip = new Matrix( 2f / vpW, 0, 0, 0, 0, -2f / vpH, 0, 0, 0, 0, 1, 0, -1, 1, 0, 1); return p * a2 * clip; } /// Weltpunkt → Bildschirm-Pixel; w > 1 heißt weiter hinten (für Größen von Lichtern). public Vector2 ToPixel(Vector3 world, Matrix view3, Matrix proj3, int vpW, int vpH, out float w) { var c = Vector4.Transform(new Vector4(world, 1), view3 * proj3); w = MathF.Max(0.001f, c.W); return new Vector2((c.X / w + 1) * 0.5f * vpW, (1 - c.Y / w) * 0.5f * vpH); } /// Design-Pixel → Bildschirm. : 1 = Ebene der Kämpfer, <1 weiter hinten. public Matrix View(float screenScale, Vector2 offset, float parallax = 1f) { float zoom = 1 + (Zoom - 1) * parallax + _punch * parallax; var c = new Vector2(W / 2, H / 2) + (Center - new Vector2(W / 2, H / 2)) * parallax + (_punchAt - Center) * _punch * 0.6f * parallax; var shake = ShakeOffset() * parallax; return Matrix.CreateTranslation(-c.X, -c.Y, 0) * Matrix.CreateScale(zoom) * Matrix.CreateTranslation(W / 2 + shake.X, H / 2 + shake.Y, 0) * Matrix.CreateScale(screenScale) * Matrix.CreateTranslation(offset.X, offset.Y, 0); } } }