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