149 lines
5.3 KiB
GDScript
149 lines
5.3 KiB
GDScript
class_name CombatFeedback
|
|
extends Node2D
|
|
|
|
signal feedback_completed
|
|
|
|
# Milliseconds are the presentation contract; durations are derived only where
|
|
# Godot's Tween API requires seconds.
|
|
const MOVEMENT_DURATION_MS := 100
|
|
const IMPACT_DURATION_MS := 140
|
|
const DEATH_DURATION_MS := 180
|
|
const MOVEMENT_DURATION := float(MOVEMENT_DURATION_MS) / 1000.0
|
|
const IMPACT_DURATION := float(IMPACT_DURATION_MS) / 1000.0
|
|
const DEATH_DURATION := float(DEATH_DURATION_MS) / 1000.0
|
|
const FLASH_COLOR := Color(1.0, 0.45, 0.45, 1.0)
|
|
# Shape and text duplicate the color cue so feedback remains distinguishable for
|
|
# players with color-vision differences.
|
|
const DAMAGE_MARKER_PREFIX := "✦ -"
|
|
|
|
var _test_mode := false
|
|
var _movement_tween_configuration := {}
|
|
var _impact_feedback_state := {}
|
|
var _death_fade_configuration := {}
|
|
var _active_impact_label: Label
|
|
|
|
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
|
|
|
|
# Public test seam: production mode remains determined from command-line args.
|
|
func set_test_mode_for_testing(enabled: bool) -> void:
|
|
_test_mode = enabled
|
|
|
|
func get_movement_tween_configuration() -> Dictionary:
|
|
return _movement_tween_configuration.duplicate()
|
|
|
|
func get_impact_feedback_state() -> Dictionary:
|
|
return _impact_feedback_state.duplicate()
|
|
|
|
func get_death_fade_configuration() -> Dictionary:
|
|
return _death_fade_configuration.duplicate()
|
|
|
|
func get_active_impact_label() -> Label:
|
|
return _active_impact_label
|
|
|
|
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)
|
|
&"projectile_fired", &"spell_cast_started":
|
|
await _show_projectile(event, actor_lookup)
|
|
&"melee", &"enemy_melee", &"damage_dealt", &"spell_damage_dealt":
|
|
await _show_impact(event, actor_lookup)
|
|
&"enemy_defeated", &"actor_died":
|
|
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 from_position := Vector2(event.payload.get("from_cell", event.cell)) * Vector2(ActorView.TILE_SIZE)
|
|
var destination := Vector2(event.payload.get("to_cell", event.cell)) * Vector2(ActorView.TILE_SIZE)
|
|
_movement_tween_configuration = {
|
|
"property": &"position",
|
|
"from": from_position,
|
|
"to": destination,
|
|
"duration_ms": MOVEMENT_DURATION_MS,
|
|
}
|
|
actor_view.position = from_position
|
|
var tween := create_tween()
|
|
tween.tween_property(actor_view, "position", destination, MOVEMENT_DURATION)
|
|
await tween.finished
|
|
|
|
func _show_projectile(event: GameEvent, actor_lookup: Callable) -> void:
|
|
var source := _find_actor_view(event.actor_id, actor_lookup)
|
|
var target := _find_actor_view(event.target_id, actor_lookup)
|
|
if source == null or target == null:
|
|
return
|
|
var projectile := ColorRect.new()
|
|
projectile.color = Color(1.0, 0.82, 0.25, 1.0)
|
|
projectile.size = Vector2(6, 6)
|
|
projectile.position = source.global_position + Vector2(13, 13)
|
|
projectile.z_index = 12
|
|
add_child(projectile)
|
|
var tween := create_tween()
|
|
tween.tween_property(projectile, "global_position", target.global_position + Vector2(13, 13), MOVEMENT_DURATION)
|
|
await tween.finished
|
|
projectile.queue_free()
|
|
|
|
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)
|
|
_impact_feedback_state = {
|
|
"flash_color": FLASH_COLOR,
|
|
"label_text": _active_impact_label.text,
|
|
"duration_ms": IMPACT_DURATION_MS,
|
|
}
|
|
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
|
|
_death_fade_configuration = {
|
|
"property": &"modulate:a",
|
|
"from_alpha": actor_view.modulate.a,
|
|
"to_alpha": 0.0,
|
|
"duration_ms": DEATH_DURATION_MS,
|
|
}
|
|
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 = "%s%d" % [DAMAGE_MARKER_PREFIX, damage]
|
|
label.position = actor_view.global_position + Vector2(6, -14)
|
|
label.z_index = 10
|
|
add_child(label)
|
|
_active_impact_label = 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
|