188 lines
6.5 KiB
C#
188 lines
6.5 KiB
C#
using System.Reflection;
|
|
using Godot;
|
|
using GdUnit4;
|
|
using static GdUnit4.Assertions;
|
|
using Movementtests.interfaces;
|
|
using Movementtests.systems.damage;
|
|
|
|
namespace Movementtests.tests;
|
|
|
|
[TestSuite, RequireGodotRuntime]
|
|
public class PlayerControllerUnitTest
|
|
{
|
|
private PlayerController _player;
|
|
|
|
[BeforeTest]
|
|
public void SetupTest()
|
|
{
|
|
_player = new PlayerController();
|
|
// We don't call _Ready() to avoid node dependency issues,
|
|
// but we need to initialize some private fields for unit testing.
|
|
SetPrivateField(_player, "_targetSpeed", 7.0f);
|
|
SetPrivateField(_player, "_gravity", 9.8f);
|
|
|
|
// Setup Combat/Health dependencies
|
|
var rHealth = new RHealth(100.0f);
|
|
_player.RHealth = rHealth;
|
|
_player.CHealth = new CHealth { RHealth = rHealth, CurrentHealth = 100.0f };
|
|
}
|
|
|
|
[AfterTest]
|
|
public void CleanupTest()
|
|
{
|
|
_player?.Free();
|
|
}
|
|
|
|
private void SetPrivateField(object obj, string fieldName, object value)
|
|
{
|
|
var field = typeof(PlayerController).GetField(fieldName, BindingFlags.NonPublic | BindingFlags.Instance);
|
|
field?.SetValue(obj, value);
|
|
}
|
|
|
|
[TestCase]
|
|
public void TestCalculateGravityForce()
|
|
{
|
|
_player.Weight = 3.0f;
|
|
// _gravity is 9.8f
|
|
AssertFloat(_player.CalculateGravityForce()).IsEqualApprox(29.4f, 0.001f);
|
|
}
|
|
|
|
[TestCase]
|
|
public void TestIsPlayerInputtingForward()
|
|
{
|
|
// Test Keyboard Input
|
|
_player.InputDeviceChanged(false); // isUsingGamepad = false
|
|
_player.OnInputMoveKeyboard(new Vector3(0, 0, -1)); // Forward is -Z in Godot
|
|
AssertBool(_player.IsPlayerInputtingForward()).IsTrue();
|
|
|
|
_player.OnInputMoveKeyboard(new Vector3(0, 0, 1)); // Backward
|
|
AssertBool(_player.IsPlayerInputtingForward()).IsFalse();
|
|
|
|
// Test Gamepad Input
|
|
_player.InputDeviceChanged(true); // isUsingGamepad = true
|
|
_player.OnInputMove(new Vector3(0, 0, -1));
|
|
AssertBool(_player.IsPlayerInputtingForward()).IsTrue();
|
|
}
|
|
|
|
[TestCase]
|
|
public void TestSetVerticalVelocity()
|
|
{
|
|
_player.Velocity = new Vector3(1, 0, 2);
|
|
_player.SetVerticalVelocity(5.0f);
|
|
AssertVector(_player.Velocity).IsEqual(new Vector3(1, 5, 2));
|
|
}
|
|
|
|
[TestCase]
|
|
public void TestComputeHVelocityGround()
|
|
{
|
|
_player.Velocity = Vector3.Zero;
|
|
_player.AccelerationFloor = 10.0f;
|
|
|
|
// Moving forward
|
|
Vector3 direction = Vector3.Forward; // (0, 0, -1)
|
|
float delta = 0.1f;
|
|
|
|
// _targetSpeed is 7.0f
|
|
// Expected velocity change: Lerp(0, -7.0, 0.1 * 10.0) -> Lerp(0, -7.0, 1.0) -> -7.0
|
|
Vector3 newVelocity = _player.ComputeHVelocity(delta, _player.AccelerationFloor, _player.DecelerationFloor, direction);
|
|
|
|
AssertVector(newVelocity).IsEqual(new Vector3(0, 0, -7.0f));
|
|
}
|
|
|
|
[TestCase]
|
|
public void TestComputeHVelocityAir()
|
|
{
|
|
_player.Velocity = new Vector3(5, 0, 0);
|
|
_player.AccelerationAir = 2.0f;
|
|
_player.DecelerationAir = 2.0f;
|
|
|
|
// No input direction (deceleration)
|
|
Vector3 direction = Vector3.Zero;
|
|
float delta = 0.5f;
|
|
|
|
// Expected velocity change: Lerp(5, 0, 0.5 * 2.0) -> Lerp(5, 0, 1.0) -> 0
|
|
Vector3 newVelocity = _player.ComputeHVelocity(delta, _player.AccelerationAir, _player.DecelerationAir, direction);
|
|
|
|
AssertVector(newVelocity).IsEqual(Vector3.Zero);
|
|
}
|
|
|
|
[TestCase]
|
|
public void TestReduceHealth()
|
|
{
|
|
// Initial health is 100
|
|
var damageRecord = new DamageRecord(Vector3.Zero, new RDamage(25.0f, EDamageTypes.Normal));
|
|
_player.ReduceHealth(_player, damageRecord);
|
|
|
|
AssertFloat(_player.CHealth.CurrentHealth).IsEqual(75.0f);
|
|
}
|
|
|
|
[TestCase]
|
|
public void TestEmpoweredActionsLeft()
|
|
{
|
|
// EmpoweredActionsLeft setter calls PlayerUi.SetNumberOfDashesLeft
|
|
// PlayerUi.SetNumberOfDashesLeft accesses _dashIcons array, which is null if _Ready() isn't called.
|
|
// We can initialize _dashIcons via reflection to allow the setter to work.
|
|
var mockUi = new PlayerUi();
|
|
var dashIcons = new TextureRect[3] { new TextureRect(), new TextureRect(), new TextureRect() };
|
|
var field = typeof(PlayerUi).GetField("_dashIcons", BindingFlags.NonPublic | BindingFlags.Instance);
|
|
field?.SetValue(mockUi, dashIcons);
|
|
|
|
_player.PlayerUi = mockUi;
|
|
|
|
_player.EmpoweredActionsLeft = 2;
|
|
AssertInt(_player.EmpoweredActionsLeft).IsEqual(2);
|
|
AssertBool(dashIcons[0].Visible).IsTrue();
|
|
AssertBool(dashIcons[1].Visible).IsTrue();
|
|
AssertBool(dashIcons[2].Visible).IsFalse();
|
|
}
|
|
|
|
[TestCase]
|
|
public void TestDashCooldownTimeout()
|
|
{
|
|
SetPrivateField(_player, "_canDash", false);
|
|
_player.DashCooldownTimeout();
|
|
bool canDash = (bool)GetPrivateField(_player, "_canDash");
|
|
AssertBool(canDash).IsTrue();
|
|
}
|
|
|
|
private object GetPrivateField(object obj, string fieldName)
|
|
{
|
|
var field = typeof(PlayerController).GetField(fieldName, BindingFlags.NonPublic | BindingFlags.Instance);
|
|
return field?.GetValue(obj);
|
|
}
|
|
|
|
[TestCase]
|
|
public void TestGetInputLocalHDirection()
|
|
{
|
|
_player.InputDeviceChanged(false); // Keyboard
|
|
_player.OnInputMoveKeyboard(new Vector3(1, 0, 1)); // Diagonal
|
|
|
|
Vector3 expected = new Vector3(1, 0, 1).Normalized();
|
|
AssertVector(_player.GetInputLocalHDirection()).IsEqualApprox(expected, new Vector3(0.001f, 0.001f, 0.001f));
|
|
}
|
|
|
|
[TestCase]
|
|
public void TestComputeKnockback()
|
|
{
|
|
var cKnockback = new CKnockback();
|
|
cKnockback.RKnockback = new RKnockback(10.0f);
|
|
_player.CKnockback = cKnockback;
|
|
|
|
// Setup knockback record
|
|
var damageRecord = new DamageRecord(new Vector3(10, 0, 0), new RDamage(0, EDamageTypes.Normal));
|
|
var knockbackRecord = new KnockbackRecord(damageRecord, 1.0f);
|
|
|
|
_player.GlobalPosition = Vector3.Zero;
|
|
cKnockback.GlobalPosition = Vector3.Zero;
|
|
|
|
_player.RegisterKnockback(knockbackRecord);
|
|
|
|
// Expected direction: GlobalPosition (0,0,0) - SourceLocation (10,0,0) = (-10,0,0)
|
|
// Normalized: (-1, 0, 0)
|
|
// finalKnockback: (-1, 0, 0) * 10.0 (Modifier) * 1.0 (ForceMultiplier) = (-10, 0, 0)
|
|
|
|
Vector3 knockback = cKnockback.ComputeKnockback();
|
|
AssertVector(knockback).IsEqual(new Vector3(-10, 0, 0));
|
|
}
|
|
}
|