using System; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Input; using Pong.Behaviours; using Syntriax.Engine.Core; using Syntriax.Engine.Core.Abstract; using Syntriax.Engine.Graphics.TwoDimensional; using Syntriax.Engine.Input; using Syntriax.Engine.Physics2D; namespace Pong; public class Game1 : Game { private GraphicsDeviceManager _graphics = null!; private PhysicsEngine2D engine; private SpriteBatch _spriteBatch = null!; private GameManager gameManager = null!; public Game1() { _graphics = new GraphicsDeviceManager(this) { PreferredBackBufferWidth = 1024, PreferredBackBufferHeight = 576 }; 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() { engine = new PhysicsEngine2D(); _spriteBatch = new SpriteBatch(GraphicsDevice); // Sprite spriteBox = new Sprite() { Texture2D = Content.Load("Sprites/Pixel") }; Sprite spriteBall = new Sprite() { Texture2D = Content.Load("Sprites/Circle") }; IGameObject gameObjectCamera = gameManager.InstantiateGameObject(); gameObjectCamera.Name = "Camera"; gameObjectCamera.Transform.Position = Vector2.Zero; gameManager.Camera = gameObjectCamera.BehaviourController.AddBehaviour(); gameManager.Camera.Viewport = GraphicsDevice.Viewport; // IGameObject gameObjectPlayArea = gameManager.InstantiateGameObject(); // PlayAreaBehaviour playAreaBehaviour = gameObjectPlayArea.BehaviourController.AddBehaviour(); // playAreaBehaviour.PlayArea = new Vector2(_graphics.PreferredBackBufferWidth, _graphics.PreferredBackBufferHeight); IGameObject gameObjectBall = gameManager.InstantiateGameObject(); gameObjectBall.Name = "Ball"; gameObjectBall.Transform.Position = Vector2.Zero; gameObjectBall.Transform.Scale = new Vector2(1f / 51.2f, 1f / 51.2f); engine.AddRigidBody(gameObjectBall.BehaviourController.AddBehaviour()); gameObjectBall.BehaviourController.AddBehaviour((System.Collections.Generic.IList)[new Vector2(1, 1) * 5f, new Vector2(-1, 1) * 5f, new Vector2(1, -1) * 5f, new Vector2(-1, -1) * 5f]).OffsetScale = Vector2.One * 51.2f; // gameObjectBall.BehaviourController.AddBehaviour(new Vector2(.1f, .1f), playAreaBehaviour, 100f); gameObjectBall.BehaviourController.AddBehaviour().Assign(spriteBall); IGameObject gameObjectBall2 = gameManager.InstantiateGameObject(); gameObjectBall2.Name = "Ball2"; gameObjectBall2.Transform.Position = Vector2.UnitY * -50f; gameObjectBall2.Transform.Scale = new Vector2(1f / 51.2f, 1f / 51.2f); RigidBody2D rigidBody = gameObjectBall2.BehaviourController.AddBehaviour(); rigidBody.Velocity = Vector2.UnitY * 100f; engine.AddRigidBody(rigidBody); gameObjectBall2.BehaviourController.AddBehaviour((System.Collections.Generic.IList)[new Vector2(1, 1) * 5f, new Vector2(-1, 1) * 5f, new Vector2(1, -1) * 5f, new Vector2(-1, -1) * 5f]).OffsetScale = Vector2.One * 51.2f; // gameObjectBall2.BehaviourController.AddBehaviour(new Vector2(.1f, .1f), playAreaBehaviour, 100f); gameObjectBall2.BehaviourController.AddBehaviour().Assign(spriteBall); IGameObject gameObjectBall3 = gameManager.InstantiateGameObject(); gameObjectBall3.Name = "Ball"; gameObjectBall3.Transform.Position = Vector2.UnitY * -120f + Vector2.UnitX * 10f; gameObjectBall3.Transform.Scale = new Vector2(1f / 51.2f, 1f / 51.2f); engine.AddRigidBody(gameObjectBall3.BehaviourController.AddBehaviour()); gameObjectBall3.BehaviourController.AddBehaviour((System.Collections.Generic.IList)[new Vector2(1, 1) * 5f, new Vector2(-1, 1) * 5f, new Vector2(1, -1) * 5f, new Vector2(-1, -1) * 5f]).OffsetScale = Vector2.One * 51.2f; // gameObjectBall3.BehaviourController.AddBehaviour(new Vector2(.1f, .1f), playAreaBehaviour, 100f); gameObjectBall3.BehaviourController.AddBehaviour().Assign(spriteBall); // IGameObject gameObjectLeft = gameManager.InstantiateGameObject(); // gameObjectLeft.Name = "Left"; // gameObjectLeft.Transform.Position = new Vector2(-452, 0f); // gameObjectLeft.Transform.Scale = new Vector2(10f, 40f); // gameObjectLeft.BehaviourController.AddBehaviour(); // gameObjectLeft.BehaviourController.AddBehaviour(Keys.W, Keys.S, 268f, -268f, 400f); // gameObjectLeft.BehaviourController.AddBehaviour().Assign(spriteBox); // IGameObject gameObjectRight = gameManager.InstantiateGameObject(); // gameObjectRight.Name = "Right"; // gameObjectRight.Transform.Position = new Vector2(452, 0f); // gameObjectRight.Transform.Scale = new Vector2(10f, 40f); // gameObjectRight.BehaviourController.AddBehaviour(); // gameObjectRight.BehaviourController.AddBehaviour(Keys.Up, Keys.Down, 268f, -268f, 400f); // gameObjectRight.BehaviourController.AddBehaviour().Assign(spriteBox); // TODO: use this.Content to load your game content here } protected override void Update(GameTime gameTime) { if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape)) 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(gameManager.Camera.Viewport.Width, 2f) + MathF.Pow(gameManager.Camera.Viewport.Height, 2f)); float currentScreenSize = MathF.Sqrt(MathF.Pow(GraphicsDevice.Viewport.Width, 2f) + MathF.Pow(GraphicsDevice.Viewport.Height, 2f)); gameManager.Camera.Zoom /= previousScreenSize / currentScreenSize; gameManager.Camera.Viewport = GraphicsDevice.Viewport; } if (Keyboard.GetState().IsKeyDown(Keys.U)) gameManager.Camera.Zoom += gameTime.ElapsedGameTime.Nanoseconds * 0.00025f; if (Keyboard.GetState().IsKeyDown(Keys.J)) gameManager.Camera.Zoom -= gameTime.ElapsedGameTime.Nanoseconds * 0.00025f; if (Keyboard.GetState().IsKeyDown(Keys.Q)) gameManager.Camera.Rotation += gameTime.ElapsedGameTime.Nanoseconds * 0.000025f; if (Keyboard.GetState().IsKeyDown(Keys.E)) gameManager.Camera.Rotation -= gameTime.ElapsedGameTime.Nanoseconds * 0.000025f; // TODO: Add your update logic here while (physicsTimer + 0.01f < gameTime.TotalGameTime.TotalMilliseconds * .001f) { physicsTimer += 0.01f; engine.Step(.01f); } gameManager.Update(gameTime); 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(gameTime); gameManager.Camera.Update(); gameManager.Draw(_spriteBatch); base.Draw(gameTime); } }