2023-11-27 12:00:25 +03:00
|
|
|
using System;
|
|
|
|
using Microsoft.Xna.Framework;
|
|
|
|
using Microsoft.Xna.Framework.Input;
|
|
|
|
using Syntriax.Engine.Core;
|
|
|
|
using Syntriax.Engine.Input;
|
|
|
|
|
|
|
|
namespace Pong.Behaviours;
|
|
|
|
|
2023-11-27 17:33:05 +03:00
|
|
|
public class MovementBallBehaviour(Vector2 StartDirection, PlayAreaBehaviour PlayAreaBehaviour, float Speed) : BehaviourOverride
|
2023-11-27 12:00:25 +03:00
|
|
|
{
|
2023-11-27 17:33:05 +03:00
|
|
|
public Vector2 StartDirection { get; private set; } = StartDirection;
|
|
|
|
public PlayAreaBehaviour PlayAreaBehaviour { get; } = PlayAreaBehaviour;
|
2023-11-27 12:00:25 +03:00
|
|
|
public float Speed { get; set; } = Speed;
|
|
|
|
|
|
|
|
protected override void OnInitialize() => StartDirection.Normalize();
|
|
|
|
|
|
|
|
protected override void OnUpdate(GameTime time)
|
|
|
|
{
|
|
|
|
GameObject.Transform.Position += StartDirection * (time.ElapsedGameTime.Nanoseconds * .001f) * Speed;
|
2023-11-27 17:33:05 +03:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
2023-11-27 12:00:25 +03:00
|
|
|
}
|
|
|
|
}
|