private IEnumerator NextCustomer()
    {
        Customer nextCustomer = null;

        // if there is not a customer to move, spawn a new one if room in shop
        yield return(new WaitUntil(() => customersWaiting.Count < customerLines.Count * 2));

        nextCustomer = Instantiate(customerPrefab, customerSpawnPos.position, customerSpawnPos.rotation).GetComponent <Customer>();
        customersWaiting.Add(nextCustomer);
        nextCustomer.SetOrder(customerOrders[0]);
        customerOrders.Remove(customerOrders[0]);

        // find shortest line
        CustomerLinePos shortestLine = FindShortestCustomerLine();

        // send customer to shortest line
        nextCustomer.SetTargetLine(shortestLine);
        shortestLine.customersInLine.Add(nextCustomer);

        yield return(new WaitForSeconds(gameManager.currentGameDay.dayLength / gameManager.currentGameDay.numOfCustomers));

        // continue only if more day/customer left
        if (gameManager.currentDayTimer > 1 && customerOrders.Count > 0)
        {
            StartCoroutine(NextCustomer());
        }
    }
    public CustomerLinePos FindShortestCustomerLine()
    {
        CustomerLinePos shortestLine = customerLines[0];

        foreach (var line in customerLines.Where(line => line.customersInLine.Count <= shortestLine.customersInLine.Count))
        {
            shortestLine = line;
        }

        return(shortestLine);
    }
    private IEnumerator WaitingInLine()
    {
        if (CurrentCustomerAIState != CustomerAIStates.Entering)
        {
            yield break;
        }

        if (Physics.SphereCast(transform.position, agent.radius, transform.forward, out RaycastHit hit, agent.stoppingDistance * 2, mask))
        {
            if (hit.transform.TryGetComponent(out CustomerAI otherCustomerAI))
            {
                if (otherCustomerAI.CurrentCustomerAIState != CustomerAIStates.Leaving &&
                    otherCustomerAI.targetLinePos == targetLinePos)
                {
                    agent.isStopped = true;
                }
                else if (otherCustomerAI.CurrentCustomerAIState == CustomerAIStates.Leaving)
                {
                    customerAboutToLeave = true;
                    yield return(new WaitForSeconds(thisCustomer.customerLeaveDelayTime * 1.5f));

                    agent.isStopped      = false;
                    customerAboutToLeave = false;
                }
            }
            else
            {
                agent.isStopped = false;
            }
        }

        if (agent.isStopped)
        {
            thisCustomer.customerAudio.ChangeCustomerAudio(CustomerAudio.CustomerAudioStates.Stop);
            yield return(new WaitWhile((() => customerAboutToLeave)));

            CustomerLinePos shortestLinePos = customerLine.FindShortestCustomerLine();
            if (targetLinePos.customersInLine.IndexOf(thisCustomer) > shortestLinePos.customersInLine.Count)
            {
                agent.isStopped = false;
                targetLinePos.customersInLine.Remove(thisCustomer);
                SetTargetLine(shortestLinePos);
                shortestLinePos.customersInLine.Add(thisCustomer);
            }
        }

        yield return(new WaitForEndOfFrame());

        if (CurrentCustomerAIState == CustomerAIStates.Entering)
        {
            StartCoroutine(WaitingInLine());
        }
    }
 public void AddNewCustomerLine(CustomerLinePos customerLine)
 {
     customerLines.Add(customerLine);
 }
 /// <summary>
 /// Sets the line that the customer will initially head towards.
 /// </summary>
 /// <param name="customerLinePos">The CustomerLinePos that they player will head towards.</param>
 public void SetTargetLine(CustomerLinePos customerLinePos)
 {
     customerAI.SetTargetLine(customerLinePos);
     customerAI.ChangeCustomerAIState(CustomerAI.CustomerAIStates.Entering);
 }
Ejemplo n.º 6
0
 private void Awake()
 {
     customerLinePos = GetComponent <CustomerLinePos>();
 }
 public void SetTargetLine(CustomerLinePos customerLinePos)
 {
     targetLinePos = customerLinePos;
     agent.SetDestination(targetLinePos.transform.position);
 }