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

167 lines
5.1 KiB
GDScript3
Raw Normal View History

2024-02-15 05:04:26 +00:00
extends CharacterBody2D
class_name GolfBall
2024-03-15 18:57:15 +00:00
@export_category("References")
## The [PuttController] that this node should poll.
@export var putt_controller: PuttController
## The [HoleController] that this node should poll.
@export var hole_controller: HoleController
## The [AudioStreamPlayer2D] that this node should play when entering the hole.
@export var hole_sound: AudioStreamPlayer2D
## The [Label] where the name of this player should be displayed.
@export var player_label: Label
@export_category("Physics")
2024-02-15 05:04:26 +00:00
## Dynamic friction subtracted from the body's velocity on each physics step.
@export var physics_friction: float
2024-03-15 18:57:15 +00:00
2024-02-15 05:04:26 +00:00
## The maximum number of bounces that the collision algorithm will consider in a single physics step.
@export var physics_max_bounces: float
2024-03-15 18:57:15 +00:00
2024-02-15 05:04:26 +00:00
## A multiplier applied to the body's velocity every time it collides with something.
@export var physics_bounce_coefficient: float
2024-03-15 18:57:15 +00:00
2024-02-15 05:04:26 +00:00
## The scene to instantiate to play the collision sound
@export var collision_sound: PackedScene
2024-03-15 18:57:15 +00:00
@export_category("Sounds")
2024-02-15 05:04:26 +00:00
## Curve mapping relative putt power to putt sound volume.
@export var collision_volume_db: Curve
2024-03-15 18:57:15 +00:00
2024-02-15 05:04:26 +00:00
## The velocity at which the maximum volume of [member collision_volume_db] is played.
@export var collision_volume_max_velocity: float
2024-03-15 18:57:15 +00:00
2024-02-16 03:26:01 +00:00
## Emitted when the ball enters the hole.
signal entered_hole(strokes: int)
2024-03-15 18:57:15 +00:00
2024-02-16 03:26:01 +00:00
## How many strokes have been performed so far.
var strokes: int = 0
2024-03-15 18:57:15 +00:00
2024-02-16 03:26:01 +00:00
## Whether the ball has entered the hole.
var in_hole: bool = false
2024-02-15 05:04:26 +00:00
## The name of the player represented by this scene.
var player_name: String = "Player":
get:
return player_name
set(value):
player_name = value
if player_label:
player_label.text = value
## The color of the player represented by this scene.
var player_color: Color = Color.WHITE:
get:
return player_color
set(value):
player_color = value
modulate = value
2024-02-15 05:04:26 +00:00
func _on_putt(putt_vector: Vector2) -> void:
2024-02-16 03:26:01 +00:00
strokes += 1
2024-02-15 05:04:26 +00:00
velocity += putt_vector
func do_movement(delta: float) -> void:
2024-02-16 03:26:01 +00:00
# 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-03-15 18:57:15 +00:00
## Reduce [field velocity] by [field physics_friction], taking the [param delta] into account.
func apply_friction(delta: float) -> void:
2024-02-16 03:26:01 +00:00
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
2024-03-15 18:57:15 +00:00
## Return whether this object can be considered stopped or not.
2024-02-16 03:26:01 +00:00
func check_has_stopped() -> bool:
return velocity.length() == 0.0
2024-03-15 18:57:15 +00:00
## Return whether this object will enter the hole on this frame or not.
2024-02-16 03:26:01 +00:00
func check_has_entered_hole() -> bool:
return check_has_stopped() and hole_controller.over_hole
2024-03-15 18:57:15 +00:00
@rpc("authority", "call_local", "reliable")
2024-03-18 05:08:31 +00:00
func rpc_do_enter_hole():
enter_hole()
func enter_hole():
2024-02-16 03:26:01 +00:00
in_hole = true
2024-03-18 05:08:31 +00:00
hide()
2024-03-16 04:56:10 +00:00
putt_controller.can_putt = false
2024-03-16 04:55:02 +00:00
if not multiplayer.is_server():
hole_sound.play()
2024-02-16 03:26:01 +00:00
entered_hole.emit(strokes)
2024-03-15 18:57:15 +00:00
## Push this object's [field global_position] to all other clients.
@rpc("authority", "call_remote", "unreliable_ordered")
2024-03-18 05:08:31 +00:00
func rpc_set_global_position(nposition: Vector2):
2024-03-15 18:57:15 +00:00
global_position = nposition
2024-02-16 03:26:01 +00:00
func _ready() -> void:
player_label.text = player_name
func _physics_process(delta) -> void:
2024-03-16 04:25:23 +00:00
if not multiplayer.is_server() and is_multiplayer_authority():
2024-03-15 18:57:15 +00:00
if not in_hole:
do_movement(delta)
2024-03-18 05:08:31 +00:00
rpc_set_global_position.rpc(global_position)
2024-03-15 18:57:15 +00:00
apply_friction(delta)
if check_has_entered_hole():
2024-03-18 05:08:31 +00:00
rpc_do_enter_hole.rpc()
2024-03-15 18:57:15 +00:00
if check_has_stopped():
putt_controller.can_putt = true