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 buttonInputs = null!; private float defaultZoomLevel = 1f; protected override void OnFirstActiveFrame() { cameraBehaviour ??= BehaviourController.GetRequiredBehaviour(); buttonInputs = Universe.FindRequiredBehaviour>(); buttonInputs.RegisterOnPress(Keys.F, SwitchToFullScreen); buttonInputs.RegisterOnPress(Keys.R, ResetCamera); } protected override void OnUpdate() { if (buttonInputs.IsPressed(Keys.U)) cameraBehaviour.Zoom += Universe.Time.DeltaTime * 5f; if (buttonInputs.IsPressed(Keys.J)) cameraBehaviour.Zoom -= Universe.Time.DeltaTime * 5f; if (buttonInputs.IsPressed(Keys.NumPad8)) cameraBehaviour.Transform.LocalPosition += Vector2D.Up * Universe.Time.DeltaTime * 500f; if (buttonInputs.IsPressed(Keys.NumPad2)) cameraBehaviour.Transform.LocalPosition -= Vector2D.Up * Universe.Time.DeltaTime * 500f; if (buttonInputs.IsPressed(Keys.NumPad6)) cameraBehaviour.Transform.LocalPosition += Vector2D.Right * Universe.Time.DeltaTime * 500f; if (buttonInputs.IsPressed(Keys.NumPad4)) cameraBehaviour.Transform.LocalPosition -= Vector2D.Right * Universe.Time.DeltaTime * 500f; if (buttonInputs.IsPressed(Keys.Q)) cameraBehaviour.Transform.Rotation += Universe.Time.DeltaTime * 45f; if (buttonInputs.IsPressed(Keys.E)) cameraBehaviour.Transform.Rotation -= Universe.Time.DeltaTime * 45f; } private void SwitchToFullScreen(IButtonInputs 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 inputs, Keys keys) { cameraBehaviour.Zoom = defaultZoomLevel; cameraBehaviour.Transform.LocalPosition = Vector2D.Zero; cameraBehaviour.Transform.LocalRotation = 0f; } }