50 lines
1 KiB
GDScript
50 lines
1 KiB
GDScript
class_name PickupArea
|
|
extends Area2D
|
|
|
|
signal picked_up()
|
|
signal dropped()
|
|
|
|
|
|
@onready var dropoff_area: Area2D = %UtilsDropoff.get_node("Area2D")
|
|
|
|
|
|
var is_picked_up = false
|
|
var is_dropable = false
|
|
var is_dropping_enabled = true
|
|
var parent: Node2D
|
|
|
|
|
|
func _ready():
|
|
parent = get_parent() as Node2D
|
|
|
|
func _on_input_event(viewport, event, shape_idx):
|
|
if event is InputEventMouseButton and event.is_pressed():
|
|
if is_picked_up and is_dropable and is_dropping_enabled:
|
|
_drop()
|
|
elif not is_picked_up:
|
|
_pickup()
|
|
|
|
func _physics_process(delta):
|
|
if is_picked_up:
|
|
parent.global_position = get_viewport().get_mouse_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()
|