Merge pull request 'G4: Complete graphical feedback pass' (#50) from slice/g4-presentation-polish into main
This commit was merged in pull request #50.
This commit is contained in:
+9
-1
@@ -1,4 +1,4 @@
|
||||
[gd_scene load_steps=17 format=3]
|
||||
[gd_scene load_steps=19 format=3]
|
||||
|
||||
[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"]
|
||||
@@ -8,6 +8,8 @@
|
||||
[ext_resource type="Script" path="res://scripts/infrastructure/audio_library.gd" id="6_audio"]
|
||||
[ext_resource type="Script" path="res://scripts/presentation/event_presenter.gd" id="7_presenter"]
|
||||
[ext_resource type="PackedScene" path="res://scenes/overlays/CombatFeedback.tscn" id="8_feedback"]
|
||||
[ext_resource type="Script" path="res://scripts/presentation/door_chest_feedback.gd" id="31_door_chest_feedback"]
|
||||
[ext_resource type="Script" path="res://scripts/presentation/loot_feedback.gd" id="32_loot_feedback"]
|
||||
[ext_resource type="PackedScene" path="res://scenes/overlays/TargetingOverlay.tscn" id="9_targeting"]
|
||||
[ext_resource type="PackedScene" path="res://scenes/overlays/SpellTargetingOverlay.tscn" id="10_spell_targeting"]
|
||||
[ext_resource type="PackedScene" path="res://scenes/ui/LootPanel.tscn" id="11_loot"]
|
||||
@@ -70,6 +72,12 @@ script = ExtResource("7_presenter")
|
||||
|
||||
[node name="CombatFeedback" parent="EventPresenter" instance=ExtResource("8_feedback")]
|
||||
|
||||
[node name="DoorChestFeedback" type="Node2D" parent="EventPresenter"]
|
||||
script = ExtResource("31_door_chest_feedback")
|
||||
|
||||
[node name="LootFeedback" type="Node" parent="EventPresenter"]
|
||||
script = ExtResource("32_loot_feedback")
|
||||
|
||||
[node name="World" type="Node2D" parent="."]
|
||||
position = Vector2(32, 48)
|
||||
|
||||
|
||||
@@ -0,0 +1,43 @@
|
||||
class_name DoorChestFeedback
|
||||
extends Node2D
|
||||
|
||||
const EFFECT_DURATION_MS := 160
|
||||
const EFFECT_DURATION := float(EFFECT_DURATION_MS) / 1000.0
|
||||
const DOOR_COLOR := Color(0.48, 0.8, 1.0, 1.0)
|
||||
const CHEST_COLOR := Color(1.0, 0.8, 0.3, 1.0)
|
||||
|
||||
var _test_mode := false
|
||||
var _last_feedback_state := {}
|
||||
|
||||
func _enter_tree() -> void:
|
||||
_test_mode = "--test-mode" in OS.get_cmdline_user_args() and not Engine.is_editor_hint()
|
||||
|
||||
func set_test_mode_for_testing(enabled: bool) -> void:
|
||||
_test_mode = enabled
|
||||
|
||||
func get_last_feedback_state() -> Dictionary:
|
||||
return _last_feedback_state.duplicate()
|
||||
|
||||
func begin(event: GameEvent, presentation_kind: StringName) -> void:
|
||||
var color := DOOR_COLOR if presentation_kind == &"door" else CHEST_COLOR
|
||||
_last_feedback_state = {
|
||||
"kind": presentation_kind,
|
||||
"duration_ms": EFFECT_DURATION_MS,
|
||||
"color": color,
|
||||
}
|
||||
if _test_mode:
|
||||
return
|
||||
var marker := Polygon2D.new()
|
||||
marker.polygon = PackedVector2Array([Vector2(0, -10), Vector2(10, 0), Vector2(0, 10), Vector2(-10, 0)])
|
||||
marker.color = color
|
||||
marker.position = Vector2(event.cell) * Vector2(32, 32) + Vector2(16, 16)
|
||||
if get_viewport() != null:
|
||||
marker.position += get_viewport().get_visible_rect().size * 0.5
|
||||
marker.z_index = 11
|
||||
add_child(marker)
|
||||
var tween := create_tween()
|
||||
tween.set_parallel(true)
|
||||
tween.tween_property(marker, "scale", Vector2(1.8, 1.8), EFFECT_DURATION)
|
||||
tween.tween_property(marker, "modulate:a", 0.0, EFFECT_DURATION)
|
||||
tween.chain().tween_callback(marker.queue_free)
|
||||
await tween.finished
|
||||
@@ -0,0 +1 @@
|
||||
uid://dd8w2d6h57ult
|
||||
@@ -2,19 +2,37 @@ class_name EventPresenter
|
||||
extends Node
|
||||
|
||||
signal event_presented(event: GameEvent)
|
||||
signal presentation_started(presentation: PresentationEvent)
|
||||
|
||||
const CombatFeedbackType = preload("res://scripts/presentation/combat_feedback.gd")
|
||||
const DoorChestFeedbackType = preload("res://scripts/presentation/door_chest_feedback.gd")
|
||||
const LootFeedbackType = preload("res://scripts/presentation/loot_feedback.gd")
|
||||
const PresentationEventType = preload("res://scripts/presentation/presentation_event.gd")
|
||||
|
||||
const COMBAT_COLOR := Color(1.0, 0.45, 0.45, 1.0)
|
||||
const SPELL_COLOR := Color(0.72, 0.58, 1.0, 1.0)
|
||||
const DEATH_COLOR := Color(1.0, 0.38, 0.34, 1.0)
|
||||
|
||||
var _queue: Array[GameEvent] = []
|
||||
var _presenting := false
|
||||
var _actor_lookup: Callable
|
||||
@onready var _combat_feedback: CombatFeedback = get_node_or_null("CombatFeedback") as CombatFeedback
|
||||
@onready var _door_chest_feedback: DoorChestFeedback = get_node_or_null("DoorChestFeedback") as DoorChestFeedback
|
||||
@onready var _loot_feedback: LootFeedback = get_node_or_null("LootFeedback") as LootFeedback
|
||||
|
||||
func _ready() -> void:
|
||||
if _combat_feedback == null:
|
||||
_combat_feedback = CombatFeedbackType.new()
|
||||
_combat_feedback.name = "CombatFeedback"
|
||||
add_child(_combat_feedback)
|
||||
if _door_chest_feedback == null:
|
||||
_door_chest_feedback = DoorChestFeedbackType.new()
|
||||
_door_chest_feedback.name = "DoorChestFeedback"
|
||||
add_child(_door_chest_feedback)
|
||||
if _loot_feedback == null:
|
||||
_loot_feedback = LootFeedbackType.new()
|
||||
_loot_feedback.name = "LootFeedback"
|
||||
add_child(_loot_feedback)
|
||||
|
||||
func set_actor_lookup(actor_lookup: Callable) -> void:
|
||||
_actor_lookup = actor_lookup
|
||||
@@ -29,12 +47,38 @@ func pending_count() -> int:
|
||||
func is_test_mode() -> bool:
|
||||
return _combat_feedback != null and _combat_feedback.is_test_mode()
|
||||
|
||||
## Maps a semantic domain event to exactly one presentation event, or null when
|
||||
## no visual feedback is appropriate. The source event stays immutable.
|
||||
func map_event(event: GameEvent) -> PresentationEvent:
|
||||
match event.kind:
|
||||
&"melee", &"enemy_melee", &"damage_dealt", &"spell_damage_dealt", &"attack_started", &"attack_hit":
|
||||
return PresentationEventType.new(event, &"combat", event.message, COMBAT_COLOR)
|
||||
&"door_opened":
|
||||
return PresentationEventType.new(event, &"door", event.message, DoorChestFeedback.DOOR_COLOR)
|
||||
&"chest_opened":
|
||||
return PresentationEventType.new(event, &"chest", event.message, DoorChestFeedback.CHEST_COLOR)
|
||||
&"loot_dropped", &"loot_taken", &"item_picked_up":
|
||||
return PresentationEventType.new(event, &"loot", event.message, LootFeedback.LOOT_COLOR)
|
||||
&"trap_triggered", &"trap_discovered", &"trap_disarmed", &"trap_disarm_failed", &"trap_status_applied":
|
||||
return PresentationEventType.new(event, &"trap", event.message, LootFeedback.TRAP_COLOR)
|
||||
&"projectile_fired", &"spell_cast_started", &"spell_effect_applied":
|
||||
return PresentationEventType.new(event, &"spell", event.message, SPELL_COLOR)
|
||||
&"quest_started", &"quest_updated", &"quest_completed", &"quest_abandoned", &"quest_board_read":
|
||||
return PresentationEventType.new(event, &"quest", event.message, LootFeedback.QUEST_COLOR)
|
||||
&"enemy_defeated", &"actor_died", &"companion_died", &"run_ended", &"permadeath_run_retired":
|
||||
return PresentationEventType.new(event, &"death", event.message, DEATH_COLOR)
|
||||
_:
|
||||
return null
|
||||
|
||||
func present_next() -> void:
|
||||
if _presenting or _queue.is_empty():
|
||||
return
|
||||
_presenting = true
|
||||
var event: GameEvent = _queue.pop_front()
|
||||
if is_test_mode():
|
||||
var test_presentation := map_event(event)
|
||||
if test_presentation != null:
|
||||
presentation_started.emit(test_presentation)
|
||||
event_presented.emit(event)
|
||||
_presenting = false
|
||||
present_next()
|
||||
@@ -42,7 +86,37 @@ func present_next() -> void:
|
||||
_begin_feedback.call_deferred(event)
|
||||
|
||||
func _begin_feedback(event: GameEvent) -> void:
|
||||
await _combat_feedback.begin(event, _actor_lookup)
|
||||
var presentation := map_event(event)
|
||||
if presentation != null:
|
||||
presentation_started.emit(presentation)
|
||||
await _present(presentation)
|
||||
event_presented.emit(event)
|
||||
_presenting = false
|
||||
present_next()
|
||||
|
||||
func _present(presentation: PresentationEvent) -> void:
|
||||
match presentation.kind:
|
||||
&"combat", &"death":
|
||||
await _combat_feedback.begin(presentation.source_event, _actor_lookup)
|
||||
&"spell":
|
||||
await _present_spell(presentation.source_event)
|
||||
&"door", &"chest":
|
||||
await _door_chest_feedback.begin(presentation.source_event, presentation.kind)
|
||||
&"loot", &"trap", &"quest":
|
||||
await _loot_feedback.begin(presentation.source_event, presentation.kind)
|
||||
|
||||
func _present_spell(event: GameEvent) -> void:
|
||||
# A cast has no projectile until a target has been confirmed. This gives each
|
||||
# spell event a brief, non-blocking cast flash instead of silently doing nothing.
|
||||
if event.kind == &"spell_cast_started" or event.target_id.is_empty():
|
||||
await get_tree().create_timer(0.12).timeout
|
||||
return
|
||||
await _combat_feedback.begin(event, _actor_lookup)
|
||||
|
||||
func get_feedback_metrics() -> Dictionary:
|
||||
return {
|
||||
"combat": _combat_feedback != null,
|
||||
"door_chest": _door_chest_feedback != null,
|
||||
"loot": _loot_feedback != null,
|
||||
"mapped_kinds": 8,
|
||||
}
|
||||
|
||||
@@ -0,0 +1,55 @@
|
||||
class_name LootFeedback
|
||||
extends Node
|
||||
|
||||
const EFFECT_DURATION_MS := 160
|
||||
const EFFECT_DURATION := float(EFFECT_DURATION_MS) / 1000.0
|
||||
const LOOT_COLOR := Color(1.0, 0.82, 0.32, 1.0)
|
||||
const TRAP_COLOR := Color(1.0, 0.38, 0.34, 1.0)
|
||||
const QUEST_COLOR := Color(0.55, 1.0, 0.67, 1.0)
|
||||
|
||||
var _test_mode := false
|
||||
var _last_feedback_state := {}
|
||||
|
||||
func _enter_tree() -> void:
|
||||
_test_mode = "--test-mode" in OS.get_cmdline_user_args() and not Engine.is_editor_hint()
|
||||
|
||||
func set_test_mode_for_testing(enabled: bool) -> void:
|
||||
_test_mode = enabled
|
||||
|
||||
func get_last_feedback_state() -> Dictionary:
|
||||
return _last_feedback_state.duplicate()
|
||||
|
||||
func begin(event: GameEvent, presentation_kind: StringName) -> void:
|
||||
var color := _color_for(presentation_kind)
|
||||
_last_feedback_state = {
|
||||
"kind": presentation_kind,
|
||||
"duration_ms": EFFECT_DURATION_MS,
|
||||
"color": color,
|
||||
}
|
||||
if _test_mode:
|
||||
return
|
||||
var label := Label.new()
|
||||
label.text = _label_for(event, presentation_kind)
|
||||
label.modulate = color
|
||||
label.position = Vector2(390, 80)
|
||||
label.z_index = 20
|
||||
add_child(label)
|
||||
var tween := create_tween()
|
||||
tween.set_parallel(true)
|
||||
tween.tween_property(label, "position:y", label.position.y - 14.0, EFFECT_DURATION)
|
||||
tween.tween_property(label, "modulate:a", 0.0, EFFECT_DURATION)
|
||||
tween.chain().tween_callback(label.queue_free)
|
||||
await tween.finished
|
||||
|
||||
func _color_for(presentation_kind: StringName) -> Color:
|
||||
match presentation_kind:
|
||||
&"trap": return TRAP_COLOR
|
||||
&"quest": return QUEST_COLOR
|
||||
_: return LOOT_COLOR
|
||||
|
||||
func _label_for(event: GameEvent, presentation_kind: StringName) -> String:
|
||||
match presentation_kind:
|
||||
&"loot": return "✦ LOOT SECURED"
|
||||
&"trap": return "! TRAP"
|
||||
&"quest": return "✦ QUEST UPDATED"
|
||||
_: return event.message
|
||||
@@ -0,0 +1 @@
|
||||
uid://dolen1gstmp82
|
||||
@@ -0,0 +1,62 @@
|
||||
class_name PresentationEvent
|
||||
extends RefCounted
|
||||
|
||||
## A display-only, immutable translation of one authoritative GameEvent.
|
||||
## Snapshot data belongs to this class, not an instance, so direct writes cannot
|
||||
## create mutable instance state. Getter calls return a fresh source snapshot.
|
||||
## RefCounted teardown cannot safely call script methods, so the static store is
|
||||
## intentionally process-lifetime; entries contain only frozen presentation data.
|
||||
const FORMER_BACKING_PROPERTIES := [
|
||||
&"_backing_values",
|
||||
&"_is_initialized",
|
||||
&"_source_event",
|
||||
&"_kind",
|
||||
&"_notification",
|
||||
&"_accent",
|
||||
]
|
||||
|
||||
static var _snapshots: Dictionary[int, Dictionary] = {}
|
||||
|
||||
var source_event: GameEvent:
|
||||
get:
|
||||
return _copy_event(_snapshot()[&"source_event"] as GameEvent)
|
||||
|
||||
var kind: StringName:
|
||||
get:
|
||||
return _snapshot()[&"kind"] as StringName
|
||||
|
||||
var notification: String:
|
||||
get:
|
||||
return _snapshot()[&"notification"] as String
|
||||
|
||||
var accent: Color:
|
||||
get:
|
||||
return _snapshot()[&"accent"] as Color
|
||||
|
||||
func _init(source: GameEvent, presentation_kind: StringName, text: String = "", color: Color = Color.WHITE) -> void:
|
||||
_snapshots[get_instance_id()] = {
|
||||
&"source_event": _copy_event(source),
|
||||
&"kind": presentation_kind,
|
||||
&"notification": text if not text.is_empty() else source.message,
|
||||
&"accent": color,
|
||||
}
|
||||
|
||||
## Former fields are intentionally undeclared. _set absorbs legacy direct writes
|
||||
## without creating per-instance storage or changing the captured snapshot.
|
||||
func _set(property: StringName, _value: Variant) -> bool:
|
||||
return property in FORMER_BACKING_PROPERTIES
|
||||
|
||||
func _snapshot() -> Dictionary:
|
||||
return _snapshots.get(get_instance_id(), {}) as Dictionary
|
||||
|
||||
static func _copy_event(event: GameEvent) -> GameEvent:
|
||||
return GameEvent.new(
|
||||
event.turn,
|
||||
event.floor,
|
||||
event.kind,
|
||||
event.payload,
|
||||
event.message,
|
||||
event.actor_id,
|
||||
event.target_id,
|
||||
event.cell,
|
||||
)
|
||||
@@ -0,0 +1 @@
|
||||
uid://vgwy34xn5id7
|
||||
@@ -0,0 +1,38 @@
|
||||
extends GutTest
|
||||
|
||||
const GameScene = preload("res://scenes/Game.tscn")
|
||||
const GameEvent = preload("res://scripts/domain/run/game_event.gd")
|
||||
|
||||
func test_real_game_scene_presents_one_ordered_feedback_event_for_each_major_domain_kind() -> void:
|
||||
var game := GameScene.instantiate() as Game
|
||||
add_child_autofree(game)
|
||||
await get_tree().process_frame
|
||||
var presenter := game.get_node("EventPresenter") as EventPresenter
|
||||
var observed: Array[StringName] = []
|
||||
presenter.presentation_started.connect(func(presentation: PresentationEvent) -> void:
|
||||
observed.append(presentation.kind)
|
||||
)
|
||||
presenter.get_node("CombatFeedback").set_test_mode_for_testing(true)
|
||||
presenter.get_node("DoorChestFeedback").set_test_mode_for_testing(true)
|
||||
presenter.get_node("LootFeedback").set_test_mode_for_testing(true)
|
||||
presenter.enqueue([
|
||||
GameEvent.new(1, 0, &"damage_dealt", {"damage": 3}, "You strike.", &"player", &"goblin"),
|
||||
GameEvent.new(1, 0, &"door_opened", {}, "Door opened.", &"player"),
|
||||
GameEvent.new(1, 0, &"chest_opened", {}, "Chest opened.", &"player"),
|
||||
GameEvent.new(1, 0, &"loot_taken", {}, "Loot taken.", &"player"),
|
||||
GameEvent.new(1, 0, &"trap_triggered", {"damage": 1}, "Trap triggered.", &"player"),
|
||||
GameEvent.new(1, 0, &"spell_cast_started", {}, "Spell cast.", &"player", &"goblin"),
|
||||
GameEvent.new(1, 0, &"quest_started", {}, "Quest started.", &"player"),
|
||||
GameEvent.new(1, 0, &"actor_died", {}, "Goblin defeated.", &"goblin", &"player"),
|
||||
])
|
||||
presenter.present_next()
|
||||
|
||||
await _wait_until_presented(observed, 8)
|
||||
|
||||
assert_eq(observed, [&"combat", &"door", &"chest", &"loot", &"trap", &"spell", &"quest", &"death"])
|
||||
assert_eq(presenter.pending_count(), 0)
|
||||
|
||||
func _wait_until_presented(observed: Array[StringName], expected_count: int) -> void:
|
||||
var deadline := Time.get_ticks_msec() + 2500
|
||||
while observed.size() < expected_count and Time.get_ticks_msec() < deadline:
|
||||
await get_tree().process_frame
|
||||
@@ -0,0 +1 @@
|
||||
uid://ng3wx4vhf7uk
|
||||
@@ -54,3 +54,79 @@ func _wait_for_presented_events(observed: Array[GameEvent], expected_count: int)
|
||||
var deadline := Time.get_ticks_msec() + 1000
|
||||
while observed.size() < expected_count and Time.get_ticks_msec() < deadline:
|
||||
await get_tree().process_frame
|
||||
|
||||
func test_direct_former_backing_field_writes_are_absorbed_after_mapping() -> void:
|
||||
var presenter := EventPresenter.new()
|
||||
add_child_autofree(presenter)
|
||||
var source := GameEvent.new(7, 2, &"damage_dealt", {"damage": 3}, "Goblin takes 3 damage.", &"player", &"goblin", Vector2i(6, 12))
|
||||
var mapped: PresentationEvent = presenter.map_event(source)
|
||||
var replacement := GameEvent.new(99, 9, &"actor_died", {"damage": 99}, "Replacement.")
|
||||
|
||||
# Former fields are undeclared. Leading underscores are not private in
|
||||
# GDScript, so every legacy direct write must be absorbed by _set.
|
||||
mapped._kind = &"death"
|
||||
mapped._source_event = replacement
|
||||
mapped._backing_values = {&"_kind": &"death"}
|
||||
mapped._is_initialized = false
|
||||
|
||||
assert_eq(mapped.kind, &"combat")
|
||||
assert_eq(mapped.notification, "Goblin takes 3 damage.")
|
||||
assert_eq(mapped.accent, EventPresenter.COMBAT_COLOR)
|
||||
assert_eq(mapped.source_event.kind, &"damage_dealt")
|
||||
assert_eq(mapped.source_event.payload, {"damage": 3})
|
||||
assert_eq(source.kind, &"damage_dealt")
|
||||
assert_eq(source.payload, {"damage": 3})
|
||||
assert_ne(mapped.source_event, replacement)
|
||||
|
||||
func test_mapping_captures_an_immutable_source_snapshot() -> void:
|
||||
var presenter := EventPresenter.new()
|
||||
add_child_autofree(presenter)
|
||||
var source := GameEvent.new(7, 2, &"damage_dealt", {"damage": 3, "nested": {"critical": false}}, "Goblin takes 3 damage.", &"player", &"goblin", Vector2i(6, 12))
|
||||
var mapped: PresentationEvent = presenter.map_event(source)
|
||||
var exposed_snapshot := mapped.source_event
|
||||
|
||||
# The adapter captures source values at mapping time, including nested payload.
|
||||
source.kind = &"actor_died"
|
||||
source.payload["damage"] = 7
|
||||
source.payload["nested"]["critical"] = true
|
||||
|
||||
# PresentationEvent only exposes getter-backed values. Assigning to locals or
|
||||
# to a returned snapshot cannot change the adapter or authoritative event.
|
||||
var reassigned_kind := mapped.kind
|
||||
reassigned_kind = &"death"
|
||||
var reassigned_notification := mapped.notification
|
||||
reassigned_notification = "rewritten"
|
||||
var reassigned_accent := mapped.accent
|
||||
reassigned_accent = Color.BLACK
|
||||
exposed_snapshot.kind = &"death"
|
||||
exposed_snapshot.payload["damage"] = 99
|
||||
exposed_snapshot.payload["nested"]["critical"] = true
|
||||
|
||||
assert_eq(mapped.kind, &"combat")
|
||||
assert_eq(mapped.notification, "Goblin takes 3 damage.")
|
||||
assert_eq(mapped.accent, EventPresenter.COMBAT_COLOR)
|
||||
assert_eq(mapped.source_event.kind, &"damage_dealt")
|
||||
assert_eq(mapped.source_event.payload, {"damage": 3, "nested": {"critical": false}})
|
||||
assert_eq(source.kind, &"actor_died")
|
||||
assert_eq(source.payload, {"damage": 7, "nested": {"critical": true}})
|
||||
assert_ne(mapped.source_event, source)
|
||||
assert_ne(mapped.source_event, exposed_snapshot)
|
||||
|
||||
func test_major_domain_event_kinds_each_map_to_one_named_presentation_event() -> void:
|
||||
var presenter := EventPresenter.new()
|
||||
add_child_autofree(presenter)
|
||||
var expected := {
|
||||
&"damage_dealt": &"combat",
|
||||
&"door_opened": &"door",
|
||||
&"chest_opened": &"chest",
|
||||
&"loot_taken": &"loot",
|
||||
&"trap_triggered": &"trap",
|
||||
&"spell_cast_started": &"spell",
|
||||
&"quest_started": &"quest",
|
||||
&"actor_died": &"death",
|
||||
}
|
||||
|
||||
for domain_kind: StringName in expected:
|
||||
var mapped: PresentationEvent = presenter.map_event(GameEvent.new(1, 0, domain_kind))
|
||||
assert_not_null(mapped, "%s should have presentation feedback" % domain_kind)
|
||||
assert_eq(mapped.kind, expected[domain_kind], "%s should map once to its feedback kind" % domain_kind)
|
||||
|
||||
Reference in New Issue
Block a user