2024-01-30 12:23:45 +03:00
|
|
|
using Microsoft.Xna.Framework.Input;
|
2024-01-30 20:13:56 +03:00
|
|
|
|
2024-01-30 12:23:45 +03:00
|
|
|
using Syntriax.Engine.Core;
|
|
|
|
using Syntriax.Engine.Input;
|
|
|
|
|
|
|
|
namespace Pong.Behaviours;
|
|
|
|
|
|
|
|
public class CameraController : BehaviourOverride
|
|
|
|
{
|
2024-01-31 18:46:54 +03:00
|
|
|
private MonoGameCamera2DBehaviour cameraBehaviour = null!;
|
2024-01-30 12:23:45 +03:00
|
|
|
private IButtonInputs<Keys> buttonInputs = null!;
|
|
|
|
|
2024-01-30 14:35:24 +03:00
|
|
|
private float defaultZoomLevel = 1f;
|
|
|
|
|
2024-01-30 12:23:45 +03:00
|
|
|
protected override void OnFirstActiveFrame()
|
|
|
|
{
|
2024-01-31 18:46:54 +03:00
|
|
|
if (BehaviourController.TryGetBehaviour(out MonoGameCamera2DBehaviour? foundCameraBehaviour))
|
2024-01-30 12:23:45 +03:00
|
|
|
cameraBehaviour = foundCameraBehaviour;
|
|
|
|
|
2024-01-31 18:46:54 +03:00
|
|
|
cameraBehaviour ??= BehaviourController.AddBehaviour<MonoGameCamera2DBehaviour>();
|
2024-01-30 12:23:45 +03:00
|
|
|
|
|
|
|
if (BehaviourController.TryGetBehaviour(out IButtonInputs<Keys>? foundButtonInputs))
|
|
|
|
buttonInputs = foundButtonInputs;
|
|
|
|
|
|
|
|
buttonInputs ??= BehaviourController.AddBehaviour<KeyboardInputsBehaviour>();
|
|
|
|
buttonInputs.RegisterOnPress(Keys.F, SwitchToFullScreen);
|
2024-01-30 14:35:24 +03:00
|
|
|
buttonInputs.RegisterOnPress(Keys.R, ResetCamera);
|
2024-01-30 12:23:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
2024-02-01 18:50:06 +03:00
|
|
|
|
|
|
|
if (buttonInputs.IsPressed(Keys.NumPad8)) cameraBehaviour.BehaviourController.GameObject.Transform.Position += Vector2D.Up.Rotate(Transform.Rotation * Math.DegreeToRadian) * Time.DeltaTimeFrame;
|
|
|
|
if (buttonInputs.IsPressed(Keys.NumPad2)) cameraBehaviour.BehaviourController.GameObject.Transform.Position -= Vector2D.Up.Rotate(Transform.Rotation * Math.DegreeToRadian) * Time.DeltaTimeFrame;
|
|
|
|
if (buttonInputs.IsPressed(Keys.NumPad6)) cameraBehaviour.BehaviourController.GameObject.Transform.Position += Vector2D.Right.Rotate(Transform.Rotation * Math.DegreeToRadian) * Time.DeltaTimeFrame;
|
|
|
|
if (buttonInputs.IsPressed(Keys.NumPad4)) cameraBehaviour.BehaviourController.GameObject.Transform.Position -= Vector2D.Right.Rotate(Transform.Rotation * Math.DegreeToRadian) * Time.DeltaTimeFrame;
|
|
|
|
|
2024-01-31 18:46:54 +03:00
|
|
|
|
2024-01-30 12:23:45 +03:00
|
|
|
if (buttonInputs.IsPressed(Keys.Q))
|
2024-01-31 18:46:54 +03:00
|
|
|
cameraBehaviour.BehaviourController.GameObject.Transform.Rotation += Time.Elapsed.Nanoseconds * 0.0025f;
|
2024-01-30 12:23:45 +03:00
|
|
|
if (buttonInputs.IsPressed(Keys.E))
|
2024-01-31 18:46:54 +03:00
|
|
|
cameraBehaviour.BehaviourController.GameObject.Transform.Rotation -= Time.Elapsed.Nanoseconds * 0.0025f;
|
2024-01-30 12:23:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
private void SwitchToFullScreen(IButtonInputs<Keys> inputs, Keys keys)
|
|
|
|
{
|
2024-01-30 20:13:56 +03:00
|
|
|
if (cameraBehaviour.Graphics.IsFullScreen)
|
2024-01-30 12:23:45 +03:00
|
|
|
return;
|
|
|
|
|
2024-01-30 20:13:56 +03:00
|
|
|
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();
|
2024-01-30 12:23:45 +03:00
|
|
|
|
2024-01-30 12:50:21 +03:00
|
|
|
float previousScreenSize = Math.Sqrt(Math.Sqr(cameraBehaviour.Viewport.Width) + Math.Sqr(cameraBehaviour.Viewport.Height));
|
2024-01-30 20:13:56 +03:00
|
|
|
float currentScreenSize = Math.Sqrt(Math.Sqr(cameraBehaviour.Graphics.GraphicsDevice.Viewport.Width) + Math.Sqr(cameraBehaviour.Graphics.GraphicsDevice.Viewport.Height));
|
2024-01-30 14:35:24 +03:00
|
|
|
defaultZoomLevel /= previousScreenSize / currentScreenSize;
|
2024-01-30 12:23:45 +03:00
|
|
|
cameraBehaviour.Zoom /= previousScreenSize / currentScreenSize;
|
2024-01-30 20:13:56 +03:00
|
|
|
cameraBehaviour.Viewport = cameraBehaviour.Graphics.GraphicsDevice.Viewport;
|
2024-01-30 12:23:45 +03:00
|
|
|
}
|
2024-01-30 14:35:24 +03:00
|
|
|
|
|
|
|
private void ResetCamera(IButtonInputs<Keys> inputs, Keys keys)
|
|
|
|
{
|
|
|
|
cameraBehaviour.Zoom = defaultZoomLevel;
|
|
|
|
Transform.Position = Vector2D.Zero;
|
|
|
|
Transform.Rotation = 0f;
|
|
|
|
}
|
2024-01-30 12:23:45 +03:00
|
|
|
}
|