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 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); } /// 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); return Matrix.CreateTranslation(-pivot) * Matrix.CreateRotationY(MathHelper.ToRadians(Yaw)) * Matrix.CreateTranslation(pivot) * Matrix.CreateTranslation(-Center.X, -EyeY, -DIST); } /// 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 = EyeY, 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); } } }