refactor: Removed Static GamePong Fields

This commit is contained in:
Syntriax 2024-01-30 20:13:56 +03:00
parent 753c0031cd
commit de2d5fb446
3 changed files with 21 additions and 19 deletions

View File

@ -1,4 +1,5 @@
using Microsoft.Xna.Framework.Input;
using Syntriax.Engine.Core;
using Syntriax.Engine.Input;
@ -41,20 +42,20 @@ public class CameraController : BehaviourOverride
private void SwitchToFullScreen(IButtonInputs<Keys> inputs, Keys keys)
{
if (GamePong.graphics.IsFullScreen)
if (cameraBehaviour.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();
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(GamePong.graphics.GraphicsDevice.Viewport.Width) + Math.Sqr(GamePong.graphics.GraphicsDevice.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 = GamePong.graphics.GraphicsDevice.Viewport;
cameraBehaviour.Viewport = cameraBehaviour.Graphics.GraphicsDevice.Viewport;
}
private void ResetCamera(IButtonInputs<Keys> inputs, Keys keys)

View File

@ -3,7 +3,7 @@ using Microsoft.Xna.Framework.Graphics;
using Syntriax.Engine.Core;
namespace Pong.Behaviours;
public class MonoGameCameraBehaviour : BehaviourOverride
public class MonoGameCameraBehaviour(GraphicsDeviceManager Graphics) : BehaviourOverride
{
public System.Action<MonoGameCameraBehaviour>? OnMatrixTransformChanged { get; set; } = null;
public System.Action<MonoGameCameraBehaviour>? OnViewportChanged { get; set; } = null;
@ -12,7 +12,7 @@ public class MonoGameCameraBehaviour : BehaviourOverride
private Matrix _matrixTransform = Matrix.Identity;
private Viewport _viewport = default;
public GraphicsDeviceManager Graphics { get; } = Graphics;
private float _zoom = 1f;
public Matrix MatrixTransform
@ -69,7 +69,7 @@ public class MonoGameCameraBehaviour : BehaviourOverride
}
protected override void OnFirstActiveFrame()
=> Viewport = GamePong.graphics.GraphicsDevice.Viewport;
=> Viewport = Graphics.GraphicsDevice.Viewport;
protected override void OnPreDraw()
{

View File

@ -16,18 +16,20 @@ namespace Pong;
public class GamePong : Game
{
public static GraphicsDeviceManager graphics = null!;
public static IPhysicsEngine2D physicsEngine = null!;
public static SpriteBatch spriteBatch = null!;
public static ShapeBatch shapeBatch = null!;
public static GameManager gameManager = null!;
private readonly GraphicsDeviceManager graphics = null!;
private IPhysicsEngine2D physicsEngine = null!;
private SpriteBatch spriteBatch = null!;
private ShapeBatch shapeBatch = null!;
private GameManager gameManager = null!;
private BehaviourCacher<IDisplayable> displayableCacher = null!;
private BehaviourCacher<IDisplayableShape> displayableShapeCacher = null!;
private MonoGameCameraBehaviour cameraBehaviour = null!;
private PongManager pongManager = null!;
private float physicsTimer = 0f;
public GamePong()
{
graphics = new GraphicsDeviceManager(this)
@ -64,7 +66,7 @@ public class GamePong : Game
IGameObject gameObjectCamera = gameManager.InstantiateGameObject<GameObject>().SetGameObject("Camera"); ;
gameObjectCamera.BehaviourController.AddBehaviour<CameraController>();
cameraBehaviour = gameObjectCamera.BehaviourController.AddBehaviour<MonoGameCameraBehaviour>();
cameraBehaviour = gameObjectCamera.BehaviourController.AddBehaviour<MonoGameCameraBehaviour>(graphics);
////////////////////////////////////////////////////////////////////////////////////
@ -153,7 +155,6 @@ public class GamePong : Game
}
base.Update(gameTime);
}
static float physicsTimer = 0f;
protected override void Draw(GameTime gameTime)
{