50 lines
1.1 KiB
GDScript
50 lines
1.1 KiB
GDScript
class_name PickupArea
|
|
extends Area2D
|
|
|
|
signal picked_up()
|
|
signal dropped()
|
|
|
|
|
|
@onready var dropoff_area: Area2D = %UtilsDropoff.get_node("Area2D")
|
|
|
|
|
|
var is_clickable = false
|
|
var is_picked_up = false
|
|
var is_dropable = false
|
|
var parent: Node2D
|
|
|
|
func _ready():
|
|
mouse_entered.connect(func(): is_clickable = true)
|
|
mouse_exited.connect(func(): is_clickable = false)
|
|
parent = get_parent() as Node2D
|
|
|
|
func _input(event):
|
|
if event is InputEventMouseButton and event.is_pressed():
|
|
if is_picked_up and is_dropable:
|
|
_drop()
|
|
elif not is_picked_up and is_clickable:
|
|
_pickup()
|
|
if is_picked_up and event is InputEventMouseMotion:
|
|
parent.global_position = event.global_position
|
|
|
|
func _exit_tree():
|
|
Input.mouse_mode = Input.MOUSE_MODE_VISIBLE
|
|
|
|
func _on_dropoff_area_entered(area):
|
|
if area == dropoff_area:
|
|
is_dropable = true
|
|
|
|
func _on_dropoff_area_exited(area):
|
|
if area == dropoff_area:
|
|
is_dropable = false
|
|
|
|
|
|
func _pickup():
|
|
is_picked_up = true
|
|
Input.mouse_mode = Input.MOUSE_MODE_HIDDEN
|
|
picked_up.emit()
|
|
|
|
func _drop():
|
|
is_picked_up = false
|
|
Input.mouse_mode = Input.MOUSE_MODE_VISIBLE
|
|
dropped.emit()
|