2023-05-19 07:18:38 +00:00
|
|
|
using Godot;
|
2023-05-20 02:42:56 +00:00
|
|
|
using Pong.Scripts.Data;
|
2023-05-19 07:18:38 +00:00
|
|
|
|
|
|
|
namespace Pong.Scripts.Managers;
|
|
|
|
|
|
|
|
public partial class WallManager : Area2D
|
|
|
|
{
|
2023-05-20 02:42:56 +00:00
|
|
|
[Export] internal PlayerNumber PlayerNumber;
|
|
|
|
|
|
|
|
private GodotObject _scoreManager;
|
|
|
|
private StringName _score = "score";
|
|
|
|
|
2023-05-19 07:18:38 +00:00
|
|
|
public override void _Ready()
|
|
|
|
{
|
2023-05-20 02:42:56 +00:00
|
|
|
_scoreManager = GetNode<GodotObject>("/root/Pong/Score");
|
2023-05-19 07:18:38 +00:00
|
|
|
}
|
|
|
|
|
2023-05-20 02:42:56 +00:00
|
|
|
/// <summary>
|
|
|
|
/// code that runs when a ball collides the wall.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="body">the body of the collider.</param>
|
2023-05-19 07:18:38 +00:00
|
|
|
private void OnBodyEntered(Node2D body)
|
|
|
|
{
|
|
|
|
if (body is not Nodes.Ball ball) return;
|
2023-05-20 02:42:56 +00:00
|
|
|
_scoreManager.Call(_score, (long)PlayerNumber);
|
|
|
|
|
2023-05-19 07:18:38 +00:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
}
|