Engine-Pong/Game/Game1.cs

201 lines
7.9 KiB
C#
Raw Normal View History

2023-11-27 17:06:33 +03:00
using System;
using Microsoft.Xna.Framework;
2023-11-23 23:59:03 +03:00
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
2023-12-05 17:10:03 +03:00
using Pong.Behaviours;
2024-01-24 12:17:21 +03:00
using Apos.Shapes;
2023-12-05 17:10:03 +03:00
2023-11-23 23:59:03 +03:00
using Syntriax.Engine.Core;
using Syntriax.Engine.Core.Abstract;
2023-12-01 17:42:07 +03:00
using Syntriax.Engine.Physics2D;
2023-12-18 22:49:26 +03:00
using Syntriax.Engine.Physics2D.Primitives;
2023-11-23 23:59:03 +03:00
namespace Pong;
public class Game1 : Game
{
2023-11-27 17:41:21 +03:00
private GraphicsDeviceManager _graphics = null!;
2023-12-01 17:42:07 +03:00
private PhysicsEngine2D engine;
2023-11-27 17:41:21 +03:00
private SpriteBatch _spriteBatch = null!;
2024-01-24 12:17:21 +03:00
private ShapeBatch _shapeBatch = null!;
2023-12-04 17:48:22 +03:00
public static GameManager gameManager = null!;
2024-01-23 12:15:24 +03:00
public static Sprite spriteBox = null!;
private MonoGameCameraBehaviour cameraBehaviour = null!;
2024-01-23 12:16:58 +03:00
2023-11-23 23:59:03 +03:00
public Game1()
{
2024-01-22 11:21:27 +03:00
engine = new PhysicsEngine2D();
2023-11-27 17:06:33 +03:00
_graphics = new GraphicsDeviceManager(this)
{
PreferredBackBufferWidth = 1024,
2024-01-24 12:17:21 +03:00
PreferredBackBufferHeight = 576,
GraphicsProfile = GraphicsProfile.HiDef
2023-11-27 17:06:33 +03:00
};
2023-11-27 17:41:21 +03:00
2023-11-23 23:59:03 +03:00
Content.RootDirectory = "Content";
IsMouseVisible = true;
}
protected override void Initialize()
{
gameManager = new();
2023-11-23 23:59:03 +03:00
// TODO: Add your initialization logic here
gameManager.Initialize();
2023-11-23 23:59:03 +03:00
base.Initialize();
}
protected override void LoadContent()
{
2023-11-30 10:48:57 +03:00
_spriteBatch = new SpriteBatch(GraphicsDevice);
2024-01-24 12:17:21 +03:00
_shapeBatch = new ShapeBatch(GraphicsDevice, Content);
2023-11-30 10:48:57 +03:00
2024-01-24 12:17:21 +03:00
// spriteBox = new Sprite() { Texture2D = Content.Load<Texture2D>("Sprites/Pixel") };
// Sprite spriteBall = new Sprite() { Texture2D = Content.Load<Texture2D>("Sprites/Circle") };
2023-11-27 11:49:05 +03:00
2023-11-27 13:46:21 +03:00
IGameObject gameObjectCamera = gameManager.InstantiateGameObject<GameObject>();
gameObjectCamera.Name = "Camera";
2024-01-22 23:47:47 +03:00
gameObjectCamera.Transform.Position = Vector2D.Zero;
2023-11-27 13:46:21 +03:00
2024-01-23 12:16:58 +03:00
cameraBehaviour = gameObjectCamera.BehaviourController.AddBehaviour<MonoGameCameraBehaviour>();
cameraBehaviour.Viewport = GraphicsDevice.Viewport;
gameManager.Camera = cameraBehaviour;
2023-11-27 13:46:21 +03:00
2024-01-24 12:17:21 +03:00
IGameObject gameObjectDiamond = gameManager.InstantiateGameObject<GameObject>();
2024-01-24 12:17:21 +03:00
gameObjectDiamond.Name = "Diamond";
gameObjectDiamond.Transform.Position = new Vector2D(0f, 0f);
gameObjectDiamond.Transform.Scale = new Vector2D(100f, 100f);
gameObjectDiamond.BehaviourController.AddBehaviour<KeyboardInputsBehaviour>();
gameObjectDiamond.BehaviourController.AddBehaviour<MovementBoxBehaviour>(Keys.W, Keys.S, 268f, -268f, 400f);
gameObjectDiamond.BehaviourController.AddBehaviour<RotatableBehaviour>();
2024-01-24 18:40:31 +03:00
gameObjectDiamond.BehaviourController.AddBehaviour<ShapeBehaviour>(new Shape([Vector2D.Up, Vector2D.One, Vector2D.Right, Vector2D.Down, Vector2D.Zero, Vector2D.Left]));
2024-01-24 18:40:12 +03:00
gameObjectDiamond.BehaviourController.AddBehaviour<ShapeAABBBehaviour>();
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>();
2023-11-23 23:59:03 +03:00
}
2024-01-23 12:16:58 +03:00
protected override void Update(GameTime gameTime)
2023-11-23 23:59:03 +03:00
{
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;
2023-11-27 13:46:21 +03:00
_graphics.PreferredBackBufferWidth = GraphicsDevice.Adapter.CurrentDisplayMode.Width;
_graphics.PreferredBackBufferHeight = GraphicsDevice.Adapter.CurrentDisplayMode.Height;
_graphics.IsFullScreen = true;
_graphics.ApplyChanges();
2024-01-23 12:16:58 +03:00
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));
2024-01-23 12:16:58 +03:00
cameraBehaviour.Zoom /= previousScreenSize / currentScreenSize;
cameraBehaviour.Viewport = GraphicsDevice.Viewport;
}
2023-11-27 13:46:21 +03:00
if (Keyboard.GetState().IsKeyDown(Keys.U))
2024-01-23 12:16:58 +03:00
cameraBehaviour.Zoom += gameTime.ElapsedGameTime.Nanoseconds * 0.00025f;
if (Keyboard.GetState().IsKeyDown(Keys.J))
2024-01-23 12:16:58 +03:00
cameraBehaviour.Zoom -= gameTime.ElapsedGameTime.Nanoseconds * 0.00025f;
2023-11-27 13:46:21 +03:00
if (Keyboard.GetState().IsKeyDown(Keys.Q))
2024-01-23 12:16:58 +03:00
cameraBehaviour.BehaviourController.GameObject.Transform.Rotation += gameTime.ElapsedGameTime.Nanoseconds * 0.000025f;
2023-11-27 13:46:21 +03:00
if (Keyboard.GetState().IsKeyDown(Keys.E))
2024-01-23 12:16:58 +03:00
cameraBehaviour.BehaviourController.GameObject.Transform.Rotation -= gameTime.ElapsedGameTime.Nanoseconds * 0.000025f;
2023-11-23 23:59:03 +03:00
2023-12-06 17:36:56 +03:00
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))
{
2023-12-06 17:45:00 +03:00
seconds += gameTime.ElapsedGameTime.Milliseconds * .005f;
2023-12-06 17:36:56 +03:00
while (physicsTimer + 0.01f < seconds)
{
Console.WriteLine($"Physics Timer: {physicsTimer}");
physicsTimer += 0.01f;
engine.Step(.01f);
}
}
if (Keyboard.GetState().IsKeyDown(Keys.B))
2023-12-01 17:42:07 +03:00
{
2023-12-06 17:45:00 +03:00
seconds -= gameTime.ElapsedGameTime.Milliseconds * .005f;
2023-12-06 17:36:56 +03:00
while (physicsTimer - 0.01f > seconds)
{
Console.WriteLine($"Physics Timer: {physicsTimer}");
physicsTimer -= 0.01f;
engine.Step(-.01f);
}
2023-12-01 17:42:07 +03:00
}
2023-12-06 17:36:56 +03:00
// while (physicsTimer + 0.01f < gameTime.TotalGameTime.TotalMilliseconds * .001f)//seconds)
// {
// Console.WriteLine($"Physics Timer: {physicsTimer}");
// physicsTimer += 0.01f;
// engine.Step(.01f);
// }
2024-01-23 12:16:58 +03:00
gameManager.Update(gameTime.ToEngineTime());
2023-11-23 23:59:03 +03:00
base.Update(gameTime);
}
2023-12-01 17:42:07 +03:00
static float physicsTimer = 0f;
2023-12-04 17:48:22 +03:00
static float seconds = 0f;
2023-11-23 23:59:03 +03:00
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(new Color() { R = 32, G = 32, B = 32 });
2023-11-23 23:59:03 +03:00
2023-12-05 15:27:31 +03:00
// gameManager.Camera.Position = gameObjectBall.Transform.Position;
// Console.WriteLine($"Pos: {gameManager.Camera.Position}");
2023-11-23 23:59:03 +03:00
// TODO: Add your drawing code here
2024-01-23 12:16:58 +03:00
gameManager.PreDraw();
2023-11-27 13:46:21 +03:00
gameManager.Camera.Update();
2024-01-23 12:15:24 +03:00
_spriteBatch.Begin(SpriteSortMode.Deferred, transformMatrix: cameraBehaviour.MatrixTransform);
foreach (IGameObject gameObject in gameManager)
2024-01-24 18:40:12 +03:00
foreach (var displayable in gameObject.BehaviourController.GetBehaviours<IDisplayable>())
2024-01-24 12:17:21 +03:00
displayable.Draw(_spriteBatch);
2024-01-23 12:15:24 +03:00
_spriteBatch.End();
2023-11-23 23:59:03 +03:00
2024-01-24 12:17:21 +03:00
_shapeBatch.Begin(cameraBehaviour.MatrixTransform);
foreach (IGameObject gameObject in gameManager)
2024-01-24 18:40:12 +03:00
foreach (var displayableShape in gameObject.BehaviourController.GetBehaviours<IDisplayableShape>())
2024-01-24 12:17:21 +03:00
displayableShape.Draw(_shapeBatch);
_shapeBatch.End();
2023-11-23 23:59:03 +03:00
base.Draw(gameTime);
}
}