refactor: monogame camera transform caching
This commit is contained in:
		@@ -5,7 +5,7 @@ using Engine.Core;
 | 
			
		||||
 | 
			
		||||
namespace Engine.Integration.MonoGame;
 | 
			
		||||
 | 
			
		||||
public class MonoGameCamera2D : Behaviour, ICamera2D, IFirstFrameUpdate, IPreDraw
 | 
			
		||||
public class MonoGameCamera2D : Behaviour, ICamera2D, IFirstFrameUpdate, ILastFrameUpdate, IPreDraw
 | 
			
		||||
{
 | 
			
		||||
    public Event<MonoGameCamera2D> OnMatrixTransformChanged { get; } = new();
 | 
			
		||||
    public Event<MonoGameCamera2D> OnViewportChanged { get; } = new();
 | 
			
		||||
@@ -56,7 +56,7 @@ public class MonoGameCamera2D : Behaviour, ICamera2D, IFirstFrameUpdate, IPreDra
 | 
			
		||||
        get => _zoom;
 | 
			
		||||
        set
 | 
			
		||||
        {
 | 
			
		||||
            float newValue = Engine.Core.Math.Max(0.1f, value);
 | 
			
		||||
            float newValue = Math.Max(0.1f, value);
 | 
			
		||||
 | 
			
		||||
            if (_zoom == newValue)
 | 
			
		||||
                return;
 | 
			
		||||
@@ -84,22 +84,21 @@ public class MonoGameCamera2D : Behaviour, ICamera2D, IFirstFrameUpdate, IPreDra
 | 
			
		||||
        return screenPosition.Scale(EngineConverterExtensions.screenScale);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public void LastActiveFrame() => Transform = null!;
 | 
			
		||||
    public void FirstActiveFrame()
 | 
			
		||||
    {
 | 
			
		||||
        Graphics = BehaviourController.UniverseObject.Universe.FindRequiredBehaviour<MonoGameWindowContainer>().Window.Graphics;
 | 
			
		||||
        Viewport = Graphics.GraphicsDevice.Viewport;
 | 
			
		||||
        Transform = BehaviourController.GetRequiredBehaviour<ITransform2D>();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public void PreDraw()
 | 
			
		||||
    {
 | 
			
		||||
        MatrixTransform =
 | 
			
		||||
            Matrix.CreateTranslation(new Vector3(-Position.X, Position.Y, 0f)) *
 | 
			
		||||
            Matrix.CreateRotationZ(Rotation * Engine.Core.Math.DegreeToRadian) *
 | 
			
		||||
            Matrix.CreateRotationZ(Rotation * Math.DegreeToRadian) *
 | 
			
		||||
            Matrix.CreateScale(Transform.Scale.X.Max(Transform.Scale.Y)) *
 | 
			
		||||
            Matrix.CreateScale(Zoom) *
 | 
			
		||||
            Matrix.CreateTranslation(new Vector3(_viewport.Width * .5f, _viewport.Height * .5f, 0f));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    protected sealed override void InitializeInternal() => Transform = BehaviourController.GetRequiredBehaviour<ITransform2D>();
 | 
			
		||||
    protected sealed override void FinalizeInternal() => Transform = null!;
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user