Skip to content

Commit

Permalink
Merge pull request #36 from Wizards-vs-Robots/wavemanager_test
Browse files Browse the repository at this point in the history
Wavemanager test
  • Loading branch information
4kills authored Jun 14, 2022
2 parents 97ebedb + 2629a29 commit dc05296
Show file tree
Hide file tree
Showing 10 changed files with 202 additions and 35 deletions.
15 changes: 8 additions & 7 deletions Assets/Scripts/WaveManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ public class WaveManager : MonoBehaviour
public float cooldown;
public float duration;
public float strength;
private List<GameObject> minions;
public List<GameObject> minions;

void Start()
public void Start()
{
//Getting "Attacker" component of game objects beforehand
//to elimite "runtime" overhead during the game and sort
Expand All @@ -56,7 +56,6 @@ IEnumerator Manage()
//before proceeding to the next wave until they are all dead.
if (minions.Count > 0)
{
// Debug.Log("There are still minions.");
yield return new WaitUntil(() => (minions.Count == 0));
}

Expand Down Expand Up @@ -104,7 +103,7 @@ private void ShowWaveOnScreen()
}
}

void Strengthen()
public void Strengthen()
{
float factor = Statics.gameMode == GameMode.LOCAL_MULTIPLAYER ? COOP_FACTOR : 1;

Expand All @@ -124,7 +123,7 @@ public void ReportDeath(GameObject entity)

// Remove the entity and drop reward
GenerateDrop(entity.transform.position);
Destroy(entity);
DestroyImmediate(entity);
minions.Remove(entity);
}
}
Expand All @@ -139,7 +138,7 @@ private void GenerateDrop(Vector3 position)
}
}

