refactor: Renamed Behaviours

This commit is contained in:
Syntriax 2024-01-31 12:17:43 +03:00
parent 8929d3282f
commit ff3202eda9
6 changed files with 30 additions and 29 deletions

View File

@ -6,7 +6,7 @@ using Syntriax.Engine.Physics2D.Abstract;
namespace Pong.Behaviours;
public class PongBall : BehaviourOverride
public class BallBehaviour : BehaviourOverride
{
public Vector2D StartDirection { get; private set; } = Vector2D.Zero;
public float Speed { get; set; } = 500f;
@ -14,12 +14,12 @@ public class PongBall : BehaviourOverride
private IRigidBody2D rigidBody = null!;
public PongBall(Vector2D startDirection, float speed)
public BallBehaviour(Vector2D startDirection, float speed)
{
StartDirection = Vector2D.Normalize(startDirection);
Speed = speed;
}
public PongBall() { }
public BallBehaviour() { }
protected override void OnFirstActiveFrame()
{
@ -32,7 +32,7 @@ public class PongBall : BehaviourOverride
rigidBody = foundRigidBody;
if (GameObject.GameManager.TryFindBehaviour(out PongManager? pongManager))
if (GameObject.GameManager.TryFindBehaviour(out PongManagerBehaviour? pongManager))
{
pongManager.OnReset += 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;
rigidBody.Velocity = Vector2D.Zero;
}
private void ResetBall(PongManager pongManager)
private void ResetBall(PongManagerBehaviour pongManager)
{
StateEnable.Enabled = true;
BehaviourController.GameObject.Transform.Position = Vector2D.Zero;

View File

@ -1,12 +1,13 @@
using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using Syntriax.Engine.Core;
using Syntriax.Engine.Input;
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 Down { get; } = Down;

View File

@ -6,11 +6,11 @@ using Syntriax.Engine.Core;
namespace Pong.Behaviours;
public class PongManager : BehaviourOverride
public class PongManagerBehaviour : BehaviourOverride
{
public Action<PongManager>? OnReset { get; set; } = null;
public Action<PongManager>? OnFinished { get; set; } = null;
public Action<PongManager>? OnScored { get; set; } = null;
public Action<PongManagerBehaviour>? OnReset { get; set; } = null;
public Action<PongManagerBehaviour>? OnFinished { get; set; } = null;
public Action<PongManagerBehaviour>? OnScored { get; set; } = null;
public int ScoreLeft { 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 PongManager() => WinScore = 5;
public PongManager(int winScore) => WinScore = winScore;
public PongManagerBehaviour() => WinScore = 5;
public PongManagerBehaviour(int winScore) => WinScore = winScore;
protected override void OnFirstActiveFrame()
{

View File

@ -3,14 +3,14 @@ using Syntriax.Engine.Core;
namespace Pong.Behaviours;
public class PongTextBehaviour : TextBehaviour
public class TextScoreBehaviour : TextBehaviour
{
public bool IsLeft { get; }
private PongManager? pongManager = null;
private PongManagerBehaviour? pongManager = null;
public PongTextBehaviour(bool IsLeft) => this.IsLeft = IsLeft;
public PongTextBehaviour(bool IsLeft, SpriteFont font) : base(font) => this.IsLeft = IsLeft;
public TextScoreBehaviour(bool IsLeft) => this.IsLeft = IsLeft;
public TextScoreBehaviour(bool IsLeft, SpriteFont font) : base(font) => this.IsLeft = IsLeft;
protected override void OnFirstActiveFrame()
{
@ -23,7 +23,7 @@ public class PongTextBehaviour : TextBehaviour
UpdateScores(pongManager);
}
private void UpdateScores(PongManager pongManager)
private void UpdateScores(PongManagerBehaviour pongManager)
{
if (IsLeft)
Text = pongManager.ScoreLeft.ToString();

View File

@ -4,7 +4,7 @@ using Syntriax.Engine.Physics2D.Abstract;
namespace Pong.Behaviours;
public class ScoreBoundary(Action OnCollision) : BehaviourOverride
public class WallScoreBehaviour(Action OnCollision) : BehaviourOverride
{
private Action OnCollision { get; } = OnCollision;

View File

@ -26,7 +26,7 @@ public class GamePong : Game
private BehaviourCacher<IDisplayableShape> displayableShapeCacher = null!;
private MonoGameCameraBehaviour cameraBehaviour = null!;
private PongManager pongManager = null!;
private PongManagerBehaviour pongManager = null!;
private float physicsTimer = 0f;
@ -71,7 +71,7 @@ public class GamePong : Game
////////////////////////////////////////////////////////////////////////////////////
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.BehaviourController.AddBehaviour<CircleBehaviour>(new Circle(Vector2D.Zero, 1f));
gameObjectBall.BehaviourController.AddBehaviour<PongBall>();
gameObjectBall.BehaviourController.AddBehaviour<BallBehaviour>();
gameObjectBall.BehaviourController.AddBehaviour<RigidBody2D>();
////////////////////////////////////////////////////////////////////////////////////
@ -87,13 +87,13 @@ public class GamePong : Game
IGameObject gameObjectLeftPaddle = gameManager.InstantiateGameObject<GameObject>().SetGameObject("Left Paddle");
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<RigidBody2D>().IsStatic = true;
IGameObject gameObjectRightPaddle = gameManager.InstantiateGameObject<GameObject>().SetGameObject("Right Paddle");
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<RigidBody2D>().IsStatic = true;
@ -111,13 +111,13 @@ public class GamePong : Game
IGameObject gameObjectWallRight = gameManager.InstantiateGameObject<GameObject>().SetGameObject("Wall Right");
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<RigidBody2D>().IsStatic = true;
IGameObject gameObjectWallLeft = gameManager.InstantiateGameObject<GameObject>().SetGameObject("Wall Left");
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<RigidBody2D>().IsStatic = true;
@ -125,11 +125,11 @@ public class GamePong : Game
IGameObject gameObjectLeftScoreText = gameManager.InstantiateGameObject<GameObject>().SetGameObject("Score Left");
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");
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)