Preserve approved pixel tower prototype as development baseline
This commit is contained in:
@@ -0,0 +1,105 @@
|
||||
class_name EventChoice
|
||||
extends RefCounted
|
||||
|
||||
const EventConditionType = preload("res://scripts/events/event_condition.gd")
|
||||
const EventActionType = preload("res://scripts/events/event_action.gd")
|
||||
|
||||
var choice_id: StringName = &""
|
||||
var text_key: StringName = &""
|
||||
var conditions: Array[EventCondition] = []
|
||||
var cost: Dictionary = {}
|
||||
var effect_preview: Dictionary = {}
|
||||
var actions: Array[EventAction] = []
|
||||
|
||||
|
||||
func configure(
|
||||
p_choice_id: StringName,
|
||||
p_text_key: StringName,
|
||||
p_conditions: Array = [],
|
||||
p_cost: Dictionary = {},
|
||||
p_effect_preview: Dictionary = {},
|
||||
p_actions: Array = []
|
||||
) -> EventChoice:
|
||||
choice_id = p_choice_id
|
||||
text_key = p_text_key
|
||||
conditions.clear()
|
||||
for raw_condition: Variant in p_conditions:
|
||||
var condition := EventConditionType.from_variant(raw_condition)
|
||||
if condition != null:
|
||||
conditions.append(condition)
|
||||
cost = p_cost.duplicate(true)
|
||||
effect_preview = p_effect_preview.duplicate(true)
|
||||
actions.clear()
|
||||
for raw_action: Variant in p_actions:
|
||||
var action := EventActionType.from_variant(raw_action)
|
||||
if action != null:
|
||||
actions.append(action)
|
||||
return self
|
||||
|
||||
|
||||
func is_valid() -> bool:
|
||||
if choice_id.is_empty() or text_key.is_empty():
|
||||
return false
|
||||
for condition: EventCondition in conditions:
|
||||
if condition == null or not condition.is_valid():
|
||||
return false
|
||||
for action: EventAction in actions:
|
||||
if action == null or not action.is_valid():
|
||||
return false
|
||||
for raw_key: Variant in cost:
|
||||
if StringName(raw_key).is_empty() or int(cost[raw_key]) < 0:
|
||||
return false
|
||||
return true
|
||||
|
||||
|
||||
func conditions_match(context: Dictionary, callbacks: Dictionary = {}) -> bool:
|
||||
for condition: EventCondition in conditions:
|
||||
if not condition.evaluate(context, callbacks):
|
||||
return false
|
||||
return true
|
||||
|
||||
|
||||
func create_snapshot() -> Dictionary:
|
||||
var condition_snapshots: Array[Dictionary] = []
|
||||
var action_snapshots: Array[Dictionary] = []
|
||||
for condition: EventCondition in conditions:
|
||||
condition_snapshots.append(condition.create_snapshot())
|
||||
for action: EventAction in actions:
|
||||
action_snapshots.append(action.create_snapshot())
|
||||
return {
|
||||
&"choice_id": String(choice_id),
|
||||
&"text_key": String(text_key),
|
||||
&"conditions": condition_snapshots,
|
||||
&"cost": cost.duplicate(true),
|
||||
&"effect_preview": effect_preview.duplicate(true),
|
||||
&"actions": action_snapshots,
|
||||
}
|
||||
|
||||
|
||||
static func from_variant(raw_value: Variant) -> EventChoice:
|
||||
if raw_value is EventChoice:
|
||||
return raw_value
|
||||
if not raw_value is Dictionary:
|
||||
return null
|
||||
return EventChoice.new().configure(
|
||||
StringName(_value(raw_value, &"choice_id", &"")),
|
||||
StringName(_value(raw_value, &"text_key", &"")),
|
||||
_array(_value(raw_value, &"conditions", [])),
|
||||
_dictionary(_value(raw_value, &"cost", {})),
|
||||
_dictionary(_value(raw_value, &"effect_preview", {})),
|
||||
_array(_value(raw_value, &"actions", []))
|
||||
)
|
||||
|
||||
|
||||
static func _value(source: Dictionary, key: StringName, fallback: Variant) -> Variant:
|
||||
if source.has(key):
|
||||
return source[key]
|
||||
return source.get(String(key), fallback)
|
||||
|
||||
|
||||
static func _array(value: Variant) -> Array:
|
||||
return value if value is Array else []
|
||||
|
||||
|
||||
static func _dictionary(value: Variant) -> Dictionary:
|
||||
return value if value is Dictionary else {}
|
||||
Reference in New Issue
Block a user