chore: updated engine
This commit is contained in:
71
Shared/Behaviours/CameraController.cs
Normal file
71
Shared/Behaviours/CameraController.cs
Normal file
@@ -0,0 +1,71 @@
|
||||
using Microsoft.Xna.Framework.Input;
|
||||
|
||||
using Syntriax.Engine.Core;
|
||||
using Syntriax.Engine.Systems.Input;
|
||||
|
||||
namespace Pong.Behaviours;
|
||||
|
||||
public class CameraController : Behaviour
|
||||
{
|
||||
private MonoGameCamera2DBehaviour cameraBehaviour = null!;
|
||||
private IButtonInputs<Keys> buttonInputs = null!;
|
||||
|
||||
private float defaultZoomLevel = 1f;
|
||||
|
||||
protected override void OnFirstActiveFrame()
|
||||
{
|
||||
if (BehaviourController.TryGetBehaviour(out MonoGameCamera2DBehaviour? foundCameraBehaviour))
|
||||
cameraBehaviour = foundCameraBehaviour;
|
||||
|
||||
cameraBehaviour ??= BehaviourController.AddBehaviour<MonoGameCamera2DBehaviour>();
|
||||
|
||||
buttonInputs = GameManager.FindBehaviour<IButtonInputs<Keys>>() ?? throw new("Unable to find inputs");
|
||||
buttonInputs.RegisterOnPress(Keys.F, SwitchToFullScreen);
|
||||
buttonInputs.RegisterOnPress(Keys.R, ResetCamera);
|
||||
}
|
||||
|
||||
protected override void OnUpdate()
|
||||
{
|
||||
if (buttonInputs.IsPressed(Keys.U))
|
||||
cameraBehaviour.Zoom += GameManager.Time.DeltaTime * 5f;
|
||||
if (buttonInputs.IsPressed(Keys.J))
|
||||
cameraBehaviour.Zoom -= GameManager.Time.DeltaTime * 5f;
|
||||
|
||||
|
||||
if (buttonInputs.IsPressed(Keys.NumPad8)) cameraBehaviour.Transform.LocalPosition += Vector2D.Up * GameManager.Time.DeltaTime * 500f;
|
||||
if (buttonInputs.IsPressed(Keys.NumPad2)) cameraBehaviour.Transform.LocalPosition -= Vector2D.Up * GameManager.Time.DeltaTime * 500f;
|
||||
if (buttonInputs.IsPressed(Keys.NumPad6)) cameraBehaviour.Transform.LocalPosition += Vector2D.Right * GameManager.Time.DeltaTime * 500f;
|
||||
if (buttonInputs.IsPressed(Keys.NumPad4)) cameraBehaviour.Transform.LocalPosition -= Vector2D.Right * GameManager.Time.DeltaTime * 500f;
|
||||
|
||||
|
||||
if (buttonInputs.IsPressed(Keys.Q))
|
||||
cameraBehaviour.Transform.Rotation += GameManager.Time.DeltaTime * 45f;
|
||||
if (buttonInputs.IsPressed(Keys.E))
|
||||
cameraBehaviour.Transform.Rotation -= GameManager.Time.DeltaTime * 45f;
|
||||
}
|
||||
|
||||
private void SwitchToFullScreen(IButtonInputs<Keys> inputs, Keys keys)
|
||||
{
|
||||
if (cameraBehaviour.Graphics.IsFullScreen)
|
||||
return;
|
||||
|
||||
cameraBehaviour.Graphics.PreferMultiSampling = false;
|
||||
cameraBehaviour.Graphics.PreferredBackBufferWidth = cameraBehaviour.Graphics.GraphicsDevice.Adapter.CurrentDisplayMode.Width;
|
||||
cameraBehaviour.Graphics.PreferredBackBufferHeight = cameraBehaviour.Graphics.GraphicsDevice.Adapter.CurrentDisplayMode.Height;
|
||||
cameraBehaviour.Graphics.IsFullScreen = true;
|
||||
cameraBehaviour.Graphics.ApplyChanges();
|
||||
|
||||
float previousScreenSize = Math.Sqrt(Math.Sqr(cameraBehaviour.Viewport.Width) + Math.Sqr(cameraBehaviour.Viewport.Height));
|
||||
float currentScreenSize = Math.Sqrt(Math.Sqr(cameraBehaviour.Graphics.GraphicsDevice.Viewport.Width) + Math.Sqr(cameraBehaviour.Graphics.GraphicsDevice.Viewport.Height));
|
||||
defaultZoomLevel /= previousScreenSize / currentScreenSize;
|
||||
cameraBehaviour.Zoom /= previousScreenSize / currentScreenSize;
|
||||
cameraBehaviour.Viewport = cameraBehaviour.Graphics.GraphicsDevice.Viewport;
|
||||
}
|
||||
|
||||
private void ResetCamera(IButtonInputs<Keys> inputs, Keys keys)
|
||||
{
|
||||
cameraBehaviour.Zoom = defaultZoomLevel;
|
||||
cameraBehaviour.Transform.LocalPosition = Vector2D.Zero;
|
||||
cameraBehaviour.Transform.LocalRotation = 0f;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user