class_name DroppedBlock
|
|
extends RigidBody3D
|
|
|
|
|
|
## 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: MeshInstance3D
|
|
@export var pickup_area: Area3D
|
|
|
|
var id: String
|
|
var resource: BlockResource
|
|
var impulse: Vector3 = Vector3.ZERO
|
|
|
|
|
|
func _ready() -> void:
|
|
animation_player.play("start_animation")
|
|
await get_tree().create_timer(pickup_timeout).timeout # Give block enough time to clear player collision
|
|
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
|
|
_load_resource_data()
|
|
_apply_material()
|
|
|
|
|
|
func _apply_material() -> void:
|
|
if block_mesh == null: return
|
|
if resource == null: return
|
|
block_mesh.set_surface_override_material(0, resource.material_texture)
|
|
|
|
func _load_resource_data() -> void:
|
|
if not id:
|
|
printerr("Could not load resource data. Block ID was empty.")
|
|
return
|
|
|
|
if not id in DBItems.data:
|
|
printerr("Could not load resource data. Unknown Block ID: ", id)
|
|
return
|
|
resource = DBItems.data[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()
|