140 lines
3.4 KiB
GDScript
140 lines
3.4 KiB
GDScript
@icon("res://components/Cursor/mouse-pointer-2.svg")
|
|
extends Base
|
|
|
|
@onready var mouse_control = $"MouseControl"
|
|
@onready var sprite_2d: Sprite2D = $MouseControl/Sprite2D
|
|
|
|
const CURSOR_UI = preload("res://components/Cursor/hover-1.png")
|
|
const CURSOR_HOLD = preload("res://components/Cursor/grab-1.png")
|
|
const CURSOR = preload("res://components/Cursor/normal-1.png")
|
|
var rotation_tween
|
|
|
|
var cursor_tween
|
|
var hovered_objects = 0
|
|
var holding = false
|
|
var held_item
|
|
|
|
func _ready() -> void:
|
|
Persister.data_persisted.connect(_on_data_persisted)
|
|
Triggerer.listen("grab_creature", _on_grab_creature)
|
|
|
|
|
|
func _on_grab_creature(data):
|
|
held_item = data.creature
|
|
holding = true
|
|
|
|
|
|
func _on_release():
|
|
holding = false
|
|
var overlapping_areas = mouse_control.get_overlapping_areas()
|
|
overlapping_areas.sort_custom(func(a, b):
|
|
return a.z_index > b.z_index
|
|
)
|
|
|
|
for area in overlapping_areas:
|
|
if area.has_method("_on_released"):
|
|
var current_node = area
|
|
var hidden = false
|
|
while current_node:
|
|
if "modulate" in current_node:
|
|
if current_node.modulate.a < 1:
|
|
hidden = true
|
|
break
|
|
current_node = current_node.get_parent()
|
|
|
|
if hidden:
|
|
continue
|
|
|
|
if not area._on_released():
|
|
break
|
|
|
|
|
|
func _on_click():
|
|
var overlapping_areas = mouse_control.get_overlapping_areas()
|
|
overlapping_areas.sort_custom(func(a, b):
|
|
return a.z_index > b.z_index
|
|
)
|
|
|
|
for area in overlapping_areas:
|
|
if area.has_method("_on_clicked"):
|
|
var current_node = area
|
|
var hidden = false
|
|
while current_node:
|
|
if "modulate" in current_node:
|
|
if current_node.modulate.a < 1:
|
|
hidden = true
|
|
break
|
|
if "visible" in current_node:
|
|
if not current_node.visible:
|
|
hidden = true
|
|
break
|
|
current_node = current_node.get_parent()
|
|
|
|
if hidden:
|
|
continue
|
|
|
|
if not area._on_clicked():
|
|
break
|
|
|
|
|
|
func _process(delta):
|
|
if Input.is_action_just_released("left_click"):
|
|
_on_release()
|
|
if Input.is_action_just_pressed("left_click"):
|
|
_on_click()
|
|
#Input.set_mouse_mode(Input.MOUSE_MODE_HIDDEN)
|
|
mouse_control.position = mouse_control.get_global_mouse_position()
|
|
|
|
if held_item:
|
|
held_item.position = mouse_control.position
|
|
|
|
if not Input.is_action_pressed("left_click"):
|
|
sprite_2d.rotation = move_toward(sprite_2d.rotation, 0, delta)
|
|
sprite_2d.scale = sprite_2d.scale.move_toward(Vector2.ONE, delta)
|
|
else:
|
|
sprite_2d.scale = sprite_2d.scale.move_toward(Vector2.ONE * 0.9, delta * 2)
|
|
sprite_2d.rotation = move_toward(sprite_2d.rotation, -0.1, delta * 2)
|
|
|
|
func _on_mouse_control_area_entered(area):
|
|
if area.has_method("_on_hovered"):
|
|
var current_node = area
|
|
var hidden = false
|
|
while current_node:
|
|
if "visible" in current_node:
|
|
if not current_node.visible:
|
|
hidden = true
|
|
break
|
|
current_node = current_node.get_parent()
|
|
|
|
if not hidden:
|
|
area._on_hovered()
|
|
hovered_objects += 1
|
|
|
|
_update_cursor()
|
|
|
|
|
|
func _on_mouse_control_area_exited(area):
|
|
if area.has_method("_on_unhovered"):
|
|
var current_node = area
|
|
var hidden = false
|
|
while current_node:
|
|
if "visible" in current_node:
|
|
if not current_node.visible:
|
|
hidden = true
|
|
break
|
|
current_node = current_node.get_parent()
|
|
|
|
if not hidden:
|
|
area._on_unhovered()
|
|
hovered_objects -= 1
|
|
|
|
_update_cursor()
|
|
|
|
|
|
func _update_cursor():
|
|
if holding:
|
|
sprite_2d.texture = CURSOR_HOLD
|
|
if hovered_objects:
|
|
sprite_2d.texture = CURSOR_UI
|
|
else:
|
|
sprite_2d.texture = CURSOR
|