test: cover combat feedback presentation contract
This commit is contained in:
@@ -3,12 +3,21 @@ extends Node2D
|
||||
|
||||
signal feedback_completed
|
||||
|
||||
const MOVEMENT_DURATION := 0.10
|
||||
const IMPACT_DURATION := 0.14
|
||||
const DEATH_DURATION := 0.18
|
||||
# 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)
|
||||
|
||||
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()
|
||||
@@ -16,6 +25,22 @@ func _enter_tree() -> void:
|
||||
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()
|
||||
@@ -36,8 +61,15 @@ 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)
|
||||
actor_view.position = Vector2(event.payload.get("from_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
|
||||
@@ -51,6 +83,11 @@ func _show_impact(event: GameEvent, actor_lookup: Callable) -> void:
|
||||
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
|
||||
@@ -61,6 +98,12 @@ 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
|
||||
@@ -71,6 +114,7 @@ func _show_damage_label(actor_view: ActorView, damage: int) -> void:
|
||||
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)
|
||||
|
||||
@@ -0,0 +1,81 @@
|
||||
extends GutTest
|
||||
|
||||
const CombatFeedback = preload("res://scripts/presentation/combat_feedback.gd")
|
||||
const GameEvent = preload("res://scripts/domain/run/game_event.gd")
|
||||
const ActorView = preload("res://scripts/presentation/actor_view.gd")
|
||||
|
||||
func test_test_mode_completes_feedback_immediately_without_presentation_work() -> void:
|
||||
var feedback := CombatFeedback.new()
|
||||
add_child_autofree(feedback)
|
||||
feedback.set_test_mode_for_testing(true)
|
||||
var completions: Array[bool] = []
|
||||
feedback.feedback_completed.connect(func() -> void: completions.append(true))
|
||||
|
||||
await feedback.begin(GameEvent.new(1, 0, &"moved"), Callable())
|
||||
|
||||
assert_eq(completions.size(), 1)
|
||||
assert_eq(feedback.get_movement_tween_configuration(), {})
|
||||
|
||||
func test_timing_contract_is_exactly_100_140_and_180_milliseconds() -> void:
|
||||
assert_eq(CombatFeedback.MOVEMENT_DURATION_MS, 100)
|
||||
assert_eq(CombatFeedback.IMPACT_DURATION_MS, 140)
|
||||
assert_eq(CombatFeedback.DEATH_DURATION_MS, 180)
|
||||
|
||||
func test_movement_exposes_its_tween_configuration() -> void:
|
||||
var feedback := _new_feedback()
|
||||
var actor := _new_actor()
|
||||
var event := GameEvent.new(1, 0, &"moved", {"from_cell": Vector2i(2, 3), "to_cell": Vector2i(4, 3)}, "", &"player")
|
||||
|
||||
feedback.begin(event, func(_actor_id: StringName) -> ActorView: return actor)
|
||||
|
||||
assert_eq(feedback.get_movement_tween_configuration(), {
|
||||
"property": &"position",
|
||||
"from": Vector2(64, 96),
|
||||
"to": Vector2(128, 96),
|
||||
"duration_ms": 100,
|
||||
})
|
||||
assert_eq(actor.position, Vector2(64, 96))
|
||||
await get_tree().create_timer(CombatFeedback.MOVEMENT_DURATION).timeout
|
||||
|
||||
func test_impact_exposes_flash_and_damage_label_state() -> void:
|
||||
var feedback := _new_feedback()
|
||||
var actor := _new_actor()
|
||||
var event := GameEvent.new(1, 0, &"damage_dealt", {"damage": 3}, "", &"player", &"goblin")
|
||||
|
||||
feedback.begin(event, func(_actor_id: StringName) -> ActorView: return actor)
|
||||
|
||||
assert_eq(actor.modulate, CombatFeedback.FLASH_COLOR)
|
||||
assert_eq(actor.last_damage, 3)
|
||||
assert_eq(feedback.get_impact_feedback_state(), {
|
||||
"flash_color": CombatFeedback.FLASH_COLOR,
|
||||
"label_text": "-3",
|
||||
"duration_ms": 140,
|
||||
})
|
||||
assert_eq(feedback.get_active_impact_label().text, "-3")
|
||||
await get_tree().create_timer(CombatFeedback.IMPACT_DURATION).timeout
|
||||
|
||||
func test_death_exposes_alpha_fade_configuration() -> void:
|
||||
var feedback := _new_feedback()
|
||||
var actor := _new_actor()
|
||||
actor.modulate = Color(1.0, 1.0, 1.0, 0.75)
|
||||
var event := GameEvent.new(1, 0, &"enemy_defeated", {}, "", &"goblin")
|
||||
|
||||
feedback.begin(event, func(_actor_id: StringName) -> ActorView: return actor)
|
||||
|
||||
assert_eq(feedback.get_death_fade_configuration(), {
|
||||
"property": &"modulate:a",
|
||||
"from_alpha": 0.75,
|
||||
"to_alpha": 0.0,
|
||||
"duration_ms": 180,
|
||||
})
|
||||
await get_tree().create_timer(CombatFeedback.DEATH_DURATION).timeout
|
||||
|
||||
func _new_feedback() -> CombatFeedback:
|
||||
var feedback := CombatFeedback.new()
|
||||
add_child_autofree(feedback)
|
||||
return feedback
|
||||
|
||||
func _new_actor() -> ActorView:
|
||||
var actor := ActorView.new()
|
||||
add_child_autofree(actor)
|
||||
return actor
|
||||
@@ -0,0 +1 @@
|
||||
uid://dhsj2xp0igavk
|
||||
Reference in New Issue
Block a user