1
Fork 0
mirror of https://github.com/Steffo99/nanogolf.git synced 2024-11-25 01:24:18 +00:00
algodist-steffo-nanogolf/scenes/golf_ball.gd

101 lines
3.7 KiB
GDScript3
Raw Normal View History

2024-02-15 05:04:26 +00:00
extends CharacterBody2D
class_name GolfBall
## Dynamic friction subtracted from the body's velocity on each physics step.
@export var physics_friction: float
## The maximum number of bounces that the collision algorithm will consider in a single physics step.
@export var physics_max_bounces: float
## A multiplier applied to the body's velocity every time it collides with something.
@export var physics_bounce_coefficient: float
## The scene to instantiate to play the collision sound
@export var collision_sound: PackedScene
## Curve mapping relative putt power to putt sound volume.
@export var collision_volume_db: Curve
## The velocity at which the maximum volume of [member collision_volume_db] is played.
@export var collision_volume_max_velocity: float
2024-02-16 03:26:01 +00:00
## Emitted when the ball enters the hole.
signal entered_hole(strokes: int)
## How many strokes have been performed so far.
var strokes: int = 0
## Whether the ball has entered the hole.
var in_hole: bool = false
2024-02-15 05:04:26 +00:00
@onready var putt_controller: PuttController = $"PuttController"
2024-02-16 03:26:01 +00:00
@onready var hole_controller: HoleController = $"HoleController"
@onready var hole_sound: AudioStreamPlayer2D = $"HoleSound"
2024-02-15 05:04:26 +00:00
func _on_putt(putt_vector: Vector2):
2024-02-16 03:26:01 +00:00
strokes += 1
2024-02-15 05:04:26 +00:00
velocity += putt_vector
2024-02-16 03:26:01 +00:00
func do_movement(delta: float):
# How much the body should move in this physics step.
var movement = velocity * delta
2024-02-15 05:04:26 +00:00
# How many times the body collided in the current physics step.
var bounces: int = 0
# While the body should still move some space, and it isn't stuck in a perpetual loop of bouncing...
while movement.length() > 0.0 and bounces < physics_max_bounces:
# Try to move!
var collision: KinematicCollision2D = move_and_collide(movement)
# If the body did not collide and performed its full movement, we're done!
if not collision:
break
# Let's try to handle the collision properly
bounces += 1
# Determine the normal of the collision (the direction the body should be pushed back in)
var collision_normal = collision.get_normal()
# Play the collision sound
if bounces == 1:
# Determine with how much speed the body collided
var collision_velocity = -collision_normal.dot(velocity)
# Create a new sound instance
var collision_sound_instance: AudioStreamPlayer2D = collision_sound.instantiate()
# Set the sound volume based on the relative collision velocity
collision_sound_instance.volume_db = min(0, collision_volume_db.sample(collision_velocity / collision_volume_max_velocity))
# Add the sound to the SceneTree so it starts playing
$"..".add_child(collision_sound_instance)
# Set the sound's global position to the current global position of the body
collision_sound_instance.global_position = global_position
# Change the velocity adequately
velocity = velocity.bounce(collision_normal)
# Reflect the remaining movement
movement = collision.get_remainder().bounce(collision_normal)
# If we collided with something in this step, we need to apply the bounce coefficient
if bounces > 0:
velocity *= physics_bounce_coefficient
2024-02-16 03:26:01 +00:00
func apply_friction(delta):
var new_velocity_length = max(0.0, velocity.length() - physics_friction * delta)
2024-02-15 05:04:26 +00:00
velocity = velocity.normalized() * new_velocity_length
2024-02-16 03:26:01 +00:00
func check_has_stopped() -> bool:
return velocity.length() == 0.0
func check_has_entered_hole() -> bool:
return check_has_stopped() and hole_controller.over_hole
func enter_hole():
in_hole = true
visible = false
hole_sound.play()
entered_hole.emit(strokes)
print("[GolfBall] Entered hole in: ", strokes)
func _physics_process(delta):
if not in_hole:
do_movement(delta)
apply_friction(delta)
if check_has_entered_hole():
enter_hole()
if check_has_stopped():
putt_controller.can_putt = true