Pong/Scripts/Ball.cs
Fries 02c5b90415 make a basic prototype for a scene.
right now theres just a moving ball, inside a box, with a working paddle. i had to make the ball kinematic and tell the ball to bounce when it hits the other direction.
2023-05-16 23:15:31 -07:00

37 lines
873 B
C#

using Godot;
namespace Pong.Scripts;
public partial class Ball : CharacterBody2D
{
[Export] private double _ballSpeed;
private Vector2 _velocity;
/// <summary>
/// this property multiples the ballSpeed by the Meter constant.
/// </summary>
private double BallSpeed => _ballSpeed * Constants.Meter;
public override void _Ready()
{
_velocity = Vector2.Left * BallSpeed;
}
public override void _PhysicsProcess(double delta)
{
Velocity = _velocity;
CollisionCheck(delta);
}
/// <summary>
/// this method moves the ball and bounces if it collides with something.
/// </summary>
/// <param name="delta">delta time from the _PhysicsProcess method.</param>
private void CollisionCheck(double delta)
{
var collision = MoveAndCollide(_velocity * delta);
if (collision == null) return;
_velocity = _velocity.Bounce(collision.GetNormal());
}
}