107 lines
5.2 KiB
GDScript
107 lines
5.2 KiB
GDScript
class_name EventEngine
|
|
extends RefCounted
|
|
|
|
const ConditionEngineType = preload("res://scripts/domain/combat/condition_engine.gd")
|
|
const EventStateType = preload("res://scripts/domain/narrative/event_state.gd")
|
|
const ItemDefinitionType = preload("res://scripts/domain/items/item_definition.gd")
|
|
const ItemInstanceType = preload("res://scripts/domain/items/item_instance.gd")
|
|
|
|
var condition_engine: ConditionEngine
|
|
|
|
func _init(new_condition_engine: ConditionEngine = null) -> void:
|
|
condition_engine = new_condition_engine if new_condition_engine != null else ConditionEngineType.new()
|
|
|
|
func trigger(definition: Dictionary, state: EventState, context: Dictionary) -> Dictionary:
|
|
if definition.is_empty() or state == null or state.event_id.is_empty():
|
|
return {"accepted": false, "reason": &"invalid_event"}
|
|
if state.completed:
|
|
return {"accepted": false, "reason": &"completed"}
|
|
if state.triggered:
|
|
return {"accepted": false, "reason": &"already_active"}
|
|
state.node_id = StringName(definition.get("start", ""))
|
|
if state.current_node(definition).is_empty():
|
|
return {"accepted": false, "reason": &"invalid_start"}
|
|
state.triggered = true
|
|
var applications := _apply_effects(state.current_node(definition).get("on_enter", []), context)
|
|
return {"accepted": true, "applications": applications, "node_id": state.node_id}
|
|
|
|
func visible_choices(definition: Dictionary, state: EventState, context: Dictionary) -> Array[Dictionary]:
|
|
if definition.is_empty() or state == null or not state.triggered or state.is_over():
|
|
return []
|
|
var choices: Array[Dictionary] = []
|
|
for option: Variant in state.current_node(definition).get("options", []):
|
|
if option is Dictionary and _conditions_met(option, context):
|
|
choices.append(option)
|
|
return choices
|
|
|
|
func choose(definition: Dictionary, state: EventState, visible_index: int, context: Dictionary) -> Dictionary:
|
|
var choices := visible_choices(definition, state, context)
|
|
if visible_index < 0 or visible_index >= choices.size():
|
|
return {"accepted": false, "reason": &"invalid_choice", "applications": [], "completed": false}
|
|
var choice: Dictionary = choices[visible_index]
|
|
var applications := _apply_effects(choice.get("effects", []), context)
|
|
var next_node := StringName(choice.get("goto", ""))
|
|
if choice.has("skill_check"):
|
|
next_node = _skill_check_destination(choice.skill_check, context)
|
|
if bool(choice.get("end", false)):
|
|
state.completed = true
|
|
elif not next_node.is_empty() and definition.get("nodes", {}).has(String(next_node)):
|
|
state.node_id = next_node
|
|
applications.append_array(_apply_effects(state.current_node(definition).get("on_enter", []), context))
|
|
else:
|
|
state.completed = true
|
|
return {
|
|
"accepted": true,
|
|
"choice": choice,
|
|
"applications": applications,
|
|
"completed": state.completed,
|
|
"node_id": state.node_id,
|
|
}
|
|
|
|
func _conditions_met(choice: Dictionary, context: Dictionary) -> bool:
|
|
if choice.has("condition"):
|
|
var condition: Variant = choice.condition
|
|
if not condition is Dictionary or not condition_engine.evaluate(condition, context):
|
|
return false
|
|
var conditions: Variant = choice.get("conditions", [])
|
|
return condition_engine.evaluate_all(conditions, context) if conditions is Array else false
|
|
|
|
func _skill_check_destination(skill_check: Dictionary, context: Dictionary) -> StringName:
|
|
var player: PlayerState = context.get("player")
|
|
if player == null:
|
|
return StringName(skill_check.get("fail_goto", ""))
|
|
var attribute := player.effective_attribute(StringName(skill_check.get("attr", "")))
|
|
var skill := player.effective_skill(StringName(skill_check.get("skill", "")))
|
|
return StringName(skill_check.get("success_goto", "")) if attribute + skill >= int(skill_check.get("dc", 0)) else StringName(skill_check.get("fail_goto", ""))
|
|
|
|
func _apply_effects(effects: Array, context: Dictionary) -> Array[Dictionary]:
|
|
var applications: Array[Dictionary] = []
|
|
for effect: Dictionary in effects:
|
|
var kind := StringName(effect.get("type", ""))
|
|
if kind == &"add_gold":
|
|
var payload := _payload(context)
|
|
payload["gold"] = maxi(0, int(payload.get("gold", 0)) + int(effect.get("amount", 0)))
|
|
applications.append({"applied": true, "type": kind, "amount": int(effect.get("amount", 0))})
|
|
elif kind == &"give_item":
|
|
applications.append(_give_item(StringName(effect.get("item", "")), context))
|
|
elif kind == &"add_xp":
|
|
var payload := _payload(context)
|
|
payload["xp"] = int(payload.get("xp", 0)) + int(effect.get("amount", 0))
|
|
applications.append({"applied": true, "type": kind, "amount": int(effect.get("amount", 0))})
|
|
else:
|
|
applications.append(condition_engine.apply_effect(effect, context))
|
|
return applications
|
|
|
|
func _give_item(item_id: StringName, context: Dictionary) -> Dictionary:
|
|
var catalog: ContentCatalog = context.get("catalog")
|
|
var inventory: InventoryState = context.get("inventory")
|
|
if catalog == null or inventory == null:
|
|
return {"applied": false, "type": &"give_item", "item_id": item_id}
|
|
var definition := ItemDefinitionType.from_catalog(catalog, item_id)
|
|
var result := inventory.add_item(ItemInstanceType.new(definition))
|
|
return {"applied": bool(result.accepted), "type": &"give_item", "item_id": item_id, "reason": result.reason}
|
|
|
|
func _payload(context: Dictionary) -> Dictionary:
|
|
var payload: Variant = context.get("payload", {})
|
|
return payload if payload is Dictionary else {}
|