UnityGameProjectsCode / Rise2Point0Game / TileControl.cs
TileControl.cs
Raw
using System.Collections.Generic;
using UnityEngine;

public class TileControl : MonoBehaviour
{
    private List<GameObject> spawnedItems = new List<GameObject>();
    private List<GameObject> itemSpawns = new List<GameObject>();
    private List<GameObject> obstacles = new List<GameObject>();

    public GameObject coinItemPrefab;
    public GameObject rareCoinItemPrefab;
    public GameObject scoreItemPrefab;
    public GameObject rareScoreItemPrefab;
    public GameObject speedItemPrefab;
    public GameObject rareSpeedItemPrefab;
    public GameObject invulnItemPrefab;
    public GameObject rareInvulnItemPrefab;
    public GameObject speedReduceItemPrefab;
    public GameObject rareSpeedReduceItemprefab;

    public List<int> normalChances = new List<int>();
    public List<int> rareChances = new List<int>();

    private void Awake()
    {
        foreach (Transform child in GetComponentsInChildren<Transform>())
        {
            if (child.CompareTag("Item Spawn"))
                itemSpawns.Add(child.gameObject);

            if (child.name.Contains("Obstacle"))
                obstacles.Add(child.gameObject);
        }

    }

    public void SpawnItems()
    {
        int spawnItems = Random.Range(0, 2);

        if (spawnItems == 1)
        {
            int numberOfItemsToSpawn;

            int useAllSpawns = Random.Range(0, 6);

            if (useAllSpawns == 0)
                numberOfItemsToSpawn = itemSpawns.Count;
            else
                numberOfItemsToSpawn = Random.Range(1, 3);

            for (int i = 0; i < numberOfItemsToSpawn; i++)
            {
                if (itemSpawns.Count > 0)
                {
                    int randomSpawnPoint = Random.Range(0, itemSpawns.Count);
                    GameObject prefabToSpawn = null;

                    int normalItemDropChance = Random.Range(0, 100); //chance for the normal group of items to spawn
                    int rareItemDropChance = Random.Range(0, 80); //chance for the rare group of items to spawn
                    int groupItemsDropChance = Random.Range(0, 100); //chance within a group of items for a number of items to be chosen
                    int interGroupItemDropChance = Random.Range(0, 100); //value used to determine what item in a select group is going to spawn

                    if (normalItemDropChance > rareItemDropChance)
                    {
                        //normal item
                        List<int> includedItems = new List<int>();
                        int targetChance = -1;

                        for (int y = 0; y < normalChances.Count; y++)
                        {
                            if (normalChances[y] > groupItemsDropChance)
                            {
                                includedItems.Add(normalChances[y]);
                            }
                        }

                        //check each item against the intergroup chance to see which one is closer to the value
                        int bestDistance = int.MaxValue;

                        for (int y = 0; y < includedItems.Count; y++)
                        {
                            if (Mathf.Abs(includedItems[y] - interGroupItemDropChance) < bestDistance)
                            {
                                bestDistance = Mathf.Abs(includedItems[y] - interGroupItemDropChance);
                                targetChance = includedItems[y];
                            }
                        }

                        if (targetChance == normalChances[0])
                            prefabToSpawn = coinItemPrefab;
                        else if (targetChance == normalChances[1])
                            prefabToSpawn = scoreItemPrefab;
                        else if (targetChance == normalChances[2])
                            prefabToSpawn = speedItemPrefab;
                        else if (targetChance == normalChances[3])
                            prefabToSpawn = invulnItemPrefab;
                        else if (targetChance == normalChances[4])
                            prefabToSpawn = speedReduceItemPrefab;
                        else
                            prefabToSpawn = coinItemPrefab;
                    }
                    else
                    {
                        //rare item
                        List<int> includedItems = new List<int>();
                        int targetChance = -1;

                        for (int y = 0; y < rareChances.Count; y++)
                        {
                            if (rareChances[y] > groupItemsDropChance)
                            {
                                includedItems.Add(rareChances[y]);
                            }
                        }

                        int bestDistance = int.MaxValue;

                        for (int y = 0; y < includedItems.Count; y++)
                        {
                            if (Mathf.Abs(includedItems[y] - interGroupItemDropChance) < bestDistance)
                            {
                                bestDistance = Mathf.Abs(includedItems[y] - interGroupItemDropChance);
                                targetChance = includedItems[y];
                            }
                        }

                        if (targetChance == rareChances[0])
                            prefabToSpawn = rareScoreItemPrefab;
                        else if (targetChance == rareChances[1])
                            prefabToSpawn = rareSpeedItemPrefab;
                        else if (targetChance == rareChances[2])
                            prefabToSpawn = rareCoinItemPrefab;
                        else if (targetChance == rareChances[3])
                            prefabToSpawn = rareInvulnItemPrefab;
                        else if (targetChance == rareChances[4])
                            prefabToSpawn = rareSpeedReduceItemprefab;
                        else
                            prefabToSpawn = coinItemPrefab;
                    }

                    spawnedItems.Add(Instantiate(prefabToSpawn as GameObject, itemSpawns[randomSpawnPoint].transform));
                    itemSpawns.RemoveAt(randomSpawnPoint);
                }
                else
                    break;
            }
        }
    }

    public void SetObstaclesState(bool newState)
    {
        foreach (GameObject obstacle in obstacles)
        {
            obstacle.SetActive(newState);
        }
    }

    public void RemoveItems()
    {
        for (int i = 0; i < spawnedItems.Count; i++)
        {
            GameObject[] tempRefs = spawnedItems.ToArray();
            Destroy(tempRefs[i]);
        }

        spawnedItems.Clear();
    }
}