250 lines
11 KiB
C#
250 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;
|
|
using Syntriax.Engine.Physics2D.Abstract;
|
|
|
|
namespace Pong;
|
|
|
|
public class Game1 : Game
|
|
{
|
|
private GraphicsDeviceManager _graphics = null!;
|
|
private IPhysicsEngine2D engine = null!;
|
|
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
|
|
{
|
|
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);
|
|
|
|
// 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 gameObjectBall = gameManager.InstantiateGameObject<GameObject>();
|
|
gameObjectBall.Name = "Ball";
|
|
gameObjectBall.Transform.Position = new Vector2D(0, 0f);
|
|
gameObjectBall.Transform.Scale = new Vector2D(10f, 10f);
|
|
gameObjectBall.BehaviourController.AddBehaviour<MovementBallBehaviour>(Vector2D.One, 500f);
|
|
gameObjectBall.BehaviourController.AddBehaviour<CircleBehaviour>(new Circle(Vector2D.Zero, 1f));
|
|
engine.AddRigidBody(gameObjectBall.BehaviourController.AddBehaviour<RigidBody2D>());
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
IGameObject gameObjectLeftPaddle = gameManager.InstantiateGameObject<GameObject>();
|
|
gameObjectLeftPaddle.Name = "Left Paddle";
|
|
gameObjectLeftPaddle.Transform.Position = new Vector2D(-468f, 0f);
|
|
gameObjectLeftPaddle.Transform.Scale = new Vector2D(15f, 60f);
|
|
gameObjectLeftPaddle.BehaviourController.AddBehaviour<MovementBoxBehaviour>(Keys.W, Keys.S, 228f, -228f, 400f);
|
|
gameObjectLeftPaddle.BehaviourController.AddBehaviour<ShapeBehaviour>(Shape.Box);
|
|
RigidBody2D rigidBodyLeftPaddle = gameObjectLeftPaddle.BehaviourController.AddBehaviour<RigidBody2D>();
|
|
rigidBodyLeftPaddle.IsStatic = true;
|
|
engine.AddRigidBody(rigidBodyLeftPaddle);
|
|
|
|
IGameObject gameObjectRightPaddle = gameManager.InstantiateGameObject<GameObject>();
|
|
gameObjectRightPaddle.Name = "Right Paddle";
|
|
gameObjectRightPaddle.Transform.Position = new Vector2D(468f, 0f);
|
|
gameObjectRightPaddle.Transform.Scale = new Vector2D(15f, 60f);
|
|
gameObjectRightPaddle.BehaviourController.AddBehaviour<MovementBoxBehaviour>(Keys.Up, Keys.Down, 228f, -228f, 400f);
|
|
gameObjectRightPaddle.BehaviourController.AddBehaviour<ShapeBehaviour>(Shape.Box);
|
|
RigidBody2D rigidBodyRightPaddle = gameObjectRightPaddle.BehaviourController.AddBehaviour<RigidBody2D>();
|
|
rigidBodyRightPaddle.IsStatic = true;
|
|
engine.AddRigidBody(rigidBodyRightPaddle);
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
IGameObject gameObjectWallTop = gameManager.InstantiateGameObject<GameObject>();
|
|
gameObjectWallTop.Name = "WallTop";
|
|
gameObjectWallTop.Transform.Position = new Vector2D(0f, 298f);
|
|
gameObjectWallTop.Transform.Scale = new Vector2D(542f, 20f);
|
|
gameObjectWallTop.BehaviourController.AddBehaviour<ShapeBehaviour>(Shape.Box);
|
|
RigidBody2D rigidBodyWallTop = gameObjectWallTop.BehaviourController.AddBehaviour<RigidBody2D>();
|
|
rigidBodyWallTop.IsStatic = true;
|
|
engine.AddRigidBody(rigidBodyWallTop);
|
|
|
|
IGameObject gameObjectWallBottom = gameManager.InstantiateGameObject<GameObject>();
|
|
gameObjectWallBottom.Name = "WallBottom";
|
|
gameObjectWallBottom.Transform.Position = new Vector2D(0f, -298f);
|
|
gameObjectWallBottom.Transform.Scale = new Vector2D(542f, 20f);
|
|
gameObjectWallBottom.BehaviourController.AddBehaviour<ShapeBehaviour>(Shape.Box);
|
|
RigidBody2D rigidBodyWallBottom = gameObjectWallBottom.BehaviourController.AddBehaviour<RigidBody2D>();
|
|
rigidBodyWallBottom.IsStatic = true;
|
|
engine.AddRigidBody(rigidBodyWallBottom);
|
|
|
|
IGameObject gameObjectWallRight = gameManager.InstantiateGameObject<GameObject>();
|
|
gameObjectWallRight.Name = "WallRight";
|
|
gameObjectWallRight.Transform.Position = new Vector2D(522f, 0f);
|
|
gameObjectWallRight.Transform.Scale = new Vector2D(20f, 318f);
|
|
gameObjectWallRight.BehaviourController.AddBehaviour<ShapeBehaviour>(Shape.Box);
|
|
RigidBody2D rigidBodyWallRight = gameObjectWallRight.BehaviourController.AddBehaviour<RigidBody2D>();
|
|
rigidBodyWallRight.IsStatic = true;
|
|
engine.AddRigidBody(rigidBodyWallRight);
|
|
|
|
IGameObject gameObjectWallLeft = gameManager.InstantiateGameObject<GameObject>();
|
|
gameObjectWallLeft.Name = "WallLeft";
|
|
gameObjectWallLeft.Transform.Position = new Vector2D(-522f, 0f);
|
|
gameObjectWallLeft.Transform.Scale = new Vector2D(20f, 318f);
|
|
gameObjectWallLeft.BehaviourController.AddBehaviour<ShapeBehaviour>(Shape.Box);
|
|
RigidBody2D rigidBodyWallLeft = gameObjectWallLeft.BehaviourController.AddBehaviour<RigidBody2D>();
|
|
rigidBodyWallLeft.IsStatic = true;
|
|
engine.AddRigidBody(rigidBodyWallLeft);
|
|
}
|
|
|
|
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);
|
|
}
|
|
}
|
|
|
|
gameManager.Update(gameTime.ToEngineTime());
|
|
while (physicsTimer + 0.01f < gameTime.TotalGameTime.TotalMilliseconds * .001f)//seconds)
|
|
{
|
|
// Console.WriteLine($"Physics Timer: {physicsTimer}");
|
|
physicsTimer += 0.01f;
|
|
engine.Step(.01f);
|
|
}
|
|
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);
|
|
}
|
|
}
|