Pong/Scripts/Managers/WallCollisionManager.cs

42 lines
1 KiB
C#
Raw Permalink Normal View History

using Pong.Scripts.Data;
using Pong.Scripts.Objects;
namespace Pong.Scripts.Managers;
public partial class WallCollisionManager : Area2D
{
[Export] internal PlayerNumber PlayerNumber;
private GodotObject _scoreManager;
private StringName _score = "score";
public override void _Ready()
{
_scoreManager = GetNode<GodotObject>("/root/Pong/Score");
}
/// <summary>
/// code that runs when a ball collides the wall.
/// </summary>
/// <param name="body">the body of the collider.</param>
private void OnBodyEntered(Node2D body)
{
if (body is not Ball ball) return;
_scoreManager.Call(_score, (long)PlayerNumber);
ResetBall(ball);
}
/// <summary>
/// reset the ball to the starting position and flick it again.
/// </summary>
/// <param name="ball">a <see cref="Ball"/> object that you want to reset.</param>
private async void ResetBall(Ball ball)
{
ball.Velocity = Vector2.Zero;
ball.Position = Vector2.Zero;
await ToSignal(GetTree().CreateTimer(0.25), SceneTreeTimer.SignalName.Timeout);
ball.FlickBall();
}
}