feat: Applied ICamera2D

This commit is contained in:
Syntriax 2024-01-31 18:46:54 +03:00
parent 35933dfd14
commit 1fd8060857
4 changed files with 40 additions and 13 deletions

2
Engine

@ -1 +1 @@
Subproject commit de336d0ee5323da3f8b5db3adc69b6afd2f1beff
Subproject commit 6c36d4d21d4f84a0ae986d4c75983235287dc413

View File

@ -7,17 +7,17 @@ namespace Pong.Behaviours;
public class CameraController : BehaviourOverride
{
private MonoGameCameraBehaviour cameraBehaviour = null!;
private MonoGameCamera2DBehaviour cameraBehaviour = null!;
private IButtonInputs<Keys> buttonInputs = null!;
private float defaultZoomLevel = 1f;
protected override void OnFirstActiveFrame()
{
if (BehaviourController.TryGetBehaviour(out MonoGameCameraBehaviour? foundCameraBehaviour))
if (BehaviourController.TryGetBehaviour(out MonoGameCamera2DBehaviour? foundCameraBehaviour))
cameraBehaviour = foundCameraBehaviour;
cameraBehaviour ??= BehaviourController.AddBehaviour<MonoGameCameraBehaviour>();
cameraBehaviour ??= BehaviourController.AddBehaviour<MonoGameCamera2DBehaviour>();
if (BehaviourController.TryGetBehaviour(out IButtonInputs<Keys>? foundButtonInputs))
buttonInputs = foundButtonInputs;
@ -34,10 +34,19 @@ public class CameraController : BehaviourOverride
if (buttonInputs.IsPressed(Keys.J))
cameraBehaviour.Zoom -= Time.Elapsed.Nanoseconds * 0.00025f;
if (buttonInputs.IsPressed(Keys.NumPad8))
cameraBehaviour.BehaviourController.GameObject.Transform.Position += Vector2D.Up;
if (buttonInputs.IsPressed(Keys.NumPad2))
cameraBehaviour.BehaviourController.GameObject.Transform.Position -= Vector2D.Up;
if (buttonInputs.IsPressed(Keys.NumPad6))
cameraBehaviour.BehaviourController.GameObject.Transform.Position += Vector2D.Right;
if (buttonInputs.IsPressed(Keys.NumPad4))
cameraBehaviour.BehaviourController.GameObject.Transform.Position -= Vector2D.Right;
if (buttonInputs.IsPressed(Keys.Q))
cameraBehaviour.BehaviourController.GameObject.Transform.Rotation += Time.Elapsed.Nanoseconds * 0.000025f;
cameraBehaviour.BehaviourController.GameObject.Transform.Rotation += Time.Elapsed.Nanoseconds * 0.0025f;
if (buttonInputs.IsPressed(Keys.E))
cameraBehaviour.BehaviourController.GameObject.Transform.Rotation -= Time.Elapsed.Nanoseconds * 0.000025f;
cameraBehaviour.BehaviourController.GameObject.Transform.Rotation -= Time.Elapsed.Nanoseconds * 0.0025f;
}
private void SwitchToFullScreen(IButtonInputs<Keys> inputs, Keys keys)

View File

@ -1,14 +1,16 @@
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Syntriax.Engine.Core;
using Syntriax.Engine.Core.Abstract;
namespace Pong.Behaviours;
public class MonoGameCameraBehaviour(GraphicsDeviceManager Graphics) : BehaviourOverride
public class MonoGameCamera2DBehaviour(GraphicsDeviceManager Graphics) : BehaviourOverride, ICamera2D
{
public System.Action<MonoGameCameraBehaviour>? OnMatrixTransformChanged { get; set; } = null;
public System.Action<MonoGameCameraBehaviour>? OnViewportChanged { get; set; } = null;
public System.Action<MonoGameCameraBehaviour>? OnZoomChanged { get; set; } = null;
public System.Action<MonoGameCamera2DBehaviour>? OnMatrixTransformChanged { get; set; } = null;
public System.Action<MonoGameCamera2DBehaviour>? OnViewportChanged { get; set; } = null;
public System.Action<MonoGameCamera2DBehaviour>? OnZoomChanged { get; set; } = null;
private Matrix _matrixTransform = Matrix.Identity;
@ -70,6 +72,22 @@ public class MonoGameCameraBehaviour(GraphicsDeviceManager Graphics) : Behaviour
set => Transform.Rotation = value;
}
System.Action<IAssignableTransform>? IAssignableTransform.OnTransformAssigned { get => GameObject.OnTransformAssigned; set => GameObject.OnTransformAssigned = value; }
ITransform IAssignableTransform.Transform => GameObject.Transform;
bool IAssignableTransform.Assign(ITransform transform) => GameObject.Assign(transform);
// TODO This causes delay since OnPreDraw calls assuming this is called in in Update
public Vector2D ScreenToWorldPosition(Vector2D screenPosition)
{
Vector2D worldPosition = Vector2.Transform(screenPosition.ToVector2(), Matrix.Invert(MatrixTransform)).ToVector2D();
return worldPosition.Scale(EngineConverter.screenScale);
}
public Vector2D WorldToScreenPosition(Vector2D worldPosition)
{
Vector2D screenPosition = Vector2.Transform(worldPosition.ToVector2(), MatrixTransform).ToVector2D();
return screenPosition.Scale(EngineConverter.screenScale);
}
protected override void OnFirstActiveFrame()
=> Viewport = Graphics.GraphicsDevice.Viewport;
@ -77,7 +95,7 @@ public class MonoGameCameraBehaviour(GraphicsDeviceManager Graphics) : Behaviour
{
MatrixTransform =
Matrix.CreateTranslation(new Vector3(-Position.X, Position.Y, 0f)) *
Matrix.CreateRotationZ(Rotation) *
Matrix.CreateRotationZ(Rotation * Math.DegreeToRadian) *
Matrix.CreateScale(Zoom) *
Matrix.CreateTranslation(new Vector3(_viewport.Width * .5f, _viewport.Height * .5f, 0f));
}

View File

@ -24,7 +24,7 @@ public class GamePong : Game
private GameManager gameManager = null!;
private BehaviourCacher<IDisplayableSprite> displayableCacher = null!;
private BehaviourCacher<IDisplayableShape> displayableShapeCacher = null!;
private MonoGameCameraBehaviour cameraBehaviour = null!;
private MonoGameCamera2DBehaviour cameraBehaviour = null!;
private PongManagerBehaviour pongManager = null!;
@ -66,7 +66,7 @@ public class GamePong : Game
IGameObject gameObjectCamera = gameManager.InstantiateGameObject<GameObject>().SetGameObject("Camera"); ;
gameObjectCamera.BehaviourController.AddBehaviour<CameraController>();
cameraBehaviour = gameObjectCamera.BehaviourController.AddBehaviour<MonoGameCameraBehaviour>(graphics);
cameraBehaviour = gameObjectCamera.BehaviourController.AddBehaviour<MonoGameCamera2DBehaviour>(graphics);
////////////////////////////////////////////////////////////////////////////////////