Exemple #1
0
    private void SpawnCustomer(GameObject newCustomer)
    {
        GameObject         customerObj = Instantiate(newCustomer);
        CustomerController customer    = customerObj.GetComponent <CustomerController>();

        // Spawn from back or front
        int  roll        = Random.Range(0, 2);
        bool isBackSpawn = roll == 0 ? true : false;

        // Spawn initial position
        if (isBackSpawn)
        {
            customerObj.transform.position = new Vector3(Random.Range(-20f, 20f), 0.5f, -200);
        }
        else
        {
            customerObj.transform.position = new Vector3(Random.Range(-20f, 20f), 0.5f, 200);
        }

        // Handle planes
        if (customer.isAerial)
        {
            customerObj.transform.position = new Vector3(customerObj.transform.position.x, Random.Range(3f, 4f), customerObj.transform.position.z);
        }

        // Initialize customer
        int level = currentLevel;

        if (currentLevel > 5)
        {
            level = 5;
        }
        List <GameObject> possibleFoods = ResourceLoader.instance.GetLevel(level).possibleFoods;
        int numFoods = Random.Range(1, 4);

        for (int i = 0; i < numFoods; i++)
        {
            Bullet randomFood = possibleFoods[Random.Range(0, possibleFoods.Count)].GetComponent <Bullet>();
            customer.AssignFoodRequirement(randomFood);
        }

        // Find a horizontal distance. This is doing separately so the car doesnt just sit directly in front of the player
        int   hRoll         = Random.Range(0, 2);
        bool  isLeftSection = roll == 0 ? true : false;
        float xPos;

        if (isLeftSection)
        {
            xPos = Random.Range(-maxDistance, -2);
        }
        else
        {
            xPos = Random.Range(2, maxDistance);
        }

        // Set position for them to move to. Make sure don't get too close or far via min and max distance
        Vector3 targetPosition = Vector3.zero;

        if (isBackSpawn)
        {
            while (targetPosition == Vector3.zero || Vector3.Distance(targetPosition, PlayerController.instance.transform.position) < minDistance)
            {
                float currMinDistance = -minDistance;
                if (customer.isAerial)
                {
                    currMinDistance = Mathf.Min(-minFlyerDistance, currMinDistance);
                }

                targetPosition = new Vector3(xPos, 0, Random.Range(-maxDistance, currMinDistance));
            }
        }
        else
        {
            while (targetPosition == Vector3.zero || Vector3.Distance(targetPosition, PlayerController.instance.transform.position) < minDistance)
            {
                float currMinDistance = minDistance;
                if (customer.isAerial)
                {
                    currMinDistance = Mathf.Max(minFlyerDistance, currMinDistance);
                }

                targetPosition = new Vector3(xPos, 0, Random.Range(currMinDistance, maxDistance));
            }
        }

        // Handle planes
        if (customer.isAerial)
        {
            targetPosition = new Vector3(targetPosition.x, Random.Range(3f, 4f), targetPosition.z);
        }

        customer.SetTargetPosition(targetPosition);

        // Get it's Customer Component and add it to the list.
        customers.Add(customer);
        aliveOrders++;
        //Debug.Log("NEW CUSTOMER SPAWNED");
    }