feat: Basic Play Area
This commit is contained in:
@@ -6,9 +6,10 @@ using Syntriax.Engine.Input;
|
||||
|
||||
namespace Pong.Behaviours;
|
||||
|
||||
public class MovementBallBehaviour(Vector2 StartDirection, float Speed) : BehaviourOverride
|
||||
public class MovementBallBehaviour(Vector2 StartDirection, PlayAreaBehaviour PlayAreaBehaviour, float Speed) : BehaviourOverride
|
||||
{
|
||||
public Vector2 StartDirection { get; } = StartDirection;
|
||||
public Vector2 StartDirection { get; private set; } = StartDirection;
|
||||
public PlayAreaBehaviour PlayAreaBehaviour { get; } = PlayAreaBehaviour;
|
||||
public float Speed { get; set; } = Speed;
|
||||
|
||||
protected override void OnInitialize() => StartDirection.Normalize();
|
||||
@@ -16,5 +17,29 @@ public class MovementBallBehaviour(Vector2 StartDirection, float Speed) : Behavi
|
||||
protected override void OnUpdate(GameTime time)
|
||||
{
|
||||
GameObject.Transform.Position += StartDirection * (time.ElapsedGameTime.Nanoseconds * .001f) * Speed;
|
||||
|
||||
float absY = MathF.Abs(GameObject.Transform.Position.Y);
|
||||
float differenceY = absY - PlayAreaBehaviour.PlayArea.Y * 0.5f;
|
||||
if (differenceY > 0f)
|
||||
{
|
||||
if (GameObject.Transform.Position.Y > 0f)
|
||||
GameObject.Transform.Position -= Vector2.UnitY * differenceY * 2f;
|
||||
else
|
||||
GameObject.Transform.Position += Vector2.UnitY * differenceY * 2f;
|
||||
|
||||
StartDirection = new(StartDirection.X, -StartDirection.Y);
|
||||
}
|
||||
|
||||
float absX = MathF.Abs(GameObject.Transform.Position.X);
|
||||
float differenceX = absX - PlayAreaBehaviour.PlayArea.X * 0.5f;
|
||||
if (differenceX > 0f)
|
||||
{
|
||||
if (GameObject.Transform.Position.X > 0f)
|
||||
GameObject.Transform.Position -= Vector2.UnitX * differenceX * 2f;
|
||||
else
|
||||
GameObject.Transform.Position += Vector2.UnitX * differenceX * 2f;
|
||||
|
||||
StartDirection = new(-StartDirection.X, StartDirection.Y);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
26
Game/Behaviours/PlayAreaBehaviour.cs
Normal file
26
Game/Behaviours/PlayAreaBehaviour.cs
Normal file
@@ -0,0 +1,26 @@
|
||||
using System;
|
||||
using Microsoft.Xna.Framework;
|
||||
using Syntriax.Engine.Core;
|
||||
|
||||
namespace Pong.Behaviours;
|
||||
|
||||
public class PlayAreaBehaviour : BehaviourOverride
|
||||
{
|
||||
public Action<PlayAreaBehaviour>? OnPlayAreaChanged { get; set; } = null;
|
||||
|
||||
private Vector2 _playArea = Vector2.Zero;
|
||||
|
||||
public Vector2 PlayArea
|
||||
{
|
||||
get => _playArea;
|
||||
set
|
||||
{
|
||||
if (_playArea == value)
|
||||
return;
|
||||
|
||||
_playArea = value;
|
||||
OnPlayAreaChanged?.Invoke(this);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user