using System; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Input; using Pong.Behaviours; using Apos.Shapes; using Syntriax.Engine.Core; using Syntriax.Engine.Core.Abstract; using Syntriax.Engine.Physics2D; using Syntriax.Engine.Physics2D.Primitives; using Syntriax.Engine.Physics2D.Abstract; namespace Pong; public class GamePong : Game { public static GraphicsDeviceManager graphics = null!; public static IPhysicsEngine2D engine = null!; public static SpriteBatch spriteBatch = null!; public static ShapeBatch shapeBatch = null!; public static GameManager gameManager = null!; private MonoGameCameraBehaviour cameraBehaviour = null!; private PongManager pongManager = null!; public GamePong() { engine = new PhysicsEngine2D { IterationCount = 3 }; graphics = new GraphicsDeviceManager(this) { PreferredBackBufferWidth = 1024, PreferredBackBufferHeight = 576, GraphicsProfile = GraphicsProfile.HiDef }; Content.RootDirectory = "Content"; IsMouseVisible = true; } protected override void Initialize() { gameManager = new(); // TODO: Add your initialization logic here gameManager.Initialize(); base.Initialize(); } protected override void LoadContent() { spriteBatch = new SpriteBatch(GraphicsDevice); shapeBatch = new ShapeBatch(GraphicsDevice, Content); SpriteFont spriteFont = Content.Load("UbuntuMono"); //////////////////////////////////////////////////////////////////////////////////// IGameObject gameObjectCamera = gameManager.InstantiateGameObject().SetGameObject("Camera"); ; gameObjectCamera.BehaviourController.AddBehaviour(); cameraBehaviour = gameObjectCamera.BehaviourController.AddBehaviour(); //////////////////////////////////////////////////////////////////////////////////// IGameObject gameObjectPongManager = gameManager.InstantiateGameObject().SetGameObject("Pong Game Manager"); pongManager = gameObjectPongManager.BehaviourController.AddBehaviour(5); //////////////////////////////////////////////////////////////////////////////////// IGameObject gameObjectBall = gameManager.InstantiateGameObject().SetGameObject("Ball"); 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); //////////////////////////////////////////////////////////////////////////////////// IGameObject gameObjectLeftPaddle = gameManager.InstantiateGameObject().SetGameObject("Left Paddle"); gameObjectLeftPaddle.Transform.SetTransform(position: new Vector2D(-468f, 0f), scale: new Vector2D(15f, 60f)); gameObjectLeftPaddle.BehaviourController.AddBehaviour(Keys.W, Keys.S, 228f, -228f, 400f); 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)); gameObjectRightPaddle.BehaviourController.AddBehaviour(Keys.Up, Keys.Down, 228f, -228f, 400f); gameObjectRightPaddle.BehaviourController.AddBehaviour(Shape.Box); RigidBody2D rigidBodyRightPaddle = gameObjectRightPaddle.BehaviourController.AddBehaviour(); rigidBodyRightPaddle.IsStatic = true; engine.AddRigidBody(rigidBodyRightPaddle); //////////////////////////////////////////////////////////////////////////////////// IGameObject gameObjectWallTop = gameManager.InstantiateGameObject().SetGameObject("Wall Top"); gameObjectWallTop.Transform.SetTransform(position: new Vector2D(0f, 308f), scale: new Vector2D(552f, 20f)); 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)); gameObjectWallRight.BehaviourController.AddBehaviour((Action)pongManager.ScoreToLeft); 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)); gameObjectWallLeft.BehaviourController.AddBehaviour((Action)pongManager.ScoreToRight); gameObjectWallLeft.BehaviourController.AddBehaviour(Shape.Box); RigidBody2D rigidBodyWallLeft = gameObjectWallLeft.BehaviourController.AddBehaviour(); rigidBodyWallLeft.IsStatic = true; engine.AddRigidBody(rigidBodyWallLeft); //////////////////////////////////////////////////////////////////////////////////// IGameObject gameObjectLeftScoreText = gameManager.InstantiateGameObject().SetGameObject("Score Left"); gameObjectLeftScoreText.Transform.SetTransform(position: new Vector2D(-250f, 250f), scale: Vector2D.One * .25f); PongTextBehaviour textBehaviourLeft = gameObjectLeftScoreText.BehaviourController.AddBehaviour(true); textBehaviourLeft.Font = spriteFont; textBehaviourLeft.Text = "0"; IGameObject gameObjectRightScoreText = gameManager.InstantiateGameObject().SetGameObject("Score Right"); gameObjectRightScoreText.Transform.SetTransform(position: new Vector2D(250f, 250f), scale: Vector2D.One * .25f); PongTextBehaviour textBehaviourRight = gameObjectRightScoreText.BehaviourController.AddBehaviour(false); textBehaviourRight.Font = spriteFont; textBehaviourRight.Text = "0"; } protected override void Update(GameTime gameTime) { if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape)) Exit(); gameManager.Update(gameTime.ToEngineTime()); while (physicsTimer + 0.01f < gameTime.TotalGameTime.TotalMilliseconds * .001f)//seconds) { physicsTimer += 0.01f; engine.Step(.01f); } base.Update(gameTime); } static float physicsTimer = 0f; protected override void Draw(GameTime gameTime) { GraphicsDevice.Clear(new Color() { R = 32, G = 32, B = 32 }); // TODO: Add your drawing code here gameManager.PreDraw(); spriteBatch.Begin(SpriteSortMode.Deferred, transformMatrix: cameraBehaviour.MatrixTransform); foreach (IGameObject gameObject in gameManager) foreach (var displayable in gameObject.BehaviourController.GetBehaviours()) displayable.Draw(spriteBatch); spriteBatch.End(); shapeBatch.Begin(cameraBehaviour.MatrixTransform); foreach (IGameObject gameObject in gameManager) foreach (var displayableShape in gameObject.BehaviourController.GetBehaviours()) displayableShape.Draw(shapeBatch); shapeBatch.End(); base.Draw(gameTime); } }