Armazon/scripts/packageitem.gd

72 lines
1.9 KiB
GDScript3
Raw Permalink Normal View History

2024-01-28 13:44:54 -05:00
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
2024-04-03 19:24:04 -04:00
@export var explosion: PackedScene = preload("res://props/explosion/nuke_blast.tscn")
@export var has_prop_physics: bool = true
2024-04-03 19:24:04 -04:00
@onready var random_explosion_delay: float = -1.0
const RANDOM_EXPLOSION_CHANCE: float = 1.0 / 50.0
2024-01-28 13:44:54 -05:00
func _init() -> void:
freeze_mode = RigidBody3D.FREEZE_MODE_STATIC
func _ready() -> void:
set_collision_layer_value(5,has_prop_physics)
2024-01-28 13:44:54 -05:00
func _physics_process(delta):
2024-04-03 19:24:04 -04:00
if random_explosion_delay > 0:
random_explosion_delay -= delta
if random_explosion_delay <= 0:
detonate()
return
2024-01-28 13:44:54 -05:00
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
2024-04-03 19:24:04 -04:00
if randf() < RANDOM_EXPLOSION_CHANCE:
random_explosion_delay = randf_range(0.05,5.0)
2024-01-28 13:44:54 -05:00
#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()