예제 #1
0
        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");
        }
예제 #2
0
        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");
        }
예제 #3
0
 private void ensureCameraExists()
 {
     if (Camera.main == null)
     {
         Camera camera = RuntimeTestHelpers.CreateObjectWithComponent <Camera>("Main Camera");
         camera.tag = "MainCamera";
     }
 }
예제 #4
0
        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");
        }
예제 #5
0
        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");
        }
예제 #6
0
        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);
        }
예제 #7
0
        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.");
        }
예제 #8
0
        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");
        }
예제 #9
0
        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));
            }
        }
예제 #10
0
        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");
        }
예제 #11
0
        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");
        }
예제 #12
0
        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");
        }
예제 #13
0
        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.");
        }
예제 #14
0
        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");
        }