2023-05-19 07:18:38 +00:00
|
|
|
using Godot;
|
2023-05-20 02:42:56 +00:00
|
|
|
using Pong.Scripts.Data;
|
2023-05-21 04:42:38 +00:00
|
|
|
using Pong.Scripts.Objects;
|
2023-05-19 07:18:38 +00:00
|
|
|
|
|
|
|
namespace Pong.Scripts.Managers;
|
|
|
|
|
2023-05-22 02:41:48 +00:00
|
|
|
public partial class WallCollisionManager : Area2D
|
2023-05-19 07:18:38 +00:00
|
|
|
{
|
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)
|
|
|
|
{
|
2023-05-21 04:42:38 +00:00
|
|
|
if (body is not 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>
|
2023-05-21 04:42:38 +00:00
|
|
|
/// <param name="ball">a <see cref="Ball"/> object that you want to reset.</param>
|
|
|
|
private async void ResetBall(Ball ball)
|
2023-05-19 07:18:38 +00:00
|
|
|
{
|
|
|
|
ball.Velocity = Vector2.Zero;
|
|
|
|
ball.Position = Vector2.Zero;
|
2023-05-22 01:19:05 +00:00
|
|
|
await ToSignal(GetTree().CreateTimer(0.25), SceneTreeTimer.SignalName.Timeout);
|
2023-05-19 07:18:38 +00:00
|
|
|
ball.FlickBall();
|
|
|
|
}
|
|
|
|
}
|