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;
|
2024-01-27 21:50:28 +03:00
|
|
|
using Syntriax.Engine.Physics2D;
|
2023-12-04 14:06:52 +03:00
|
|
|
using Syntriax.Engine.Physics2D.Abstract;
|
2023-11-27 12:00:25 +03:00
|
|
|
|
|
|
|
namespace Pong.Behaviours;
|
2024-01-23 12:16:58 +03:00
|
|
|
public class MovementBallBehaviour(Vector2D StartDirection, float Speed) : BehaviourOverride
|
2023-11-27 12:00:25 +03:00
|
|
|
{
|
2024-01-23 12:16:58 +03:00
|
|
|
public Vector2D StartDirection { get; private set; } = Vector2D.Normalize(StartDirection);
|
2023-11-27 12:00:25 +03:00
|
|
|
public float Speed { get; set; } = Speed;
|
|
|
|
|
2024-01-27 21:50:28 +03:00
|
|
|
private IRigidBody2D rigidBody = null!;
|
|
|
|
|
2024-01-23 12:16:58 +03:00
|
|
|
protected override void OnFirstActiveFrame()
|
2023-11-27 12:00:25 +03:00
|
|
|
{
|
2024-01-27 21:50:28 +03:00
|
|
|
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}.");
|
|
|
|
|
|
|
|
foundRigidBody.Velocity = StartDirection * Speed;
|
|
|
|
foundCollider.OnCollisionDetected += OnCollisionDetected;
|
2023-12-04 14:06:52 +03:00
|
|
|
|
2024-01-27 21:50:28 +03:00
|
|
|
rigidBody = foundRigidBody;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void OnCollisionDetected(ICollider2D collider2D, CollisionDetectionInformation information)
|
|
|
|
{
|
|
|
|
rigidBody.Velocity = rigidBody.Velocity.Reflect(information.Normal);
|
2023-11-27 12:00:25 +03:00
|
|
|
}
|
2023-12-04 14:06:52 +03:00
|
|
|
|
|
|
|
// 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);
|
|
|
|
// }
|
|
|
|
// }
|
2023-11-27 12:00:25 +03:00
|
|
|
}
|