64 lines
2.8 KiB
C#
64 lines
2.8 KiB
C#
|
|
using System;
|
||
|
|
using Microsoft.Xna.Framework;
|
||
|
|
|
||
|
|
namespace GladiatorIdle.Arena
|
||
|
|
{
|
||
|
|
/// <summary>
|
||
|
|
/// 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).
|
||
|
|
/// </summary>
|
||
|
|
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;
|
||
|
|
|
||
|
|
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); }
|
||
|
|
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; }
|
||
|
|
}
|
||
|
|
|
||
|
|
/// 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);
|
||
|
|
}
|
||
|
|
|
||
|
|
/// Design-Pixel → Bildschirm. <paramref name="parallax"/>: 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);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|