195 lines
9.1 KiB
C#
195 lines
9.1 KiB
C#
using System;
|
|
|
|
using Microsoft.Xna.Framework;
|
|
using Microsoft.Xna.Framework.Graphics;
|
|
using Microsoft.Xna.Framework.Input;
|
|
|
|
using Apos.Shapes;
|
|
|
|
using Pong.Behaviours;
|
|
using Syntriax.Engine.Network;
|
|
|
|
using Syntriax.Engine.Core;
|
|
using Syntriax.Engine.Core.Abstract;
|
|
using Syntriax.Engine.Physics2D;
|
|
using Syntriax.Engine.Physics2D.Abstract;
|
|
using Syntriax.Engine.Physics2D.Primitives;
|
|
|
|
namespace Pong;
|
|
|
|
public class GamePong : Game
|
|
{
|
|
private readonly GraphicsDeviceManager graphics = null!;
|
|
private IPhysicsEngine2D physicsEngine = null!;
|
|
private SpriteBatch spriteBatch = null!;
|
|
private ShapeBatch shapeBatch = null!;
|
|
|
|
private GameManager gameManager = null!;
|
|
private BehaviourCollector<IDisplayableSprite> displayableCollector = null!;
|
|
private BehaviourCollector<IDisplayableShape> displayableShapeCollector = null!;
|
|
private MonoGameCamera2DBehaviour cameraBehaviour = null!;
|
|
|
|
private PongManagerBehaviour pongManager = null!;
|
|
|
|
private float physicsTimer = 0f;
|
|
|
|
public GamePong()
|
|
{
|
|
graphics = new GraphicsDeviceManager(this)
|
|
{
|
|
PreferredBackBufferWidth = 1024,
|
|
PreferredBackBufferHeight = 576,
|
|
GraphicsProfile = GraphicsProfile.HiDef
|
|
};
|
|
|
|
Content.RootDirectory = "Content";
|
|
IsMouseVisible = true;
|
|
}
|
|
|
|
protected override void Initialize()
|
|
{
|
|
// TODO: Add your initialization logic here
|
|
gameManager = new();
|
|
displayableCollector = new(gameManager);
|
|
displayableShapeCollector = new(gameManager);
|
|
physicsEngine = new PhysicsEngine2DCollector(gameManager) { IterationPerStep = 3 };
|
|
|
|
gameManager.Initialize();
|
|
|
|
base.Initialize();
|
|
}
|
|
|
|
protected override void LoadContent()
|
|
{
|
|
spriteBatch = new SpriteBatch(GraphicsDevice);
|
|
shapeBatch = new ShapeBatch(GraphicsDevice, Content);
|
|
SpriteFont spriteFont = Content.Load<SpriteFont>("UbuntuMono");
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
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";
|
|
}
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
IGameObject gameObjectCamera = gameManager.InstantiateGameObject().SetGameObject("Camera"); ;
|
|
gameObjectCamera.BehaviourController.AddBehaviour<CameraController>();
|
|
cameraBehaviour = gameObjectCamera.BehaviourController.AddBehaviour<MonoGameCamera2DBehaviour>(graphics);
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
IGameObject gameObjectPongManager = gameManager.InstantiateGameObject().SetGameObject("Pong Game Manager");
|
|
pongManager = gameObjectPongManager.BehaviourController.AddBehaviour<PongManagerBehaviour>(5);
|
|
pongManager.Id = "pongManager";
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
IGameObject gameObjectBall = gameManager.InstantiateGameObject().SetGameObject("Ball");
|
|
gameObjectBall.Transform.SetTransform(position: new Vector2D(0, 0f), scale: new Vector2D(10f, 10f));
|
|
|
|
gameObjectBall.BehaviourController.AddBehaviour<CircleBehaviour>(new Circle(Vector2D.Zero, 1f));
|
|
gameObjectBall.BehaviourController.AddBehaviour<BallBehaviour>().Id = "ball";
|
|
gameObjectBall.BehaviourController.AddBehaviour<RigidBody2D>();
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
IGameObject gameObjectLeftPaddle = gameManager.InstantiateGameObject().SetGameObject("Left Paddle");
|
|
gameObjectLeftPaddle.Transform.SetTransform(position: new Vector2D(-468f, 0f), scale: new Vector2D(15f, 60f));
|
|
|
|
gameObjectLeftPaddle.BehaviourController.AddBehaviour<PaddleBehaviour>(Keys.W, Keys.S, 228f, -228f, 400f).Id = "leftPaddle";
|
|
gameObjectLeftPaddle.BehaviourController.AddBehaviour<ShapeBehaviour>(Shape.Box);
|
|
gameObjectLeftPaddle.BehaviourController.AddBehaviour<RigidBody2D>().IsStatic = true;
|
|
|
|
IGameObject gameObjectRightPaddle = gameManager.InstantiateGameObject().SetGameObject("Right Paddle");
|
|
gameObjectRightPaddle.Transform.SetTransform(position: new Vector2D(468f, 0f), scale: new Vector2D(15f, 60f));
|
|
gameObjectRightPaddle.BehaviourController.AddBehaviour<PaddleBehaviour>(Keys.Up, Keys.Down, 228f, -228f, 400f).Id = "rightPaddle";
|
|
gameObjectRightPaddle.BehaviourController.AddBehaviour<ShapeBehaviour>(Shape.Box);
|
|
gameObjectRightPaddle.BehaviourController.AddBehaviour<RigidBody2D>().IsStatic = true;
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
IGameObject gameObjectWallTop = gameManager.InstantiateGameObject().SetGameObject("Wall Top");
|
|
gameObjectWallTop.Transform.SetTransform(position: new Vector2D(0f, 308f), scale: new Vector2D(552f, 20f));
|
|
gameObjectWallTop.BehaviourController.AddBehaviour<ShapeBehaviour>(Shape.Box);
|
|
gameObjectWallTop.BehaviourController.AddBehaviour<RigidBody2D>().IsStatic = true;
|
|
|
|
IGameObject gameObjectWallBottom = gameManager.InstantiateGameObject().SetGameObject("Wall Bottom");
|
|
gameObjectWallBottom.Transform.SetTransform(position: new Vector2D(0f, -308f), scale: new Vector2D(552f, 20f));
|
|
gameObjectWallBottom.BehaviourController.AddBehaviour<ShapeBehaviour>(Shape.Box);
|
|
gameObjectWallBottom.BehaviourController.AddBehaviour<RigidBody2D>().IsStatic = true;
|
|
|
|
IGameObject gameObjectWallRight = gameManager.InstantiateGameObject().SetGameObject("Wall Right");
|
|
gameObjectWallRight.Transform.SetTransform(position: new Vector2D(532f, 0f), scale: new Vector2D(20f, 328f));
|
|
gameObjectWallRight.BehaviourController.AddBehaviour<WallScoreBehaviour>((Action)pongManager.ScoreToLeft);
|
|
gameObjectWallRight.BehaviourController.AddBehaviour<ShapeBehaviour>(Shape.Box);
|
|
gameObjectWallRight.BehaviourController.AddBehaviour<RigidBody2D>().IsStatic = true;
|
|
|
|
IGameObject gameObjectWallLeft = gameManager.InstantiateGameObject().SetGameObject("Wall Left");
|
|
gameObjectWallLeft.Transform.SetTransform(position: new Vector2D(-532f, 0f), scale: new Vector2D(20f, 328f));
|
|
gameObjectWallLeft.BehaviourController.AddBehaviour<WallScoreBehaviour>((Action)pongManager.ScoreToRight);
|
|
gameObjectWallLeft.BehaviourController.AddBehaviour<ShapeBehaviour>(Shape.Box);
|
|
gameObjectWallLeft.BehaviourController.AddBehaviour<RigidBody2D>().IsStatic = true;
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
IGameObject gameObjectLeftScoreText = gameManager.InstantiateGameObject().SetGameObject("Score Left");
|
|
gameObjectLeftScoreText.Transform.SetTransform(position: new Vector2D(-250f, 250f), scale: Vector2D.One * .25f);
|
|
gameObjectLeftScoreText.BehaviourController.AddBehaviour<TextScoreBehaviour>(true, spriteFont);
|
|
|
|
IGameObject gameObjectRightScoreText = gameManager.InstantiateGameObject().SetGameObject("Score Right");
|
|
gameObjectRightScoreText.Transform.SetTransform(position: new Vector2D(250f, 250f), scale: Vector2D.One * .25f);
|
|
gameObjectRightScoreText.BehaviourController.AddBehaviour<TextScoreBehaviour>(false, spriteFont);
|
|
}
|
|
|
|
protected override void Update(GameTime gameTime)
|
|
{
|
|
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
|
|
Exit();
|
|
|
|
gameManager.Update(gameTime.ToEngineTime());
|
|
while (physicsTimer + 0.01f < gameTime.TotalGameTime.TotalMilliseconds * .001f)//seconds)
|
|
{
|
|
physicsTimer += 0.01f;
|
|
physicsEngine.Step(.01f);
|
|
}
|
|
base.Update(gameTime);
|
|
}
|
|
|
|
protected override void Draw(GameTime gameTime)
|
|
{
|
|
GraphicsDevice.Clear(new Color() { R = 32, G = 32, B = 32 });
|
|
|
|
// TODO: Add your drawing code here
|
|
gameManager.PreDraw();
|
|
|
|
spriteBatch.Begin(SpriteSortMode.Deferred, transformMatrix: cameraBehaviour.MatrixTransform);
|
|
foreach (var displayable in displayableCollector)
|
|
displayable.Draw(spriteBatch);
|
|
spriteBatch.End();
|
|
|
|
shapeBatch.Begin(cameraBehaviour.MatrixTransform);
|
|
foreach (var displayableShape in displayableShapeCollector)
|
|
displayableShape.Draw(shapeBatch);
|
|
shapeBatch.End();
|
|
|
|
base.Draw(gameTime);
|
|
}
|
|
}
|