69 lines
3.1 KiB
C#
69 lines
3.1 KiB
C#
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()
|
|
{
|
|
cameraBehaviour ??= BehaviourController.GetRequiredBehaviour<MonoGameCamera2DBehaviour>();
|
|
buttonInputs = GameManager.FindRequiredBehaviour<IButtonInputs<Keys>>();
|
|
|
|
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;
|
|
}
|
|
}
|