2024-04-15 00:45:18 +00:00
|
|
|
extends Node2D
|
2024-04-13 21:40:34 +00:00
|
|
|
class_name MoveTowardsMouse
|
|
|
|
|
2024-04-15 00:45:18 +00:00
|
|
|
|
|
|
|
signal move(movement: Vector2)
|
|
|
|
signal detached
|
|
|
|
signal captured
|
|
|
|
|
2024-04-14 02:30:23 +00:00
|
|
|
@onready var game := MainGame.get_ancestor(self)
|
2024-04-13 21:40:34 +00:00
|
|
|
|
2024-04-15 00:45:18 +00:00
|
|
|
@export var speed: float = 100.0
|
|
|
|
@export var can_detach: bool = false
|
|
|
|
|
2024-04-15 23:00:49 +00:00
|
|
|
@export var state_holder: StateHolder
|
|
|
|
@export var state_name: StringName = ''
|
2024-04-15 00:45:18 +00:00
|
|
|
|
|
|
|
|
2024-04-15 23:00:49 +00:00
|
|
|
var is_captured: bool :
|
|
|
|
get:
|
|
|
|
return state_holder.state == state_name
|
2024-04-13 21:40:34 +00:00
|
|
|
|
2024-04-14 16:20:08 +00:00
|
|
|
func get_followed_global_position():
|
|
|
|
return game.camera.get_global_mouse_position()
|
2024-04-15 00:45:18 +00:00
|
|
|
|
|
|
|
|
|
|
|
func get_followed_mouse_position():
|
|
|
|
var global_followed_position: Vector2 = get_followed_global_position()
|
|
|
|
var relative_followed_position: Vector2 = global_followed_position - global_position
|
|
|
|
return relative_followed_position
|
|
|
|
|
|
|
|
|
2024-04-15 23:00:49 +00:00
|
|
|
func _ready():
|
|
|
|
if state_holder == null:
|
|
|
|
if state_name == '':
|
|
|
|
state_holder = StateHolder.new()
|
|
|
|
else:
|
|
|
|
state_holder = StateHolder.get_default(self)
|
|
|
|
|
2024-04-15 00:45:18 +00:00
|
|
|
func _physics_process(delta: float) -> void:
|
2024-04-15 23:00:49 +00:00
|
|
|
if is_captured:
|
|
|
|
var relative_followed_position: Vector2 = get_followed_mouse_position()
|
|
|
|
var direction: Vector2 = position.direction_to(relative_followed_position)
|
|
|
|
var actual_speed: float = min(delta * speed, relative_followed_position.length()) # Don't overshoot.
|
|
|
|
var movement: Vector2 = direction * actual_speed
|
|
|
|
move.emit(movement)
|
2024-04-15 00:45:18 +00:00
|
|
|
|
|
|
|
func _on_capture_area_mouse_entered() -> void:
|
2024-04-15 23:00:49 +00:00
|
|
|
if state_holder.propose_state(state_name, 70, func (): detached.emit()):
|
|
|
|
captured.emit()
|
2024-04-15 00:45:18 +00:00
|
|
|
|
|
|
|
func _on_capture_area_mouse_exited() -> void:
|
|
|
|
if can_detach:
|
2024-04-15 23:00:49 +00:00
|
|
|
state_holder.remove_state(state_name)
|
|
|
|
|
2024-04-15 00:45:18 +00:00
|
|
|
|