refactor: General

This commit is contained in:
Syntriax 2024-01-30 12:43:30 +03:00
parent 677fec7acc
commit b7d49e41c3
6 changed files with 119 additions and 40 deletions

2
Engine

@ -1 +1 @@
Subproject commit 4000e761a7827bb76d657b10a3341248b90b0aa5 Subproject commit 07666359f26d4761914aac05d73fdbe0020b0b6a

View File

@ -0,0 +1,72 @@
using System;
using Syntriax.Engine.Core;
using Syntriax.Engine.Physics2D;
using Syntriax.Engine.Physics2D.Abstract;
namespace Pong.Behaviours;
public class PongBall : BehaviourOverride
{
public Vector2D StartDirection { get; private set; } = Vector2D.Zero;
public float Speed { get; set; } = 500f;
public float SpeedUpMultiplier { get; set; } = .0125f;
private IRigidBody2D rigidBody = null!;
public PongBall(Vector2D startDirection, float speed)
{
StartDirection = Vector2D.Normalize(startDirection);
Speed = speed;
}
public PongBall() { }
protected override void OnFirstActiveFrame()
{
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}.");
foundCollider.OnCollisionDetected += OnCollisionDetected;
rigidBody = foundRigidBody;
if (GameObject.GameManager.TryFindBehaviour(out PongManager? pongManager))
{
pongManager.OnReset += ResetBall;
pongManager.OnScored += ResetBall;
pongManager.OnFinished += DisableBall;
}
}
private void DisableBall(PongManager pongManager)
{
BehaviourController.GameObject.Transform.Position = Vector2D.Zero;
rigidBody.Velocity = Vector2D.Zero;
}
private void ResetBall(PongManager pongManager)
{
StateEnable.Enabled = true;
BehaviourController.GameObject.Transform.Position = Vector2D.Zero;
rigidBody.Velocity = Vector2D.One.Normalized * Speed;
}
protected override void OnUpdate()
{
if (rigidBody.Velocity.MagnitudeSquared <= 0.01f)
return;
Vector2D speedUp = rigidBody.Velocity.Normalized * (float)Time.Elapsed.TotalMilliseconds;
rigidBody.Velocity += speedUp * SpeedUpMultiplier;
}
private void OnCollisionDetected(ICollider2D collider2D, CollisionDetectionInformation information)
{
if (Syntriax.Engine.Core.Math.Abs(information.Normal.Dot(Vector2D.Right)) > .25)
rigidBody.Velocity = information.Left.Transform.Position.FromTo(information.Right.Transform.Position).Normalized * rigidBody.Velocity.Magnitude;
else
rigidBody.Velocity = rigidBody.Velocity.Reflect(information.Normal);
}
}

View File

@ -3,22 +3,23 @@ using System;
using Microsoft.Xna.Framework.Input; using Microsoft.Xna.Framework.Input;
using Syntriax.Engine.Core; using Syntriax.Engine.Core;
using Syntriax.Engine.Input;
namespace Pong.Behaviours; namespace Pong.Behaviours;
public class PongScoreboard(int WinScore) : BehaviourOverride public class PongManager : BehaviourOverride
{ {
public Action? OnReset { get; set; } = null; public Action<PongManager>? OnReset { get; set; } = null;
public Action? OnFinished { get; set; } = null; public Action<PongManager>? OnFinished { get; set; } = null;
public Action? OnScored { get; set; } = null; public Action<PongManager>? 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;
public int ScoreSum => ScoreLeft + ScoreRight; public int ScoreSum => ScoreLeft + ScoreRight;
public int WinScore { get; } = WinScore; public int WinScore { get; } = 5;
public PongManager() => WinScore = 5;
public PongManager(int winScore) => WinScore = winScore;
protected override void OnFirstActiveFrame() protected override void OnFirstActiveFrame()
{ {
@ -34,7 +35,7 @@ public class PongScoreboard(int WinScore) : BehaviourOverride
public void ScoreToLeft() public void ScoreToLeft()
{ {
ScoreLeft++; ScoreLeft++;
OnScored?.Invoke(); OnScored?.Invoke(this);
CheckFinish(); CheckFinish();
} }
@ -42,7 +43,7 @@ public class PongScoreboard(int WinScore) : BehaviourOverride
public void ScoreToRight() public void ScoreToRight()
{ {
ScoreRight++; ScoreRight++;
OnScored?.Invoke(); OnScored?.Invoke(this);
CheckFinish(); CheckFinish();
} }
@ -50,7 +51,7 @@ public class PongScoreboard(int WinScore) : BehaviourOverride
public void Reset() public void Reset()
{ {
ScoreLeft = ScoreRight = 0; ScoreLeft = ScoreRight = 0;
OnReset?.Invoke(); OnReset?.Invoke(this);
} }
private void CheckFinish() private void CheckFinish()
@ -58,6 +59,6 @@ public class PongScoreboard(int WinScore) : BehaviourOverride
int halfwayScore = (int)(WinScore * .5f); int halfwayScore = (int)(WinScore * .5f);
if (ScoreLeft > halfwayScore || ScoreRight > halfwayScore) if (ScoreLeft > halfwayScore || ScoreRight > halfwayScore)
OnFinished?.Invoke(); OnFinished?.Invoke(this);
} }
} }

