74 lines
2.4 KiB
GDScript
74 lines
2.4 KiB
GDScript
class_name Gavel extends Node2D
|
|
|
|
signal gavel_hit
|
|
|
|
var swinging
|
|
var click_radius = 32 # will need to determine click radius based on sprite size -- should be equal
|
|
var swing_threshold = 64 # will also need to scale this based on sprite size
|
|
var swing_start = Vector2(0, 0)
|
|
|
|
@export var gavel_root_position = Vector2(0, 0)
|
|
@export var audio_player: AudioStreamPlayer2D
|
|
|
|
enum GavelState { HOVER, SWING, UNHOVER, IDLE }
|
|
var current_state: GavelState = GavelState.IDLE
|
|
|
|
#func _input(event: InputEvent) -> void:
|
|
#if event is InputEventMouseButton and event.button_index == MOUSE_BUTTON_LEFT:
|
|
## check to see if the player clicked on the gavel
|
|
#if (event.position - $Sprite2D.position).length() < click_radius:
|
|
#if not swinging and event.pressed():
|
|
#swinging = true
|
|
#swing_start = InputEventMouseMotion.screen_relative
|
|
#if swinging and not event.pressed():
|
|
#swinging = false
|
|
#
|
|
#if event is InputEventMouseMotion and swinging:
|
|
#$Sprite2D.position = event.position
|
|
#if InputEventMouseMotion.screen_relative.length() - swing_start.length() == swing_threshold:
|
|
#gavel_hit.emit()
|
|
#$Sprite2D.position = gavel_root_position
|
|
|
|
func transition(new_state: GavelState):
|
|
match new_state:
|
|
GavelState.HOVER:
|
|
if current_state == GavelState.UNHOVER:
|
|
$GavelAnimations.animation_finished.disconnect(_on_unhover_complete)
|
|
$GavelAnimations.play("Hover")
|
|
GavelState.IDLE:
|
|
$TextureButton.mouse_default_cursor_shape = Control.CURSOR_ARROW
|
|
$GavelAnimations.play("Idle")
|
|
GavelState.UNHOVER:
|
|
$GavelAnimations.play("Unhover")
|
|
$GavelAnimations.animation_finished.connect(_on_unhover_complete)
|
|
GavelState.SWING:
|
|
$TextureButton.mouse_default_cursor_shape = Control.CURSOR_DRAG
|
|
audio_player.play()
|
|
gavel_hit.emit()
|
|
if current_state == GavelState.SWING:
|
|
$GavelAnimations.frame = 0
|
|
$GavelAnimations.play("Press")
|
|
|
|
current_state = new_state
|
|
|
|
|
|
func _on_texture_button_button_down() -> void:
|
|
transition(GavelState.SWING)
|
|
|
|
func _on_texture_button_mouse_entered() -> void:
|
|
transition(GavelState.HOVER)
|
|
|
|
func _on_texture_button_mouse_exited() -> void:
|
|
transition(GavelState.UNHOVER)
|
|
#if $TextureButton.pressed:
|
|
|
|
#else:
|
|
#$GavelAnimations.play("Hover", -1.0)
|
|
#$GavelAnimations.play("Idle")
|
|
|
|
func _on_unhover_complete() -> void:
|
|
$GavelAnimations.animation_finished.disconnect(_on_unhover_complete)
|
|
transition(GavelState.IDLE)
|
|
|
|
func _on_texture_button_button_release() -> void:
|
|
$TextureButton.mouse_default_cursor_shape = Control.CURSOR_ARROW
|