2024-04-13 21:40:34 +00:00
|
|
|
extends Node2D
|
|
|
|
class_name MoveTowardsMouse
|
|
|
|
|
|
|
|
|
|
|
|
signal move(movement: Vector2)
|
2024-04-14 03:12:10 +00:00
|
|
|
signal detached
|
2024-04-14 01:55:57 +00:00
|
|
|
signal captured
|
2024-04-13 21:40:34 +00:00
|
|
|
|
|
|
|
|
2024-04-14 02:04:26 +00:00
|
|
|
@export_range(-500, 500, 1) var speed: float = 100.0
|
2024-04-14 03:12:10 +00:00
|
|
|
@export var can_detach: bool = false
|
2024-04-13 21:40:34 +00:00
|
|
|
|
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-14 03:12:10 +00:00
|
|
|
enum State { DETACHED, CAPTURED }
|
2024-04-14 01:55:57 +00:00
|
|
|
|
2024-04-14 03:12:10 +00:00
|
|
|
var state: State = State.DETACHED
|
2024-04-14 01:55:57 +00:00
|
|
|
|
|
|
|
|
2024-04-13 21:40:34 +00:00
|
|
|
func get_relative_mouse_position():
|
2024-04-14 02:30:23 +00:00
|
|
|
var global_mouse_position: Vector2 = game.camera.get_global_mouse_position()
|
2024-04-13 21:40:34 +00:00
|
|
|
var relative_mouse_position: Vector2 = global_mouse_position - global_position
|
|
|
|
return relative_mouse_position
|
|
|
|
|
|
|
|
|
|
|
|
func _physics_process(delta: float) -> void:
|
2024-04-14 01:55:57 +00:00
|
|
|
match state:
|
|
|
|
State.CAPTURED:
|
|
|
|
var direction: Vector2 = position.direction_to(get_relative_mouse_position())
|
|
|
|
var movement: Vector2 = direction * delta * speed
|
|
|
|
move.emit(movement)
|
|
|
|
|
|
|
|
func _on_capture_area_mouse_entered() -> void:
|
|
|
|
state = State.CAPTURED
|
|
|
|
captured.emit()
|
2024-04-14 03:12:10 +00:00
|
|
|
|
|
|
|
func _on_capture_area_mouse_exited() -> void:
|
|
|
|
if can_detach:
|
|
|
|
state = State.DETACHED
|
|
|
|
detached.emit()
|