Files
DungeonCrawlerWorld/tests/unit/test_hud_layout.gd
T

109 lines
4.7 KiB
GDScript

extends GutTest
const GameScene = preload("res://scenes/Game.tscn")
var game: Game
func before_each() -> void:
game = GameScene.instantiate()
add_child_autofree(game)
await get_tree().process_frame
func test_help_band_displays_the_exact_control_labels() -> void:
var help_label := game.get_node_or_null("HUD/HelpLabel") as Label
assert_not_null(help_label)
assert_string_contains(help_label.text, "Déplacer : ZQSD / Flèches")
assert_string_contains(help_label.text, "nord-est : pavé num. 9")
assert_string_contains(help_label.text, "Interagir : Espace ou E")
func test_xp_bar_shows_progress_inside_the_current_level_and_refreshes_after_xp_gain() -> void:
var hud := game.get_node("HUD") as GameHud
var state := game.get_game_state()
var xp_bar := hud.get_node_or_null("ExperienceBar") as ProgressBar
var xp_label := hud.get_node_or_null("ExperienceLabel") as Label
assert_not_null(xp_bar)
assert_not_null(xp_label)
assert_eq(xp_bar.min_value, 0.0)
assert_eq(xp_bar.max_value, 100.0)
assert_eq(xp_bar.value, 0.0)
assert_eq(xp_label.text, "Niveau 1 — XP 0/100")
state.player.progression.set_total_xp(125)
hud.refresh(state, [], game.get_run_state())
assert_eq(xp_bar.max_value, 200.0)
assert_eq(xp_bar.value, 25.0)
assert_eq(xp_label.text, "Niveau 2 — XP 25/200")
func test_hud_uses_viewport_anchored_panels_with_distinct_stats_and_log_zones() -> void:
var hud := game.get_node("HUD") as GameHud
var layout := hud.get_node_or_null("ResponsiveLayout") as Control
var stats_panel := hud.get_node_or_null("ResponsiveLayout/StatsPanel") as PanelContainer
var log_panel := hud.get_node_or_null("ResponsiveLayout/LogPanel") as PanelContainer
var player_health := hud.get_node("PlayerHealthLabel") as Label
var enemy_health := hud.get_node("EnemyHealthLabel") as Label
var event_log := hud.get_node("EventLog") as RichTextLabel
assert_not_null(layout)
assert_eq(layout.anchor_right, 1.0)
assert_eq(layout.anchor_bottom, 1.0)
assert_not_null(stats_panel)
assert_not_null(log_panel)
assert_true(stats_panel.get_global_rect().encloses(player_health.get_global_rect()))
assert_true(stats_panel.get_global_rect().encloses(enemy_health.get_global_rect()))
assert_true(log_panel.get_global_rect().encloses(event_log.get_global_rect()))
assert_false(stats_panel.get_global_rect().intersects(log_panel.get_global_rect()))
var visible_stats: Array[Control] = [
hud.get_node("PlayerHealthLabel"),
hud.get_node("PlayerFocusLabel"),
hud.get_node("ExperienceBar"),
hud.get_node("GoldLabel"),
hud.get_node("QuestTrackerLabel"),
hud.get_node("SpellLabel"),
hud.get_node("EnemyHealthLabel"),
hud.get_node("CombatSummaryLabel"),
hud.get_node("DeadlineLabel"),
]
for first_index in range(visible_stats.size()):
for second_index in range(first_index + 1, visible_stats.size()):
assert_false(
visible_stats[first_index].get_global_rect().intersects(visible_stats[second_index].get_global_rect()),
"%s and %s must not overlap" % [visible_stats[first_index].name, visible_stats[second_index].name]
)
func test_long_deadline_text_wraps_and_stays_inside_the_stats_panel() -> void:
var hud := game.get_node("HUD") as GameHud
var state := game.get_game_state()
var run := game.get_run_state()
run.current_floor = 1
run.ensure_floor_deadline(1)
hud.refresh(state, [], run)
await get_tree().process_frame
var stats_panel := hud.get_node("ResponsiveLayout/StatsPanel") as PanelContainer
var deadline := hud.get_node("DeadlineLabel") as Label
assert_true(stats_panel.get_global_rect().encloses(deadline.get_global_rect()))
assert_lte(deadline.get_minimum_size().x, deadline.size.x)
assert_gt(deadline.size.y, deadline.get_line_height())
func test_compact_hud_leaves_a_dominant_unobstructed_central_play_area() -> void:
var hud := game.get_node("HUD") as GameHud
var stats_panel := hud.get_node("ResponsiveLayout/StatsPanel") as PanelContainer
var log_panel := hud.get_node("ResponsiveLayout/LogPanel") as PanelContainer
var prompt_panel := hud.get_node("ResponsiveLayout/PromptPanel") as PanelContainer
var help_panel := hud.get_node("ResponsiveLayout/HelpPanel") as PanelContainer
var panels: Array[PanelContainer] = [stats_panel, log_panel, prompt_panel, help_panel]
var occupied_area := 0.0
var central_play_area := Rect2(
Game.REFERENCE_VIEWPORT.x * 0.25,
Game.REFERENCE_VIEWPORT.y * 0.17,
Game.REFERENCE_VIEWPORT.x * 0.5,
Game.REFERENCE_VIEWPORT.y * 0.66
)
for panel in panels:
var panel_rect: Rect2 = panel.get_global_rect()
occupied_area += panel_rect.get_area()
assert_false(panel_rect.intersects(central_play_area), "%s obscures the central play area" % panel.name)
assert_lte(occupied_area, float(Game.REFERENCE_VIEWPORT.x * Game.REFERENCE_VIEWPORT.y) * 0.18)