50 lines
1.6 KiB
GDScript
50 lines
1.6 KiB
GDScript
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
|
|
@export var void_excess_items: bool = false
|
|
|
|
var storage_total: int = 0
|
|
var storage: Dictionary[Item, int] = {}
|
|
|
|
func can_accept_item(item: Item) -> bool:
|
|
return ((storage_total < storage_size) or void_excess_items) \
|
|
and (accepted_items.has(item) or accepted_items.is_empty())
|
|
|
|
func offer_item(item: Item) -> bool:
|
|
if !can_accept_item(item):
|
|
return false
|
|
if storage_total >= storage_size and void_excess_items:
|
|
return true # void item
|
|
var old_count: int = storage.get(item, 0)
|
|
storage.set(item, old_count + 1)
|
|
storage_total += 1
|
|
print("Added %s to %s storage; previously had %d, new total %d" % [item.name, get_parent().name, old_count, storage_total])
|
|
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])
|
|
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])
|
|
return true
|