From d66b11911913eccb3de8214e58f9e427cbd53822 Mon Sep 17 00:00:00 2001 From: Syntriax Date: Wed, 31 Jan 2024 12:51:23 +0300 Subject: [PATCH] refactor: Constructor Placements --- Game/Behaviours/BallBehaviour.cs | 14 +++++++------- Game/Behaviours/CircleBehaviour.cs | 10 +++++----- Game/Behaviours/MonoGameCameraBehaviour.cs | 4 +++- Game/Behaviours/MovementBallBehaviour.cs | 16 ++++++++-------- Game/Behaviours/ShapeAABBBehaviour.cs | 10 +++++----- Game/Behaviours/ShapeBehaviour.cs | 10 +++++----- Game/Behaviours/TextBehaviour.cs | 6 +++--- Game/Behaviours/TextScoreBehaviour.cs | 6 +++--- Game/Behaviours/WallScoreBehaviour.cs | 1 + 9 files changed, 40 insertions(+), 37 deletions(-) diff --git a/Game/Behaviours/BallBehaviour.cs b/Game/Behaviours/BallBehaviour.cs index 95377d3..d8800bc 100644 --- a/Game/Behaviours/BallBehaviour.cs +++ b/Game/Behaviours/BallBehaviour.cs @@ -15,13 +15,6 @@ public class BallBehaviour : BehaviourOverride private readonly Random random = new(); private IRigidBody2D rigidBody = null!; - public BallBehaviour(Vector2D startDirection, float speed) - { - StartDirection = Vector2D.Normalize(startDirection); - Speed = speed; - } - public BallBehaviour() { } - protected override void OnFirstActiveFrame() { if (!BehaviourController.TryGetBehaviour(out IRigidBody2D? foundRigidBody)) @@ -78,4 +71,11 @@ public class BallBehaviour : BehaviourOverride else rigidBody.Velocity = rigidBody.Velocity.Reflect(information.Normal); } + + public BallBehaviour() { } + public BallBehaviour(Vector2D startDirection, float speed) + { + StartDirection = Vector2D.Normalize(startDirection); + Speed = speed; + } } diff --git a/Game/Behaviours/CircleBehaviour.cs b/Game/Behaviours/CircleBehaviour.cs index 0593d1a..ae224ff 100644 --- a/Game/Behaviours/CircleBehaviour.cs +++ b/Game/Behaviours/CircleBehaviour.cs @@ -9,11 +9,6 @@ namespace Pong.Behaviours; public class CircleBehaviour : Syntriax.Engine.Physics2D.Collider2DCircleBehaviour, IDisplayableShape { - public CircleBehaviour(Circle circle) : base(circle) { } - public CircleBehaviour(Circle circle, float thickness) : base(circle) { Thickness = thickness; } - public CircleBehaviour(Circle circle, Color color) : base(circle) { Color = color; } - public CircleBehaviour(Circle circle, Color color, float thickness) : base(circle) { Thickness = thickness; Color = color; } - public Color Color { get; set; } = Color.White; public float Thickness { get; set; } = .5f; @@ -26,4 +21,9 @@ public class CircleBehaviour : Syntriax.Engine.Physics2D.Collider2DCircleBehavio shapeBatch.BorderCircle(CircleWorld.Center.ToDisplayVector2(), CircleWorld.Radius, Color); } + + public CircleBehaviour(Circle circle) : base(circle) { } + public CircleBehaviour(Circle circle, float thickness) : base(circle) { Thickness = thickness; } + public CircleBehaviour(Circle circle, Color color) : base(circle) { Color = color; } + public CircleBehaviour(Circle circle, Color color, float thickness) : base(circle) { Thickness = thickness; Color = color; } } diff --git a/Game/Behaviours/MonoGameCameraBehaviour.cs b/Game/Behaviours/MonoGameCameraBehaviour.cs index 4a0501d..63e57b3 100644 --- a/Game/Behaviours/MonoGameCameraBehaviour.cs +++ b/Game/Behaviours/MonoGameCameraBehaviour.cs @@ -3,6 +3,7 @@ using Microsoft.Xna.Framework.Graphics; using Syntriax.Engine.Core; namespace Pong.Behaviours; + public class MonoGameCameraBehaviour(GraphicsDeviceManager Graphics) : BehaviourOverride { public System.Action? OnMatrixTransformChanged { get; set; } = null; @@ -12,9 +13,10 @@ public class MonoGameCameraBehaviour(GraphicsDeviceManager Graphics) : Behaviour private Matrix _matrixTransform = Matrix.Identity; private Viewport _viewport = default; - public GraphicsDeviceManager Graphics { get; } = Graphics; private float _zoom = 1f; + public GraphicsDeviceManager Graphics { get; } = Graphics; + public Matrix MatrixTransform { get => _matrixTransform; diff --git a/Game/Behaviours/MovementBallBehaviour.cs b/Game/Behaviours/MovementBallBehaviour.cs index 56f7d9d..6998aeb 100644 --- a/Game/Behaviours/MovementBallBehaviour.cs +++ b/Game/Behaviours/MovementBallBehaviour.cs @@ -5,6 +5,7 @@ using Syntriax.Engine.Physics2D; using Syntriax.Engine.Physics2D.Abstract; namespace Pong.Behaviours; + public class MovementBallBehaviour : BehaviourOverride { public Vector2D StartDirection { get; private set; } = Vector2D.Zero; @@ -13,13 +14,6 @@ public class MovementBallBehaviour : BehaviourOverride private IRigidBody2D rigidBody = null!; - public MovementBallBehaviour(Vector2D startDirection, float speed) - { - StartDirection = Vector2D.Normalize(startDirection); - Speed = speed; - } - public MovementBallBehaviour() { } - protected override void OnFirstActiveFrame() { if (!BehaviourController.TryGetBehaviour(out IRigidBody2D? foundRigidBody)) @@ -44,10 +38,16 @@ public class MovementBallBehaviour : BehaviourOverride private void OnCollisionDetected(ICollider2D collider2D, CollisionDetectionInformation information) { - // if (information.Left.BehaviourController.GameObject == GameObject) 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); } + + public MovementBallBehaviour() { } + public MovementBallBehaviour(Vector2D startDirection, float speed) + { + StartDirection = Vector2D.Normalize(startDirection); + Speed = speed; + } } diff --git a/Game/Behaviours/ShapeAABBBehaviour.cs b/Game/Behaviours/ShapeAABBBehaviour.cs index 397861e..391019d 100644 --- a/Game/Behaviours/ShapeAABBBehaviour.cs +++ b/Game/Behaviours/ShapeAABBBehaviour.cs @@ -14,11 +14,6 @@ public class ShapeAABBBehaviour : BehaviourOverride, IDisplayableShape { private IShapeCollider2D? shapeCollider = null; - public ShapeAABBBehaviour() { } - public ShapeAABBBehaviour(float Thickness) { this.Thickness = Thickness; } - public ShapeAABBBehaviour(Color color) { Color = color; } - public ShapeAABBBehaviour(Color color, float Thickness) { this.Thickness = Thickness; Color = color; } - public Color Color { get; set; } = Color.White; public float Thickness { get; set; } = .5f; public bool display = true; @@ -45,4 +40,9 @@ public class ShapeAABBBehaviour : BehaviourOverride, IDisplayableShape shapeBatch.DrawRectangle(aabb.Center.ApplyDisplayScale().Subtract(aabb.SizeHalf).ToVector2(), aabb.Size.ToVector2(), Color.Transparent, Color.Blue); } + + public ShapeAABBBehaviour() { } + public ShapeAABBBehaviour(float Thickness) { this.Thickness = Thickness; } + public ShapeAABBBehaviour(Color color) { Color = color; } + public ShapeAABBBehaviour(Color color, float Thickness) { this.Thickness = Thickness; Color = color; } } diff --git a/Game/Behaviours/ShapeBehaviour.cs b/Game/Behaviours/ShapeBehaviour.cs index 5628134..ffbb52d 100644 --- a/Game/Behaviours/ShapeBehaviour.cs +++ b/Game/Behaviours/ShapeBehaviour.cs @@ -9,11 +9,6 @@ namespace Pong.Behaviours; public class ShapeBehaviour : Syntriax.Engine.Physics2D.Collider2DShapeBehaviour, IDisplayableShape { - 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; @@ -30,4 +25,9 @@ public class ShapeBehaviour : Syntriax.Engine.Physics2D.Collider2DShapeBehaviour shapeBatch.DrawLine(ShapeWorld[i].ToDisplayVector2(), ShapeWorld[i + 1].ToDisplayVector2(), Thickness, Color, Color); shapeBatch.DrawLine(ShapeWorld[0].ToDisplayVector2(), ShapeWorld[^1].ToDisplayVector2(), 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; } } diff --git a/Game/Behaviours/TextBehaviour.cs b/Game/Behaviours/TextBehaviour.cs index df1ee66..0710f20 100644 --- a/Game/Behaviours/TextBehaviour.cs +++ b/Game/Behaviours/TextBehaviour.cs @@ -8,9 +8,6 @@ namespace Pong.Behaviours; public class TextBehaviour : BehaviourOverride, IDisplayableSprite { - public TextBehaviour() { } - public TextBehaviour(SpriteFont font) => Font = font; - public SpriteFont? Font { get; set; } = null; public int Size { get; set; } = 16; public string Text { get; set; } = string.Empty; @@ -22,4 +19,7 @@ public class TextBehaviour : BehaviourOverride, IDisplayableSprite spriteBatch.DrawString(Font, Text, Transform.Position.ToDisplayVector2(), Color.White, Transform.Rotation, Vector2.One * .5f, Transform.Scale.Magnitude, SpriteEffects.None, 0f); } + + public TextBehaviour() { } + public TextBehaviour(SpriteFont font) => Font = font; } diff --git a/Game/Behaviours/TextScoreBehaviour.cs b/Game/Behaviours/TextScoreBehaviour.cs index c896806..452f660 100644 --- a/Game/Behaviours/TextScoreBehaviour.cs +++ b/Game/Behaviours/TextScoreBehaviour.cs @@ -9,9 +9,6 @@ public class TextScoreBehaviour : TextBehaviour private PongManagerBehaviour? pongManager = null; - public TextScoreBehaviour(bool IsLeft) => this.IsLeft = IsLeft; - public TextScoreBehaviour(bool IsLeft, SpriteFont font) : base(font) => this.IsLeft = IsLeft; - protected override void OnFirstActiveFrame() { if (!GameObject.GameManager.TryFindBehaviour(out pongManager)) @@ -30,4 +27,7 @@ public class TextScoreBehaviour : TextBehaviour else Text = pongManager.ScoreRight.ToString(); } + + public TextScoreBehaviour(bool IsLeft) => this.IsLeft = IsLeft; + public TextScoreBehaviour(bool IsLeft, SpriteFont font) : base(font) => this.IsLeft = IsLeft; } diff --git a/Game/Behaviours/WallScoreBehaviour.cs b/Game/Behaviours/WallScoreBehaviour.cs index b9c3c4b..bd8cf05 100644 --- a/Game/Behaviours/WallScoreBehaviour.cs +++ b/Game/Behaviours/WallScoreBehaviour.cs @@ -1,4 +1,5 @@ using System; + using Syntriax.Engine.Core; using Syntriax.Engine.Physics2D.Abstract;