43 lines
1.2 KiB
GDScript3
43 lines
1.2 KiB
GDScript3
|
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
|
||
|
|
||
|
var storage_total: int = 0
|
||
|
var storage: Dictionary[Item, int] = {}
|
||
|
|
||
|
func can_accept_item(item: Item) -> bool:
|
||
|
return (storage_total < storage_size) and (accepted_items.has(item) or accepted_items.is_empty())
|
||
|
|
||
|
func offer_item(item: Item) -> bool:
|
||
|
if !can_accept_item(item):
|
||
|
return false
|
||
|
storage_total += 1
|
||
|
var old_count: int = storage.get(item, 0)
|
||
|
storage.set(item, old_count + 1)
|
||
|
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])
|
||
|
return true
|