feat: add event-driven combat feedback

This commit is contained in:
goose
2026-10-02 13:07:40 -04:00
parent 351a8ad8f6
commit f2f2a42c40
13 changed files with 264 additions and 15 deletions
+83
View File
@@ -0,0 +1,83 @@
class_name CombatFeedback
extends Node2D
signal feedback_completed
const MOVEMENT_DURATION := 0.10
const IMPACT_DURATION := 0.14
const DEATH_DURATION := 0.18
const FLASH_COLOR := Color(1.0, 0.45, 0.45, 1.0)
var _test_mode := false
func _enter_tree() -> void:
_test_mode = "--test-mode" in OS.get_cmdline_user_args() and not Engine.is_editor_hint()
func is_test_mode() -> bool:
return _test_mode
func begin(event: GameEvent, actor_lookup: Callable) -> void:
if _test_mode:
feedback_completed.emit()
return
match event.kind:
&"moved", &"enemy_moved":
await _show_movement(event, actor_lookup)
&"melee", &"enemy_melee", &"damage_dealt":
await _show_impact(event, actor_lookup)
&"enemy_defeated":
await _show_death(event, actor_lookup)
# Existing and future domain events are intentionally presentation no-ops.
_:
pass
feedback_completed.emit()
func _show_movement(event: GameEvent, actor_lookup: Callable) -> void:
var actor_view := _find_actor_view(event.actor_id, actor_lookup)
if actor_view == null:
return
var destination := Vector2(event.payload.get("to_cell", event.cell)) * Vector2(ActorView.TILE_SIZE)
actor_view.position = Vector2(event.payload.get("from_cell", event.cell)) * Vector2(ActorView.TILE_SIZE)
var tween := create_tween()
tween.tween_property(actor_view, "position", destination, MOVEMENT_DURATION)
await tween.finished
func _show_impact(event: GameEvent, actor_lookup: Callable) -> void:
var target_id := event.target_id if not event.target_id.is_empty() else event.actor_id
var actor_view := _find_actor_view(target_id, actor_lookup)
if actor_view == null:
return
var original_modulate := actor_view.modulate
actor_view.last_damage = int(event.payload.get("damage", 0))
actor_view.modulate = FLASH_COLOR
_show_damage_label(actor_view, actor_view.last_damage)
var tween := create_tween()
tween.tween_interval(IMPACT_DURATION)
await tween.finished
if is_instance_valid(actor_view):
actor_view.modulate = original_modulate
func _show_death(event: GameEvent, actor_lookup: Callable) -> void:
var actor_view := _find_actor_view(event.actor_id, actor_lookup)
if actor_view == null:
return
var tween := create_tween()
tween.tween_property(actor_view, "modulate:a", 0.0, DEATH_DURATION)
await tween.finished
func _show_damage_label(actor_view: ActorView, damage: int) -> void:
var label := Label.new()
label.text = "-%d" % damage
label.position = actor_view.global_position + Vector2(6, -14)
label.z_index = 10
add_child(label)
var tween := create_tween()
tween.set_parallel(true)
tween.tween_property(label, "position:y", label.position.y - 16.0, IMPACT_DURATION)
tween.tween_property(label, "modulate:a", 0.0, IMPACT_DURATION)
tween.chain().tween_callback(label.queue_free)
func _find_actor_view(actor_id: StringName, actor_lookup: Callable) -> ActorView:
if actor_lookup.is_valid():
return actor_lookup.call(actor_id) as ActorView
return null
@@ -0,0 +1 @@
uid://5v7pii7x4m1w
+48
View File
@@ -0,0 +1,48 @@
class_name EventPresenter
extends Node
signal event_presented(event: GameEvent)
const CombatFeedbackType = preload("res://scripts/presentation/combat_feedback.gd")
var _queue: Array[GameEvent] = []
var _presenting := false
var _actor_lookup: Callable
@onready var _combat_feedback: CombatFeedback = get_node_or_null("CombatFeedback") as CombatFeedback
func _ready() -> void:
if _combat_feedback == null:
_combat_feedback = CombatFeedbackType.new()
_combat_feedback.name = "CombatFeedback"
add_child(_combat_feedback)
func set_actor_lookup(actor_lookup: Callable) -> void:
_actor_lookup = actor_lookup
func enqueue(events: Array[GameEvent]) -> void:
for event in events:
_queue.append(event)
func pending_count() -> int:
return _queue.size() + (1 if _presenting else 0)
func is_test_mode() -> bool:
return _combat_feedback != null and _combat_feedback.is_test_mode()
func present_next() -> void:
if _presenting or _queue.is_empty():
return
_presenting = true
var event: GameEvent = _queue.pop_front()
if is_test_mode():
event_presented.emit(event)
_presenting = false
present_next()
return
_begin_feedback.call_deferred(event)
func _begin_feedback(event: GameEvent) -> void:
await _combat_feedback.begin(event, _actor_lookup)
event_presented.emit(event)
_presenting = false
present_next()
@@ -0,0 +1 @@
uid://dti482yv2w6xk