using Godot; namespace Pong.Scripts; public partial class Ball : CharacterBody2D { [Export] private double _ballSpeed; [Export] private double _maxRandomAngle; private Vector2 _velocity; /// /// this property multiples the ballSpeed by the Meter constant. /// private double BallSpeed => _ballSpeed * Constants.Meter; public override void _Ready() { // this should dispose when this method goes out of scope (this means after the velocity variable is assigned) using var rng = new RandomNumberGenerator(); // set the velocity currently to the left of the screen with a random y angle. _velocity = Vector2.Left * BallSpeed + new Vector2(0, rng.RandfRange(-_maxRandomAngle, _maxRandomAngle) * BallSpeed); } public override void _PhysicsProcess(double delta) { Velocity = _velocity; CollisionCheck(delta); } /// /// this method moves the ball and bounces if it collides with something. /// /// delta time from the _PhysicsProcess method. private void CollisionCheck(double delta) { var collision = MoveAndCollide(_velocity * delta); if (collision == null) return; _velocity = _velocity.Bounce(collision.GetNormal()); } }