59 lines
1.5 KiB
GDScript
59 lines
1.5 KiB
GDScript
extends RigidBody3D
|
|
class_name PackageItem
|
|
|
|
signal touched()
|
|
signal collide(time: float)
|
|
|
|
@onready var held: bool = false
|
|
|
|
@onready var last_position: Array[Vector3] = []
|
|
@onready var last_collision_count: int = 0
|
|
|
|
@export var explosion: PackedScene
|
|
|
|
func _init() -> void:
|
|
freeze_mode = RigidBody3D.FREEZE_MODE_STATIC
|
|
func _ready() -> void:
|
|
pass
|
|
|
|
func _physics_process(delta):
|
|
if held:
|
|
global_transform.origin = CameraRumble.get_mouse_pos()
|
|
last_position.push_front(global_position)
|
|
last_position.resize(min(last_position.size(), 10))
|
|
linear_velocity = Vector3.ZERO
|
|
|
|
if not Input.is_mouse_button_pressed(MOUSE_BUTTON_LEFT):
|
|
drop()
|
|
|
|
var collis_count: int = get_colliding_bodies().size()
|
|
if collis_count > last_collision_count:
|
|
emit_signal("collide", 0)
|
|
last_collision_count = collis_count
|
|
|
|
func _input_event(camera: Camera3D, event: InputEvent, position: Vector3, normal: Vector3, shape_idx: int) -> void:
|
|
if event is InputEventMouseButton:
|
|
if event.button_index == MOUSE_BUTTON_LEFT and event.pressed:
|
|
pickup()
|
|
|
|
func pickup():
|
|
if held: return
|
|
#freeze = true
|
|
CameraRumble.emit_signal("grab")
|
|
CameraRumble.holding = true
|
|
held = true
|
|
emit_signal("touched")
|
|
|
|
func drop():
|
|
if held:
|
|
#freeze = false
|
|
linear_velocity = (last_position[0] - last_position[1]) / get_physics_process_delta_time()
|
|
held = false
|
|
CameraRumble.holding = false
|
|
|
|
func detonate():
|
|
var blast: Explosion = explosion.instantiate()
|
|
get_parent().add_child(blast)
|
|
blast.global_transform = global_transform
|
|
queue_free()
|