Exemplo n.º 1
0
    void SpawnItem(ShelfSpawns shelf, ShelfNo loc, GameObject spawn, bool flip)
    {
        spawnIndex++;
        spawnsLeft--;
        currentDensity++;

        Vector3 pos;

        switch (loc)
        {
        case ShelfNo.Top:
            pos = shelf.GetPos(loc, spawnedTop);
            spawnedTop++;
            break;

        case ShelfNo.Middle:
            pos = shelf.GetPos(loc, spawnedMiddle);
            spawnedMiddle++;
            break;

        case ShelfNo.Bottom:
            pos = shelf.GetPos(loc, spawnedBottom);
            spawnedBottom++;
            break;

        default:
            pos = Vector3.zero;
            Debug.LogError("Incorrect Shelf Number");
            break;
        }

        spawn.transform.position = pos;

        if (flip)
        {
            spawn.transform.rotation = Quaternion.Euler(0, 180, 0);
        }
        else
        {
            spawn.transform.rotation = Quaternion.Euler(0, 0, 0);
        }

        spawn.SetActive(true);
    }
Exemplo n.º 2
0
    void SpawnSide(GameObject[] shelves, int density, bool flip)
    {
        //reference to shelf scripts

        ShelfSpawns[] spawns = new ShelfSpawns[shelves.Length];

        for (int i = 0; i < spawns.Length; i++)
        {
            spawns[i] = shelves[i].GetComponent <ShelfSpawns>();
        }

        //determine density


        float d = (float)density;

        int shelfDensity = Mathf.CeilToInt(d / (float)shelvesperS) - 2;

        d = (float)shelfDensity;

        d = d / 20f;

        int topDensity    = Mathf.CeilToInt(d * 9);
        int middleDensity = Mathf.CeilToInt(d * 7);
        int bottomDensity = Mathf.CeilToInt(d * 4);

        if (topDensity > topSpawnNo)
        {
            middleDensity += topDensity - topSpawnNo;

            topDensity = topSpawnNo;
        }

        if (middleDensity > middleSpawnNo)
        {
            bottomDensity += middleDensity - middleSpawnNo;
            middleDensity  = middleSpawnNo;
        }

        if (bottomDensity > bottomSpawnNo)
        {
            Debug.Log("bot: " + bottomDensity);
            bottomDensity = bottomSpawnNo;
        }

        List <int> bag = new List <int>();

        for (int i = 0; i < spawns.Length; i++)
        {
            //ask for top tier positions

            bag.Clear();

            for (int j = 0; j < topSpawnNo; j++)
            {
                bag.Add(j);
            }

            for (int j = 0; j < topDensity; j++)
            {
                int index = bag[Random.Range(0, bag.Count)];

                bag.Remove(index);

                SpawnItem(spawns[i].GetPos(ShelfNo.Top, index), flip);
            }

            //ask for mid tier positions

            bag.Clear();

            for (int j = 0; j < middleSpawnNo; j++)
            {
                bag.Add(j);
            }

            for (int j = 0; j < middleDensity; j++)
            {
                int index = bag[Random.Range(0, bag.Count)];

                bag.Remove(index);

                SpawnItem(spawns[i].GetPos(ShelfNo.Middle, index), flip);
            }

            //ask for bottom tier positions

            bag.Clear();

            for (int j = 0; j < bottomSpawnNo; j++)
            {
                bag.Add(j);
            }

            for (int j = 0; j < bottomDensity; j++)
            {
                int index = bag[Random.Range(0, bag.Count)];

                bag.Remove(index);

                SpawnItem(spawns[i].GetPos(ShelfNo.Bottom, index), flip);
            }
        }
    }
Exemplo n.º 3
0
    void SpawnShelf(ShelfSpawns shelf, GameObject[] spawns, bool flip)
    {
        spawnIndex = 0;
        spawnsLeft = spawns.Length;
        int iterations = 0;

        int maxTop    = shelf.GetMaxTop();
        int maxMiddle = shelf.GetMaxMiddle();
        int maxBottom = shelf.GetMaxBottom();

        spawnedTop    = 0;
        spawnedMiddle = 0;
        spawnedBottom = 0;

        while (spawnsLeft > 0)
        {
            if (spawnedTop == maxTop && spawnedMiddle == maxMiddle && spawnedBottom == maxBottom)
            {
                Debug.LogError("Shelf full, excess: " + spawnsLeft);
                spawnsLeft = 0;
            }

            if (spawnsLeft > 1)
            {
                if (spawnedTop < maxTop)
                {
                    SpawnItem(shelf, ShelfNo.Top, spawns[spawnIndex], flip);

                    //assign second
                    if (spawnedTop < maxTop)
                    {
                        SpawnItem(shelf, ShelfNo.Top, spawns[spawnIndex], flip);
                    }
                }

                if (spawnsLeft > 1)
                {
                    if (spawnedMiddle < maxMiddle)
                    {
                        SpawnItem(shelf, ShelfNo.Middle, spawns[spawnIndex], flip);

                        //assign second
                        if (spawnedMiddle < maxMiddle)
                        {
                            SpawnItem(shelf, ShelfNo.Middle, spawns[spawnIndex], flip);
                        }
                    }
                }

                if (spawnsLeft > 0)
                {
                    if (spawnedBottom < maxBottom)
                    {
                        SpawnItem(shelf, ShelfNo.Bottom, spawns[spawnIndex], flip);
                    }
                }
            }
            else
            {
                //exactly 1 left
                if (spawnedTop < maxTop)
                {
                    SpawnItem(shelf, ShelfNo.Top, spawns[spawnIndex], flip);
                }
                else if (spawnedMiddle < maxMiddle)
                {
                    SpawnItem(shelf, ShelfNo.Middle, spawns[spawnIndex], flip);
                }
                else if (spawnedBottom < maxBottom)
                {
                    SpawnItem(shelf, ShelfNo.Bottom, spawns[spawnIndex], flip);
                }
                else
                {
                    Debug.LogError("Can't place last item.");
                    spawnsLeft = 0;
                }
            }

            iterations++;

            if (iterations > 30000)
            {
                Debug.LogError("While loop stuck.");
                break;
            }
        }
    }