Engine-Pong/Game/GamePong.cs

169 lines
8.1 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;
2024-01-30 12:51:30 +03:00
public class GamePong : Game
2023-11-23 23:59:03 +03:00
{
private readonly GraphicsDeviceManager graphics = null!;
private IPhysicsEngine2D physicsEngine = null!;
private SpriteBatch spriteBatch = null!;
private ShapeBatch shapeBatch = null!;
2024-01-30 12:23:45 +03:00
private GameManager gameManager = null!;
private BehaviourCacher<IDisplayableSprite> displayableCacher = null!;
private BehaviourCacher<IDisplayableShape> displayableShapeCacher = null!;
2024-01-31 18:46:54 +03:00
private MonoGameCamera2DBehaviour cameraBehaviour = null!;
2024-01-31 12:17:43 +03:00
private PongManagerBehaviour pongManager = null!;
2023-11-23 23:59:03 +03:00
private float physicsTimer = 0f;
2024-01-30 12:51:30 +03:00
public GamePong()
2023-11-23 23:59:03 +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()
{
// TODO: Add your initialization logic here
2023-11-23 23:59:03 +03:00
gameManager = new();
displayableCacher = new(gameManager);
displayableShapeCacher = new(gameManager);
2024-02-01 12:35:54 +03:00
physicsEngine = new PhysicsEngine2DCacher(gameManager) { IterationPerStep = 3 };
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
2024-01-30 12:59:41 +03:00
////////////////////////////////////////////////////////////////////////////////////
2023-11-27 13:46:21 +03:00
2024-01-30 12:59:41 +03:00
IGameObject gameObjectCamera = gameManager.InstantiateGameObject<GameObject>().SetGameObject("Camera"); ;
2024-01-30 12:23:45 +03:00
gameObjectCamera.BehaviourController.AddBehaviour<CameraController>();
2024-01-31 18:46:54 +03:00
cameraBehaviour = gameObjectCamera.BehaviourController.AddBehaviour<MonoGameCamera2DBehaviour>(graphics);
2023-11-27 13:46:21 +03:00
2024-01-28 15:45:39 +03:00
////////////////////////////////////////////////////////////////////////////////////
2024-01-30 12:59:41 +03:00
IGameObject gameObjectPongManager = gameManager.InstantiateGameObject<GameObject>().SetGameObject("Pong Game Manager");
2024-01-31 12:17:43 +03:00
pongManager = gameObjectPongManager.BehaviourController.AddBehaviour<PongManagerBehaviour>(5);
2024-01-28 15:45:39 +03:00
////////////////////////////////////////////////////////////////////////////////////
2024-01-30 12:59:41 +03:00
IGameObject gameObjectBall = gameManager.InstantiateGameObject<GameObject>().SetGameObject("Ball");
gameObjectBall.Transform.SetTransform(position: new Vector2D(0, 0f), scale: new Vector2D(10f, 10f));
2024-01-27 21:50:28 +03:00
gameObjectBall.BehaviourController.AddBehaviour<CircleBehaviour>(new Circle(Vector2D.Zero, 1f));
2024-01-31 12:17:43 +03:00
gameObjectBall.BehaviourController.AddBehaviour<BallBehaviour>();
gameObjectBall.BehaviourController.AddBehaviour<RigidBody2D>();
2024-01-27 21:50:28 +03:00
////////////////////////////////////////////////////////////////////////////////////
2024-01-30 12:59:41 +03:00
IGameObject gameObjectLeftPaddle = gameManager.InstantiateGameObject<GameObject>().SetGameObject("Left Paddle");
gameObjectLeftPaddle.Transform.SetTransform(position: new Vector2D(-468f, 0f), scale: new Vector2D(15f, 60f));
2024-01-31 12:17:43 +03:00
gameObjectLeftPaddle.BehaviourController.AddBehaviour<PaddleBehaviour>(Keys.W, Keys.S, 228f, -228f, 400f);
2024-01-27 21:50:28 +03:00
gameObjectLeftPaddle.BehaviourController.AddBehaviour<ShapeBehaviour>(Shape.Box);
gameObjectLeftPaddle.BehaviourController.AddBehaviour<RigidBody2D>().IsStatic = true;
2024-01-27 21:50:28 +03:00
2024-01-30 12:59:41 +03:00
IGameObject gameObjectRightPaddle = gameManager.InstantiateGameObject<GameObject>().SetGameObject("Right Paddle");
gameObjectRightPaddle.Transform.SetTransform(position: new Vector2D(468f, 0f), scale: new Vector2D(15f, 60f));
2024-01-31 12:17:43 +03:00
gameObjectRightPaddle.BehaviourController.AddBehaviour<PaddleBehaviour>(Keys.Up, Keys.Down, 228f, -228f, 400f);
2024-01-27 21:50:28 +03:00
gameObjectRightPaddle.BehaviourController.AddBehaviour<ShapeBehaviour>(Shape.Box);
gameObjectRightPaddle.BehaviourController.AddBehaviour<RigidBody2D>().IsStatic = true;
2024-01-27 21:50:28 +03:00
////////////////////////////////////////////////////////////////////////////////////
2024-01-30 12:59:41 +03:00
IGameObject gameObjectWallTop = gameManager.InstantiateGameObject<GameObject>().SetGameObject("Wall Top");
gameObjectWallTop.Transform.SetTransform(position: new Vector2D(0f, 308f), scale: new Vector2D(552f, 20f));
2024-01-27 21:50:28 +03:00
gameObjectWallTop.BehaviourController.AddBehaviour<ShapeBehaviour>(Shape.Box);
gameObjectWallTop.BehaviourController.AddBehaviour<RigidBody2D>().IsStatic = true;
2024-01-27 21:50:28 +03:00
2024-01-30 12:59:41 +03:00
IGameObject gameObjectWallBottom = gameManager.InstantiateGameObject<GameObject>().SetGameObject("Wall Bottom");
gameObjectWallBottom.Transform.SetTransform(position: new Vector2D(0f, -308f), scale: new Vector2D(552f, 20f));
2024-01-27 21:50:28 +03:00
gameObjectWallBottom.BehaviourController.AddBehaviour<ShapeBehaviour>(Shape.Box);
gameObjectWallBottom.BehaviourController.AddBehaviour<RigidBody2D>().IsStatic = true;
2024-01-27 21:50:28 +03:00
2024-01-30 12:59:41 +03:00
IGameObject gameObjectWallRight = gameManager.InstantiateGameObject<GameObject>().SetGameObject("Wall Right");
gameObjectWallRight.Transform.SetTransform(position: new Vector2D(532f, 0f), scale: new Vector2D(20f, 328f));
2024-01-31 12:32:16 +03:00
gameObjectWallRight.BehaviourController.AddBehaviour<WallScoreBehaviour>((Action)pongManager.ScoreToLeft);
2024-01-27 21:50:28 +03:00
gameObjectWallRight.BehaviourController.AddBehaviour<ShapeBehaviour>(Shape.Box);
gameObjectWallRight.BehaviourController.AddBehaviour<RigidBody2D>().IsStatic = true;
2024-01-27 21:50:28 +03:00
2024-01-30 12:59:41 +03:00
IGameObject gameObjectWallLeft = gameManager.InstantiateGameObject<GameObject>().SetGameObject("Wall Left");
gameObjectWallLeft.Transform.SetTransform(position: new Vector2D(-532f, 0f), scale: new Vector2D(20f, 328f));
2024-01-31 12:32:16 +03:00
gameObjectWallLeft.BehaviourController.AddBehaviour<WallScoreBehaviour>((Action)pongManager.ScoreToRight);
2024-01-27 21:50:28 +03:00
gameObjectWallLeft.BehaviourController.AddBehaviour<ShapeBehaviour>(Shape.Box);
gameObjectWallLeft.BehaviourController.AddBehaviour<RigidBody2D>().IsStatic = true;
2024-01-28 15:45:39 +03:00
////////////////////////////////////////////////////////////////////////////////////
2024-01-30 12:59:41 +03:00
IGameObject gameObjectLeftScoreText = gameManager.InstantiateGameObject<GameObject>().SetGameObject("Score Left");
gameObjectLeftScoreText.Transform.SetTransform(position: new Vector2D(-250f, 250f), scale: Vector2D.One * .25f);
2024-01-31 12:17:43 +03:00
gameObjectLeftScoreText.BehaviourController.AddBehaviour<TextScoreBehaviour>(true, spriteFont);
2024-01-30 12:59:41 +03:00
IGameObject gameObjectRightScoreText = gameManager.InstantiateGameObject<GameObject>().SetGameObject("Score Right");
gameObjectRightScoreText.Transform.SetTransform(position: new Vector2D(250f, 250f), scale: Vector2D.One * .25f);
2024-01-31 12:17:43 +03:00
gameObjectRightScoreText.BehaviourController.AddBehaviour<TextScoreBehaviour>(false, spriteFont);
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;
physicsEngine.Step(.01f);
2024-01-25 18:52:47 +03:00
}
2023-11-23 23:59:03 +03:00
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(new Color() { R = 32, G = 32, B = 32 });
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);
foreach (var displayable in displayableCacher)
displayable.Draw(spriteBatch);
2024-01-30 12:23:45 +03:00
spriteBatch.End();
2023-11-23 23:59:03 +03:00
2024-01-30 12:23:45 +03:00
shapeBatch.Begin(cameraBehaviour.MatrixTransform);
foreach (var displayableShape in displayableShapeCacher)
displayableShape.Draw(shapeBatch);
2024-01-30 12:23:45 +03:00
shapeBatch.End();
2024-01-24 12:17:21 +03:00
2023-11-23 23:59:03 +03:00
base.Draw(gameTime);
}
}