Engine-Pong/Game/GamePong.cs

201 lines
9.6 KiB
C#
Raw Normal View History

using System;
2024-02-02 12:53:25 +03:00
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
2024-01-24 12:17:21 +03:00
using Apos.Shapes;
2023-12-05 17:10:03 +03:00
2024-02-02 12:53:25 +03:00
using Pong.Behaviours;
using Syntriax.Engine.Network;
2024-02-02 12:53:25 +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;
2024-01-27 21:50:28 +03:00
using Syntriax.Engine.Physics2D.Abstract;
2024-02-02 12:53:25 +03:00
using Syntriax.Engine.Physics2D.Primitives;
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!;
2024-02-09 09:44:25 +03:00
private BehaviourCollector<IDisplayableSprite> displayableCollector = null!;
private BehaviourCollector<IDisplayableShape> displayableShapeCollector = null!;
private BehaviourCollector<IMonoGameContentLoader> monoGameContentLoaderCollector = 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();
2024-02-09 09:44:25 +03:00
displayableCollector = new(gameManager);
displayableShapeCollector = new(gameManager);
monoGameContentLoaderCollector = new(gameManager);
physicsEngine = new PhysicsEngine2DCollector(gameManager) { IterationPerStep = 3 };
2024-02-09 09:44:25 +03:00
monoGameContentLoaderCollector.OnCollected += (_, cached) => cached.LoadContent(Content);
2024-02-05 12:37:23 +03:00
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-02-12 16:35:24 +03:00
string[] commandLineArguments = Environment.GetCommandLineArgs();
if (commandLineArguments.Length != 1)
{
if (commandLineArguments[1].Equals("server", StringComparison.OrdinalIgnoreCase))
{
gameManager.InstantiateGameObject().BehaviourController.AddBehaviour<NetworkServer>().Start(8888, 2);
Window.Title = "Pong - Server";
}
else
{
Window.Title = $"Pong - Client -> {commandLineArguments[1]}";
gameManager.InstantiateGameObject().BehaviourController.AddBehaviour<NetworkClient>().Connect(commandLineArguments[1], 8888);
}
}
else
{
gameManager.InstantiateGameObject().BehaviourController.AddBehaviour<NetworkClient>().Connect("127.0.0.1", 8888);
Window.Title = $"Pong - Client -> 127.0.0.1";
}
////////////////////////////////////////////////////////////////////////////////////
2024-02-05 12:11:37 +03:00
IGameObject gameObjectCamera = gameManager.InstantiateGameObject().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-02-05 12:11:37 +03:00
IGameObject gameObjectPongManager = gameManager.InstantiateGameObject().SetGameObject("Pong Game Manager");
2024-01-31 12:17:43 +03:00
pongManager = gameObjectPongManager.BehaviourController.AddBehaviour<PongManagerBehaviour>(5);
pongManager.Id = "pongManager";
2024-01-28 15:45:39 +03:00
////////////////////////////////////////////////////////////////////////////////////
2024-02-05 12:11:37 +03:00
IGameObject gameObjectBall = gameManager.InstantiateGameObject().SetGameObject("Ball");
2024-01-30 12:59:41 +03:00
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-02-02 12:53:25 +03:00
gameObjectBall.BehaviourController.AddBehaviour<BallBehaviour>().Id = "ball";
gameObjectBall.BehaviourController.AddBehaviour<RigidBody2D>();
2024-01-27 21:50:28 +03:00
////////////////////////////////////////////////////////////////////////////////////
2024-02-05 12:11:37 +03:00
IGameObject gameObjectLeftPaddle = gameManager.InstantiateGameObject().SetGameObject("Left Paddle");
2024-01-30 12:59:41 +03:00
gameObjectLeftPaddle.Transform.SetTransform(position: new Vector2D(-468f, 0f), scale: new Vector2D(15f, 60f));
gameObjectLeftPaddle.BehaviourController.AddBehaviour<NetworkedKeyboardInputs>().Id = "leftPaddleInput";
gameObjectLeftPaddle.BehaviourController.AddBehaviour<PaddleBehaviour>(Keys.W, Keys.S, 228f, -228f, 400f).Id = "leftPaddle";
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-02-05 12:11:37 +03:00
IGameObject gameObjectRightPaddle = gameManager.InstantiateGameObject().SetGameObject("Right Paddle");
2024-01-30 12:59:41 +03:00
gameObjectRightPaddle.Transform.SetTransform(position: new Vector2D(468f, 0f), scale: new Vector2D(15f, 60f));
gameObjectRightPaddle.BehaviourController.AddBehaviour<NetworkedKeyboardInputs>().Id = "rightPaddleInput";
gameObjectRightPaddle.BehaviourController.AddBehaviour<PaddleBehaviour>(Keys.Up, Keys.Down, 228f, -228f, 400f).Id = "rightPaddle";
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-02-05 12:11:37 +03:00
IGameObject gameObjectWallTop = gameManager.InstantiateGameObject().SetGameObject("Wall Top");
2024-01-30 12:59:41 +03:00
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-02-05 12:11:37 +03:00
IGameObject gameObjectWallBottom = gameManager.InstantiateGameObject().SetGameObject("Wall Bottom");
2024-01-30 12:59:41 +03:00
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-02-05 12:11:37 +03:00
IGameObject gameObjectWallRight = gameManager.InstantiateGameObject().SetGameObject("Wall Right");
2024-01-30 12:59:41 +03:00
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-02-05 12:11:37 +03:00
IGameObject gameObjectWallLeft = gameManager.InstantiateGameObject().SetGameObject("Wall Left");
2024-01-30 12:59:41 +03:00
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-02-05 12:11:37 +03:00
IGameObject gameObjectLeftScoreText = gameManager.InstantiateGameObject().SetGameObject("Score Left");
2024-01-30 12:59:41 +03:00
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
2024-02-05 12:11:37 +03:00
IGameObject gameObjectRightScoreText = gameManager.InstantiateGameObject().SetGameObject("Score Right");
2024-01-30 12:59:41 +03:00
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);
2024-02-09 09:44:25 +03:00
foreach (var displayable in displayableCollector)
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);
2024-02-09 09:44:25 +03:00
foreach (var displayableShape in displayableShapeCollector)
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);
}
}