Files
Defensaria/test/test_enemy.gd

108 lines
5.1 KiB
GDScript

extends GutTest
var EnemyScene = load("res://Game/Mobs/forkman.tscn") # Path to enemy PackedScene
var _enemy_instance = null
var _mock_parent = null
func before_each():
# Mock parent instead of PathFollow2D
_mock_parent = PathFollow2D.new()
_mock_parent.name = "PathFollow2D_Mock"
add_child(_mock_parent)
gut.p(" → PathFollow2D mock created")
# Instantiate scene
_enemy_instance = EnemyScene.instantiate()
_mock_parent.add_child(_enemy_instance)
gut.p(" → Enemy instance instantiated from: %s" % EnemyScene.resource_path)
# Initialize child nodes
await get_tree().process_frame # Required for complete _ready() execution
gut.p(" → Process frame awaited for initialization")
func after_each():
_enemy_instance.queue_free()
_mock_parent.queue_free()
# Check for children
func test_scene_initialization():
gut.p("\n▶ TEST: Scene Initialization")
var character_body = _enemy_instance.get_node("CharacterBody2D")
var health_bar = _enemy_instance.get_node("ProgressBar")
gut.p(" → CharacterBody2D: %s" % ("✓ Found" if character_body else "✗ Missing"))
assert_not_null(character_body, "Missing CharacterBody2D")
gut.p(" → ProgressBar: %s" % ("✓ Found" if health_bar else "✗ Missing"))
assert_not_null(health_bar, "Missing ProgressBar")
# Check damage mechanics
func test_shield_damage_reduction():
gut.p("\n▶ TEST: Shield Damage Reduction")
_enemy_instance.current_shield = 50
_enemy_instance.current_health = 100
gut.p(" → Initial: Shield=%d, Health=%d" % [_enemy_instance.current_shield, _enemy_instance.current_health])
_enemy_instance.enemy_hurt(30)
gut.p(" → After 30 damage: Shield=%d (expected 20), Health=%d (expected 100)" % [_enemy_instance.current_shield, _enemy_instance.current_health])
assert_eq(_enemy_instance.current_shield, 20, "Incorrect shield value")
assert_eq(_enemy_instance.current_health, 100, "Health should remain unchanged with this damage amount")
func test_shield_break_and_health_damage():
gut.p("\n▶ TEST: Shield Break and Health Damage")
_enemy_instance.current_shield = 20
_enemy_instance.current_health = 100
gut.p(" → Initial: Shield=%d, Health=%d" % [_enemy_instance.current_shield, _enemy_instance.current_health])
_enemy_instance.enemy_hurt(30)
gut.p(" → After 30 damage: Shield=%d (expected 0), Health=%d (expected 90)" % [_enemy_instance.current_shield, _enemy_instance.current_health])
assert_eq(_enemy_instance.current_shield, 0, "Shield should be depleted")
assert_eq(_enemy_instance.current_health, 90, "Incorrect health value")
func test_health_damage_without_shield():
gut.p("\n▶ TEST: Health Damage Without Shield")
_enemy_instance.current_shield = 0
_enemy_instance.current_health = 80
gut.p(" → Initial: Shield=%d, Health=%d" % [_enemy_instance.current_shield, _enemy_instance.current_health])
_enemy_instance.enemy_hurt(25)
gut.p(" → After 25 damage: Shield=%d (expected 0), Health=%d (expected 55)" % [_enemy_instance.current_shield, _enemy_instance.current_health])
assert_eq(_enemy_instance.current_shield, 0, "Shield should remain zero")
assert_eq(_enemy_instance.current_health, 55, "Incorrect health amount")
#Check for animation change
func test_hurt_animation_plays_on_damage():
gut.p("\n▶ TEST: Hurt Animation on Damage")
_enemy_instance.enemy_hurt(10)
var anim_sprite = _enemy_instance.get_node("CharacterBody2D/AnimatedSprite2D")
gut.p(" → Animation: %s (expected 'hurt')" % anim_sprite.animation)
assert_eq(anim_sprite.animation, "hurt", "Hurt animation must be playing")
#Check animation speed change
func test_animation_speed_scales_with_speed():
gut.p("\n▶ TEST: Animation Speed Scaling")
_enemy_instance.set_character_data()
var anim_sprite = _enemy_instance.get_node("CharacterBody2D/AnimatedSprite2D")
var expected_speed = snapped(_enemy_instance.speed / 100.0 , 0.1)
var actual_speed = snapped(anim_sprite.speed_scale, 0.1)
gut.p(" → Character speed: %d" % _enemy_instance.speed)
gut.p(" → Animation speed: %s (expected %s)" % [actual_speed, expected_speed])
assert_eq(actual_speed, expected_speed, "Animation speed should match movement speed")
#check for colorchange on healthbar
func test_health_bar_color_changes_with_shield():
gut.p("\n▶ TEST: Health Bar Color with Shield")
_enemy_instance.current_shield = 50
_enemy_instance.adjust_health_bar()
var fill_style = _enemy_instance.get_node("ProgressBar").get_theme_stylebox("fill")
gut.p(" → Shield: %d, Color: %s (expected blue)" % [_enemy_instance.current_shield, fill_style.bg_color])
assert_eq(fill_style.bg_color, Color(0,0,1), "Must be blue in case of shield")
#Check character flip
func test_character_flips_based_on_movement_direction():
gut.p("\n▶ TEST: Character Flip Direction")
var initial_scale = _mock_parent.scale.x
_mock_parent.position.x += 10
_enemy_instance.move_character(0.1)
gut.p(" → Moving right: scale=%s (expected > 0)" % _mock_parent.scale.x)
assert_gt(_mock_parent.scale.x, 0, "Positive scaling when moving to the right")
_mock_parent.position.x -= 20
_enemy_instance.move_character(0.1)
gut.p(" → Moving left: scale=%s (expected < 0)" % _mock_parent.scale.x)
assert_lt(_mock_parent.scale.x, 0, "Negative scaling when moving left")