TowerGame/scripts/building_components/consumer.gd

50 lines
1.6 KiB
GDScript3
Raw Permalink Normal View History

2025-04-21 20:49:29 +01:00
extends Node
class_name Consumer
signal item_added(item: Item)
@export var accepted_items: Array[Item] = []
@export var storage_size: int = 4
@export var max_inputs: int = 1
2025-04-23 11:56:29 +01:00
@export var void_excess_items: bool = false
2025-04-21 20:49:29 +01:00
var storage_total: int = 0
var storage: Dictionary[Item, int] = {}
func can_accept_item(item: Item) -> bool:
2025-04-23 11:56:29 +01:00
return ((storage_total < storage_size) or void_excess_items) \
and (accepted_items.has(item) or accepted_items.is_empty())
2025-04-21 20:49:29 +01:00
func offer_item(item: Item) -> bool:
if !can_accept_item(item):
return false
2025-04-23 11:56:29 +01:00
if storage_total >= storage_size and void_excess_items:
return true # void item
2025-04-21 20:49:29 +01:00
var old_count: int = storage.get(item, 0)
storage.set(item, old_count + 1)
2025-04-23 11:56:29 +01:00
storage_total += 1
print("Added %s to %s storage; previously had %d, new total %d" % [item.name, get_parent().name, old_count, storage_total])
2025-04-21 20:49:29 +01:00
item_added.emit(item)
return true
func check_storage_for_item(item: Item) -> bool:
return check_storage_for_items({item:1})
func check_storage_for_items(items: Dictionary[Item, int]) -> bool:
for item in items.keys():
if storage.get(item, 0) < items[item]:
return false
return true
func take_item_from_storage(item: Item) -> bool:
return take_items_from_storage({item: 1})
func take_items_from_storage(items: Dictionary[Item, int]) -> bool:
if !check_storage_for_items(items):
return false
for item in items.keys():
var old_count: int = storage.get(item, 0)
storage.set(item, old_count - items[item])
2025-04-23 11:56:29 +01:00
storage_total -= items[item]
print("Removed %s from %s storage; previously had %d, new total %d" % [item.name, get_parent().name, old_count, storage_total])
2025-04-21 20:49:29 +01:00
return true