View File

@ -0,0 +1,27 @@
using Syntriax.Engine.Core;
namespace Pong.Behaviours;
public class PongTextBehaviour(bool IsLeft) : TextBehaviour
{
public bool IsLeft { get; } = IsLeft;
private PongManager? pongManager = null;
protected override void OnFirstActiveFrame()
{
if (!GameObject.GameManager.TryFindBehaviour(out pongManager))
return;
pongManager.OnScored += UpdateScores;
pongManager.OnReset += UpdateScores;
}
private void UpdateScores(PongManager pongManager)
{
if (IsLeft)
Text = pongManager.ScoreLeft.ToString();
else
Text = pongManager.ScoreRight.ToString();
}
}

View File

@ -1,5 +1,6 @@
using Microsoft.Xna.Framework; using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Graphics;
using Syntriax.Engine.Core; using Syntriax.Engine.Core;
using Syntriax.Engine.Core.Abstract; using Syntriax.Engine.Core.Abstract;

View File

@ -23,7 +23,7 @@ public class Game1 : Game
public static GameManager gameManager = null!; public static GameManager gameManager = null!;
private MonoGameCameraBehaviour cameraBehaviour = null!; private MonoGameCameraBehaviour cameraBehaviour = null!;
private PongScoreboard pongScoreboard; private PongManager pongManager;
public Game1() public Game1()
{ {
@ -72,7 +72,7 @@ public class Game1 : Game
IGameObject gameObjectPongManager = gameManager.InstantiateGameObject<GameObject>(); IGameObject gameObjectPongManager = gameManager.InstantiateGameObject<GameObject>();
gameObjectPongManager.Name = "Pong Game Manager"; gameObjectPongManager.Name = "Pong Game Manager";
pongScoreboard = gameObjectPongManager.BehaviourController.AddBehaviour<PongScoreboard>(5); pongManager = gameObjectPongManager.BehaviourController.AddBehaviour<PongManager>(5);
//////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////
@ -81,7 +81,7 @@ public class Game1 : Game
gameObjectBall.Transform.Position = new Vector2D(0, 0f); gameObjectBall.Transform.Position = new Vector2D(0, 0f);
gameObjectBall.Transform.Scale = new Vector2D(10f, 10f); gameObjectBall.Transform.Scale = new Vector2D(10f, 10f);
gameObjectBall.BehaviourController.AddBehaviour<CircleBehaviour>(new Circle(Vector2D.Zero, 1f)); gameObjectBall.BehaviourController.AddBehaviour<CircleBehaviour>(new Circle(Vector2D.Zero, 1f));
MovementBallBehaviour movementBallBehaviour = gameObjectBall.BehaviourController.AddBehaviour<MovementBallBehaviour>(); PongBall ballBehaviour = gameObjectBall.BehaviourController.AddBehaviour<PongBall>();
RigidBody2D rigidBodyBall = gameObjectBall.BehaviourController.AddBehaviour<RigidBody2D>(); RigidBody2D rigidBodyBall = gameObjectBall.BehaviourController.AddBehaviour<RigidBody2D>();
engine.AddRigidBody(rigidBodyBall); engine.AddRigidBody(rigidBodyBall);
@ -131,7 +131,7 @@ public class Game1 : Game
gameObjectWallRight.Name = "WallRight"; gameObjectWallRight.Name = "WallRight";
gameObjectWallRight.Transform.Position = new Vector2D(532f, 0f); gameObjectWallRight.Transform.Position = new Vector2D(532f, 0f);
gameObjectWallRight.Transform.Scale = new Vector2D(20f, 328f); gameObjectWallRight.Transform.Scale = new Vector2D(20f, 328f);
gameObjectWallRight.BehaviourController.AddBehaviour<ScoreBoundary>((Action)pongScoreboard.ScoreToLeft); gameObjectWallRight.BehaviourController.AddBehaviour<ScoreBoundary>((Action)pongManager.ScoreToLeft);
gameObjectWallRight.BehaviourController.AddBehaviour<ShapeBehaviour>(Shape.Box); gameObjectWallRight.BehaviourController.AddBehaviour<ShapeBehaviour>(Shape.Box);
RigidBody2D rigidBodyWallRight = gameObjectWallRight.BehaviourController.AddBehaviour<RigidBody2D>(); RigidBody2D rigidBodyWallRight = gameObjectWallRight.BehaviourController.AddBehaviour<RigidBody2D>();
rigidBodyWallRight.IsStatic = true; rigidBodyWallRight.IsStatic = true;
@ -141,7 +141,7 @@ public class Game1 : Game
gameObjectWallLeft.Name = "WallLeft"; gameObjectWallLeft.Name = "WallLeft";
gameObjectWallLeft.Transform.Position = new Vector2D(-532f, 0f); gameObjectWallLeft.Transform.Position = new Vector2D(-532f, 0f);
gameObjectWallLeft.Transform.Scale = new Vector2D(20f, 328f); gameObjectWallLeft.Transform.Scale = new Vector2D(20f, 328f);
gameObjectWallLeft.BehaviourController.AddBehaviour<ScoreBoundary>((Action)pongScoreboard.ScoreToRight); gameObjectWallLeft.BehaviourController.AddBehaviour<ScoreBoundary>((Action)pongManager.ScoreToRight);
gameObjectWallLeft.BehaviourController.AddBehaviour<ShapeBehaviour>(Shape.Box); gameObjectWallLeft.BehaviourController.AddBehaviour<ShapeBehaviour>(Shape.Box);
RigidBody2D rigidBodyWallLeft = gameObjectWallLeft.BehaviourController.AddBehaviour<RigidBody2D>(); RigidBody2D rigidBodyWallLeft = gameObjectWallLeft.BehaviourController.AddBehaviour<RigidBody2D>();
rigidBodyWallLeft.IsStatic = true; rigidBodyWallLeft.IsStatic = true;
@ -153,38 +153,16 @@ public class Game1 : Game
gameObjectLeftScoreText.Name = "Score Left"; gameObjectLeftScoreText.Name = "Score Left";
gameObjectLeftScoreText.Transform.Position = new Vector2D(-250f, 250f); gameObjectLeftScoreText.Transform.Position = new Vector2D(-250f, 250f);
gameObjectLeftScoreText.Transform.Scale = Vector2D.One * .25f; gameObjectLeftScoreText.Transform.Scale = Vector2D.One * .25f;
TextBehaviour textBehaviourLeft = gameObjectLeftScoreText.BehaviourController.AddBehaviour<TextBehaviour>(); PongTextBehaviour textBehaviourLeft = gameObjectLeftScoreText.BehaviourController.AddBehaviour<PongTextBehaviour>(true);
textBehaviourLeft.Font = spriteFont; textBehaviourLeft.Font = spriteFont;
textBehaviourLeft.Text = "0"; textBehaviourLeft.Text = "0";
IGameObject gameObjectRightScoreText = gameManager.InstantiateGameObject<GameObject>(); IGameObject gameObjectRightScoreText = gameManager.InstantiateGameObject<GameObject>();
gameObjectRightScoreText.Name = "Score Right"; gameObjectRightScoreText.Name = "Score Right";
gameObjectRightScoreText.Transform.Position = new Vector2D(250f, 250f); gameObjectRightScoreText.Transform.Position = new Vector2D(250f, 250f);
gameObjectRightScoreText.Transform.Scale = Vector2D.One * .25f; gameObjectRightScoreText.Transform.Scale = Vector2D.One * .25f;
TextBehaviour textBehaviourRight = gameObjectRightScoreText.BehaviourController.AddBehaviour<TextBehaviour>(); PongTextBehaviour textBehaviourRight = gameObjectRightScoreText.BehaviourController.AddBehaviour<PongTextBehaviour>(false);
textBehaviourRight.Font = spriteFont; textBehaviourRight.Font = spriteFont;
textBehaviourRight.Text = "0"; textBehaviourRight.Text = "0";
pongScoreboard.OnScored += () =>
{
gameObjectBall.Transform.Position = Vector2D.Zero;
rigidBodyBall.Velocity = rigidBodyBall.Velocity.Normalized * movementBallBehaviour.Speed;
textBehaviourLeft.Text = pongScoreboard.ScoreLeft.ToString();
textBehaviourRight.Text = pongScoreboard.ScoreRight.ToString();
};
pongScoreboard.OnFinished += () =>
{
rigidBodyBall.BehaviourController.GameObject.Transform.Position = Vector2D.Zero;
rigidBodyBall.Velocity = Vector2D.Zero;
};
pongScoreboard.OnReset += () =>
{
gameObjectBall.StateEnable.Enabled = true;
rigidBodyBall.BehaviourController.GameObject.Transform.Position = Vector2D.Zero;
rigidBodyBall.Velocity = Vector2D.One.Normalized * movementBallBehaviour.Speed;
textBehaviourLeft.Text = pongScoreboard.ScoreLeft.ToString();
textBehaviourRight.Text = pongScoreboard.ScoreRight.ToString();
};
} }
protected override void Update(GameTime gameTime) protected override void Update(GameTime gameTime)