Engine-Pong/Game/Behaviours/CameraController.cs

67 lines
2.8 KiB
C#
Raw Normal View History

2024-01-30 12:23:45 +03:00
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<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()
{
if (BehaviourController.TryGetBehaviour(out MonoGameCameraBehaviour? foundCameraBehaviour))
cameraBehaviour = foundCameraBehaviour;
cameraBehaviour ??= BehaviourController.AddBehaviour<MonoGameCameraBehaviour>();
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;
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<Keys> inputs, Keys keys)
{
2024-01-30 12:51:30 +03:00
if (GamePong.graphics.IsFullScreen)
2024-01-30 12:23:45 +03:00
return;
2024-01-30 12:51:30 +03:00
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();
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 12:51:30 +03:00
float currentScreenSize = Math.Sqrt(Math.Sqr(GamePong.graphics.GraphicsDevice.Viewport.Width) + Math.Sqr(GamePong.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 12:51:30 +03:00
cameraBehaviour.Viewport = GamePong.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
}