102 lines
2.7 KiB
C#
102 lines
2.7 KiB
C#
using System;
|
|
|
|
using Microsoft.Xna.Framework;
|
|
using Microsoft.Xna.Framework.Graphics;
|
|
|
|
using Syntriax.Engine.Core;
|
|
using Syntriax.Engine.Core.Abstract;
|
|
|
|
namespace Pong.Behaviours;
|
|
|
|
public class MonoGameCameraBehaviour : BehaviourOverride
|
|
{
|
|
public Action<MonoGameCameraBehaviour>? OnPositionChanged { get; set; } = null;
|
|
public Action<MonoGameCameraBehaviour>? OnMatrixTransformChanged { get; set; } = null;
|
|
public Action<MonoGameCameraBehaviour>? OnViewportChanged { get; set; } = null;
|
|
public Action<MonoGameCameraBehaviour>? OnRotationChanged { get; set; } = null;
|
|
public Action<MonoGameCameraBehaviour>? 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 = value >= .1f ? value : .1f;
|
|
|
|
if (_zoom == newValue)
|
|
return;
|
|
|
|
_zoom = newValue;
|
|
OnZoomChanged?.Invoke(this);
|
|
}
|
|
}
|
|
|
|
public float Rotation
|
|
{
|
|
get => Transform.Rotation;
|
|
set => Transform.Rotation = value;
|
|
}
|
|
|
|
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));
|
|
}
|
|
|
|
protected override void OnInitialize()
|
|
{
|
|
Transform.OnRotationChanged += OnTransformRotationChanged;
|
|
Transform.OnPositionChanged += OnTransformPositionChanged;
|
|
}
|
|
|
|
protected override void OnFinalize()
|
|
{
|
|
Transform.OnRotationChanged -= OnTransformRotationChanged;
|
|
Transform.OnPositionChanged -= OnTransformPositionChanged;
|
|
}
|
|
|
|
private void OnTransformRotationChanged(ITransform _) => OnRotationChanged?.Invoke(this);
|
|
private void OnTransformPositionChanged(ITransform _) => OnPositionChanged?.Invoke(this);
|
|
}
|