Engine-Pong/Game/Game1.cs

208 lines
9.6 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;
2024-01-27 21:50:28 +03:00
using Syntriax.Engine.Physics2D.Abstract;
2023-11-23 23:59:03 +03:00
namespace Pong;
public class Game1 : Game
{
2024-01-30 12:23:45 +03:00
public static GraphicsDeviceManager graphics = null!;
public static IPhysicsEngine2D engine = null!;
public static SpriteBatch spriteBatch = null!;
public static ShapeBatch shapeBatch = null!;
2023-12-04 17:48:22 +03:00
public static GameManager gameManager = null!;
2024-01-30 12:23:45 +03:00
2024-01-23 12:15:24 +03:00
private MonoGameCameraBehaviour cameraBehaviour = null!;
2024-01-30 12:43:30 +03:00
private PongManager pongManager;
2023-11-23 23:59:03 +03:00
public Game1()
{
2024-01-27 21:50:28 +03:00
engine = new PhysicsEngine2D
{
IterationCount = 3
};
2024-01-22 11:21:27 +03:00
2024-01-30 12:23:45 +03:00
graphics = new GraphicsDeviceManager(this)
2023-11-27 17:06:33 +03:00
{
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()
{
2024-01-30 12:23:45 +03:00
spriteBatch = new SpriteBatch(GraphicsDevice);
shapeBatch = new ShapeBatch(GraphicsDevice, Content);
2024-01-28 15:45:39 +03:00
SpriteFont spriteFont = Content.Load<SpriteFont>("UbuntuMono");
2023-11-30 10:48:57 +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-30 12:23:45 +03:00
gameObjectCamera.BehaviourController.AddBehaviour<CameraController>();
2024-01-23 12:16:58 +03:00
cameraBehaviour = gameObjectCamera.BehaviourController.AddBehaviour<MonoGameCameraBehaviour>();
cameraBehaviour.Viewport = GraphicsDevice.Viewport;
2023-11-27 13:46:21 +03:00
2024-01-28 15:45:39 +03:00
////////////////////////////////////////////////////////////////////////////////////
2024-01-28 15:45:39 +03:00
IGameObject gameObjectPongManager = gameManager.InstantiateGameObject<GameObject>();
gameObjectPongManager.Name = "Pong Game Manager";
2024-01-30 12:43:30 +03:00
pongManager = gameObjectPongManager.BehaviourController.AddBehaviour<PongManager>(5);
2024-01-28 15:45:39 +03:00
////////////////////////////////////////////////////////////////////////////////////
2024-01-27 21:50:28 +03:00
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<CircleBehaviour>(new Circle(Vector2D.Zero, 1f));
2024-01-30 12:43:30 +03:00
PongBall ballBehaviour = gameObjectBall.BehaviourController.AddBehaviour<PongBall>();
2024-01-28 15:45:39 +03:00
RigidBody2D rigidBodyBall = gameObjectBall.BehaviourController.AddBehaviour<RigidBody2D>();
engine.AddRigidBody(rigidBodyBall);
2024-01-27 21:50:28 +03:00
////////////////////////////////////////////////////////////////////////////////////
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, 308f);
gameObjectWallTop.Transform.Scale = new Vector2D(552f, 20f);
2024-01-27 21:50:28 +03:00
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, -308f);
gameObjectWallBottom.Transform.Scale = new Vector2D(552f, 20f);
2024-01-27 21:50:28 +03:00
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(532f, 0f);
gameObjectWallRight.Transform.Scale = new Vector2D(20f, 328f);
2024-01-30 12:43:30 +03:00
gameObjectWallRight.BehaviourController.AddBehaviour<ScoreBoundary>((Action)pongManager.ScoreToLeft);
2024-01-27 21:50:28 +03:00
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(-532f, 0f);
gameObjectWallLeft.Transform.Scale = new Vector2D(20f, 328f);
2024-01-30 12:43:30 +03:00
gameObjectWallLeft.BehaviourController.AddBehaviour<ScoreBoundary>((Action)pongManager.ScoreToRight);
2024-01-27 21:50:28 +03:00
gameObjectWallLeft.BehaviourController.AddBehaviour<ShapeBehaviour>(Shape.Box);
RigidBody2D rigidBodyWallLeft = gameObjectWallLeft.BehaviourController.AddBehaviour<RigidBody2D>();
rigidBodyWallLeft.IsStatic = true;
engine.AddRigidBody(rigidBodyWallLeft);
2024-01-28 15:45:39 +03:00
////////////////////////////////////////////////////////////////////////////////////
2024-01-28 15:45:39 +03:00
IGameObject gameObjectLeftScoreText = gameManager.InstantiateGameObject<GameObject>();
gameObjectLeftScoreText.Name = "Score Left";
gameObjectLeftScoreText.Transform.Position = new Vector2D(-250f, 250f);
gameObjectLeftScoreText.Transform.Scale = Vector2D.One * .25f;
2024-01-30 12:43:30 +03:00
PongTextBehaviour textBehaviourLeft = gameObjectLeftScoreText.BehaviourController.AddBehaviour<PongTextBehaviour>(true);
2024-01-28 15:45:39 +03:00
textBehaviourLeft.Font = spriteFont;
textBehaviourLeft.Text = "0";
IGameObject gameObjectRightScoreText = gameManager.InstantiateGameObject<GameObject>();
gameObjectRightScoreText.Name = "Score Right";
gameObjectRightScoreText.Transform.Position = new Vector2D(250f, 250f);
gameObjectRightScoreText.Transform.Scale = Vector2D.One * .25f;
2024-01-30 12:43:30 +03:00
PongTextBehaviour textBehaviourRight = gameObjectRightScoreText.BehaviourController.AddBehaviour<PongTextBehaviour>(false);
2024-01-28 15:45:39 +03:00
textBehaviourRight.Font = spriteFont;
textBehaviourRight.Text = "0";
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();
2024-01-27 21:50:28 +03:00
gameManager.Update(gameTime.ToEngineTime());
2024-01-25 18:52:47 +03:00
while (physicsTimer + 0.01f < gameTime.TotalGameTime.TotalMilliseconds * .001f)//seconds)
{
physicsTimer += 0.01f;
engine.Step(.01f);
}
2023-11-23 23:59:03 +03:00
base.Update(gameTime);
}
2023-12-01 17:42:07 +03:00
static float physicsTimer = 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();
2024-01-23 12:15:24 +03:00
2024-01-30 12:23:45 +03:00
spriteBatch.Begin(SpriteSortMode.Deferred, transformMatrix: cameraBehaviour.MatrixTransform);
2024-01-23 12:15:24 +03:00
foreach (IGameObject gameObject in gameManager)
2024-01-24 18:40:12 +03:00
foreach (var displayable in gameObject.BehaviourController.GetBehaviours<IDisplayable>())
2024-01-30 12:23:45 +03:00
displayable.Draw(spriteBatch);
spriteBatch.End();
2023-11-23 23:59:03 +03:00
2024-01-30 12:23:45 +03:00
shapeBatch.Begin(cameraBehaviour.MatrixTransform);
2024-01-24 12:17:21 +03:00
foreach (IGameObject gameObject in gameManager)
2024-01-24 18:40:12 +03:00
foreach (var displayableShape in gameObject.BehaviourController.GetBehaviours<IDisplayableShape>())
2024-01-30 12:23:45 +03:00
displayableShape.Draw(shapeBatch);
shapeBatch.End();
2024-01-24 12:17:21 +03:00
2023-11-23 23:59:03 +03:00
base.Draw(gameTime);
}
}