refactor: Renamed Behaviours
This commit is contained in:
parent
8929d3282f
commit
ff3202eda9
|
@ -6,7 +6,7 @@ using Syntriax.Engine.Physics2D.Abstract;
|
||||||
|
|
||||||
namespace Pong.Behaviours;
|
namespace Pong.Behaviours;
|
||||||
|
|
||||||
public class PongBall : BehaviourOverride
|
public class BallBehaviour : BehaviourOverride
|
||||||
{
|
{
|
||||||
public Vector2D StartDirection { get; private set; } = Vector2D.Zero;
|
public Vector2D StartDirection { get; private set; } = Vector2D.Zero;
|
||||||
public float Speed { get; set; } = 500f;
|
public float Speed { get; set; } = 500f;
|
||||||
|
@ -14,12 +14,12 @@ public class PongBall : BehaviourOverride
|
||||||
|
|
||||||
private IRigidBody2D rigidBody = null!;
|
private IRigidBody2D rigidBody = null!;
|
||||||
|
|
||||||
public PongBall(Vector2D startDirection, float speed)
|
public BallBehaviour(Vector2D startDirection, float speed)
|
||||||
{
|
{
|
||||||
StartDirection = Vector2D.Normalize(startDirection);
|
StartDirection = Vector2D.Normalize(startDirection);
|
||||||
Speed = speed;
|
Speed = speed;
|
||||||
}
|
}
|
||||||
public PongBall() { }
|
public BallBehaviour() { }
|
||||||
|
|
||||||
protected override void OnFirstActiveFrame()
|
protected override void OnFirstActiveFrame()
|
||||||
{
|
{
|
||||||
|
@ -32,7 +32,7 @@ public class PongBall : BehaviourOverride
|
||||||
|
|
||||||
rigidBody = foundRigidBody;
|
rigidBody = foundRigidBody;
|
||||||
|
|
||||||
if (GameObject.GameManager.TryFindBehaviour(out PongManager? pongManager))
|
if (GameObject.GameManager.TryFindBehaviour(out PongManagerBehaviour? pongManager))
|
||||||
{
|
{
|
||||||
pongManager.OnReset += ResetBall;
|
pongManager.OnReset += ResetBall;
|
||||||
pongManager.OnScored += ResetBall;
|
pongManager.OnScored += ResetBall;
|
||||||
|
@ -40,13 +40,13 @@ public class PongBall : BehaviourOverride
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void DisableBall(PongManager pongManager)
|
private void DisableBall(PongManagerBehaviour pongManager)
|
||||||
{
|
{
|
||||||
BehaviourController.GameObject.Transform.Position = Vector2D.Zero;
|
BehaviourController.GameObject.Transform.Position = Vector2D.Zero;
|
||||||
rigidBody.Velocity = Vector2D.Zero;
|
rigidBody.Velocity = Vector2D.Zero;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void ResetBall(PongManager pongManager)
|
private void ResetBall(PongManagerBehaviour pongManager)
|
||||||
{
|
{
|
||||||
StateEnable.Enabled = true;
|
StateEnable.Enabled = true;
|
||||||
BehaviourController.GameObject.Transform.Position = Vector2D.Zero;
|
BehaviourController.GameObject.Transform.Position = Vector2D.Zero;
|
|
@ -1,12 +1,13 @@
|
||||||
using System;
|
using System;
|
||||||
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;
|
||||||
|
|
||||||
namespace Pong.Behaviours;
|
namespace Pong.Behaviours;
|
||||||
|
|
||||||
public class MovementBoxBehaviour(Keys Up, Keys Down, float High, float Low, float Speed) : BehaviourOverride
|
public class PaddleBehaviour(Keys Up, Keys Down, float High, float Low, float Speed) : BehaviourOverride
|
||||||
{
|
{
|
||||||
private Keys Up { get; } = Up;
|
private Keys Up { get; } = Up;
|
||||||
private Keys Down { get; } = Down;
|
private Keys Down { get; } = Down;
|
|
@ -6,11 +6,11 @@ using Syntriax.Engine.Core;
|
||||||
|
|
||||||
namespace Pong.Behaviours;
|
namespace Pong.Behaviours;
|
||||||
|
|
||||||
public class PongManager : BehaviourOverride
|
public class PongManagerBehaviour : BehaviourOverride
|
||||||
{
|
{
|
||||||
public Action<PongManager>? OnReset { get; set; } = null;
|
public Action<PongManagerBehaviour>? OnReset { get; set; } = null;
|
||||||
public Action<PongManager>? OnFinished { get; set; } = null;
|
public Action<PongManagerBehaviour>? OnFinished { get; set; } = null;
|
||||||
public Action<PongManager>? OnScored { get; set; } = null;
|
public Action<PongManagerBehaviour>? OnScored { get; set; } = null;
|
||||||
|
|
||||||
public int ScoreLeft { get; private set; } = 0;
|
public int ScoreLeft { get; private set; } = 0;
|
||||||
public int ScoreRight { get; private set; } = 0;
|
public int ScoreRight { get; private set; } = 0;
|
||||||
|
@ -18,8 +18,8 @@ public class PongManager : BehaviourOverride
|
||||||
|
|
||||||
public int WinScore { get; } = 5;
|
public int WinScore { get; } = 5;
|
||||||
|
|
||||||
public PongManager() => WinScore = 5;
|
public PongManagerBehaviour() => WinScore = 5;
|
||||||
public PongManager(int winScore) => WinScore = winScore;
|
public PongManagerBehaviour(int winScore) => WinScore = winScore;
|
||||||
|
|
||||||
protected override void OnFirstActiveFrame()
|
protected override void OnFirstActiveFrame()
|
||||||
{
|
{
|
|
@ -3,14 +3,14 @@ using Syntriax.Engine.Core;
|
||||||
|
|
||||||
namespace Pong.Behaviours;
|
namespace Pong.Behaviours;
|
||||||
|
|
||||||
public class PongTextBehaviour : TextBehaviour
|
public class TextScoreBehaviour : TextBehaviour
|
||||||
{
|
{
|
||||||
public bool IsLeft { get; }
|
public bool IsLeft { get; }
|
||||||
|
|
||||||
private PongManager? pongManager = null;
|
private PongManagerBehaviour? pongManager = null;
|
||||||
|
|
||||||
public PongTextBehaviour(bool IsLeft) => this.IsLeft = IsLeft;
|
public TextScoreBehaviour(bool IsLeft) => this.IsLeft = IsLeft;
|
||||||
public PongTextBehaviour(bool IsLeft, SpriteFont font) : base(font) => this.IsLeft = IsLeft;
|
public TextScoreBehaviour(bool IsLeft, SpriteFont font) : base(font) => this.IsLeft = IsLeft;
|
||||||
|
|
||||||
protected override void OnFirstActiveFrame()
|
protected override void OnFirstActiveFrame()
|
||||||
{
|
{
|
||||||
|
@ -23,7 +23,7 @@ public class PongTextBehaviour : TextBehaviour
|
||||||
UpdateScores(pongManager);
|
UpdateScores(pongManager);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void UpdateScores(PongManager pongManager)
|
private void UpdateScores(PongManagerBehaviour pongManager)
|
||||||
{
|
{
|
||||||
if (IsLeft)
|
if (IsLeft)
|
||||||
Text = pongManager.ScoreLeft.ToString();
|
Text = pongManager.ScoreLeft.ToString();
|
|
@ -4,7 +4,7 @@ using Syntriax.Engine.Physics2D.Abstract;
|
||||||
|
|
||||||
namespace Pong.Behaviours;
|
namespace Pong.Behaviours;
|
||||||
|
|
||||||
public class ScoreBoundary(Action OnCollision) : BehaviourOverride
|
public class WallScoreBehaviour(Action OnCollision) : BehaviourOverride
|
||||||
{
|
{
|
||||||
private Action OnCollision { get; } = OnCollision;
|
private Action OnCollision { get; } = OnCollision;
|
||||||
|
|
|
@ -26,7 +26,7 @@ public class GamePong : Game
|
||||||
private BehaviourCacher<IDisplayableShape> displayableShapeCacher = null!;
|
private BehaviourCacher<IDisplayableShape> displayableShapeCacher = null!;
|
||||||
private MonoGameCameraBehaviour cameraBehaviour = null!;
|
private MonoGameCameraBehaviour cameraBehaviour = null!;
|
||||||
|
|
||||||
private PongManager pongManager = null!;
|
private PongManagerBehaviour pongManager = null!;
|
||||||
|
|
||||||
private float physicsTimer = 0f;
|
private float physicsTimer = 0f;
|
||||||
|
|
||||||
|
@ -71,7 +71,7 @@ public class GamePong : Game
|
||||||
////////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
IGameObject gameObjectPongManager = gameManager.InstantiateGameObject<GameObject>().SetGameObject("Pong Game Manager");
|
IGameObject gameObjectPongManager = gameManager.InstantiateGameObject<GameObject>().SetGameObject("Pong Game Manager");
|
||||||
pongManager = gameObjectPongManager.BehaviourController.AddBehaviour<PongManager>(5);
|
pongManager = gameObjectPongManager.BehaviourController.AddBehaviour<PongManagerBehaviour>(5);
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
@ -79,7 +79,7 @@ public class GamePong : Game
|
||||||
gameObjectBall.Transform.SetTransform(position: new Vector2D(0, 0f), scale: new Vector2D(10f, 10f));
|
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<CircleBehaviour>(new Circle(Vector2D.Zero, 1f));
|
||||||
gameObjectBall.BehaviourController.AddBehaviour<PongBall>();
|
gameObjectBall.BehaviourController.AddBehaviour<BallBehaviour>();
|
||||||
gameObjectBall.BehaviourController.AddBehaviour<RigidBody2D>();
|
gameObjectBall.BehaviourController.AddBehaviour<RigidBody2D>();
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
@ -87,13 +87,13 @@ public class GamePong : Game
|
||||||
IGameObject gameObjectLeftPaddle = gameManager.InstantiateGameObject<GameObject>().SetGameObject("Left Paddle");
|
IGameObject gameObjectLeftPaddle = gameManager.InstantiateGameObject<GameObject>().SetGameObject("Left Paddle");
|
||||||
gameObjectLeftPaddle.Transform.SetTransform(position: new Vector2D(-468f, 0f), scale: new Vector2D(15f, 60f));
|
gameObjectLeftPaddle.Transform.SetTransform(position: new Vector2D(-468f, 0f), scale: new Vector2D(15f, 60f));
|
||||||
|
|
||||||
gameObjectLeftPaddle.BehaviourController.AddBehaviour<MovementBoxBehaviour>(Keys.W, Keys.S, 228f, -228f, 400f);
|
gameObjectLeftPaddle.BehaviourController.AddBehaviour<PaddleBehaviour>(Keys.W, Keys.S, 228f, -228f, 400f);
|
||||||
gameObjectLeftPaddle.BehaviourController.AddBehaviour<ShapeBehaviour>(Shape.Box);
|
gameObjectLeftPaddle.BehaviourController.AddBehaviour<ShapeBehaviour>(Shape.Box);
|
||||||
gameObjectLeftPaddle.BehaviourController.AddBehaviour<RigidBody2D>().IsStatic = true;
|
gameObjectLeftPaddle.BehaviourController.AddBehaviour<RigidBody2D>().IsStatic = true;
|
||||||
|
|
||||||
IGameObject gameObjectRightPaddle = gameManager.InstantiateGameObject<GameObject>().SetGameObject("Right Paddle");
|
IGameObject gameObjectRightPaddle = gameManager.InstantiateGameObject<GameObject>().SetGameObject("Right Paddle");
|
||||||
gameObjectRightPaddle.Transform.SetTransform(position: new Vector2D(468f, 0f), scale: new Vector2D(15f, 60f));
|
gameObjectRightPaddle.Transform.SetTransform(position: new Vector2D(468f, 0f), scale: new Vector2D(15f, 60f));
|
||||||
gameObjectRightPaddle.BehaviourController.AddBehaviour<MovementBoxBehaviour>(Keys.Up, Keys.Down, 228f, -228f, 400f);
|
gameObjectRightPaddle.BehaviourController.AddBehaviour<PaddleBehaviour>(Keys.Up, Keys.Down, 228f, -228f, 400f);
|
||||||
gameObjectRightPaddle.BehaviourController.AddBehaviour<ShapeBehaviour>(Shape.Box);
|
gameObjectRightPaddle.BehaviourController.AddBehaviour<ShapeBehaviour>(Shape.Box);
|
||||||
gameObjectRightPaddle.BehaviourController.AddBehaviour<RigidBody2D>().IsStatic = true;
|
gameObjectRightPaddle.BehaviourController.AddBehaviour<RigidBody2D>().IsStatic = true;
|
||||||
|
|
||||||
|
@ -111,13 +111,13 @@ public class GamePong : Game
|
||||||
|
|
||||||
IGameObject gameObjectWallRight = gameManager.InstantiateGameObject<GameObject>().SetGameObject("Wall Right");
|
IGameObject gameObjectWallRight = gameManager.InstantiateGameObject<GameObject>().SetGameObject("Wall Right");
|
||||||
gameObjectWallRight.Transform.SetTransform(position: new Vector2D(532f, 0f), scale: new Vector2D(20f, 328f));
|
gameObjectWallRight.Transform.SetTransform(position: new Vector2D(532f, 0f), scale: new Vector2D(20f, 328f));
|
||||||
gameObjectWallRight.BehaviourController.AddBehaviour<ScoreBoundary>((Action)pongManager.ScoreToLeft);
|
gameObjectWallRight.BehaviourController.AddBehaviour<PongScoreWall>((Action)pongManager.ScoreToLeft);
|
||||||
gameObjectWallRight.BehaviourController.AddBehaviour<ShapeBehaviour>(Shape.Box);
|
gameObjectWallRight.BehaviourController.AddBehaviour<ShapeBehaviour>(Shape.Box);
|
||||||
gameObjectWallRight.BehaviourController.AddBehaviour<RigidBody2D>().IsStatic = true;
|
gameObjectWallRight.BehaviourController.AddBehaviour<RigidBody2D>().IsStatic = true;
|
||||||
|
|
||||||
IGameObject gameObjectWallLeft = gameManager.InstantiateGameObject<GameObject>().SetGameObject("Wall Left");
|
IGameObject gameObjectWallLeft = gameManager.InstantiateGameObject<GameObject>().SetGameObject("Wall Left");
|
||||||
gameObjectWallLeft.Transform.SetTransform(position: new Vector2D(-532f, 0f), scale: new Vector2D(20f, 328f));
|
gameObjectWallLeft.Transform.SetTransform(position: new Vector2D(-532f, 0f), scale: new Vector2D(20f, 328f));
|
||||||
gameObjectWallLeft.BehaviourController.AddBehaviour<ScoreBoundary>((Action)pongManager.ScoreToRight);
|
gameObjectWallLeft.BehaviourController.AddBehaviour<PongScoreWall>((Action)pongManager.ScoreToRight);
|
||||||
gameObjectWallLeft.BehaviourController.AddBehaviour<ShapeBehaviour>(Shape.Box);
|
gameObjectWallLeft.BehaviourController.AddBehaviour<ShapeBehaviour>(Shape.Box);
|
||||||
gameObjectWallLeft.BehaviourController.AddBehaviour<RigidBody2D>().IsStatic = true;
|
gameObjectWallLeft.BehaviourController.AddBehaviour<RigidBody2D>().IsStatic = true;
|
||||||
|
|
||||||
|
@ -125,11 +125,11 @@ public class GamePong : Game
|
||||||
|
|
||||||
IGameObject gameObjectLeftScoreText = gameManager.InstantiateGameObject<GameObject>().SetGameObject("Score Left");
|
IGameObject gameObjectLeftScoreText = gameManager.InstantiateGameObject<GameObject>().SetGameObject("Score Left");
|
||||||
gameObjectLeftScoreText.Transform.SetTransform(position: new Vector2D(-250f, 250f), scale: Vector2D.One * .25f);
|
gameObjectLeftScoreText.Transform.SetTransform(position: new Vector2D(-250f, 250f), scale: Vector2D.One * .25f);
|
||||||
gameObjectLeftScoreText.BehaviourController.AddBehaviour<PongTextBehaviour>(true, spriteFont);
|
gameObjectLeftScoreText.BehaviourController.AddBehaviour<TextScoreBehaviour>(true, spriteFont);
|
||||||
|
|
||||||
IGameObject gameObjectRightScoreText = gameManager.InstantiateGameObject<GameObject>().SetGameObject("Score Right");
|
IGameObject gameObjectRightScoreText = gameManager.InstantiateGameObject<GameObject>().SetGameObject("Score Right");
|
||||||
gameObjectRightScoreText.Transform.SetTransform(position: new Vector2D(250f, 250f), scale: Vector2D.One * .25f);
|
gameObjectRightScoreText.Transform.SetTransform(position: new Vector2D(250f, 250f), scale: Vector2D.One * .25f);
|
||||||
gameObjectRightScoreText.BehaviourController.AddBehaviour<PongTextBehaviour>(false, spriteFont);
|
gameObjectRightScoreText.BehaviourController.AddBehaviour<TextScoreBehaviour>(false, spriteFont);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override void Update(GameTime gameTime)
|
protected override void Update(GameTime gameTime)
|
||||||
|
|
Loading…
Reference in New Issue