Files
TheTree_mock/scripts/events/event_action.gd
T

69 lines
1.7 KiB
GDScript

class_name EventAction
extends RefCounted
enum Type {
SET_FLAG,
ADD_RELATIONSHIP_VALUE,
DAMAGE_SHIELD,
ADD_LEDGER_ENTRY,
ADD_OR_REMOVE_ITEM,
CREATE_TASK,
CREATE_INCIDENT,
SET_FACILITY_STATE,
TELEPORT_ENTITY_FOR_RECOVERY,
SCHEDULE_EVENT,
SET_ROUTE_LOCK,
}
var action_id: StringName = &""
var type: Type = Type.SET_FLAG
var payload: Dictionary = {}
func configure(
p_action_id: StringName,
p_type: Variant,
p_payload: Dictionary = {}
) -> EventAction:
action_id = p_action_id
type = _type_from_variant(p_type)
payload = p_payload.duplicate(true)
return self
func is_valid() -> bool:
return not action_id.is_empty() and type >= Type.SET_FLAG and type <= Type.SET_ROUTE_LOCK
func create_snapshot() -> Dictionary:
return {
&"action_id": String(action_id),
&"type": Type.keys()[type],
&"payload": payload.duplicate(true),
}
static func from_variant(raw_value: Variant) -> EventAction:
if raw_value is EventAction:
return raw_value
if not raw_value is Dictionary:
return null
return EventAction.new().configure(
StringName(_value(raw_value, &"action_id", &"")),
_value(raw_value, &"type", "SET_FLAG"),
_value(raw_value, &"payload", {}) if _value(raw_value, &"payload", {}) is Dictionary else {}
)
static func _type_from_variant(raw_value: Variant) -> Type:
if raw_value is int:
return clampi(int(raw_value), Type.SET_FLAG, Type.SET_ROUTE_LOCK) as Type
var index := Type.keys().find(String(raw_value).to_upper())
return (index if index >= 0 else Type.SET_FLAG) as Type
static func _value(source: Dictionary, key: StringName, fallback: Variant) -> Variant:
if source.has(key):
return source[key]
return source.get(String(key), fallback)