Compare commits
2
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
e94ca7e4a3 | ||
|
|
ec12c9b35b |
+4
-1
@@ -1,4 +1,4 @@
|
|||||||
[gd_scene load_steps=13 format=3]
|
[gd_scene load_steps=14 format=3]
|
||||||
|
|
||||||
[ext_resource type="Script" path="res://scripts/game/game.gd" id="1_game"]
|
[ext_resource type="Script" path="res://scripts/game/game.gd" id="1_game"]
|
||||||
[ext_resource type="Script" path="res://scripts/presentation/map_renderer.gd" id="2_map"]
|
[ext_resource type="Script" path="res://scripts/presentation/map_renderer.gd" id="2_map"]
|
||||||
@@ -15,6 +15,7 @@
|
|||||||
[ext_resource type="PackedScene" path="res://scenes/ui/ShopPanel.tscn" id="13_shop"]
|
[ext_resource type="PackedScene" path="res://scenes/ui/ShopPanel.tscn" id="13_shop"]
|
||||||
[ext_resource type="PackedScene" path="res://scenes/ui/CraftingPanel.tscn" id="14_crafting"]
|
[ext_resource type="PackedScene" path="res://scenes/ui/CraftingPanel.tscn" id="14_crafting"]
|
||||||
[ext_resource type="PackedScene" path="res://scenes/ui/DialoguePanel.tscn" id="15_dialogue"]
|
[ext_resource type="PackedScene" path="res://scenes/ui/DialoguePanel.tscn" id="15_dialogue"]
|
||||||
|
[ext_resource type="PackedScene" path="res://scenes/ui/EventPanel.tscn" id="16_event"]
|
||||||
|
|
||||||
[sub_resource type="StyleBoxFlat" id="StyleBox_hud_panel"]
|
[sub_resource type="StyleBoxFlat" id="StyleBox_hud_panel"]
|
||||||
bg_color = Color(0.035, 0.055, 0.09, 0.9)
|
bg_color = Color(0.035, 0.055, 0.09, 0.9)
|
||||||
@@ -89,6 +90,8 @@ script = ExtResource("4_hud")
|
|||||||
|
|
||||||
[node name="DialoguePanel" parent="HUD" instance=ExtResource("15_dialogue")]
|
[node name="DialoguePanel" parent="HUD" instance=ExtResource("15_dialogue")]
|
||||||
|
|
||||||
|
[node name="EventPanel" parent="HUD" instance=ExtResource("16_event")]
|
||||||
|
|
||||||
[node name="ResponsiveLayout" type="Control" parent="HUD"]
|
[node name="ResponsiveLayout" type="Control" parent="HUD"]
|
||||||
layout_mode = 3
|
layout_mode = 3
|
||||||
anchors_preset = 15
|
anchors_preset = 15
|
||||||
|
|||||||
@@ -0,0 +1,91 @@
|
|||||||
|
[gd_scene load_steps=3 format=3]
|
||||||
|
|
||||||
|
[ext_resource type="Script" path="res://scripts/presentation/event_panel.gd" id="1_event"]
|
||||||
|
|
||||||
|
[sub_resource type="StyleBoxFlat" id="StyleBox_event_panel"]
|
||||||
|
bg_color = Color(0.035, 0.055, 0.09, 0.97)
|
||||||
|
border_width_left = 3
|
||||||
|
border_width_top = 3
|
||||||
|
border_width_right = 3
|
||||||
|
border_width_bottom = 3
|
||||||
|
border_color = Color(0.82, 0.66, 0.25, 0.95)
|
||||||
|
corner_radius_top_left = 8
|
||||||
|
corner_radius_top_right = 8
|
||||||
|
corner_radius_bottom_right = 8
|
||||||
|
corner_radius_bottom_left = 8
|
||||||
|
|
||||||
|
[node name="EventPanel" type="PanelContainer"]
|
||||||
|
visible = false
|
||||||
|
layout_mode = 1
|
||||||
|
anchors_preset = 8
|
||||||
|
anchor_left = 0.5
|
||||||
|
anchor_top = 0.5
|
||||||
|
anchor_right = 0.5
|
||||||
|
anchor_bottom = 0.5
|
||||||
|
offset_left = -300.0
|
||||||
|
offset_top = -205.0
|
||||||
|
offset_right = 300.0
|
||||||
|
offset_bottom = 205.0
|
||||||
|
grow_horizontal = 2
|
||||||
|
grow_vertical = 2
|
||||||
|
theme_override_styles/panel = SubResource("StyleBox_event_panel")
|
||||||
|
script = ExtResource("1_event")
|
||||||
|
|
||||||
|
[node name="Title" type="Label" parent="."]
|
||||||
|
layout_mode = 1
|
||||||
|
anchors_preset = 10
|
||||||
|
anchor_left = 0.5
|
||||||
|
anchor_right = 0.5
|
||||||
|
offset_left = -270.0
|
||||||
|
offset_top = 18.0
|
||||||
|
offset_right = 270.0
|
||||||
|
offset_bottom = 46.0
|
||||||
|
grow_horizontal = 2
|
||||||
|
theme_override_colors/font_color = Color(1, 0.84, 0.42, 1)
|
||||||
|
theme_override_font_sizes/font_size = 21
|
||||||
|
horizontal_alignment = 1
|
||||||
|
|
||||||
|
[node name="Body" type="RichTextLabel" parent="."]
|
||||||
|
layout_mode = 1
|
||||||
|
anchors_preset = 15
|
||||||
|
anchor_right = 1.0
|
||||||
|
anchor_bottom = 1.0
|
||||||
|
offset_left = 30.0
|
||||||
|
offset_top = 58.0
|
||||||
|
offset_right = -30.0
|
||||||
|
offset_bottom = -175.0
|
||||||
|
grow_horizontal = 2
|
||||||
|
grow_vertical = 2
|
||||||
|
theme_override_colors/default_color = Color(0.92, 0.95, 1, 1)
|
||||||
|
theme_override_font_sizes/normal_font_size = 18
|
||||||
|
|
||||||
|
[node name="Entries" type="RichTextLabel" parent="."]
|
||||||
|
layout_mode = 1
|
||||||
|
anchors_preset = 15
|
||||||
|
anchor_right = 1.0
|
||||||
|
anchor_bottom = 1.0
|
||||||
|
offset_left = 30.0
|
||||||
|
offset_top = 238.0
|
||||||
|
offset_right = -30.0
|
||||||
|
offset_bottom = -48.0
|
||||||
|
grow_horizontal = 2
|
||||||
|
grow_vertical = 2
|
||||||
|
theme_override_colors/default_color = Color(1, 0.9, 0.5, 1)
|
||||||
|
theme_override_font_sizes/normal_font_size = 17
|
||||||
|
|
||||||
|
[node name="Help" type="Label" parent="."]
|
||||||
|
layout_mode = 1
|
||||||
|
anchors_preset = 10
|
||||||
|
anchor_left = 0.5
|
||||||
|
anchor_top = 1.0
|
||||||
|
anchor_right = 0.5
|
||||||
|
anchor_bottom = 1.0
|
||||||
|
offset_left = -270.0
|
||||||
|
offset_top = -42.0
|
||||||
|
offset_right = 270.0
|
||||||
|
offset_bottom = -18.0
|
||||||
|
grow_horizontal = 2
|
||||||
|
theme_override_colors/font_color = Color(1, 0.84, 0.42, 1)
|
||||||
|
theme_override_font_sizes/font_size = 16
|
||||||
|
text = "↑/↓ Choisir Entrée Confirmer Échap Partir"
|
||||||
|
horizontal_alignment = 1
|
||||||
@@ -0,0 +1,106 @@
|
|||||||
|
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 {}
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
uid://1obxnr8v4ywo
|
||||||
@@ -0,0 +1,40 @@
|
|||||||
|
class_name EventState
|
||||||
|
extends RefCounted
|
||||||
|
|
||||||
|
# State is deliberately small and serializable into FloorState.events. A world
|
||||||
|
# event can be entered once, resumed while open, and can never be retriggered
|
||||||
|
# after its terminal choice has completed it.
|
||||||
|
var event_id: StringName = &""
|
||||||
|
var node_id: StringName = &""
|
||||||
|
var triggered := false
|
||||||
|
var completed := false
|
||||||
|
|
||||||
|
func _init(new_event_id: StringName = &"") -> void:
|
||||||
|
event_id = new_event_id
|
||||||
|
|
||||||
|
static func from_record(record: Dictionary) -> EventState:
|
||||||
|
var state := EventState.new(StringName(record.get("event_id", "")))
|
||||||
|
state.node_id = StringName(record.get("node_id", ""))
|
||||||
|
state.triggered = bool(record.get("triggered", false))
|
||||||
|
state.completed = bool(record.get("completed", record.get("resolved", false)))
|
||||||
|
return state
|
||||||
|
|
||||||
|
func to_record(record: Dictionary = {}) -> Dictionary:
|
||||||
|
var serialized := record.duplicate(true)
|
||||||
|
serialized["event_id"] = event_id
|
||||||
|
serialized["node_id"] = node_id
|
||||||
|
serialized["triggered"] = triggered
|
||||||
|
serialized["completed"] = completed
|
||||||
|
# Keep the existing world-populator field compatible with prior saves.
|
||||||
|
serialized["resolved"] = completed
|
||||||
|
return serialized
|
||||||
|
|
||||||
|
func current_node(definition: Dictionary) -> Dictionary:
|
||||||
|
var nodes: Variant = definition.get("nodes", {})
|
||||||
|
if not nodes is Dictionary:
|
||||||
|
return {}
|
||||||
|
var node: Variant = nodes.get(String(node_id), {})
|
||||||
|
return node if node is Dictionary else {}
|
||||||
|
|
||||||
|
func is_over() -> bool:
|
||||||
|
return completed
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
uid://5vv66glu5mgv
|
||||||
@@ -26,6 +26,8 @@ const InventoryPanelType = preload("res://scripts/presentation/inventory_panel.g
|
|||||||
const EconomyType = preload("res://scripts/domain/items/economy.gd")
|
const EconomyType = preload("res://scripts/domain/items/economy.gd")
|
||||||
const CraftingEngineType = preload("res://scripts/domain/items/crafting_engine.gd")
|
const CraftingEngineType = preload("res://scripts/domain/items/crafting_engine.gd")
|
||||||
const DialogueEngineType = preload("res://scripts/domain/dialogue/dialogue_engine.gd")
|
const DialogueEngineType = preload("res://scripts/domain/dialogue/dialogue_engine.gd")
|
||||||
|
const EventEngineType = preload("res://scripts/domain/narrative/event_engine.gd")
|
||||||
|
const EventStateType = preload("res://scripts/domain/narrative/event_state.gd")
|
||||||
const PlayerStateType = preload("res://scripts/domain/entities/player_state.gd")
|
const PlayerStateType = preload("res://scripts/domain/entities/player_state.gd")
|
||||||
@onready var event_presenter: EventPresenter = $EventPresenter
|
@onready var event_presenter: EventPresenter = $EventPresenter
|
||||||
@onready var map_renderer: MapRenderer = $World/MapRenderer
|
@onready var map_renderer: MapRenderer = $World/MapRenderer
|
||||||
@@ -40,6 +42,7 @@ const PlayerStateType = preload("res://scripts/domain/entities/player_state.gd")
|
|||||||
@onready var shop_panel: ShopPanel = $HUD/ShopPanel
|
@onready var shop_panel: ShopPanel = $HUD/ShopPanel
|
||||||
@onready var crafting_panel: CraftingPanel = $HUD/CraftingPanel
|
@onready var crafting_panel: CraftingPanel = $HUD/CraftingPanel
|
||||||
@onready var dialogue_panel: DialoguePanel = $HUD/DialoguePanel
|
@onready var dialogue_panel: DialoguePanel = $HUD/DialoguePanel
|
||||||
|
@onready var event_panel: EventPanel = $HUD/EventPanel
|
||||||
var pending_loot: Dictionary = {}
|
var pending_loot: Dictionary = {}
|
||||||
var active_shop_npc: Dictionary = {}
|
var active_shop_npc: Dictionary = {}
|
||||||
@onready var audio_library = $AudioLibrary
|
@onready var audio_library = $AudioLibrary
|
||||||
@@ -56,8 +59,10 @@ var spell_catalog: ContentCatalog
|
|||||||
var spell_state: SpellState
|
var spell_state: SpellState
|
||||||
var crafting_engine: CraftingEngine
|
var crafting_engine: CraftingEngine
|
||||||
var dialogue_engine: DialogueEngine
|
var dialogue_engine: DialogueEngine
|
||||||
|
var event_engine: EventEngine
|
||||||
var dialogue_player: PlayerState
|
var dialogue_player: PlayerState
|
||||||
var active_dialogue_npc: Dictionary = {}
|
var active_dialogue_npc: Dictionary = {}
|
||||||
|
var active_event_index := -1
|
||||||
|
|
||||||
func configure_new_run(settings: RunSettings, player_name: String, base_archetype: StringName) -> void:
|
func configure_new_run(settings: RunSettings, player_name: String, base_archetype: StringName) -> void:
|
||||||
pending_run_settings = settings
|
pending_run_settings = settings
|
||||||
@@ -100,6 +105,7 @@ func _initialize_run_state() -> void:
|
|||||||
spell_catalog = ContentCatalogType.load_from_directory("res://data/ascii")
|
spell_catalog = ContentCatalogType.load_from_directory("res://data/ascii")
|
||||||
crafting_engine = CraftingEngineType.new(spell_catalog)
|
crafting_engine = CraftingEngineType.new(spell_catalog)
|
||||||
dialogue_engine = DialogueEngineType.new()
|
dialogue_engine = DialogueEngineType.new()
|
||||||
|
event_engine = EventEngineType.new()
|
||||||
dialogue_player = PlayerStateType.new({"id": game_state.player.id})
|
dialogue_player = PlayerStateType.new({"id": game_state.player.id})
|
||||||
game_state.content_catalog = spell_catalog
|
game_state.content_catalog = spell_catalog
|
||||||
spell_state = SpellStateType.new([&"spell_epines_empoisonnees"])
|
spell_state = SpellStateType.new([&"spell_epines_empoisonnees"])
|
||||||
@@ -159,6 +165,20 @@ func _prepare_exit_capture_state() -> void:
|
|||||||
_sync_camera_to_player()
|
_sync_camera_to_player()
|
||||||
|
|
||||||
func _unhandled_input(event: InputEvent) -> void:
|
func _unhandled_input(event: InputEvent) -> void:
|
||||||
|
if event_panel.visible and event is InputEventKey and event.is_pressed():
|
||||||
|
if event.keycode == KEY_UP:
|
||||||
|
event_panel.select_next(-1)
|
||||||
|
elif event.keycode == KEY_DOWN:
|
||||||
|
event_panel.select_next(1)
|
||||||
|
elif event.keycode == KEY_ENTER or event.keycode == KEY_SPACE:
|
||||||
|
_complete_event_choice()
|
||||||
|
elif event.keycode == KEY_ESCAPE:
|
||||||
|
_close_event()
|
||||||
|
else:
|
||||||
|
get_viewport().set_input_as_handled()
|
||||||
|
return
|
||||||
|
get_viewport().set_input_as_handled()
|
||||||
|
return
|
||||||
if dialogue_panel.visible and event is InputEventKey and event.is_pressed():
|
if dialogue_panel.visible and event is InputEventKey and event.is_pressed():
|
||||||
if event.keycode == KEY_UP:
|
if event.keycode == KEY_UP:
|
||||||
dialogue_panel.select_next(-1)
|
dialogue_panel.select_next(-1)
|
||||||
@@ -442,6 +462,10 @@ func get_actor_cell_rect(actor_id: StringName) -> Rect2:
|
|||||||
return Rect2()
|
return Rect2()
|
||||||
|
|
||||||
func _process_interact() -> Array[TurnResult]:
|
func _process_interact() -> Array[TurnResult]:
|
||||||
|
var event_index := _event_at_player_cell()
|
||||||
|
if event_index >= 0:
|
||||||
|
_open_event(event_index)
|
||||||
|
return []
|
||||||
var npc := _npc_at_or_adjacent()
|
var npc := _npc_at_or_adjacent()
|
||||||
if not npc.is_empty():
|
if not npc.is_empty():
|
||||||
_open_dialogue(npc)
|
_open_dialogue(npc)
|
||||||
@@ -468,6 +492,71 @@ func _process_interact() -> Array[TurnResult]:
|
|||||||
_transition_floor(results[0].from_cell)
|
_transition_floor(results[0].from_cell)
|
||||||
return results
|
return results
|
||||||
|
|
||||||
|
func _event_at_player_cell() -> int:
|
||||||
|
for index in range(game_state.floor_state.events.size()):
|
||||||
|
var record: Dictionary = game_state.floor_state.events[index]
|
||||||
|
var serialized_cell: Variant = record.get("cell", [])
|
||||||
|
if serialized_cell is Array and serialized_cell.size() >= 2 and Vector2i(int(serialized_cell[0]), int(serialized_cell[1])) == game_state.player.cell:
|
||||||
|
return index
|
||||||
|
return -1
|
||||||
|
|
||||||
|
func _open_event(index: int) -> void:
|
||||||
|
var record: Dictionary = game_state.floor_state.events[index]
|
||||||
|
var state := EventStateType.from_record(record)
|
||||||
|
var definition := spell_catalog.get_event(state.event_id)
|
||||||
|
if definition.is_empty():
|
||||||
|
_record_narrative_event(&"event_unavailable", state.event_id, {}, "Événement indisponible.")
|
||||||
|
return
|
||||||
|
if state.completed:
|
||||||
|
_record_narrative_event(&"event_already_completed", state.event_id, {}, "Cet événement est déjà terminé.")
|
||||||
|
return
|
||||||
|
var context := {"player": dialogue_player, "payload": {"gold": run_state.gold}, "catalog": spell_catalog, "inventory": run_state.inventory}
|
||||||
|
var opened := event_engine.trigger(definition, state, context)
|
||||||
|
if not bool(opened.get("accepted", false)):
|
||||||
|
_record_narrative_event(&"event_unavailable", state.event_id, {"reason": opened.get("reason", &"")}, "Événement indisponible.")
|
||||||
|
return
|
||||||
|
active_event_index = index
|
||||||
|
_store_event_state(state, context)
|
||||||
|
event_panel.present(event_engine, definition, state, context)
|
||||||
|
_record_narrative_event(&"event_started", state.event_id, {"node_id": state.node_id}, "Vous examinez : %s." % String(definition.get("id", state.event_id)))
|
||||||
|
|
||||||
|
func _complete_event_choice() -> void:
|
||||||
|
var state := event_panel.current_state()
|
||||||
|
var choices := event_panel.visible_choices()
|
||||||
|
if state == null or choices.is_empty():
|
||||||
|
return
|
||||||
|
var choice: Dictionary = choices[event_panel._selected_index]
|
||||||
|
var node_id := state.node_id
|
||||||
|
var outcome := event_panel.choose_selected()
|
||||||
|
if not bool(outcome.get("accepted", false)):
|
||||||
|
return
|
||||||
|
_store_event_state(state, event_panel._context)
|
||||||
|
_record_narrative_event(&"event_choice_selected", state.event_id, {"node_id": node_id, "choice_text": String(choice.get("text", "")), "applications": outcome.get("applications", [])}, String(choice.get("text", "")))
|
||||||
|
if bool(outcome.get("completed", false)):
|
||||||
|
_close_event(false)
|
||||||
|
_record_narrative_event(&"event_completed", state.event_id, {}, "Événement terminé.")
|
||||||
|
|
||||||
|
func _close_event(journal: bool = true) -> void:
|
||||||
|
if not event_panel.visible:
|
||||||
|
return
|
||||||
|
var state := event_panel.current_state()
|
||||||
|
event_panel.dismiss()
|
||||||
|
active_event_index = -1
|
||||||
|
if journal and state != null:
|
||||||
|
_record_narrative_event(&"event_closed", state.event_id, {}, "Vous quittez l’événement.")
|
||||||
|
|
||||||
|
func _store_event_state(state: EventState, context: Dictionary) -> void:
|
||||||
|
if active_event_index < 0 or active_event_index >= game_state.floor_state.events.size():
|
||||||
|
return
|
||||||
|
var record: Dictionary = game_state.floor_state.events[active_event_index]
|
||||||
|
game_state.floor_state.events[active_event_index] = state.to_record(record)
|
||||||
|
run_state.gold = int(context.get("payload", {}).get("gold", run_state.gold))
|
||||||
|
|
||||||
|
func _record_narrative_event(kind: StringName, event_id: StringName, payload: Dictionary, message: String) -> Array[GameEvent]:
|
||||||
|
var event_payload := {"event_id": event_id}
|
||||||
|
event_payload.merge(payload, true)
|
||||||
|
return _record_ranged_event(kind, &"", event_payload, message)
|
||||||
|
|
||||||
func _process_ground_pickup() -> Array[TurnResult]:
|
func _process_ground_pickup() -> Array[TurnResult]:
|
||||||
var pickup := run_state.inventory.pick_up_ground_item(game_state.floor_state, game_state.player.cell)
|
var pickup := run_state.inventory.pick_up_ground_item(game_state.floor_state, game_state.player.cell)
|
||||||
var item: ItemInstance = pickup.item
|
var item: ItemInstance = pickup.item
|
||||||
|
|||||||
@@ -0,0 +1,67 @@
|
|||||||
|
class_name EventPanel
|
||||||
|
extends PanelContainer
|
||||||
|
|
||||||
|
@onready var title: Label = $Title
|
||||||
|
@onready var body: RichTextLabel = $Body
|
||||||
|
@onready var entries: RichTextLabel = $Entries
|
||||||
|
|
||||||
|
var _engine: EventEngine
|
||||||
|
var _definition: Dictionary = {}
|
||||||
|
var _state: EventState
|
||||||
|
var _context: Dictionary = {}
|
||||||
|
var _selected_index := 0
|
||||||
|
|
||||||
|
func present(engine: EventEngine, definition: Dictionary, state: EventState, context: Dictionary) -> void:
|
||||||
|
_engine = engine
|
||||||
|
_definition = definition
|
||||||
|
_state = state
|
||||||
|
_context = context
|
||||||
|
_selected_index = 0
|
||||||
|
refresh()
|
||||||
|
visible = true
|
||||||
|
|
||||||
|
func dismiss() -> void:
|
||||||
|
visible = false
|
||||||
|
_engine = null
|
||||||
|
_definition = {}
|
||||||
|
_state = null
|
||||||
|
_context = {}
|
||||||
|
|
||||||
|
func select_next(delta: int) -> void:
|
||||||
|
var count := visible_choices().size()
|
||||||
|
if count == 0:
|
||||||
|
return
|
||||||
|
_selected_index = posmod(_selected_index + delta, count)
|
||||||
|
refresh()
|
||||||
|
|
||||||
|
func choose_selected() -> Dictionary:
|
||||||
|
if _engine == null or _state == null:
|
||||||
|
return {"accepted": false, "reason": &"inactive"}
|
||||||
|
var outcome := _engine.choose(_definition, _state, _selected_index, _context)
|
||||||
|
if bool(outcome.get("accepted", false)) and not _state.is_over():
|
||||||
|
_selected_index = 0
|
||||||
|
refresh()
|
||||||
|
return outcome
|
||||||
|
|
||||||
|
func current_state() -> EventState:
|
||||||
|
return _state
|
||||||
|
|
||||||
|
func definition() -> Dictionary:
|
||||||
|
return _definition
|
||||||
|
|
||||||
|
func visible_choices() -> Array[Dictionary]:
|
||||||
|
return _engine.visible_choices(_definition, _state, _context) if _engine != null and _state != null else []
|
||||||
|
|
||||||
|
func refresh() -> void:
|
||||||
|
if _state == null:
|
||||||
|
return
|
||||||
|
var node := _state.current_node(_definition)
|
||||||
|
title.text = "Énigme" if _definition.get("tags", []).has("puzzle") else "Événement"
|
||||||
|
var text_value: Variant = node.get("text", "")
|
||||||
|
body.text = "\n".join(text_value) if text_value is Array else String(text_value)
|
||||||
|
var lines: PackedStringArray = []
|
||||||
|
var choices := visible_choices()
|
||||||
|
for index in range(choices.size()):
|
||||||
|
var cursor := "> " if index == _selected_index else " "
|
||||||
|
lines.append("%s%s" % [cursor, String(choices[index].get("text", ""))])
|
||||||
|
entries.text = "\n".join(lines)
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
uid://i2kut70gntxk
|
||||||
@@ -0,0 +1,71 @@
|
|||||||
|
extends GutTest
|
||||||
|
|
||||||
|
const GameScene = preload("res://scenes/Game.tscn")
|
||||||
|
|
||||||
|
var game: Game
|
||||||
|
|
||||||
|
func before_each() -> void:
|
||||||
|
game = GameScene.instantiate()
|
||||||
|
add_child_autofree(game)
|
||||||
|
await get_tree().process_frame
|
||||||
|
|
||||||
|
func test_real_input_dispatches_one_shot_note_event_and_persists_completion() -> void:
|
||||||
|
var state := game.get_game_state()
|
||||||
|
state.enemies.clear()
|
||||||
|
state.floor_state.events.clear()
|
||||||
|
state.floor_state.events.append({"event_id": &"event_narrative_note", "cell": [state.player.cell.x, state.player.cell.y], "triggered": false, "completed": false})
|
||||||
|
|
||||||
|
await _dispatch_key(KEY_E)
|
||||||
|
var panel := game.get_node("HUD/EventPanel") as PanelContainer
|
||||||
|
assert_true(panel.visible)
|
||||||
|
assert_string_contains((panel.get_node("Title") as Label).text, "Événement")
|
||||||
|
assert_string_contains((panel.get_node("Entries") as RichTextLabel).get_parsed_text(), "Prendre la note")
|
||||||
|
assert_eq(game.get_run_state().recent_events(1)[0].kind, &"event_started")
|
||||||
|
|
||||||
|
await _dispatch_key(KEY_ENTER)
|
||||||
|
assert_true(game.get_dialogue_player().flags.get(&"found_warning_note", false))
|
||||||
|
assert_true(panel.visible)
|
||||||
|
await _dispatch_key(KEY_ENTER)
|
||||||
|
assert_false(panel.visible)
|
||||||
|
assert_true(state.floor_state.events[0].completed)
|
||||||
|
assert_eq(game.get_run_state().recent_events(2).map(func(event: GameEvent) -> StringName: return event.kind), [&"event_choice_selected", &"event_completed"])
|
||||||
|
|
||||||
|
await _dispatch_key(KEY_E)
|
||||||
|
assert_false(panel.visible)
|
||||||
|
assert_eq(game.get_run_state().recent_events(1)[0].kind, &"event_already_completed")
|
||||||
|
|
||||||
|
func test_real_input_solves_pressure_plate_puzzle_and_journals_reward() -> void:
|
||||||
|
var state := game.get_game_state()
|
||||||
|
state.enemies.clear()
|
||||||
|
state.floor_state.events.clear()
|
||||||
|
state.floor_state.events.append({"event_id": &"event_puzzle_pressure_plates", "cell": [state.player.cell.x, state.player.cell.y], "triggered": false, "completed": false})
|
||||||
|
game.get_dialogue_player().skills[&"observation"] = 2
|
||||||
|
game.get_dialogue_player().attributes.set_attribute(&"per", 12)
|
||||||
|
|
||||||
|
await _dispatch_key(KEY_E)
|
||||||
|
var panel := game.get_node("HUD/EventPanel") as PanelContainer
|
||||||
|
assert_true(panel.visible)
|
||||||
|
assert_string_contains((panel.get_node("Title") as Label).text, "Énigme")
|
||||||
|
assert_string_contains((panel.get_node("Entries") as RichTextLabel).get_parsed_text(), "Chercher un mecanisme")
|
||||||
|
|
||||||
|
await _dispatch_key(KEY_ENTER)
|
||||||
|
assert_string_contains((panel.get_node("Body") as RichTextLabel).get_parsed_text(), "compartiment cache")
|
||||||
|
assert_eq(game.get_run_state().gold, 25)
|
||||||
|
assert_true(game.get_run_state().inventory.items.any(func(item: ItemInstance) -> bool: return item.definition.id == &"consumable_stimpack"))
|
||||||
|
await _dispatch_key(KEY_ENTER)
|
||||||
|
assert_false(panel.visible)
|
||||||
|
assert_true(state.floor_state.events[0].completed)
|
||||||
|
assert_eq(game.get_run_state().recent_events(1)[0].kind, &"event_completed")
|
||||||
|
|
||||||
|
func _dispatch_key(keycode: Key) -> void:
|
||||||
|
Input.parse_input_event(_key_event(keycode, true))
|
||||||
|
await get_tree().process_frame
|
||||||
|
Input.parse_input_event(_key_event(keycode, false))
|
||||||
|
await get_tree().process_frame
|
||||||
|
|
||||||
|
func _key_event(keycode: Key, pressed: bool) -> InputEventKey:
|
||||||
|
var event := InputEventKey.new()
|
||||||
|
event.keycode = keycode
|
||||||
|
event.physical_keycode = keycode
|
||||||
|
event.pressed = pressed
|
||||||
|
return event
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
uid://dgd4fqg8hf632
|
||||||
@@ -0,0 +1,48 @@
|
|||||||
|
extends GutTest
|
||||||
|
|
||||||
|
const ContentCatalogType = preload("res://scripts/domain/content/content_catalog.gd")
|
||||||
|
const EventEngineType = preload("res://scripts/domain/narrative/event_engine.gd")
|
||||||
|
const EventStateType = preload("res://scripts/domain/narrative/event_state.gd")
|
||||||
|
const PlayerStateType = preload("res://scripts/domain/entities/player_state.gd")
|
||||||
|
const InventoryStateType = preload("res://scripts/domain/inventory/inventory_state.gd")
|
||||||
|
|
||||||
|
func test_one_shot_event_dispatch_persists_trigger_choice_and_completion() -> void:
|
||||||
|
var catalog := ContentCatalogType.load_from_directory("res://data/ascii")
|
||||||
|
var engine: EventEngine = EventEngineType.new()
|
||||||
|
var record: Dictionary = {"event_id": &"event_narrative_note", "cell": [4, 4], "triggered": false, "completed": false}
|
||||||
|
var state: EventState = EventStateType.from_record(record)
|
||||||
|
var player: PlayerState = PlayerStateType.new()
|
||||||
|
|
||||||
|
var opened: Dictionary = engine.trigger(catalog.get_event(&"event_narrative_note"), state, {"player": player})
|
||||||
|
assert_true(opened.accepted)
|
||||||
|
assert_true(state.triggered)
|
||||||
|
assert_eq(state.node_id, &"root")
|
||||||
|
assert_true(EventStateType.from_record(state.to_record(record)).triggered)
|
||||||
|
|
||||||
|
var definition: Dictionary = catalog.get_event(&"event_narrative_note")
|
||||||
|
var keep_index: int = engine.visible_choices(definition, state, {"player": player}).find_custom(func(choice: Dictionary) -> bool: return String(choice.text).contains("Prendre"))
|
||||||
|
var chosen: Dictionary = engine.choose(definition, state, keep_index, {"player": player})
|
||||||
|
assert_true(chosen.accepted)
|
||||||
|
assert_eq(state.node_id, &"keep")
|
||||||
|
assert_true(player.flags.get(&"found_warning_note", false))
|
||||||
|
|
||||||
|
var finish: Dictionary = engine.choose(definition, state, 0, {"player": player})
|
||||||
|
assert_true(finish.completed)
|
||||||
|
assert_true(EventStateType.from_record(state.to_record(record)).completed)
|
||||||
|
assert_false(engine.trigger(definition, state, {"player": player}).accepted)
|
||||||
|
|
||||||
|
func test_puzzle_skill_check_dispatches_authored_solution_effects() -> void:
|
||||||
|
var catalog := ContentCatalogType.load_from_directory("res://data/ascii")
|
||||||
|
var engine: EventEngine = EventEngineType.new()
|
||||||
|
var player: PlayerState = PlayerStateType.new({"skills": {"observation": 2}, "attributes": {"per": 12}})
|
||||||
|
var state: EventState = EventStateType.new(&"event_puzzle_pressure_plates")
|
||||||
|
var context: Dictionary = {"player": player, "payload": {"gold": 0}, "catalog": catalog, "inventory": InventoryStateType.new()}
|
||||||
|
|
||||||
|
var definition: Dictionary = catalog.get_event(state.event_id)
|
||||||
|
assert_true(engine.trigger(definition, state, context).accepted)
|
||||||
|
var solve_index: int = engine.visible_choices(definition, state, context).find_custom(func(choice: Dictionary) -> bool: return String(choice.text).contains("mecanisme"))
|
||||||
|
var outcome: Dictionary = engine.choose(definition, state, solve_index, context)
|
||||||
|
|
||||||
|
assert_eq(state.node_id, &"solved")
|
||||||
|
assert_eq(int(context.payload.gold), 25)
|
||||||
|
assert_true(outcome.applications.any(func(application: Dictionary) -> bool: return application.type == &"give_item" and application.item_id == &"consumable_stimpack"))
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
uid://bqgmkmeisl2js
|
||||||
Reference in New Issue
Block a user