feat: Basic Pong Setting

This commit is contained in:
Syntriax 2024-01-27 21:50:28 +03:00
parent 91b05c97c4
commit ffe4fc044e
6 changed files with 93 additions and 74 deletions

2
Engine

@ -1 +1 @@
Subproject commit 6a104d8abd013334ed2787c4b4c6fa6481e4c6a4 Subproject commit c1c1676b9a4c822b0e8f8adf23262127d7a11fba

View File

@ -18,6 +18,8 @@ public class CircleBehaviour : Syntriax.Engine.Physics2D.Collider2DCircleBehavio
public void Draw(ShapeBatch shapeBatch) public void Draw(ShapeBatch shapeBatch)
{ {
Recalculate();
shapeBatch.BorderCircle(CircleWorld.Center.ToDisplayVector2(), CircleWorld.Radius, Color); shapeBatch.BorderCircle(CircleWorld.Center.ToDisplayVector2(), CircleWorld.Radius, Color);
} }
} }

View File

@ -3,6 +3,7 @@ using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input; using Microsoft.Xna.Framework.Input;
using Syntriax.Engine.Core; using Syntriax.Engine.Core;
using Syntriax.Engine.Input; using Syntriax.Engine.Input;
using Syntriax.Engine.Physics2D;
using Syntriax.Engine.Physics2D.Abstract; using Syntriax.Engine.Physics2D.Abstract;
namespace Pong.Behaviours; namespace Pong.Behaviours;
@ -11,12 +12,24 @@ public class MovementBallBehaviour(Vector2D StartDirection, float Speed) : Behav
public Vector2D StartDirection { get; private set; } = Vector2D.Normalize(StartDirection); public Vector2D StartDirection { get; private set; } = Vector2D.Normalize(StartDirection);
public float Speed { get; set; } = Speed; public float Speed { get; set; } = Speed;
private IRigidBody2D rigidBody = null!;
protected override void OnFirstActiveFrame() protected override void OnFirstActiveFrame()
{ {
if (!BehaviourController.TryGetBehaviour(out IRigidBody2D? rigidBody)) if (!BehaviourController.TryGetBehaviour(out IRigidBody2D? foundRigidBody))
throw new Exception($"Where's my {nameof(IRigidBody2D)}????"); throw new Exception($"{nameof(IRigidBody2D)} is missing on {GameObject.Name}.");
if (!BehaviourController.TryGetBehaviour(out ICollider2D? foundCollider))
throw new Exception($"{nameof(ICollider2D)} is missing on {GameObject.Name}.");
rigidBody.Velocity = StartDirection * Speed; foundRigidBody.Velocity = StartDirection * Speed;
foundCollider.OnCollisionDetected += OnCollisionDetected;
rigidBody = foundRigidBody;
}
private void OnCollisionDetected(ICollider2D collider2D, CollisionDetectionInformation information)
{
rigidBody.Velocity = rigidBody.Velocity.Reflect(information.Normal);
} }
// protected override void OnUpdate(GameTime time) // protected override void OnUpdate(GameTime time)

View File

