|
class_name DroppedBlock
|
|
extends RigidBody3D
|
|
|
|
|
|
const PREFAB: PackedScene = preload("res://scenes/blocks/dropped_block.tscn")
|
|
|
|
## The amount of time to wait before PickupArea is set to monitorable[br]
|
|
## This is to ensure that the item isn't picked up immediately after throwing
|
|
@export var pickup_timeout: int = 1
|
|
@export var animation_player: AnimationPlayer
|
|
@export var block_mesh: BlockMesh
|
|
@export var pickup_area: Area3D
|
|
|
|
var id: String
|
|
var impulse: Vector3 = Vector3.ZERO
|
|
|
|
|
|
func _ready() -> void:
|
|
## AnimationPlayer handles modifying BlockMesh scale, position, and rotation
|
|
## The scale is NOT applied to any other node, including RigidBody and CollisionShape
|
|
animation_player.play("start_animation")
|
|
|
|
## Wait for pickup_timeout to ensure player does not trigger
|
|
## pickup immediately after throwing
|
|
await get_tree().create_timer(pickup_timeout).timeout
|
|
pickup_area.monitoring = true
|
|
|
|
|
|
# TODO: Rename to something else (maybe remove())
|
|
# Move to signal?
|
|
func destroy_block() -> void:
|
|
queue_free()
|
|
|
|
func get_id() -> String:
|
|
return id
|
|
|
|
func initialize(block_id: String, start_position: Vector3, drop_direction: Vector3 = Vector3.ZERO, drop_impulse: float = 0.0) -> void:
|
|
set_id(block_id)
|
|
position = start_position
|
|
impulse = drop_direction * drop_impulse
|
|
apply_central_impulse(impulse)
|
|
|
|
func set_id(block_id: String) -> void:
|
|
id = block_id
|
|
if id in DBItems.data:
|
|
block_mesh.apply_material(DBItems.data[id].material_texture)
|
|
else:
|
|
printerr("Unknown Block ID: ", id)
|
|
|
|
|
|
func _on_pickup_area_body_entered(body: Node3D) -> void:
|
|
if not body is Player: return
|
|
|
|
# TODO: Update stack count dynamically
|
|
var stack_count: int = 1
|
|
var item: DBItemResource = (DBItems.data[get_id()] as Resource).duplicate()
|
|
item.amount = stack_count
|
|
InventoryManager.item_picked_up.emit(item)
|
|
queue_free()
|