Skip to content

Commit

Permalink
Add hashset solution to spawn unique upgrades
Browse files Browse the repository at this point in the history
Generates 3 unique numbers and assign those to the spawned upgrades
  • Loading branch information
tohanss committed May 25, 2021
1 parent d9f90ef commit c5b1752
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
9 changes: 3 additions & 6 deletions Assets/Scripts/Odyn/Upgrades/GeneralUpgrade.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,20 @@
public class GeneralUpgrade : MonoBehaviour
{
private GameObject toolTip;
private Upgrade thisUpgrade;
[HideInInspector]
public Upgrade thisUpgrade;
private UpgradeDropper upgradeDropper;

public int dropIndex;

private void Start()
{
upgradeDropper = GameObject.Find("UpgradeDropper(Clone)").GetComponent<UpgradeDropper>();

// min is incluseive, max is exclusive
// picks a random upgrade for this instance from the list upgrades
thisUpgrade = upgradeDropper.upgrades[Random.Range(0, upgradeDropper.upgrades.Count)];

toolTip = gameObject.transform.GetChild(0).gameObject;
toolTip.transform.GetChild(1).GetComponent<TextMeshPro>().SetText(thisUpgrade.description, true);
toolTip.transform.GetChild(2).GetComponent<TextMeshPro>().SetText(thisUpgrade.abilityName, true);

GetComponent<Animator>().runtimeAnimatorController = thisUpgrade.animator;
StartCoroutine(decreaseLight());
}
Expand Down
17 changes: 16 additions & 1 deletion Assets/Scripts/Odyn/Upgrades/UpgradeDropper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,22 @@ public void dropUpgrade(Vector2 offset)
currentDrops[numDroppedUpgrades-1][0] = Instantiate(upgradePrefab, spawnPosition + offset, Quaternion.identity);
currentDrops[numDroppedUpgrades-1][1] = Instantiate(upgradePrefab, spawnPosition + offset + new Vector2(3f,0), Quaternion.identity);
currentDrops[numDroppedUpgrades-1][2] = Instantiate(upgradePrefab, spawnPosition + offset + new Vector2(-3f,0), Quaternion.identity);


// Generates the upgrades in a way that all three are unique
HashSet<int> upgradeNumbers = new HashSet<int>();

while (upgradeNumbers.Count < 3)
{
upgradeNumbers.Add(Random.Range(0, upgrades.Count));
}

int pos = 0;
foreach (int number in upgradeNumbers)
{
currentDrops[numDroppedUpgrades-1][pos].thisUpgrade = upgrades[number];
pos++;
}

// Tells all upgrades what order they were dropped in, so that we can remove the associated
// upgrades when picking one up
for (int i = 0; i < 3; i++)
Expand Down

0 comments on commit c5b1752

Please sign in to comment.