immersive-home/app/content/system/controller_left/controller_left.gd

169 lines
4.6 KiB
GDScript3
Raw Normal View History

extends XRController3D
const Entity = preload ("res://content/entities/entity.gd")
2024-05-06 19:22:12 +03:00
const Pointer = preload ("res://lib/utils/pointer/pointer.gd")
const Initiator = preload ("res://lib/utils/pointer/initiator.gd")
const Finger = preload ("res://lib/utils/touch/finger.gd")
const Touch = preload ("res://lib/utils/touch/touch.gd")
const Collide = preload ("res://lib/utils/touch/collide.gd")
const Miniature = preload ("res://content/system/house/mini/miniature.gd")
@onready var main = $"/root/Main"
@onready var hand = $hand_l
@onready var hand_mesh = $hand_l/Armature/Skeleton3D/mesh_Hand_L
@onready var index_tip = $IndexTip
@onready var thumb_tip = $ThumbTip
@onready var middle_tip = $MiddleTip
@onready var mini_view_button = $Palm/QuickActions/MiniView
@onready var temperature_button = $Palm/QuickActions/Temperature
@onready var humidity_button = $Palm/QuickActions/Humidity
@onready var palm = $Palm
@onready var ray: RayCast3D = $Raycast
@onready var area = $trash_bin/Area3D
@onready var trash_bin = $trash_bin
@onready var animation = $AnimationPlayer
2024-05-06 19:22:12 +03:00
@onready var quick_actions = $Palm/QuickActions
var initiator: Initiator = Initiator.new()
var collide: Collide
var pointer: Pointer
var press_distance = 0.02
var grip_distance = 0.02
var pressed = false
var grabbed = false
var to_delete = []
var trash_bin_visible: bool = true:
set(value):
if trash_bin_visible == value:
return
if value:
add_child(trash_bin)
else:
if animation.is_playing():
await animation.animation_finished
remove_child(trash_bin)
trash_bin_visible = value
var trash_bin_large: bool = false:
set(value):
if trash_bin_large == value:
return
if value:
animation.play("add_trashbin")
else:
animation.play_backwards("add_trashbin")
trash_bin_large = value
func _ready():
trash_bin_visible = false
2024-05-06 19:22:12 +03:00
_setup_hand()
2023-11-27 01:23:19 +02:00
EventSystem.on_grab_down.connect(func(event: EventPointer):
2024-05-06 14:02:20 +03:00
trash_bin_visible=_get_entity(event.target) != null
)
2023-11-22 02:44:07 +02:00
EventSystem.on_grab_move.connect(func(event):
if !trash_bin_visible:
return
2024-05-06 14:02:20 +03:00
var entity=_get_entity(event.target)
if entity is Entity&&area.overlaps_body(entity):
if !to_delete.has(entity):
to_delete.append(entity)
trash_bin_large=true
else:
2024-05-06 14:02:20 +03:00
to_delete.erase(entity)
trash_bin_large=false
)
2023-11-27 01:23:19 +02:00
EventSystem.on_grab_up.connect(func(_event: EventPointer):
if !trash_bin_visible:
return
for entity in to_delete:
entity.queue_free()
to_delete.clear()
trash_bin_large=false
trash_bin_visible=false
2024-05-06 14:02:20 +03:00
House.body.save_all_entities()
)
2024-05-06 14:02:20 +03:00
2024-05-06 19:22:12 +03:00
func _process(_delta):
if main.camera.global_transform.basis.z.dot(palm.global_transform.basis.x) > 0.85:
if quick_actions.is_inside_tree() == false: palm.add_child(quick_actions)
else:
if quick_actions.is_inside_tree(): palm.remove_child(quick_actions)
func _physics_process(_delta):
var distance_trigger = index_tip.global_position.distance_to(thumb_tip.global_position)
var distance_grab = middle_tip.global_position.distance_to(thumb_tip.global_position)
var trigger_close = distance_trigger <= press_distance
var grab_close = distance_grab <= grip_distance
if trigger_close&&!pressed:
initiator.on_press.emit(Initiator.EventType.TRIGGER)
pressed = true
elif !trigger_close&&pressed:
initiator.on_release.emit(Initiator.EventType.TRIGGER)
pressed = false
if grab_close&&!grabbed:
initiator.on_press.emit(Initiator.EventType.GRIP)
grabbed = true
elif !grab_close&&grabbed:
initiator.on_release.emit(Initiator.EventType.GRIP)
grabbed = false
2024-05-06 14:02:20 +03:00
func _get_entity(node: Node):
if node is Entity:
return node
if node.get_parent() == null:
return null
2024-05-06 19:22:12 +03:00
return _get_entity(node.get_parent())
func _setup_hand():
TouchManager.add_finger(Finger.Type.INDEX_LEFT, $IndexTip/TouchArea)
collide = Collide.new(hand, hand_mesh, index_tip)
add_child(collide)
mini_view_button.on_button_up.connect(func():
House.body.mini_view.small.value=!House.body.mini_view.small.value
)
temperature_button.on_button_up.connect(func():
if House.body.mini_view.heatmap_type.value == Miniature.HeatmapType.TEMPERATURE:
House.body.mini_view.heatmap_type.value=Miniature.HeatmapType.NONE
else:
House.body.mini_view.heatmap_type.value=Miniature.HeatmapType.TEMPERATURE
)
humidity_button.on_button_up.connect(func():
if House.body.mini_view.heatmap_type.value == Miniature.HeatmapType.HUMIDITY:
House.body.mini_view.heatmap_type.value=Miniature.HeatmapType.NONE
else:
House.body.mini_view.heatmap_type.value=Miniature.HeatmapType.HUMIDITY
)
initiator.type = Initiator.Type.HAND_LEFT
initiator.node = self
pointer = Pointer.new(initiator, ray)
add_child(pointer)