using Microsoft.Xna.Framework.Input; using Syntriax.Engine.Core; using Syntriax.Engine.Input; namespace Pong.Behaviours; public class CameraController : BehaviourOverride { private MonoGameCameraBehaviour cameraBehaviour = null!; private IButtonInputs buttonInputs = null!; private float defaultZoomLevel = 1f; protected override void OnFirstActiveFrame() { if (BehaviourController.TryGetBehaviour(out MonoGameCameraBehaviour? foundCameraBehaviour)) cameraBehaviour = foundCameraBehaviour; cameraBehaviour ??= BehaviourController.AddBehaviour(); if (BehaviourController.TryGetBehaviour(out IButtonInputs? foundButtonInputs)) buttonInputs = foundButtonInputs; buttonInputs ??= BehaviourController.AddBehaviour(); buttonInputs.RegisterOnPress(Keys.F, SwitchToFullScreen); buttonInputs.RegisterOnPress(Keys.R, ResetCamera); } protected override void OnUpdate() { if (buttonInputs.IsPressed(Keys.U)) cameraBehaviour.Zoom += Time.Elapsed.Nanoseconds * 0.00025f; if (buttonInputs.IsPressed(Keys.J)) cameraBehaviour.Zoom -= Time.Elapsed.Nanoseconds * 0.00025f; if (buttonInputs.IsPressed(Keys.Q)) cameraBehaviour.BehaviourController.GameObject.Transform.Rotation += Time.Elapsed.Nanoseconds * 0.000025f; if (buttonInputs.IsPressed(Keys.E)) cameraBehaviour.BehaviourController.GameObject.Transform.Rotation -= Time.Elapsed.Nanoseconds * 0.000025f; } private void SwitchToFullScreen(IButtonInputs inputs, Keys keys) { if (GamePong.graphics.IsFullScreen) return; GamePong.graphics.PreferMultiSampling = false; GamePong.graphics.PreferredBackBufferWidth = GamePong.graphics.GraphicsDevice.Adapter.CurrentDisplayMode.Width; GamePong.graphics.PreferredBackBufferHeight = GamePong.graphics.GraphicsDevice.Adapter.CurrentDisplayMode.Height; GamePong.graphics.IsFullScreen = true; GamePong.graphics.ApplyChanges(); float previousScreenSize = Math.Sqrt(Math.Sqr(cameraBehaviour.Viewport.Width) + Math.Sqr(cameraBehaviour.Viewport.Height)); float currentScreenSize = Math.Sqrt(Math.Sqr(GamePong.graphics.GraphicsDevice.Viewport.Width) + Math.Sqr(GamePong.graphics.GraphicsDevice.Viewport.Height)); defaultZoomLevel /= previousScreenSize / currentScreenSize; cameraBehaviour.Zoom /= previousScreenSize / currentScreenSize; cameraBehaviour.Viewport = GamePong.graphics.GraphicsDevice.Viewport; } private void ResetCamera(IButtonInputs inputs, Keys keys) { cameraBehaviour.Zoom = defaultZoomLevel; Transform.Position = Vector2D.Zero; Transform.Rotation = 0f; } }