feat: add event-driven combat feedback
This commit is contained in:
@@ -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
|
||||
Reference in New Issue
Block a user