feat: Basic Pong Setting
This commit is contained in:
parent
91b05c97c4
commit
ffe4fc044e
2
Engine
2
Engine
|
@ -1 +1 @@
|
|||
Subproject commit 6a104d8abd013334ed2787c4b4c6fa6481e4c6a4
|
||||
Subproject commit c1c1676b9a4c822b0e8f8adf23262127d7a11fba
|
|
@ -18,6 +18,8 @@ public class CircleBehaviour : Syntriax.Engine.Physics2D.Collider2DCircleBehavio
|
|||
|
||||
public void Draw(ShapeBatch shapeBatch)
|
||||
{
|
||||
Recalculate();
|
||||
|
||||
shapeBatch.BorderCircle(CircleWorld.Center.ToDisplayVector2(), CircleWorld.Radius, Color);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -3,6 +3,7 @@ using Microsoft.Xna.Framework;
|
|||
using Microsoft.Xna.Framework.Input;
|
||||
using Syntriax.Engine.Core;
|
||||
using Syntriax.Engine.Input;
|
||||
using Syntriax.Engine.Physics2D;
|
||||
using Syntriax.Engine.Physics2D.Abstract;
|
||||
|
||||
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 float Speed { get; set; } = Speed;
|
||||
|
||||
private IRigidBody2D rigidBody = null!;
|
||||
|
||||
protected override void OnFirstActiveFrame()
|
||||
{
|
||||
if (!BehaviourController.TryGetBehaviour(out IRigidBody2D? rigidBody))
|
||||
throw new Exception($"Where's my {nameof(IRigidBody2D)}????");
|
||||
if (!BehaviourController.TryGetBehaviour(out IRigidBody2D? foundRigidBody))
|
||||
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)
|
||||
|
|
|
@ -35,9 +35,7 @@ public class MovementBoxBehaviour(Keys Up, Keys Down, float High, float Low, flo
|
|||
protected override void OnFirstActiveFrame()
|
||||
{
|
||||
if (!BehaviourController.TryGetBehaviour<IButtonInputs<Keys>>(out var behaviourResult))
|
||||
throw new Exception($"{nameof(IButtonInputs<Keys>)} is missing on ${GameObject.Name}.");
|
||||
|
||||
inputs = behaviourResult;
|
||||
inputs = behaviourResult ?? BehaviourController.AddBehaviour<KeyboardInputsBehaviour>();
|
||||
|
||||
inputs.RegisterOnPress(Up, OnUpPressed);
|
||||
inputs.RegisterOnRelease(Up, OnUpReleased);
|
||||
|
|
|
@ -8,16 +8,18 @@ namespace Pong.Behaviours;
|
|||
|
||||
public class ShapeBehaviour : Syntriax.Engine.Physics2D.Collider2DShapeBehaviour, IDisplayableShape
|
||||
{
|
||||
public ShapeBehaviour(Shape Shape) { this.ShapeLocal = Shape; }
|
||||
public ShapeBehaviour(Shape Shape, float Thickness) { this.ShapeLocal = Shape; this.Thickness = Thickness; }
|
||||
public ShapeBehaviour(Shape Shape, Color color) { this.ShapeLocal = Shape; Color = color; }
|
||||
public ShapeBehaviour(Shape Shape, Color color, float Thickness) { this.ShapeLocal = Shape; this.Thickness = Thickness; Color = color; }
|
||||
public ShapeBehaviour(Shape shape) : base(shape) { }
|
||||
public ShapeBehaviour(Shape shape, float thickness) : base(shape) { Thickness = thickness; }
|
||||
public ShapeBehaviour(Shape shape, Color color) : base(shape) { Color = color; }
|
||||
public ShapeBehaviour(Shape shape, Color color, float thickness) : base(shape) { Thickness = thickness; Color = color; }
|
||||
|
||||
public Color Color { get; set; } = Color.White;
|
||||
public float Thickness { get; set; } = .5f;
|
||||
|
||||
public void Draw(ShapeBatch shapeBatch)
|
||||
{
|
||||
Recalculate();
|
||||
|
||||
int count = ShapeWorld.Vertices.Count;
|
||||
|
||||
shapeBatch.BorderCircle(Transform.Position.ToDisplayVector2(), 5f, Color.DarkRed);
|
||||
|
|
130
Game/Game1.cs
130
Game/Game1.cs
|
@ -10,13 +10,14 @@ using Syntriax.Engine.Core;
|
|||
using Syntriax.Engine.Core.Abstract;
|
||||
using Syntriax.Engine.Physics2D;
|
||||
using Syntriax.Engine.Physics2D.Primitives;
|
||||
using Syntriax.Engine.Physics2D.Abstract;
|
||||
|
||||
namespace Pong;
|
||||
|
||||
public class Game1 : Game
|
||||
{
|
||||
private GraphicsDeviceManager _graphics = null!;
|
||||
private PhysicsEngine2D engine;
|
||||
private IPhysicsEngine2D engine = null!;
|
||||
private SpriteBatch _spriteBatch = null!;
|
||||
private ShapeBatch _shapeBatch = null!;
|
||||
public static GameManager gameManager = null!;
|
||||
|
@ -26,7 +27,10 @@ public class Game1 : Game
|
|||
|
||||
public Game1()
|
||||
{
|
||||
engine = new PhysicsEngine2D();
|
||||
engine = new PhysicsEngine2D
|
||||
{
|
||||
IterationCount = 3
|
||||
};
|
||||
|
||||
_graphics = new GraphicsDeviceManager(this)
|
||||
{
|
||||
|
@ -65,72 +69,73 @@ public class Game1 : Game
|
|||
cameraBehaviour.Viewport = GraphicsDevice.Viewport;
|
||||
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>();
|
||||
gameObjectCircle2.Name = "Circle2";
|
||||
gameObjectCircle2.Transform.Position = new Vector2D(5f, 50f);
|
||||
gameObjectCircle2.Transform.Scale = new Vector2D(25f, 25f);
|
||||
gameObjectCircle2.BehaviourController.AddBehaviour<MovementMouseBehaviour>();
|
||||
gameObjectCircle2.BehaviourController.AddBehaviour<CircleBehaviour>(new Circle(Vector2D.Zero, 1f));
|
||||
engine.AddRigidBody(gameObjectCircle2.BehaviourController.AddBehaviour<RigidBody2D>());
|
||||
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 gameObjectDiamond = gameManager.InstantiateGameObject<GameObject>();
|
||||
gameObjectDiamond.Name = "Diamond";
|
||||
gameObjectDiamond.Transform.Position = new Vector2D(-150f, -150f);
|
||||
gameObjectDiamond.Transform.Scale = new Vector2D(50f, 50f);
|
||||
gameObjectDiamond.BehaviourController.AddBehaviour<KeyboardInputsBehaviour>();
|
||||
gameObjectDiamond.BehaviourController.AddBehaviour<MovementBoxBehaviour>(Keys.W, Keys.S, 268f, -268f, 400f);
|
||||
gameObjectDiamond.BehaviourController.AddBehaviour<RotatableBehaviour>();
|
||||
gameObjectDiamond.BehaviourController.AddBehaviour<ShapeBehaviour>(new Shape([Vector2D.Up, Vector2D.One, Vector2D.Right, Vector2D.Down, -Vector2D.One, Vector2D.Left]));
|
||||
gameObjectDiamond.BehaviourController.AddBehaviour<ShapeAABBBehaviour>();
|
||||
engine.AddRigidBody(gameObjectDiamond.BehaviourController.AddBehaviour<RigidBody2D>());
|
||||
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 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 Test = gameManager.InstantiateGameObject<GameObject>();
|
||||
Test.Name = i.ToString();
|
||||
Test.Transform.Position = new Vector2D((i - 6) * 150, 0f);
|
||||
Test.Transform.Scale = new Vector2D(75f, 75f);
|
||||
Test.BehaviourController.AddBehaviour<KeyboardInputsBehaviour>();
|
||||
Test.BehaviourController.AddBehaviour<MovementBoxBehaviour>(Keys.W, Keys.S, 268f, -268f, 400f);
|
||||
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 gameObjectWallTop = gameManager.InstantiateGameObject<GameObject>();
|
||||
gameObjectWallTop.Name = "WallTop";
|
||||
gameObjectWallTop.Transform.Position = new Vector2D(0f, 298f);
|
||||
gameObjectWallTop.Transform.Scale = new Vector2D(542f, 20f);
|
||||
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, -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>();
|
||||
// gameObjectShape.Name = "Shape";
|
||||
// gameObjectShape.Transform.Position = new Vector2D(250f, 0f);
|
||||
// gameObjectShape.Transform.Scale = new Vector2D(100f, 100f);
|
||||
// gameObjectShape.BehaviourController.AddBehaviour<KeyboardInputsBehaviour>();
|
||||
// gameObjectShape.BehaviourController.AddBehaviour<MovementBoxBehaviour>(Keys.W, Keys.S, 268f, -268f, 400f);
|
||||
// gameObjectShape.BehaviourController.AddBehaviour<RotatableBehaviour>();
|
||||
// gameObjectShape.BehaviourController.AddBehaviour<ShapeBehaviour>(new Shape([Vector2D.Up, Vector2D.One, Vector2D.Right, Vector2D.Down, Vector2D.Zero, Vector2D.Left]));
|
||||
// gameObjectShape.BehaviourController.AddBehaviour<ShapeAABBBehaviour>();
|
||||
IGameObject gameObjectWallRight = gameManager.InstantiateGameObject<GameObject>();
|
||||
gameObjectWallRight.Name = "WallRight";
|
||||
gameObjectWallRight.Transform.Position = new Vector2D(522f, 0f);
|
||||
gameObjectWallRight.Transform.Scale = new Vector2D(20f, 318f);
|
||||
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(-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)
|
||||
|
@ -204,14 +209,13 @@ public class Game1 : Game
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
gameManager.Update(gameTime.ToEngineTime());
|
||||
while (physicsTimer + 0.01f < gameTime.TotalGameTime.TotalMilliseconds * .001f)//seconds)
|
||||
{
|
||||
// Console.WriteLine($"Physics Timer: {physicsTimer}");
|
||||
physicsTimer += 0.01f;
|
||||
engine.Step(.01f);
|
||||
}
|
||||
gameManager.Update(gameTime.ToEngineTime());
|
||||
base.Update(gameTime);
|
||||
}
|
||||
static float physicsTimer = 0f;
|
||||
|
|
Loading…
Reference in New Issue