19 lines
627 B
C#
19 lines
627 B
C#
using Godot;
|
|
using Godot.Collections;
|
|
|
|
public partial class Ball : RigidBody2D {
|
|
[Export] private AudioStreamPlayer audioStreamPlayer;
|
|
[Export] private Array<AudioStream> sounds;
|
|
private Vector2 velocity;
|
|
|
|
public override void _Ready() => velocity = new(500, GD.RandRange(-100, 100));
|
|
|
|
public override void _PhysicsProcess(double delta) {
|
|
KinematicCollision2D collision = MoveAndCollide(velocity * (float)delta);
|
|
if (collision != null) {
|
|
velocity = velocity.Bounce(collision.GetNormal());
|
|
audioStreamPlayer.Stream = collision.GetCollider() is Player ? sounds[1] : sounds[0];
|
|
audioStreamPlayer.Play();
|
|
}
|
|
}
|
|
} |