refactor: Constructor Placements

This commit is contained in:
Syntriax 2024-01-31 12:51:23 +03:00
parent e7ee460323
commit d66b119119
9 changed files with 40 additions and 37 deletions

View File

@ -15,13 +15,6 @@ public class BallBehaviour : BehaviourOverride
private readonly Random random = new(); private readonly Random random = new();
private IRigidBody2D rigidBody = null!; private IRigidBody2D rigidBody = null!;
public BallBehaviour(Vector2D startDirection, float speed)
{
StartDirection = Vector2D.Normalize(startDirection);
Speed = speed;
}
public BallBehaviour() { }
protected override void OnFirstActiveFrame() protected override void OnFirstActiveFrame()
{ {
if (!BehaviourController.TryGetBehaviour(out IRigidBody2D? foundRigidBody)) if (!BehaviourController.TryGetBehaviour(out IRigidBody2D? foundRigidBody))
@ -78,4 +71,11 @@ public class BallBehaviour : BehaviourOverride
else else
rigidBody.Velocity = rigidBody.Velocity.Reflect(information.Normal); rigidBody.Velocity = rigidBody.Velocity.Reflect(information.Normal);
} }
public BallBehaviour() { }
public BallBehaviour(Vector2D startDirection, float speed)
{
StartDirection = Vector2D.Normalize(startDirection);
Speed = speed;
}
} }

View File

@ -9,11 +9,6 @@ namespace Pong.Behaviours;
public class CircleBehaviour : Syntriax.Engine.Physics2D.Collider2DCircleBehaviour, IDisplayableShape 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 Color Color { get; set; } = Color.White;
public float Thickness { get; set; } = .5f; 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); 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; }
} }

View File

@ -3,6 +3,7 @@ using Microsoft.Xna.Framework.Graphics;
using Syntriax.Engine.Core; using Syntriax.Engine.Core;
namespace Pong.Behaviours; namespace Pong.Behaviours;
public class MonoGameCameraBehaviour(GraphicsDeviceManager Graphics) : BehaviourOverride public class MonoGameCameraBehaviour(GraphicsDeviceManager Graphics) : BehaviourOverride
{ {
public System.Action<MonoGameCameraBehaviour>? OnMatrixTransformChanged { get; set; } = null; public System.Action<MonoGameCameraBehaviour>? OnMatrixTransformChanged { get; set; } = null;
@ -12,9 +13,10 @@ public class MonoGameCameraBehaviour(GraphicsDeviceManager Graphics) : Behaviour
private Matrix _matrixTransform = Matrix.Identity; private Matrix _matrixTransform = Matrix.Identity;
private Viewport _viewport = default; private Viewport _viewport = default;
public GraphicsDeviceManager Graphics { get; } = Graphics;
private float _zoom = 1f; private float _zoom = 1f;
public GraphicsDeviceManager Graphics { get; } = Graphics;
public Matrix MatrixTransform public Matrix MatrixTransform
{ {
get => _matrixTransform; get => _matrixTransform;

View File

@ -5,6 +5,7 @@ using Syntriax.Engine.Physics2D;
using Syntriax.Engine.Physics2D.Abstract; using Syntriax.Engine.Physics2D.Abstract;
namespace Pong.Behaviours; namespace Pong.Behaviours;
public class MovementBallBehaviour : BehaviourOverride public class MovementBallBehaviour : BehaviourOverride
{ {
public Vector2D StartDirection { get; private set; } = Vector2D.Zero; public Vector2D StartDirection { get; private set; } = Vector2D.Zero;
@ -13,13 +14,6 @@ public class MovementBallBehaviour : BehaviourOverride
private IRigidBody2D rigidBody = null!; private IRigidBody2D rigidBody = null!;
public MovementBallBehaviour(Vector2D startDirection, float speed)
{
StartDirection = Vector2D.Normalize(startDirection);
Speed = speed;
}
public MovementBallBehaviour() { }
protected override void OnFirstActiveFrame() protected override void OnFirstActiveFrame()
{ {
if (!BehaviourController.TryGetBehaviour(out IRigidBody2D? foundRigidBody)) if (!BehaviourController.TryGetBehaviour(out IRigidBody2D? foundRigidBody))
@ -44,10 +38,16 @@ public class MovementBallBehaviour : BehaviourOverride
private void OnCollisionDetected(ICollider2D collider2D, CollisionDetectionInformation information) 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) 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; rigidBody.Velocity = information.Left.Transform.Position.FromTo(information.Right.Transform.Position).Normalized * rigidBody.Velocity.Magnitude;
else else
rigidBody.Velocity = rigidBody.Velocity.Reflect(information.Normal); rigidBody.Velocity = rigidBody.Velocity.Reflect(information.Normal);
} }
public MovementBallBehaviour() { }
public MovementBallBehaviour(Vector2D startDirection, float speed)
{
StartDirection = Vector2D.Normalize(startDirection);
Speed = speed;
}
} }

View File

@ -14,11 +14,6 @@ public class ShapeAABBBehaviour : BehaviourOverride, IDisplayableShape
{ {
private IShapeCollider2D? shapeCollider = null; 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 Color Color { get; set; } = Color.White;
public float Thickness { get; set; } = .5f; public float Thickness { get; set; } = .5f;
public bool display = true; 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); 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; }
} }

View File

@ -9,11 +9,6 @@ namespace Pong.Behaviours;
public class ShapeBehaviour : Syntriax.Engine.Physics2D.Collider2DShapeBehaviour, IDisplayableShape 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 Color Color { get; set; } = Color.White;
public float Thickness { get; set; } = .5f; 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[i].ToDisplayVector2(), ShapeWorld[i + 1].ToDisplayVector2(), Thickness, Color, Color);
shapeBatch.DrawLine(ShapeWorld[0].ToDisplayVector2(), ShapeWorld[^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; }
} }

View File

@ -8,9 +8,6 @@ namespace Pong.Behaviours;
public class TextBehaviour : BehaviourOverride, IDisplayableSprite public class TextBehaviour : BehaviourOverride, IDisplayableSprite
{ {
public TextBehaviour() { }
public TextBehaviour(SpriteFont font) => Font = font;
public SpriteFont? Font { get; set; } = null; public SpriteFont? Font { get; set; } = null;
public int Size { get; set; } = 16; public int Size { get; set; } = 16;
public string Text { get; set; } = string.Empty; 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); 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;
} }

View File

@ -9,9 +9,6 @@ public class TextScoreBehaviour : TextBehaviour
private PongManagerBehaviour? pongManager = null; 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() protected override void OnFirstActiveFrame()
{ {
if (!GameObject.GameManager.TryFindBehaviour(out pongManager)) if (!GameObject.GameManager.TryFindBehaviour(out pongManager))
@ -30,4 +27,7 @@ public class TextScoreBehaviour : TextBehaviour
else else
Text = pongManager.ScoreRight.ToString(); Text = pongManager.ScoreRight.ToString();
} }
public TextScoreBehaviour(bool IsLeft) => this.IsLeft = IsLeft;
public TextScoreBehaviour(bool IsLeft, SpriteFont font) : base(font) => this.IsLeft = IsLeft;
} }

View File

@ -1,4 +1,5 @@
using System; using System;
using Syntriax.Engine.Core; using Syntriax.Engine.Core;
using Syntriax.Engine.Physics2D.Abstract; using Syntriax.Engine.Physics2D.Abstract;