58 lines
1.9 KiB
GDScript
58 lines
1.9 KiB
GDScript
class_name StorageDefinition
|
|
extends Resource
|
|
|
|
@export var storage_id: StringName
|
|
@export var display_name: String
|
|
@export_range(0.0, 1000000.0, 0.1) var capacity := StorageStateRecord.DEFAULT_CAPACITY
|
|
@export var accepted_item_ids: Array[StringName] = []
|
|
@export var accepted_item_tag_ids: Array[StringName] = []
|
|
@export var capability_tag_ids: Array[StringName] = []
|
|
@export var routing_priority := 0
|
|
@export var deposit_action_id: StringName
|
|
@export var withdrawal_action_id: StringName
|
|
@export var presentation_cue_id: StringName
|
|
|
|
|
|
func validate() -> Array[String]:
|
|
var errors: Array[String] = []
|
|
if storage_id.is_empty():
|
|
errors.append("storage_id is empty")
|
|
if display_name.is_empty():
|
|
errors.append("display_name is empty for '%s'" % storage_id)
|
|
if not is_finite(capacity) or capacity <= 0.0:
|
|
errors.append("capacity must be positive for '%s'" % storage_id)
|
|
if accepted_item_ids.is_empty() and accepted_item_tag_ids.is_empty():
|
|
errors.append("storage '%s' must accept an item or item tag" % storage_id)
|
|
_validate_unique_ids(accepted_item_ids, "accepted item", errors)
|
|
_validate_unique_ids(accepted_item_tag_ids, "accepted item tag", errors)
|
|
_validate_unique_ids(capability_tag_ids, "capability tag", errors)
|
|
return errors
|
|
|
|
|
|
func accepts_item(item: ItemDefinition) -> bool:
|
|
if item == null:
|
|
return false
|
|
if item.item_id in accepted_item_ids:
|
|
return true
|
|
for tag_id in item.capability_tag_ids:
|
|
if tag_id in accepted_item_tag_ids:
|
|
return true
|
|
return false
|
|
|
|
|
|
func create_state(initial_amounts: Dictionary = {}) -> StorageStateRecord:
|
|
return StorageStateRecord.create(storage_id, initial_amounts, capacity)
|
|
|
|
|
|
static func _validate_unique_ids(
|
|
ids: Array[StringName], label: String, errors: Array[String]
|
|
) -> void:
|
|
var seen := {}
|
|
for value in ids:
|
|
if value.is_empty():
|
|
errors.append("%s ID is empty" % label)
|
|
elif seen.has(value):
|
|
errors.append("duplicate %s '%s'" % [label, value])
|
|
else:
|
|
seen[value] = true
|