Engine-Pong/Game/Game1.cs

246 lines
11 KiB
C#

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;
namespace Pong;
public class Game1 : Game
{
private GraphicsDeviceManager _graphics = null!;
private PhysicsEngine2D engine;
private SpriteBatch _spriteBatch = null!;
private ShapeBatch _shapeBatch = null!;
public static GameManager gameManager = null!;
public static Sprite spriteBox = null!;
private MonoGameCameraBehaviour cameraBehaviour = null!;
public Game1()
{
engine = new PhysicsEngine2D();
_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);
// spriteBox = new Sprite() { Texture2D = Content.Load<Texture2D>("Sprites/Pixel") };
// Sprite spriteBall = new Sprite() { Texture2D = Content.Load<Texture2D>("Sprites/Circle") };
IGameObject gameObjectCamera = gameManager.InstantiateGameObject<GameObject>();
gameObjectCamera.Name = "Camera";
gameObjectCamera.Transform.Position = Vector2D.Zero;
cameraBehaviour = gameObjectCamera.BehaviourController.AddBehaviour<MonoGameCameraBehaviour>();
cameraBehaviour.Viewport = GraphicsDevice.Viewport;
gameManager.Camera = cameraBehaviour;
// IGameObject gameObjectCircle = gameManager.InstantiateGameObject<GameObject>();
// gameObjectCircle.Name = "Circle";
// gameObjectCircle.Transform.Position = new Vector2D(0f, -50f);
// gameObjectCircle.Transform.Scale = new Vector2D(25f, 25f);
// gameObjectCircle.BehaviourController.AddBehaviour<KeyboardInputsBehaviour>();
// gameObjectCircle.BehaviourController.AddBehaviour<MovementBoxBehaviour>(Keys.W, Keys.S, 268f, -268f, 400f);
// gameObjectCircle.BehaviourController.AddBehaviour<CircleBehaviour>(new Circle(Vector2D.Zero, 1f));
// engine.AddRigidBody(gameObjectCircle.BehaviourController.AddBehaviour<RigidBody2D>());
IGameObject gameObjectCircle2 = gameManager.InstantiateGameObject<GameObject>();
gameObjectCircle2.Name = "Circle2";
gameObjectCircle2.Transform.Position = new Vector2D(5f, 50f);
gameObjectCircle2.Transform.Scale = new Vector2D(25f, 25f);
gameObjectCircle2.BehaviourController.AddBehaviour<MovementMouseBehaviour>();
gameObjectCircle2.BehaviourController.AddBehaviour<CircleBehaviour>(new Circle(Vector2D.Zero, 1f));
engine.AddRigidBody(gameObjectCircle2.BehaviourController.AddBehaviour<RigidBody2D>());
IGameObject gameObjectDiamond = gameManager.InstantiateGameObject<GameObject>();
gameObjectDiamond.Name = "Diamond";
gameObjectDiamond.Transform.Position = new Vector2D(-150f, -150f);
gameObjectDiamond.Transform.Scale = new Vector2D(50f, 50f);
gameObjectDiamond.BehaviourController.AddBehaviour<KeyboardInputsBehaviour>();
gameObjectDiamond.BehaviourController.AddBehaviour<MovementBoxBehaviour>(Keys.W, Keys.S, 268f, -268f, 400f);
gameObjectDiamond.BehaviourController.AddBehaviour<RotatableBehaviour>();
gameObjectDiamond.BehaviourController.AddBehaviour<ShapeBehaviour>(new Shape([Vector2D.Up, Vector2D.One, Vector2D.Right, Vector2D.Down, -Vector2D.One, Vector2D.Left]));
gameObjectDiamond.BehaviourController.AddBehaviour<ShapeAABBBehaviour>();
engine.AddRigidBody(gameObjectDiamond.BehaviourController.AddBehaviour<RigidBody2D>());
IGameObject gameObjectBox = gameManager.InstantiateGameObject<GameObject>();
gameObjectBox.Name = "Box";
gameObjectBox.Transform.Position = new Vector2D(150f, -150f);
gameObjectBox.Transform.Scale = new Vector2D(100f, 100f);
gameObjectBox.BehaviourController.AddBehaviour<KeyboardInputsBehaviour>();
gameObjectBox.BehaviourController.AddBehaviour<MovementBoxBehaviour>(Keys.W, Keys.S, 268f, -268f, 400f);
gameObjectBox.BehaviourController.AddBehaviour<RotatableBehaviour>();
gameObjectBox.BehaviourController.AddBehaviour<ShapeBehaviour>(Shape.Pentagon);
gameObjectBox.BehaviourController.AddBehaviour<ShapeAABBBehaviour>();
engine.AddRigidBody(gameObjectBox.BehaviourController.AddBehaviour<RigidBody2D>());
for (int i = 3; i < 10; i++)
{
IGameObject Test = gameManager.InstantiateGameObject<GameObject>();
Test.Name = i.ToString();
Test.Transform.Position = new Vector2D((i - 6) * 150, 0f);
Test.Transform.Scale = new Vector2D(75f, 75f);
Test.BehaviourController.AddBehaviour<KeyboardInputsBehaviour>();
Test.BehaviourController.AddBehaviour<MovementBoxBehaviour>(Keys.W, Keys.S, 268f, -268f, 400f);
Test.BehaviourController.AddBehaviour<RotatableBehaviour>();
Test.BehaviourController.AddBehaviour<ShapeBehaviour>(Shape.CreateNgon(i));
Test.BehaviourController.AddBehaviour<ShapeAABBBehaviour>();
RigidBody2D rigidBody = Test.BehaviourController.AddBehaviour<RigidBody2D>();
rigidBody.AngularVelocity = 90f;
engine.AddRigidBody(rigidBody);
}
// IGameObject gameObjectShape = gameManager.InstantiateGameObject<GameObject>();
// gameObjectShape.Name = "Shape";
// gameObjectShape.Transform.Position = new Vector2D(250f, 0f);
// gameObjectShape.Transform.Scale = new Vector2D(100f, 100f);
// gameObjectShape.BehaviourController.AddBehaviour<KeyboardInputsBehaviour>();
// gameObjectShape.BehaviourController.AddBehaviour<MovementBoxBehaviour>(Keys.W, Keys.S, 268f, -268f, 400f);
// gameObjectShape.BehaviourController.AddBehaviour<RotatableBehaviour>();
// gameObjectShape.BehaviourController.AddBehaviour<ShapeBehaviour>(new Shape([Vector2D.Up, Vector2D.One, Vector2D.Right, Vector2D.Down, Vector2D.Zero, Vector2D.Left]));
// gameObjectShape.BehaviourController.AddBehaviour<ShapeAABBBehaviour>();
}
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(cameraBehaviour.Viewport.Width, 2f) + MathF.Pow(cameraBehaviour.Viewport.Height, 2f));
float currentScreenSize = MathF.Sqrt(MathF.Pow(GraphicsDevice.Viewport.Width, 2f) + MathF.Pow(GraphicsDevice.Viewport.Height, 2f));
cameraBehaviour.Zoom /= previousScreenSize / currentScreenSize;
cameraBehaviour.Viewport = GraphicsDevice.Viewport;
}
if (Keyboard.GetState().IsKeyDown(Keys.U))
cameraBehaviour.Zoom += gameTime.ElapsedGameTime.Nanoseconds * 0.00025f;
if (Keyboard.GetState().IsKeyDown(Keys.J))
cameraBehaviour.Zoom -= gameTime.ElapsedGameTime.Nanoseconds * 0.00025f;
if (Keyboard.GetState().IsKeyDown(Keys.Q))
cameraBehaviour.BehaviourController.GameObject.Transform.Rotation += gameTime.ElapsedGameTime.Nanoseconds * 0.000025f;
if (Keyboard.GetState().IsKeyDown(Keys.E))
cameraBehaviour.BehaviourController.GameObject.Transform.Rotation -= gameTime.ElapsedGameTime.Nanoseconds * 0.000025f;
if (Keyboard.GetState().IsKeyDown(Keys.N))
{
seconds = 70f;
while (physicsTimer + 0.01f < seconds)
{
physicsTimer += 0.01f;
engine.Step(.01f);
}
}
if (Keyboard.GetState().IsKeyDown(Keys.M))
{
seconds = 0f;
while (physicsTimer - 0.01f > seconds)
{
physicsTimer -= 0.01f;
engine.Step(-.01f);
}
}
if (Keyboard.GetState().IsKeyDown(Keys.Space))
{
seconds += gameTime.ElapsedGameTime.Milliseconds * .005f;
while (physicsTimer + 0.01f < seconds)
{
Console.WriteLine($"Physics Timer: {physicsTimer}");
physicsTimer += 0.01f;
engine.Step(.01f);
}
}
if (Keyboard.GetState().IsKeyDown(Keys.B))
{
seconds -= gameTime.ElapsedGameTime.Milliseconds * .005f;
while (physicsTimer - 0.01f > seconds)
{
Console.WriteLine($"Physics Timer: {physicsTimer}");
physicsTimer -= 0.01f;
engine.Step(-.01f);
}
}
while (physicsTimer + 0.01f < gameTime.TotalGameTime.TotalMilliseconds * .001f)//seconds)
{
// Console.WriteLine($"Physics Timer: {physicsTimer}");
physicsTimer += 0.01f;
engine.Step(.01f);
}
gameManager.Update(gameTime.ToEngineTime());
base.Update(gameTime);
}
static float physicsTimer = 0f;
static float seconds = 0f;
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(new Color() { R = 32, G = 32, B = 32 });
// gameManager.Camera.Position = gameObjectBall.Transform.Position;
// Console.WriteLine($"Pos: {gameManager.Camera.Position}");
// TODO: Add your drawing code here
gameManager.PreDraw();
gameManager.Camera.Update();
_spriteBatch.Begin(SpriteSortMode.Deferred, transformMatrix: cameraBehaviour.MatrixTransform);
foreach (IGameObject gameObject in gameManager)
foreach (var displayable in gameObject.BehaviourController.GetBehaviours<IDisplayable>())
displayable.Draw(_spriteBatch);
_spriteBatch.End();
_shapeBatch.Begin(cameraBehaviour.MatrixTransform);
foreach (IGameObject gameObject in gameManager)
foreach (var displayableShape in gameObject.BehaviourController.GetBehaviours<IDisplayableShape>())
displayableShape.Draw(_shapeBatch);
_shapeBatch.End();
base.Draw(gameTime);
}
}