feat: Upgraded Engine to BehaviourCachers

This commit is contained in:
Syntriax 2024-01-30 19:46:41 +03:00
parent a734f52b38
commit 753c0031cd
2 changed files with 17 additions and 25 deletions

2
Engine

@ -1 +1 @@
Subproject commit 3817ebebfe3c3d328219656033f2518da6a59350 Subproject commit de336d0ee5323da3f8b5db3adc69b6afd2f1beff

View File

@ -17,21 +17,19 @@ namespace Pong;
public class GamePong : Game public class GamePong : Game
{ {
public static GraphicsDeviceManager graphics = null!; public static GraphicsDeviceManager graphics = null!;
public static IPhysicsEngine2D engine = null!; public static IPhysicsEngine2D physicsEngine = null!;
public static SpriteBatch spriteBatch = null!; public static SpriteBatch spriteBatch = null!;
public static ShapeBatch shapeBatch = null!; public static ShapeBatch shapeBatch = null!;
public static GameManager gameManager = null!; public static GameManager gameManager = null!;
private BehaviourCacher<IDisplayable> displayableCacher = null!;
private BehaviourCacher<IDisplayableShape> displayableShapeCacher = null!;
private MonoGameCameraBehaviour cameraBehaviour = null!; private MonoGameCameraBehaviour cameraBehaviour = null!;
private PongManager pongManager = null!; private PongManager pongManager = null!;
public GamePong() public GamePong()
{ {
engine = new PhysicsEngine2D
{
IterationCount = 3
};
graphics = new GraphicsDeviceManager(this) graphics = new GraphicsDeviceManager(this)
{ {
PreferredBackBufferWidth = 1024, PreferredBackBufferWidth = 1024,
@ -45,9 +43,12 @@ public class GamePong : Game
protected override void Initialize() protected override void Initialize()
{ {
gameManager = new();
// TODO: Add your initialization logic here // TODO: Add your initialization logic here
gameManager = new();
displayableCacher = new(gameManager);
displayableShapeCacher = new(gameManager);
physicsEngine = new PhysicsEngine2DCacher(gameManager) { IterationCount = 3 };
gameManager.Initialize(); gameManager.Initialize();
base.Initialize(); base.Initialize();
@ -76,9 +77,8 @@ public class GamePong : Game
gameObjectBall.Transform.SetTransform(position: new Vector2D(0, 0f), scale: new Vector2D(10f, 10f)); gameObjectBall.Transform.SetTransform(position: new Vector2D(0, 0f), scale: new Vector2D(10f, 10f));
gameObjectBall.BehaviourController.AddBehaviour<CircleBehaviour>(new Circle(Vector2D.Zero, 1f)); gameObjectBall.BehaviourController.AddBehaviour<CircleBehaviour>(new Circle(Vector2D.Zero, 1f));
PongBall ballBehaviour = gameObjectBall.BehaviourController.AddBehaviour<PongBall>(); gameObjectBall.BehaviourController.AddBehaviour<PongBall>();
RigidBody2D rigidBodyBall = gameObjectBall.BehaviourController.AddBehaviour<RigidBody2D>(); gameObjectBall.BehaviourController.AddBehaviour<RigidBody2D>();
engine.AddRigidBody(rigidBodyBall);
//////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////
@ -89,7 +89,6 @@ public class GamePong : Game
gameObjectLeftPaddle.BehaviourController.AddBehaviour<ShapeBehaviour>(Shape.Box); gameObjectLeftPaddle.BehaviourController.AddBehaviour<ShapeBehaviour>(Shape.Box);
RigidBody2D rigidBodyLeftPaddle = gameObjectLeftPaddle.BehaviourController.AddBehaviour<RigidBody2D>(); RigidBody2D rigidBodyLeftPaddle = gameObjectLeftPaddle.BehaviourController.AddBehaviour<RigidBody2D>();
rigidBodyLeftPaddle.IsStatic = true; rigidBodyLeftPaddle.IsStatic = true;
engine.AddRigidBody(rigidBodyLeftPaddle);
IGameObject gameObjectRightPaddle = gameManager.InstantiateGameObject<GameObject>().SetGameObject("Right Paddle"); IGameObject gameObjectRightPaddle = gameManager.InstantiateGameObject<GameObject>().SetGameObject("Right Paddle");
gameObjectRightPaddle.Transform.SetTransform(position: new Vector2D(468f, 0f), scale: new Vector2D(15f, 60f)); gameObjectRightPaddle.Transform.SetTransform(position: new Vector2D(468f, 0f), scale: new Vector2D(15f, 60f));
@ -97,7 +96,6 @@ public class GamePong : Game
gameObjectRightPaddle.BehaviourController.AddBehaviour<ShapeBehaviour>(Shape.Box); gameObjectRightPaddle.BehaviourController.AddBehaviour<ShapeBehaviour>(Shape.Box);
RigidBody2D rigidBodyRightPaddle = gameObjectRightPaddle.BehaviourController.AddBehaviour<RigidBody2D>(); RigidBody2D rigidBodyRightPaddle = gameObjectRightPaddle.BehaviourController.AddBehaviour<RigidBody2D>();
rigidBodyRightPaddle.IsStatic = true; rigidBodyRightPaddle.IsStatic = true;
engine.AddRigidBody(rigidBodyRightPaddle);
//////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////
@ -106,14 +104,12 @@ public class GamePong : Game
gameObjectWallTop.BehaviourController.AddBehaviour<ShapeBehaviour>(Shape.Box); gameObjectWallTop.BehaviourController.AddBehaviour<ShapeBehaviour>(Shape.Box);
RigidBody2D rigidBodyWallTop = gameObjectWallTop.BehaviourController.AddBehaviour<RigidBody2D>(); RigidBody2D rigidBodyWallTop = gameObjectWallTop.BehaviourController.AddBehaviour<RigidBody2D>();
rigidBodyWallTop.IsStatic = true; rigidBodyWallTop.IsStatic = true;
engine.AddRigidBody(rigidBodyWallTop);
IGameObject gameObjectWallBottom = gameManager.InstantiateGameObject<GameObject>().SetGameObject("Wall Bottom"); IGameObject gameObjectWallBottom = gameManager.InstantiateGameObject<GameObject>().SetGameObject("Wall Bottom");
gameObjectWallBottom.Transform.SetTransform(position: new Vector2D(0f, -308f), scale: new Vector2D(552f, 20f)); gameObjectWallBottom.Transform.SetTransform(position: new Vector2D(0f, -308f), scale: new Vector2D(552f, 20f));
gameObjectWallBottom.BehaviourController.AddBehaviour<ShapeBehaviour>(Shape.Box); gameObjectWallBottom.BehaviourController.AddBehaviour<ShapeBehaviour>(Shape.Box);
RigidBody2D rigidBodyWallBottom = gameObjectWallBottom.BehaviourController.AddBehaviour<RigidBody2D>(); RigidBody2D rigidBodyWallBottom = gameObjectWallBottom.BehaviourController.AddBehaviour<RigidBody2D>();
rigidBodyWallBottom.IsStatic = true; rigidBodyWallBottom.IsStatic = true;
engine.AddRigidBody(rigidBodyWallBottom);
IGameObject gameObjectWallRight = gameManager.InstantiateGameObject<GameObject>().SetGameObject("Wall Right"); IGameObject gameObjectWallRight = gameManager.InstantiateGameObject<GameObject>().SetGameObject("Wall Right");
gameObjectWallRight.Transform.SetTransform(position: new Vector2D(532f, 0f), scale: new Vector2D(20f, 328f)); gameObjectWallRight.Transform.SetTransform(position: new Vector2D(532f, 0f), scale: new Vector2D(20f, 328f));
@ -121,7 +117,6 @@ public class GamePong : Game
gameObjectWallRight.BehaviourController.AddBehaviour<ShapeBehaviour>(Shape.Box); gameObjectWallRight.BehaviourController.AddBehaviour<ShapeBehaviour>(Shape.Box);
RigidBody2D rigidBodyWallRight = gameObjectWallRight.BehaviourController.AddBehaviour<RigidBody2D>(); RigidBody2D rigidBodyWallRight = gameObjectWallRight.BehaviourController.AddBehaviour<RigidBody2D>();
rigidBodyWallRight.IsStatic = true; rigidBodyWallRight.IsStatic = true;
engine.AddRigidBody(rigidBodyWallRight);
IGameObject gameObjectWallLeft = gameManager.InstantiateGameObject<GameObject>().SetGameObject("Wall Left"); IGameObject gameObjectWallLeft = gameManager.InstantiateGameObject<GameObject>().SetGameObject("Wall Left");
gameObjectWallLeft.Transform.SetTransform(position: new Vector2D(-532f, 0f), scale: new Vector2D(20f, 328f)); gameObjectWallLeft.Transform.SetTransform(position: new Vector2D(-532f, 0f), scale: new Vector2D(20f, 328f));
@ -129,7 +124,6 @@ public class GamePong : Game
gameObjectWallLeft.BehaviourController.AddBehaviour<ShapeBehaviour>(Shape.Box); gameObjectWallLeft.BehaviourController.AddBehaviour<ShapeBehaviour>(Shape.Box);
RigidBody2D rigidBodyWallLeft = gameObjectWallLeft.BehaviourController.AddBehaviour<RigidBody2D>(); RigidBody2D rigidBodyWallLeft = gameObjectWallLeft.BehaviourController.AddBehaviour<RigidBody2D>();
rigidBodyWallLeft.IsStatic = true; rigidBodyWallLeft.IsStatic = true;
engine.AddRigidBody(rigidBodyWallLeft);
//////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////
@ -155,7 +149,7 @@ public class GamePong : Game
while (physicsTimer + 0.01f < gameTime.TotalGameTime.TotalMilliseconds * .001f)//seconds) while (physicsTimer + 0.01f < gameTime.TotalGameTime.TotalMilliseconds * .001f)//seconds)
{ {
physicsTimer += 0.01f; physicsTimer += 0.01f;
engine.Step(.01f); physicsEngine.Step(.01f);
} }
base.Update(gameTime); base.Update(gameTime);
} }
@ -169,14 +163,12 @@ public class GamePong : Game
gameManager.PreDraw(); gameManager.PreDraw();
spriteBatch.Begin(SpriteSortMode.Deferred, transformMatrix: cameraBehaviour.MatrixTransform); spriteBatch.Begin(SpriteSortMode.Deferred, transformMatrix: cameraBehaviour.MatrixTransform);
foreach (IGameObject gameObject in gameManager) foreach (var displayable in displayableCacher)
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 (var displayableShape in displayableShapeCacher)
foreach (var displayableShape in gameObject.BehaviourController.GetBehaviours<IDisplayableShape>())
displayableShape.Draw(shapeBatch); displayableShape.Draw(shapeBatch);
shapeBatch.End(); shapeBatch.End();