Add item_dump

This commit is contained in:
Daniel Snider 2022-08-16 00:29:52 -07:00
parent 9fc5043c7b
commit b215e201e0
2 changed files with 63 additions and 0 deletions

11
scenes/item_dump.tscn Normal file
View File

@ -0,0 +1,11 @@
[gd_scene load_steps=3 format=2]
[ext_resource path="res://scripts/item_dump.gd" type="Script" id=1]
[sub_resource type="BoxShape" id=1]
[node name="item_dump" type="Area"]
script = ExtResource( 1 )
[node name="CollisionShape" type="CollisionShape" parent="."]
shape = SubResource( 1 )

52
scripts/item_dump.gd Normal file
View File

@ -0,0 +1,52 @@
extends Spatial
signal item_dump_completed
signal item_dumped
# Godot is not configurable enough (without great pains)
# to make this look less dumb
export var item_type_0: Resource
export var item_count_0: int
export var item_type_1: Resource
export var item_count_1: int
export var item_type_2: Resource
export var item_count_2: int
var remaining := {}
export var enabled = true
func _ready():
reset()
func reset():
remaining.clear()
if item_type_0 and item_count_0 > 0:
remaining[item_type_0] = item_count_0
if item_type_1 and item_count_1 > 0:
remaining[item_type_1] = item_count_1
if item_type_2 and item_count_2 > 0:
remaining[item_type_2] = item_count_2
func on_player_interact(player) -> bool:
if not enabled:
return false
if remaining.empty():
return false
if not player.has_item():
return false
if not remaining.has(player.item_in_hand.item_type):
return false
assert(remaining[player.item_in_hand.item_type] > 0, "remaining dictionary should always erase keys with a value of 0")
# Remove the player's item and mark off the item on the remaining list
var item = player.drop_item_in_hand()
remaining[item.item_type] -= 1
if remaining[item.item_type] <= 0:
remaining.erase(item.item_type)
item.queue_free()
emit_signal("item_dumped")
# Check if everything's been checked off
if remaining.empty():
emit_signal("item_dump_completed")
return true