Pong/Scripts/Managers/WallManager.cs
Fries 9b0ebcefba walls can reset the ball to its starting position
WallManager binds to the BodyEntered signal on the Area2D class and
detects if the body that entered the area is the ball class, and if it
is, it will reset the ball to its starting position. right now, theres
no scoring system. this will be added later.
2023-05-19 00:18:38 -07:00

29 lines
655 B
C#

using Godot;
namespace Pong.Scripts.Managers;
public partial class WallManager : Area2D
{
public override void _Ready()
{
BodyEntered += OnBodyEntered;
}
private void OnBodyEntered(Node2D body)
{
if (body is not Nodes.Ball ball) return;
ResetBall(ball);
}
/// <summary>
/// reset the ball to the starting position and flick it again.
/// </summary>
/// <param name="ball">a <see cref="Nodes.Ball"/> object that you want to reset.</param>
private async void ResetBall(Nodes.Ball ball)
{
ball.Velocity = Vector2.Zero;
ball.Position = Vector2.Zero;
await ToSignal(GetTree().CreateTimer(0.25), "timeout");
ball.FlickBall();
}
}