150 lines
4.5 KiB
C#
150 lines
4.5 KiB
C#
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();
|
|
_player._targetSpeed = 7.0f;
|
|
_player._gravity = 9.8f;
|
|
|
|
var rHealth = new RHealth(100.0f);
|
|
_player.RHealth = rHealth;
|
|
_player.CHealth = new CHealth { RHealth = rHealth, CurrentHealth = 100.0f };
|
|
}
|
|
|
|
[AfterTest]
|
|
public void CleanupTest()
|
|
{
|
|
_player?.Free();
|
|
}
|
|
|
|
[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);
|
|
_player.OnInputMoveKeyboard(Vector3.Forward);
|
|
AssertBool(_player.IsPlayerInputtingForward()).IsTrue();
|
|
|
|
_player.OnInputMoveKeyboard(Vector3.Back);
|
|
AssertBool(_player.IsPlayerInputtingForward()).IsFalse();
|
|
|
|
// Test Gamepad Input
|
|
_player.InputDeviceChanged(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;
|
|
|
|
float delta = 0.1f;
|
|
Vector3 newVelocity = _player.ComputeHVelocity(delta, _player.AccelerationFloor, _player.DecelerationFloor, Vector3.Forward);
|
|
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;
|
|
|
|
float delta = 0.5f;
|
|
Vector3 newVelocity = _player.ComputeHVelocity(delta, _player.AccelerationAir, _player.DecelerationAir, Vector3.Zero);
|
|
|
|
AssertVector(newVelocity).IsEqual(Vector3.Zero);
|
|
}
|
|
|
|
[TestCase]
|
|
public void TestReduceHealth()
|
|
{
|
|
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()
|
|
{
|
|
var mockUi = new PlayerUi();
|
|
var dashIcons = new TextureRect[3] { new TextureRect(), new TextureRect(), new TextureRect() };
|
|
mockUi._dashIcons = 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()
|
|
{
|
|
_player._canDash = false;
|
|
_player.DashCooldownTimeout();
|
|
AssertBool(_player._canDash).IsTrue();
|
|
}
|
|
|
|
[TestCase]
|
|
public void TestGetInputLocalHDirection()
|
|
{
|
|
_player.InputDeviceChanged(false);
|
|
_player.OnInputMoveKeyboard(new Vector3(1, 0, 1));
|
|
|
|
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;
|
|
|
|
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);
|
|
|
|
Vector3 knockback = cKnockback.ComputeKnockback();
|
|
AssertVector(knockback).IsEqual(new Vector3(-10, 0, 0));
|
|
}
|
|
}
|