using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; using Syntriax.Engine.Core; namespace Pong.Behaviours; public class MonoGameCameraBehaviour : BehaviourOverride { public System.Action? OnMatrixTransformChanged { get; set; } = null; public System.Action? OnViewportChanged { get; set; } = null; public System.Action? OnZoomChanged { get; set; } = null; private Matrix _matrixTransform = Matrix.Identity; private Viewport _viewport = default; private float _zoom = 1f; public Matrix MatrixTransform { get => _matrixTransform; set { if (_matrixTransform == value) return; _matrixTransform = value; OnMatrixTransformChanged?.Invoke(this); } } public Vector2D Position { get => Transform.Position; set => Transform.Position = value; } public Viewport Viewport { get => _viewport; set { if (_viewport.Equals(value)) return; _viewport = value; OnViewportChanged?.Invoke(this); } } public float Zoom { get => _zoom; set { float newValue = Math.Max(0.1f, value); if (_zoom == newValue) return; _zoom = newValue; OnZoomChanged?.Invoke(this); } } public float Rotation { get => Transform.Rotation; set => Transform.Rotation = value; } protected override void OnFirstActiveFrame() => Viewport = GamePong.graphics.GraphicsDevice.Viewport; protected override void OnPreDraw() { MatrixTransform = Matrix.CreateTranslation(new Vector3(-Position.X, Position.Y, 0f)) * Matrix.CreateRotationZ(Rotation) * Matrix.CreateScale(Zoom) * Matrix.CreateTranslation(new Vector3(_viewport.Width * .5f, _viewport.Height * .5f, 0f)); } }