1
Fork 0
mirror of https://github.com/Steffo99/nanogolf.git synced 2024-11-28 02:54:19 +00:00
algodist-steffo-nanogolf/scenes/putt_controller.gd

117 lines
3 KiB
GDScript3
Raw Normal View History

2024-02-15 05:04:26 +00:00
extends Node2D
class_name PuttController
2024-03-15 18:57:15 +00:00
@export_category("References")
## The [Sprite2D] used to calculate [field sprite_texture_width] from.
@export var sprite: Sprite2D
## The [AudioStreamPlayer2D] to play when a putt happens.
@export var sound: AudioStreamPlayer2D
@export_category("Physics")
2024-02-15 05:04:26 +00:00
## The maximum impulse that a putt can have.
@export var putt_max_impulse: float
2024-03-15 18:57:15 +00:00
@export_category("Scale")
2024-02-15 05:04:26 +00:00
## How many game units a pixel of screen mouse movement corresponds to.
@export var putt_drag_scale: float
2024-03-15 18:57:15 +00:00
2024-02-15 05:19:18 +00:00
## Length multiplier of the putt ghost
@export var putt_ghost_scale: float
2024-03-15 18:57:15 +00:00
2024-02-15 05:04:26 +00:00
## Curve mapping relative putt impulse to putt sound volume.
@export var putt_volume: Curve
2024-03-15 18:57:15 +00:00
2024-02-15 05:04:26 +00:00
## Emitted when a putt has happened.
signal putt(putt_vector: Vector2)
2024-03-15 18:57:15 +00:00
## The width in pixels that the putt ghost should have.
2024-02-15 05:04:26 +00:00
@onready var sprite_texture_width: float = sprite.texture.get_width()
2024-03-15 18:57:15 +00:00
## Whether a putt is currently in progress of not.
##
## If this is true, then [field drag_start_point] should contain a value.
2024-02-15 05:04:26 +00:00
var is_putting: bool = false
2024-03-15 18:57:15 +00:00
## The position on the screen where the putt has started.
var drag_start_point: Vector2
## Whether a putt can currently be performed or not.
2024-02-15 05:04:26 +00:00
var can_putt: bool = false:
get:
return can_putt
set(value):
can_putt = value
if not value:
is_putting = false
func _input(event: InputEvent):
if can_putt:
2024-03-15 18:57:15 +00:00
if event is InputEventMouseButton and event.button_index == MOUSE_BUTTON_LEFT:
2024-02-15 05:04:26 +00:00
if event.pressed:
if not is_putting:
start_putt(event.position)
else:
2024-02-16 03:26:01 +00:00
push_warning("Attempted to start putt while another was in progress.")
2024-02-15 05:04:26 +00:00
else:
if is_putting:
end_putt(event.position)
else:
2024-02-16 03:26:01 +00:00
push_warning("Attempted to end putt while none was in progress.")
2024-02-15 05:04:26 +00:00
if is_putting:
2024-03-15 18:57:15 +00:00
update_putt_ghost(compute_putt(event.position, drag_start_point))
2024-02-15 05:04:26 +00:00
if event is InputEventMouseMotion:
if is_putting:
2024-03-15 18:57:15 +00:00
update_putt_ghost(compute_putt(event.position, drag_start_point))
2024-02-15 05:04:26 +00:00
func update_putt_ghost(putt_vector: Vector2):
sprite.rotation = putt_vector.angle()
2024-02-15 05:19:18 +00:00
sprite.scale.x = putt_vector.length() * putt_ghost_scale / sprite_texture_width
sprite.position = position - putt_vector * putt_ghost_scale * 0.5
2024-02-15 05:04:26 +00:00
func compute_putt(start_position: Vector2, end_position: Vector2) -> Vector2:
var vector: Vector2 = -(end_position - start_position)
vector *= putt_drag_scale
if vector.length() > putt_max_impulse:
vector = vector.normalized() * putt_max_impulse
return vector
func start_putt(start_position: Vector2):
visible = true
is_putting = true
2024-03-15 18:57:15 +00:00
drag_start_point = start_position
2024-02-15 05:04:26 +00:00
func end_putt(end_position: Vector2):
visible = false
is_putting = false
can_putt = false
2024-03-15 18:57:15 +00:00
var putt_vector = compute_putt(drag_start_point, end_position)
2024-02-15 05:04:26 +00:00
putt.emit(putt_vector)
2024-03-18 05:49:25 +00:00
play_putt_sound.rpc(putt_vector)
2024-02-15 05:04:26 +00:00
2024-03-18 05:49:25 +00:00
@rpc("authority", "call_local", "reliable")
2024-02-15 05:04:26 +00:00
func play_putt_sound(putt_vector: Vector2):
2024-03-19 02:55:32 +00:00
if multiplayer.is_server():
return
2024-02-15 05:04:26 +00:00
var putt_impulse: float = putt_vector.length()
sound.volume_db = putt_volume.sample(putt_impulse / putt_max_impulse)
sound.play()
2024-03-18 06:23:17 +00:00