refactor: Camera Controller
This commit is contained in:
parent
958a75552f
commit
a9c7b8b660
|
@ -0,0 +1,55 @@
|
||||||
|
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!;
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
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)
|
||||||
|
{
|
||||||
|
if (Game1.graphics.IsFullScreen)
|
||||||
|
return;
|
||||||
|
|
||||||
|
Game1.graphics.PreferMultiSampling = false;
|
||||||
|
Game1.graphics.PreferredBackBufferWidth = Game1.graphics.GraphicsDevice.Adapter.CurrentDisplayMode.Width;
|
||||||
|
Game1.graphics.PreferredBackBufferHeight = Game1.graphics.GraphicsDevice.Adapter.CurrentDisplayMode.Height;
|
||||||
|
Game1.graphics.IsFullScreen = true;
|
||||||
|
Game1.graphics.ApplyChanges();
|
||||||
|
|
||||||
|
float previousScreenSize = Math.Sqrt(Math.Pow(cameraBehaviour.Viewport.Width, 2f) + Math.Pow(cameraBehaviour.Viewport.Height, 2f));
|
||||||
|
float currentScreenSize = Math.Sqrt(Math.Pow(Game1.graphics.GraphicsDevice.Viewport.Width, 2f) + Math.Pow(Game1.graphics.GraphicsDevice.Viewport.Height, 2f));
|
||||||
|
cameraBehaviour.Zoom /= previousScreenSize / currentScreenSize;
|
||||||
|
cameraBehaviour.Viewport = Game1.graphics.GraphicsDevice.Viewport;
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,17 +1,12 @@
|
||||||
using Microsoft.Xna.Framework;
|
using Microsoft.Xna.Framework;
|
||||||
using Microsoft.Xna.Framework.Graphics;
|
using Microsoft.Xna.Framework.Graphics;
|
||||||
|
|
||||||
using Syntriax.Engine.Core;
|
using Syntriax.Engine.Core;
|
||||||
using Syntriax.Engine.Core.Abstract;
|
|
||||||
|
|
||||||
namespace Pong.Behaviours;
|
namespace Pong.Behaviours;
|
||||||
|
|
||||||
public class MonoGameCameraBehaviour : BehaviourOverride
|
public class MonoGameCameraBehaviour : BehaviourOverride
|
||||||
{
|
{
|
||||||
public System.Action<MonoGameCameraBehaviour>? OnPositionChanged { get; set; } = null;
|
|
||||||
public System.Action<MonoGameCameraBehaviour>? OnMatrixTransformChanged { get; set; } = null;
|
public System.Action<MonoGameCameraBehaviour>? OnMatrixTransformChanged { get; set; } = null;
|
||||||
public System.Action<MonoGameCameraBehaviour>? OnViewportChanged { get; set; } = null;
|
public System.Action<MonoGameCameraBehaviour>? OnViewportChanged { get; set; } = null;
|
||||||
public System.Action<MonoGameCameraBehaviour>? OnRotationChanged { get; set; } = null;
|
|
||||||
public System.Action<MonoGameCameraBehaviour>? OnZoomChanged { get; set; } = null;
|
public System.Action<MonoGameCameraBehaviour>? OnZoomChanged { get; set; } = null;
|
||||||
|
|
||||||
private Matrix _matrixTransform = Matrix.Identity;
|
private Matrix _matrixTransform = Matrix.Identity;
|
||||||
|
@ -81,19 +76,4 @@ public class MonoGameCameraBehaviour : BehaviourOverride
|
||||||
Matrix.CreateScale(Zoom) *
|
Matrix.CreateScale(Zoom) *
|
||||||
Matrix.CreateTranslation(new Vector3(_viewport.Width * .5f, _viewport.Height * .5f, 0f));
|
Matrix.CreateTranslation(new Vector3(_viewport.Width * .5f, _viewport.Height * .5f, 0f));
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override void OnInitialize()
|
|
||||||
{
|
|
||||||
Transform.OnRotationChanged += OnTransformRotationChanged;
|
|
||||||
Transform.OnPositionChanged += OnTransformPositionChanged;
|
|
||||||
}
|
|
||||||
|
|
||||||
protected override void OnFinalize()
|
|
||||||
{
|
|
||||||
Transform.OnRotationChanged -= OnTransformRotationChanged;
|
|
||||||
Transform.OnPositionChanged -= OnTransformPositionChanged;
|
|
||||||
}
|
|
||||||
|
|
||||||
private void OnTransformRotationChanged(ITransform _) => OnRotationChanged?.Invoke(this);
|
|
||||||
private void OnTransformPositionChanged(ITransform _) => OnPositionChanged?.Invoke(this);
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,12 +16,12 @@ namespace Pong;
|
||||||
|
|
||||||
public class Game1 : Game
|
public class Game1 : Game
|
||||||
{
|
{
|
||||||
private GraphicsDeviceManager _graphics = null!;
|
public static GraphicsDeviceManager graphics = null!;
|
||||||
private IPhysicsEngine2D engine = null!;
|
public static IPhysicsEngine2D engine = null!;
|
||||||
private SpriteBatch _spriteBatch = null!;
|
public static SpriteBatch spriteBatch = null!;
|
||||||
private ShapeBatch _shapeBatch = null!;
|
public static ShapeBatch shapeBatch = null!;
|
||||||
public static GameManager gameManager = null!;
|
public static GameManager gameManager = null!;
|
||||||
public static Sprite spriteBox = null!;
|
|
||||||
private MonoGameCameraBehaviour cameraBehaviour = null!;
|
private MonoGameCameraBehaviour cameraBehaviour = null!;
|
||||||
private PongScoreboard pongScoreboard;
|
private PongScoreboard pongScoreboard;
|
||||||
|
|
||||||
|
@ -32,7 +32,7 @@ public class Game1 : Game
|
||||||
IterationCount = 3
|
IterationCount = 3
|
||||||
};
|
};
|
||||||
|
|
||||||
_graphics = new GraphicsDeviceManager(this)
|
graphics = new GraphicsDeviceManager(this)
|
||||||
{
|
{
|
||||||
PreferredBackBufferWidth = 1024,
|
PreferredBackBufferWidth = 1024,
|
||||||
PreferredBackBufferHeight = 576,
|
PreferredBackBufferHeight = 576,
|
||||||
|
@ -55,17 +55,16 @@ public class Game1 : Game
|
||||||
|
|
||||||
protected override void LoadContent()
|
protected override void LoadContent()
|
||||||
{
|
{
|
||||||
_spriteBatch = new SpriteBatch(GraphicsDevice);
|
spriteBatch = new SpriteBatch(GraphicsDevice);
|
||||||
_shapeBatch = new ShapeBatch(GraphicsDevice, Content);
|
shapeBatch = new ShapeBatch(GraphicsDevice, Content);
|
||||||
SpriteFont spriteFont = Content.Load<SpriteFont>("UbuntuMono");
|
SpriteFont spriteFont = Content.Load<SpriteFont>("UbuntuMono");
|
||||||
|
|
||||||
// spriteBox = new Sprite() { Texture2D = Content.Load<Texture2D>("Sprites/Pixel") };
|
|
||||||
// Sprite spriteBall = new Sprite() { Texture2D = Content.Load<Texture2D>("Sprites/Circle") };
|
|
||||||
|
|
||||||
IGameObject gameObjectCamera = gameManager.InstantiateGameObject<GameObject>();
|
IGameObject gameObjectCamera = gameManager.InstantiateGameObject<GameObject>();
|
||||||
gameObjectCamera.Name = "Camera";
|
gameObjectCamera.Name = "Camera";
|
||||||
gameObjectCamera.Transform.Position = Vector2D.Zero;
|
gameObjectCamera.Transform.Position = Vector2D.Zero;
|
||||||
|
|
||||||
|
gameObjectCamera.BehaviourController.AddBehaviour<CameraController>();
|
||||||
|
|
||||||
cameraBehaviour = gameObjectCamera.BehaviourController.AddBehaviour<MonoGameCameraBehaviour>();
|
cameraBehaviour = gameObjectCamera.BehaviourController.AddBehaviour<MonoGameCameraBehaviour>();
|
||||||
cameraBehaviour.Viewport = GraphicsDevice.Viewport;
|
cameraBehaviour.Viewport = GraphicsDevice.Viewport;
|
||||||
|
|
||||||
|
@ -193,33 +192,6 @@ public class Game1 : Game
|
||||||
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
|
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
|
||||||
Exit();
|
Exit();
|
||||||
|
|
||||||
if (Keyboard.GetState().IsKeyDown(Keys.F))
|
|
||||||
{
|
|
||||||
if (_graphics.IsFullScreen)
|
|
||||||
return;
|
|
||||||
|
|
||||||
_graphics.PreferMultiSampling = false;
|
|
||||||
_graphics.PreferredBackBufferWidth = GraphicsDevice.Adapter.CurrentDisplayMode.Width;
|
|
||||||
_graphics.PreferredBackBufferHeight = GraphicsDevice.Adapter.CurrentDisplayMode.Height;
|
|
||||||
_graphics.IsFullScreen = true;
|
|
||||||
_graphics.ApplyChanges();
|
|
||||||
|
|
||||||
float previousScreenSize = MathF.Sqrt(MathF.Pow(cameraBehaviour.Viewport.Width, 2f) + MathF.Pow(cameraBehaviour.Viewport.Height, 2f));
|
|
||||||
float currentScreenSize = MathF.Sqrt(MathF.Pow(GraphicsDevice.Viewport.Width, 2f) + MathF.Pow(GraphicsDevice.Viewport.Height, 2f));
|
|
||||||
cameraBehaviour.Zoom /= previousScreenSize / currentScreenSize;
|
|
||||||
cameraBehaviour.Viewport = GraphicsDevice.Viewport;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (Keyboard.GetState().IsKeyDown(Keys.U))
|
|
||||||
cameraBehaviour.Zoom += gameTime.ElapsedGameTime.Nanoseconds * 0.00025f;
|
|
||||||
if (Keyboard.GetState().IsKeyDown(Keys.J))
|
|
||||||
cameraBehaviour.Zoom -= gameTime.ElapsedGameTime.Nanoseconds * 0.00025f;
|
|
||||||
|
|
||||||
if (Keyboard.GetState().IsKeyDown(Keys.Q))
|
|
||||||
cameraBehaviour.BehaviourController.GameObject.Transform.Rotation += gameTime.ElapsedGameTime.Nanoseconds * 0.000025f;
|
|
||||||
if (Keyboard.GetState().IsKeyDown(Keys.E))
|
|
||||||
cameraBehaviour.BehaviourController.GameObject.Transform.Rotation -= gameTime.ElapsedGameTime.Nanoseconds * 0.000025f;
|
|
||||||
|
|
||||||
if (Keyboard.GetState().IsKeyDown(Keys.Space))
|
if (Keyboard.GetState().IsKeyDown(Keys.Space))
|
||||||
pongScoreboard.Reset();
|
pongScoreboard.Reset();
|
||||||
|
|
||||||
|
@ -243,17 +215,17 @@ public class Game1 : Game
|
||||||
// TODO: Add your drawing code here
|
// TODO: Add your drawing code here
|
||||||
gameManager.PreDraw();
|
gameManager.PreDraw();
|
||||||
|
|
||||||
_spriteBatch.Begin(SpriteSortMode.Deferred, transformMatrix: cameraBehaviour.MatrixTransform);
|
spriteBatch.Begin(SpriteSortMode.Deferred, transformMatrix: cameraBehaviour.MatrixTransform);
|
||||||
foreach (IGameObject gameObject in gameManager)
|
foreach (IGameObject gameObject in gameManager)
|
||||||
foreach (var displayable in gameObject.BehaviourController.GetBehaviours<IDisplayable>())
|
foreach (var displayable in gameObject.BehaviourController.GetBehaviours<IDisplayable>())
|
||||||
displayable.Draw(_spriteBatch);
|
displayable.Draw(spriteBatch);
|
||||||
_spriteBatch.End();
|
spriteBatch.End();
|
||||||
|
|
||||||
_shapeBatch.Begin(cameraBehaviour.MatrixTransform);
|
shapeBatch.Begin(cameraBehaviour.MatrixTransform);
|
||||||
foreach (IGameObject gameObject in gameManager)
|
foreach (IGameObject gameObject in gameManager)
|
||||||
foreach (var displayableShape in gameObject.BehaviourController.GetBehaviours<IDisplayableShape>())
|
foreach (var displayableShape in gameObject.BehaviourController.GetBehaviours<IDisplayableShape>())
|
||||||
displayableShape.Draw(_shapeBatch);
|
displayableShape.Draw(shapeBatch);
|
||||||
_shapeBatch.End();
|
shapeBatch.End();
|
||||||
|
|
||||||
base.Draw(gameTime);
|
base.Draw(gameTime);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue