Fries
41f7e7b1a0
but the paddles and score text stay where they are. i plan on adding the paddles and score text moving later.
42 lines
1 KiB
C#
42 lines
1 KiB
C#
using Godot;
|
|
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();
|
|
}
|
|
}
|