Pong/Scripts/Managers/WallCollisionManager.cs
Fries 604d79e654 the paddle can now scale to the resolution
i added a GameArea class which makes it easier to operate on in coordinates bounds with the GameCoordinate struct wrapper which does calculations for me. the GameArea class also has a normalization system which is how the ScalingManager can teleport items in bounds to relatively the same place when resized.

it took quite a while to get the paddle to teleport. looks like im supposed to modify the position inside IntegrateForces instead of PhysicsProcess which is my mistake.
2023-05-23 00:21:54 -07:00

41 lines
1 KiB
C#

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();
}
}