From b215e201e07b39330a2a893b32a3222c8b99bd75 Mon Sep 17 00:00:00 2001 From: Daniel Snider Date: Tue, 16 Aug 2022 00:29:52 -0700 Subject: [PATCH] Add item_dump --- scenes/item_dump.tscn | 11 +++++++++ scripts/item_dump.gd | 52 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+) create mode 100644 scenes/item_dump.tscn create mode 100644 scripts/item_dump.gd diff --git a/scenes/item_dump.tscn b/scenes/item_dump.tscn new file mode 100644 index 0000000..2b1ab34 --- /dev/null +++ b/scenes/item_dump.tscn @@ -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 ) diff --git a/scripts/item_dump.gd b/scripts/item_dump.gd new file mode 100644 index 0000000..3872d66 --- /dev/null +++ b/scripts/item_dump.gd @@ -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