public void testFireDelay() { WeaponConfig config = new WeaponConfig(); config.projectilePrefab = RuntimeTestHelpers.CreatePrefabWithComponent <ProjectileBehaviour>(); config.fireInterval = 1f; config.sprayAngle = 0f; Weapon weapon = new Weapon(config); // can fire immediately Assert.IsTrue(weapon.CanFire, "Weapon cannot fire upon creation"); // fire weapon Assert.IsTrue(weapon.FireWeapon(Vector3.zero, Vector3.up, DamageDealer.Default), "Weapon didn't fire"); // can't re-fire immediately Assert.IsFalse(weapon.CanFire, "Weapon can fire without any delay"); Assert.IsFalse(weapon.FireWeapon(Vector3.zero, Vector3.up, DamageDealer.Default), "Weapon fired without any delay"); // wait part of delay time weapon.UpdateTimer(0.25f); // can't re-fire after partial delay Assert.IsFalse(weapon.CanFire, "Weapon can fire without full delay"); Assert.IsFalse(weapon.FireWeapon(Vector3.zero, Vector3.up, DamageDealer.Default), "Weapon fired without full delay"); // wait rest of delay time weapon.UpdateTimer(0.75f); // weapon fires again after rest of delay Assert.IsTrue(weapon.CanFire, "Weapon cannot fire after delay"); Assert.IsTrue(weapon.FireWeapon(Vector3.zero, Vector3.up, DamageDealer.Default), "Weapon didn't fire after delay"); }
public void testProjectileDuration() { ProjectileBehaviour projectile = RuntimeTestHelpers.CreateObjectWithComponent <ProjectileBehaviour>(); projectile.speed = 1f; projectile.lifetime = 1f; projectile.damage = 1f; projectile.force = 1f; // start projectile projectile.InitializeAndActivate(Vector3.zero, Vector3.forward, DamageDealer.Default); Assert.IsTrue(projectile.IsAlive, "Projectile not alive after activation"); // still alive after .5s projectile.CallPrivateMethod("UpdateLifeTimer", 0.5f); Assert.IsTrue(projectile.IsAlive, "Projectile not alive after .5s"); // not alive after 1s projectile.CallPrivateMethod("UpdateLifeTimer", 0.5f); Assert.IsFalse(projectile.IsAlive, "Projectile alive after lifetime"); // projectile hasn't moved without calling update methods VectorAssert.AreEqual(Vector3.zero, projectile.PhysicsPosition, DELTA, "Physics position moved"); VectorAssert.AreEqual(Vector3.zero, projectile.transform.position, DELTA, "Transform position moved"); // physics can't move while not alive projectile.CallPrivateMethod("PhysicsUpdate", 1f); VectorAssert.AreEqual(Vector3.zero, projectile.PhysicsPosition, DELTA, "Physics position moved"); projectile.CallPrivateMethod("MoveToRaycastGoal", 1f); VectorAssert.AreEqual(Vector3.zero, projectile.transform.position, DELTA, "Transform position moved"); }
private void ensureCameraExists() { if (Camera.main == null) { Camera camera = RuntimeTestHelpers.CreateObjectWithComponent <Camera>("Main Camera"); camera.tag = "MainCamera"; } }
public void testConfigValidationBadSprayAngle() { WeaponConfig config = new WeaponConfig(); config.projectilePrefab = RuntimeTestHelpers.CreatePrefabWithComponent <ProjectileBehaviour>(); config.fireInterval = 1f; config.sprayAngle = -1f; Assert.IsFalse(config.Validate(), "Valid despite negative spray angle"); }
public void testAutoPoolManagerCreation() { PoolableBehaviour prefab = RuntimeTestHelpers.CreatePrefabWithComponent <TestablePoolableBehaviour>("Test Poolable"); // request object without pre-creating PoolManager PoolableBehaviour obj = PoolManager.Instance.GetPooledObject(prefab); Assert.NotNull(obj, "Retrieved null object from pool"); Assert.AreNotEqual(prefab, obj, "Received prefab itself from pool"); }
public void testEmptyPool() { PoolableBehaviour prefab = RuntimeTestHelpers.CreatePrefabWithComponent <TestablePoolableBehaviour>("Test Poolable"); int initialSize = 0; int incrementSize = 1; ObjectPool pool = new ObjectPool(prefab, initialSize, incrementSize); PoolableBehaviour retrieved = pool.GetFromPool(); Assert.IsNotNull(retrieved); }
public void SingleDamageHandlerTest() { DamageReceiver receiver = RuntimeTestHelpers.CreateObjectWithComponent <DamageReceiver>("Damage Receiver"); MockDamageHandlerBehaviour handler = receiver.AddComponent <MockDamageHandlerBehaviour>(); receiver.CallAwakeAndStartRecursive(); Assert.False(handler.wasDamaged, "Handler damaged before test conditions."); receiver.ApplyDamage(1f, Vector3.zero, Vector3.zero, DamageDealer.Default); Assert.True(handler.wasDamaged, "Handler not damaged."); }
public void TestDeathHandler() { DamageReceiver receiver = RuntimeTestHelpers.CreateObjectWithComponent <DamageReceiver>("Health Test"); HealthBehaviour health = receiver.AddComponent <HealthBehaviour>(); MockDeathHandlerBehaviour handler = receiver.AddComponent <MockDeathHandlerBehaviour>(); health.maxHealth = 1f; receiver.CallAwakeAndStartRecursive(); Assert.False(handler.isDead, "Handler dead before taking damage"); receiver.ApplyDamage(1f, Vector3.zero, Vector3.zero, DamageDealer.Default); Assert.True(handler.isDead, "Handler still alive after taking damage"); }
public void testSprayAngle() { WeaponConfig config = new WeaponConfig(); config.projectilePrefab = RuntimeTestHelpers.CreatePrefabWithComponent <ProjectileBehaviour>(); config.fireInterval = 1f; config.sprayAngle = 10f; Weapon weapon = new Weapon(config); // randomly test 10,000 random angles // not deterministic, but also difficult to test deterministically without deeper understanding of Quaternion implementation for (int i = 0; i < 10000; i++) { testAngle(Random.onUnitSphere, Random.Range(1f, 90f)); } }
public void testTransformFollowPhysics() { ProjectileBehaviour projectile = RuntimeTestHelpers.CreateObjectWithComponent <ProjectileBehaviour>(); projectile.speed = 1f; projectile.lifetime = 10f; projectile.damage = 1f; projectile.force = 1f; // start projectile projectile.InitializeAndActivate(Vector3.zero, Vector3.forward, DamageDealer.Default); // test no movement VectorAssert.AreEqual(Vector3.zero, projectile.PhysicsPosition, DELTA, "Physics position not zero"); VectorAssert.AreEqual(Vector3.zero, projectile.transform.position, DELTA, "Transform position not zero"); // move physics position forward 2s projectile.CallPrivateMethod("PhysicsUpdate", 2f); // test physics moved but not game object VectorAssert.AreEqual(Vector3.forward * 2f, projectile.PhysicsPosition, DELTA, "Physics position incorrect"); VectorAssert.AreEqual(Vector3.zero, projectile.transform.position, DELTA, "Transform position not zero"); // move transform position by half step projectile.CallPrivateMethod("MoveToRaycastGoal", 1f); VectorAssert.AreEqual(Vector3.forward * 2f, projectile.PhysicsPosition, DELTA, "Physics position incorrect"); VectorAssert.AreEqual(Vector3.forward, projectile.transform.position, DELTA, "Transform position not half of physics position"); // move transform position by another half step projectile.CallPrivateMethod("MoveToRaycastGoal", 1f); VectorAssert.AreEqual(Vector3.forward * 2f, projectile.PhysicsPosition, DELTA, "Physics position incorrect"); VectorAssert.AreEqual(projectile.PhysicsPosition, projectile.transform.position, DELTA, "Transform position does not match physics position"); // transform position shouldn't move without physics position moving projectile.CallPrivateMethod("MoveToRaycastGoal", 5f); VectorAssert.AreEqual(Vector3.forward * 2f, projectile.PhysicsPosition, DELTA, "Physics position incorrect"); VectorAssert.AreEqual(projectile.PhysicsPosition, projectile.transform.position, DELTA, "Transform position does not match physics position"); }
public void testPoolReuse() { PoolableBehaviour prefab = RuntimeTestHelpers.CreatePrefabWithComponent <TestablePoolableBehaviour>("Test Poolable"); ObjectPool pool = new ObjectPool(prefab, 1, 1); // pool should never return the prefab itself PoolableBehaviour retrieved = pool.GetFromPool(); Assert.AreNotEqual(prefab, retrieved, "Prefab itself returned from pool"); // pool should not return an in-use object PoolableBehaviour retrieved2 = pool.GetFromPool(); Assert.AreNotEqual(retrieved, retrieved2, "Pool returned in-use object twice"); // object returned to pool should be retrieved from pool retrieved.ReturnToPool(); PoolableBehaviour retrieved3 = pool.GetFromPool(); Assert.AreEqual(retrieved, retrieved3, "Pool did not re-use returned object"); }
public void TestHealthRemoved() { DamageReceiver receiver = RuntimeTestHelpers.CreateObjectWithComponent <DamageReceiver>("Health Test"); HealthBehaviour health = receiver.AddComponent <HealthBehaviour>(); health.maxHealth = 1f; receiver.CallAwakeAndStartRecursive(); Assert.True(health.IsAlive, "Dead before taking damage"); Assert.AreEqual(1f, health.CurrentHealth, 0.001f, "Health doesn't match specified value"); receiver.ApplyDamage(0.75f, Vector3.zero, Vector3.zero, DamageDealer.Default); Assert.True(health.IsAlive, "Dead before all health removed"); Assert.AreEqual(0.25f, health.CurrentHealth, 0.001f, "Health doesn't match specified value minus removed amount"); receiver.ApplyDamage(0.25f, Vector3.zero, Vector3.zero, DamageDealer.Default); Assert.False(health.IsAlive, "Still alive after all health removed"); Assert.AreEqual(0f, health.CurrentHealth, 0.001f, "Still health remaining after dead"); }
public void testCameraFade() { ensureCameraExists(); CameraAimBehaviour camera = RuntimeTestHelpers.CreateObjectWithComponent <CameraAimBehaviour>("Camera Aim"); MeshRenderer renderer = camera.AddComponent <MeshRenderer>(); renderer.sharedMaterial = new Material(Shader.Find("Standard")); camera.charRenderer = renderer; camera.transparencyMaterial = new Material(Shader.Find("Standard")); camera.CallAwakeAndStartRecursive(); camera.minFadeDistance = 1f; camera.maxFadeDistance = 2f; // move past max distance camera.CallPrivateMethod("FadeCameraDistance", 3f); Assert.IsTrue(renderer.enabled, "Renderer not displayed"); Assert.AreNotEqual(renderer.sharedMaterial, camera.transparencyMaterial, "Using transparent material when too far away."); // move to midpoint camera.CallPrivateMethod("FadeCameraDistance", 1.5f); Assert.IsTrue(renderer.enabled, "Renderer not displayed"); Assert.AreEqual(renderer.sharedMaterial, camera.transparencyMaterial, "Not using transparent material when in fade range"); Assert.AreEqual(camera.transparencyMaterial.color.a, 0.5f, 0.01f, "Expected 50% opacity in middle of fade range"); // move up before min distance camera.CallPrivateMethod("FadeCameraDistance", 0.5f); Assert.IsFalse(renderer.enabled, "Renderer displayed when not expected"); // reset past max distance camera.CallPrivateMethod("FadeCameraDistance", 3f); Assert.IsTrue(renderer.enabled, "Renderer not displayed"); Assert.AreNotEqual(renderer.sharedMaterial, camera.transparencyMaterial, "Using transparent material when too far away."); }
public void testProjectileTransformMoveNotAlive() { ProjectileBehaviour projectile = RuntimeTestHelpers.CreateObjectWithComponent <ProjectileBehaviour>(); projectile.speed = 1f; projectile.lifetime = 1f; projectile.damage = 1f; projectile.force = 1f; // start projectile projectile.InitializeAndActivate(Vector3.zero, Vector3.forward, DamageDealer.Default); projectile.CallPrivateMethod("PhysicsUpdate", 1f); projectile.CallPrivateMethod("UpdateLifeTimer", 1f); // assert projectile not alive and transform not updated VectorAssert.AreEqual(Vector3.forward, projectile.PhysicsPosition, DELTA, "Physics position not correct"); Assert.IsFalse(projectile.IsAlive, "Projectile alive after lifetime"); VectorAssert.AreEqual(Vector3.zero, projectile.transform.position, DELTA, "Transform position not correct"); // transform moves to final position regardless of time projectile.CallPrivateMethod("MoveToRaycastGoal", 0.01f); VectorAssert.AreEqual(Vector3.forward, projectile.transform.position, DELTA, "Transform did not move to final position"); }