@ -35,9 +35,7 @@ public class MovementBoxBehaviour(Keys Up, Keys Down, float High, float Low, flo
protected override void OnFirstActiveFrame() protected override void OnFirstActiveFrame()
{ {
if (!BehaviourController.TryGetBehaviour<IButtonInputs<Keys>>(out var behaviourResult)) if (!BehaviourController.TryGetBehaviour<IButtonInputs<Keys>>(out var behaviourResult))
throw new Exception($"{nameof(IButtonInputs<Keys>)} is missing on ${GameObject.Name}."); inputs = behaviourResult ?? BehaviourController.AddBehaviour<KeyboardInputsBehaviour>();
inputs = behaviourResult;
inputs.RegisterOnPress(Up, OnUpPressed); inputs.RegisterOnPress(Up, OnUpPressed);
inputs.RegisterOnRelease(Up, OnUpReleased); inputs.RegisterOnRelease(Up, OnUpReleased);

View File

@ -8,16 +8,18 @@ namespace Pong.Behaviours;
public class ShapeBehaviour : Syntriax.Engine.Physics2D.Collider2DShapeBehaviour, IDisplayableShape public class ShapeBehaviour : Syntriax.Engine.Physics2D.Collider2DShapeBehaviour, IDisplayableShape
{ {
public ShapeBehaviour(Shape Shape) { this.ShapeLocal = Shape; } public ShapeBehaviour(Shape shape) : base(shape) { }
public ShapeBehaviour(Shape Shape, float Thickness) { this.ShapeLocal = Shape; this.Thickness = Thickness; } public ShapeBehaviour(Shape shape, float thickness) : base(shape) { Thickness = thickness; }
public ShapeBehaviour(Shape Shape, Color color) { this.ShapeLocal = Shape; Color = color; } public ShapeBehaviour(Shape shape, Color color) : base(shape) { Color = color; }
public ShapeBehaviour(Shape Shape, Color color, float Thickness) { this.ShapeLocal = Shape; this.Thickness = Thickness; Color = color; } public ShapeBehaviour(Shape shape, Color color, float thickness) : base(shape) { Thickness = thickness; Color = color; }
public Color Color { get; set; } = Color.White; public Color Color { get; set; } = Color.White;
public float Thickness { get; set; } = .5f; public float Thickness { get; set; } = .5f;
public void Draw(ShapeBatch shapeBatch) public void Draw(ShapeBatch shapeBatch)
{ {
Recalculate();
int count = ShapeWorld.Vertices.Count; int count = ShapeWorld.Vertices.Count;
shapeBatch.BorderCircle(Transform.Position.ToDisplayVector2(), 5f, Color.DarkRed); shapeBatch.BorderCircle(Transform.Position.ToDisplayVector2(), 5f, Color.DarkRed);

View File

@ -10,13 +10,14 @@ using Syntriax.Engine.Core;
using Syntriax.Engine.Core.Abstract; using Syntriax.Engine.Core.Abstract;
using Syntriax.Engine.Physics2D; using Syntriax.Engine.Physics2D;
using Syntriax.Engine.Physics2D.Primitives; using Syntriax.Engine.Physics2D.Primitives;
using Syntriax.Engine.Physics2D.Abstract;
namespace Pong; namespace Pong;
public class Game1 : Game public class Game1 : Game
{ {
private GraphicsDeviceManager _graphics = null!; private GraphicsDeviceManager _graphics = null!;
private PhysicsEngine2D engine; private IPhysicsEngine2D engine = null!;
private SpriteBatch _spriteBatch = null!; private SpriteBatch _spriteBatch = null!;
private ShapeBatch _shapeBatch = null!; private ShapeBatch _shapeBatch = null!;
public static GameManager gameManager = null!; public static GameManager gameManager = null!;
@ -26,7 +27,10 @@ public class Game1 : Game
public Game1() public Game1()
{ {
engine = new PhysicsEngine2D(); engine = new PhysicsEngine2D
{
IterationCount = 3
};
_graphics = new GraphicsDeviceManager(this) _graphics = new GraphicsDeviceManager(this)
{ {
@ -65,72 +69,73 @@ public class Game1 : Game
cameraBehaviour.Viewport = GraphicsDevice.Viewport; cameraBehaviour.Viewport = GraphicsDevice.Viewport;
gameManager.Camera = cameraBehaviour; gameManager.Camera = cameraBehaviour;
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<MovementBallBehaviour>(Vector2D.One, 500f);
gameObjectBall.BehaviourController.AddBehaviour<CircleBehaviour>(new Circle(Vector2D.Zero, 1f));
engine.AddRigidBody(gameObjectBall.BehaviourController.AddBehaviour<RigidBody2D>());
// IGameObject gameObjectCircle = gameManager.InstantiateGameObject<GameObject>(); ////////////////////////////////////////////////////////////////////////////////////
// gameObjectCircle.Name = "Circle";
// gameObjectCircle.Transform.Position = new Vector2D(0f, -50f);
// gameObjectCircle.Transform.Scale = new Vector2D(25f, 25f);
// gameObjectCircle.BehaviourController.AddBehaviour<KeyboardInputsBehaviour>();
// gameObjectCircle.BehaviourController.AddBehaviour<MovementBoxBehaviour>(Keys.W, Keys.S, 268f, -268f, 400f);
// gameObjectCircle.BehaviourController.AddBehaviour<CircleBehaviour>(new Circle(Vector2D.Zero, 1f));
// engine.AddRigidBody(gameObjectCircle.BehaviourController.AddBehaviour<RigidBody2D>());
IGameObject gameObjectCircle2 = gameManager.InstantiateGameObject<GameObject>(); IGameObject gameObjectLeftPaddle = gameManager.InstantiateGameObject<GameObject>();
gameObjectCircle2.Name = "Circle2"; gameObjectLeftPaddle.Name = "Left Paddle";
gameObjectCircle2.Transform.Position = new Vector2D(5f, 50f); gameObjectLeftPaddle.Transform.Position = new Vector2D(-468f, 0f);
gameObjectCircle2.Transform.Scale = new Vector2D(25f, 25f); gameObjectLeftPaddle.Transform.Scale = new Vector2D(15f, 60f);
gameObjectCircle2.BehaviourController.AddBehaviour<MovementMouseBehaviour>(); gameObjectLeftPaddle.BehaviourController.AddBehaviour<MovementBoxBehaviour>(Keys.W, Keys.S, 228f, -228f, 400f);
gameObjectCircle2.BehaviourController.AddBehaviour<CircleBehaviour>(new Circle(Vector2D.Zero, 1f)); gameObjectLeftPaddle.BehaviourController.AddBehaviour<ShapeBehaviour>(Shape.Box);
engine.AddRigidBody(gameObjectCircle2.BehaviourController.AddBehaviour<RigidBody2D>()); RigidBody2D rigidBodyLeftPaddle = gameObjectLeftPaddle.BehaviourController.AddBehaviour<RigidBody2D>();
rigidBodyLeftPaddle.IsStatic = true;
engine.AddRigidBody(rigidBodyLeftPaddle);
IGameObject gameObjectDiamond = gameManager.InstantiateGameObject<GameObject>(); IGameObject gameObjectRightPaddle = gameManager.InstantiateGameObject<GameObject>();
gameObjectDiamond.Name = "Diamond"; gameObjectRightPaddle.Name = "Right Paddle";
gameObjectDiamond.Transform.Position = new Vector2D(-150f, -150f); gameObjectRightPaddle.Transform.Position = new Vector2D(468f, 0f);
gameObjectDiamond.Transform.Scale = new Vector2D(50f, 50f); gameObjectRightPaddle.Transform.Scale = new Vector2D(15f, 60f);
gameObjectDiamond.BehaviourController.AddBehaviour<KeyboardInputsBehaviour>(); gameObjectRightPaddle.BehaviourController.AddBehaviour<MovementBoxBehaviour>(Keys.Up, Keys.Down, 228f, -228f, 400f);
gameObjectDiamond.BehaviourController.AddBehaviour<MovementBoxBehaviour>(Keys.W, Keys.S, 268f, -268f, 400f); gameObjectRightPaddle.BehaviourController.AddBehaviour<ShapeBehaviour>(Shape.Box);
gameObjectDiamond.BehaviourController.AddBehaviour<RotatableBehaviour>(); RigidBody2D rigidBodyRightPaddle = gameObjectRightPaddle.BehaviourController.AddBehaviour<RigidBody2D>();
gameObjectDiamond.BehaviourController.AddBehaviour<ShapeBehaviour>(new Shape([Vector2D.Up, Vector2D.One, Vector2D.Right, Vector2D.Down, -Vector2D.One, Vector2D.Left])); rigidBodyRightPaddle.IsStatic = true;
gameObjectDiamond.BehaviourController.AddBehaviour<ShapeAABBBehaviour>(); engine.AddRigidBody(rigidBodyRightPaddle);
engine.AddRigidBody(gameObjectDiamond.BehaviourController.AddBehaviour<RigidBody2D>());
IGameObject gameObjectBox = gameManager.InstantiateGameObject<GameObject>(); ////////////////////////////////////////////////////////////////////////////////////
gameObjectBox.Name = "Box";
gameObjectBox.Transform.Position = new Vector2D(150f, -150f);
gameObjectBox.Transform.Scale = new Vector2D(100f, 100f);
gameObjectBox.BehaviourController.AddBehaviour<KeyboardInputsBehaviour>();
gameObjectBox.BehaviourController.AddBehaviour<MovementBoxBehaviour>(Keys.W, Keys.S, 268f, -268f, 400f);
gameObjectBox.BehaviourController.AddBehaviour<RotatableBehaviour>();
gameObjectBox.BehaviourController.AddBehaviour<ShapeBehaviour>(Shape.Pentagon);
gameObjectBox.BehaviourController.AddBehaviour<ShapeAABBBehaviour>();
engine.AddRigidBody(gameObjectBox.BehaviourController.AddBehaviour<RigidBody2D>());
for (int i = 3; i < 10; i++) IGameObject gameObjectWallTop = gameManager.InstantiateGameObject<GameObject>();
{ gameObjectWallTop.Name = "WallTop";
IGameObject Test = gameManager.InstantiateGameObject<GameObject>(); gameObjectWallTop.Transform.Position = new Vector2D(0f, 298f);
Test.Name = i.ToString(); gameObjectWallTop.Transform.Scale = new Vector2D(542f, 20f);
Test.Transform.Position = new Vector2D((i - 6) * 150, 0f); gameObjectWallTop.BehaviourController.AddBehaviour<ShapeBehaviour>(Shape.Box);
Test.Transform.Scale = new Vector2D(75f, 75f); RigidBody2D rigidBodyWallTop = gameObjectWallTop.BehaviourController.AddBehaviour<RigidBody2D>();
Test.BehaviourController.AddBehaviour<KeyboardInputsBehaviour>(); rigidBodyWallTop.IsStatic = true;
Test.BehaviourController.AddBehaviour<MovementBoxBehaviour>(Keys.W, Keys.S, 268f, -268f, 400f); engine.AddRigidBody(rigidBodyWallTop);
Test.BehaviourController.AddBehaviour<RotatableBehaviour>();
Test.BehaviourController.AddBehaviour<ShapeBehaviour>(Shape.CreateNgon(i));
Test.BehaviourController.AddBehaviour<ShapeAABBBehaviour>();
RigidBody2D rigidBody = Test.BehaviourController.AddBehaviour<RigidBody2D>();
rigidBody.AngularVelocity = 90f;
engine.AddRigidBody(rigidBody);
}
IGameObject gameObjectWallBottom = gameManager.InstantiateGameObject<GameObject>();
gameObjectWallBottom.Name = "WallBottom";
gameObjectWallBottom.Transform.Position = new Vector2D(0f, -298f);
gameObjectWallBottom.Transform.Scale = new Vector2D(542f, 20f);
gameObjectWallBottom.BehaviourController.AddBehaviour<ShapeBehaviour>(Shape.Box);
RigidBody2D rigidBodyWallBottom = gameObjectWallBottom.BehaviourController.AddBehaviour<RigidBody2D>();
rigidBodyWallBottom.IsStatic = true;
engine.AddRigidBody(rigidBodyWallBottom);
// IGameObject gameObjectShape = gameManager.InstantiateGameObject<GameObject>(); IGameObject gameObjectWallRight = gameManager.InstantiateGameObject<GameObject>();
// gameObjectShape.Name = "Shape"; gameObjectWallRight.Name = "WallRight";
// gameObjectShape.Transform.Position = new Vector2D(250f, 0f); gameObjectWallRight.Transform.Position = new Vector2D(522f, 0f);
// gameObjectShape.Transform.Scale = new Vector2D(100f, 100f); gameObjectWallRight.Transform.Scale = new Vector2D(20f, 318f);
// gameObjectShape.BehaviourController.AddBehaviour<KeyboardInputsBehaviour>(); gameObjectWallRight.BehaviourController.AddBehaviour<ShapeBehaviour>(Shape.Box);
// gameObjectShape.BehaviourController.AddBehaviour<MovementBoxBehaviour>(Keys.W, Keys.S, 268f, -268f, 400f); RigidBody2D rigidBodyWallRight = gameObjectWallRight.BehaviourController.AddBehaviour<RigidBody2D>();
// gameObjectShape.BehaviourController.AddBehaviour<RotatableBehaviour>(); rigidBodyWallRight.IsStatic = true;
// gameObjectShape.BehaviourController.AddBehaviour<ShapeBehaviour>(new Shape([Vector2D.Up, Vector2D.One, Vector2D.Right, Vector2D.Down, Vector2D.Zero, Vector2D.Left])); engine.AddRigidBody(rigidBodyWallRight);
// gameObjectShape.BehaviourController.AddBehaviour<ShapeAABBBehaviour>();
IGameObject gameObjectWallLeft = gameManager.InstantiateGameObject<GameObject>();
gameObjectWallLeft.Name = "WallLeft";
gameObjectWallLeft.Transform.Position = new Vector2D(-522f, 0f);
gameObjectWallLeft.Transform.Scale = new Vector2D(20f, 318f);
gameObjectWallLeft.BehaviourController.AddBehaviour<ShapeBehaviour>(Shape.Box);
RigidBody2D rigidBodyWallLeft = gameObjectWallLeft.BehaviourController.AddBehaviour<RigidBody2D>();
rigidBodyWallLeft.IsStatic = true;
engine.AddRigidBody(rigidBodyWallLeft);
} }
protected override void Update(GameTime gameTime) protected override void Update(GameTime gameTime)
@ -204,14 +209,13 @@ public class Game1 : Game
} }
} }
gameManager.Update(gameTime.ToEngineTime());
while (physicsTimer + 0.01f < gameTime.TotalGameTime.TotalMilliseconds * .001f)//seconds) while (physicsTimer + 0.01f < gameTime.TotalGameTime.TotalMilliseconds * .001f)//seconds)
{ {
// Console.WriteLine($"Physics Timer: {physicsTimer}"); // Console.WriteLine($"Physics Timer: {physicsTimer}");
physicsTimer += 0.01f; physicsTimer += 0.01f;
engine.Step(.01f); engine.Step(.01f);
} }
gameManager.Update(gameTime.ToEngineTime());
base.Update(gameTime); base.Update(gameTime);
} }
static float physicsTimer = 0f; static float physicsTimer = 0f;