Exemplo n.º 1
0
    /* Spawns a new random customer every "timeForNextCustomer" seconds
     * as long maxCustomers is not reached.
     */
    private IEnumerator SpawnCustomer()
    {
        int index;

        while (true)
        {
            yield return(new WaitForSeconds(timeToNextCustomer));

            if (countCustomers < maxCustomers)
            {
                //don't use the same customer twice in a row
                do
                {
                    index = Random.Range(0, customers.Length);
                } while (index == lastCustomerIndex);

                lastCustomerIndex = index;
                countCustomers   += 1;

                //get random products
                Product[] products = randomProducts.GetRandomProducts();

                //calculate time till customer leaves
                float time = TimeTillLeave(products);

                //get free target point
                TargetPoint point          = null;
                int         freePointIndex = -1;
                for (int i = 0; i < targetPoints.Length; i++)
                {
                    point = targetPoints[i].GetComponent <TargetPoint>();
                    if (!point.GetIsUsed())
                    {
                        point.SetIsUsed(true);
                        freePointIndex = i;
                        break; //if free point was found
                    }
                }

                //spawning...
                if (freePointIndex >= 0)
                {
                    GameObject cust      = Instantiate(customers[index], spawnPoint.position, transform.rotation, customerParent);
                    Vector3    targetPos = targetPoints[freePointIndex].transform.position;
                    cust.GetComponent <Customer>().Setup(targetPos, endPoint.position, products, freePointIndex, time, this, customerUI);
                    point.SetCustomer(cust.GetComponent <Customer>());
                    freePointIndex = -1;
                }
            }
            else
            {
                StopCoroutine("SpawnCustomer");
            }
        }
    }