void Spawn(Attacker attacker, Vector3 center)
public void Spawn(Attacker attacker, Vector3 center)
{
//Generate normalized vector from random angle and stretch it to match specified range.
float angle = UnityEngine.Random.Range(0F, (float)(Math.PI * 2));
Expand Down Expand Up @@ -184,16 +183,18 @@ private void AddToOptions(Attacker attacker, List<Attacker> options)
//This function generates descrete points in time in an intervall
//I = [0; #duration] using sub-second timesteps. They are sorted in
//ascending order.
List<Tuple<float, Attacker>> GenerateSpawnPattern()
public List<Tuple<float, Attacker>> GenerateSpawnPattern()
{
List<Attacker> options = GetValidSpawningOptions();
int pivot = options.Count - 1;
int upper = (int) (duration * TIME_PRECISION);
float quota = strength;
//Debug.Log("Quota: " + quota);

Debug.Log("a");
List<Tuple<float, Attacker>> output = new List<Tuple<float, Attacker>>();
while (quota > 0) {
Debug.Log("Quota: " + quota);
Attacker selected = options[pivot];
float baseStrength = selected.GetStrength();
float scaledStrength = baseStrength * selected.GetStrengthScale();
Expand Down
33 changes: 33 additions & 0 deletions Assets/Tests/Editmode/MinionConstellationTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using System;
using System.Collections;
using System.Collections.Generic;
using NUnit.Framework;
using Robot;
using UnityEditor.SceneManagement;
using UnityEditor.SearchService;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.TestTools;

public class MinionConstellationTest
{
[Test]
public void TestMinionConstellation()
{
EditorSceneManager.OpenScene("Assets/Scenes/Game.unity");

var manager = GameObject
.FindGameObjectsWithTag("WaveManager")[0]
.GetComponent<WaveManager>();
manager.Start();

List<Tuple<float, Attacker>> pattern = manager.GenerateSpawnPattern();
int firstRobotCount = 0;
foreach (Tuple<float, Attacker> entry in pattern) {
Assert.IsTrue(entry.Item2.strength == 10);
firstRobotCount++;
}

Assert.IsTrue(firstRobotCount == 10);
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

41 changes: 41 additions & 0 deletions Assets/Tests/Editmode/ParameterGrowthTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using System.Runtime.CompilerServices;
using NUnit.Framework;
using Robot;
using UnityEditor.SceneManagement;
using UnityEngine;
using UnityEngine.TestTools;
using UnityEngine.UIElements;

public class ParameterGrowthTest
{
[Test]
public void TestParameterGrowth()
{
EditorSceneManager.OpenScene("Assets/Scenes/Game.unity");

float eta = 10e-4F;
float[] cooldowns = new float[3]{1F, 0.9950125F, 0.9900498F};
float[] durations = new float[3]{5F, 4.900993F, 4.803947F};
float[] strengths = new float[3]{100F, 104.0811F, 108.3287F};
var manager = GameObject
.FindGameObjectsWithTag("WaveManager")[0]
.GetComponent<WaveManager>();
manager.cooldown = 1F;
manager.duration = 5F;
manager.strength = 100F;
manager.wave = 1;

for (int i = 0; i < 3; i++) {
Assert.IsTrue(Mathf.Abs(manager.cooldown - cooldowns[i]) < eta);
Assert.IsTrue(Mathf.Abs(manager.duration - durations[i]) < eta);
Assert.IsTrue(Mathf.Abs(manager.strength - strengths[i]) < eta);

manager.wave += 1;
manager.Strengthen();
}
}
}
11 changes: 11 additions & 0 deletions Assets/Tests/Editmode/ParameterGrowthTest.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

50 changes: 50 additions & 0 deletions Assets/Tests/Editmode/ReportDeathTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using System;
using System.Collections;
using System.Collections.Generic;
using NUnit.Framework;
using Robot;
using UnityEditor.SceneManagement;
using UnityEngine;
using UnityEngine.TestTools;

public class ReportDeathTest
{
[Test]
public void TestDeathAlreadyReported()
{
EditorSceneManager.OpenScene("Assets/Scenes/Game.unity");

Statics.Initialize();
var manager = GameObject
.FindGameObjectsWithTag("WaveManager")[0]
.GetComponent<WaveManager>();
manager.Start();

// Simulate spawning
var spawnVector = new Vector3(0F, 0F, 0F);
var firstAttacker = manager.attackers[0];
manager.Spawn(firstAttacker, spawnVector);

// Take first minion
var firstMinion = manager.minions[0];
var strength = (int) firstMinion.GetComponent<Attacker>().GetStrength();

// Report death first time
var sizeBefore = manager.minions.Count;
manager.ReportDeath(firstMinion);
var currentSize = manager.minions.Count;

// Assert functioning first report
Assert.IsTrue(currentSize < sizeBefore);
Assert.IsTrue(Statics.GetScoreModel().GetScore() == strength);

// Report death as second time (should be ignored)
sizeBefore = currentSize;
manager.ReportDeath(firstMinion);
currentSize = manager.minions.Count;

// Assert ignored death report
Assert.IsTrue(currentSize == sizeBefore);
Assert.IsTrue(Statics.GetScoreModel().GetScore() == strength);
}
}
11 changes: 11 additions & 0 deletions Assets/Tests/Editmode/ReportDeathTest.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 36 additions & 0 deletions Assets/Tests/Editmode/SpawnRobotTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System;
using System.Collections;
using System.Collections.Generic;
using NUnit.Framework;
using Robot;
using UnityEditor.SceneManagement;
using UnityEngine;
using UnityEngine.TestTools;

public class SpawnRobotTest
{
[Test]
public void TestSpawnRobot()
{
EditorSceneManager.OpenScene("Assets/Scenes/Game.unity");

var manager = GameObject
.FindGameObjectsWithTag("WaveManager")[0]
.GetComponent<WaveManager>();
manager.Start();

// Spawn attacker
var spawnVector = new Vector3(0F, 0F, 0F);
var firstAttacker = manager.attackers[0];
manager.Spawn(firstAttacker, spawnVector);

// Retrieve instance
var attackerInstance = manager.minions[0];
var difVector = attackerInstance.transform.position - spawnVector;
var distance = difVector.magnitude;

// Check if in valid range
Assert.IsTrue(distance >= WaveManager.MIN_SPAWN_RADIUS &&
distance <= WaveManager.MAX_SPAWN_RADIUS);
}
}
11 changes: 11 additions & 0 deletions Assets/Tests/Editmode/SpawnRobotTest.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 0 additions & 27 deletions Assets/Tests/Playmode/Testtest.cs

This file was deleted.

0 comments on commit dc05296

Please sign in to comment.