コード例 #1
0
    private IEnumerator PatronLoop()
    {
        while (_completedTasks < PatronSettings.DesiredTasks)
        {
            _fulfilled = false;

            // --- IDLE STATE ---

            State = PatronState.IDLE;
            for (int i = 0; i < PatronSettings.IdleLocations; i++)
            {
                var location = PickIdleLocation();
                Agent.enabled = true;
                Agent.SetDestination(location);
                Animator.SetTrigger("walk");
                yield return(new WaitUntil(ReachedDestination));

                Agent.enabled = false;
                Animator.SetTrigger("stop");
                yield return(new WaitForSeconds(Random.Range(PatronSettings.IdleTime.x, PatronSettings.IdleTime.y)));
            }

            // --- IDLE OVER ---

            _desiredTask  = PickTask2();
            State         = PatronState.WALKING;
            Agent.enabled = true;
            Agent.SetDestination(_desiredTask.Location.position);
            Animator.SetTrigger("walk");
            yield return(new WaitUntil(ReachedDestination));

            // --- WAIT STATE ---

            State = PatronState.WAITING;
            // play animation
            Agent.enabled   = false;
            _rb.velocity    = Vector3.zero;
            _rb.isKinematic = true;
            transform.SetPositionAndRotation(_desiredTask.Location.position, _desiredTask.Location.rotation);
            Animator.SetTrigger(_desiredTask.TaskLocation.Task.PatronString);
            // init variables for loop
            float patienceRemaining = PatronSettings.Patience;
            float fulfillment       = 0f;

            // reset Overhead UI
            PatronUI.SetPatienceActive(true);
            PatronUI.SetPatience(0f);
            PatronUI.SetFulfillmentActive(true);
            PatronUI.SetFulfillment(0f);

            while (patienceRemaining > 0f)
            {
                // set Overhead UI values
                PatronUI.SetPatience(Mathf.InverseLerp(PatronSettings.Patience, 0, patienceRemaining));
                PatronUI.SetFulfillment(Mathf.InverseLerp(0, _desiredTask.TaskLocation.Task.RequiredTime, fulfillment));

                bool fulfilling = _desiredTask.TaskLocation.Performing >= _desiredTask.TaskLocation.Task.RequiredPlayerCount &&
                                  (_desiredTask.TaskLocation.Task.RequiresPot ? _desiredTask.TaskLocation.UseLeft > 0f : true);
                if (fulfilling)
                {
                    fulfillment += Time.deltaTime;
                    if (fulfillment >= _desiredTask.TaskLocation.Task.RequiredTime)
                    {
                        _fulfilled = true;
                        _completedTasks++;
                        //play Sound
                        AudioSource.clip = HappyClips[Random.Range(0, HappyClips.Length)];
                        AudioSource.Play();

                        GameEvents.TaskFulfilled?.Invoke(this);
                        GameEvents.AddMoney(1);
                        break;
                    }
                }
                patienceRemaining -= Time.deltaTime;
                yield return(null);
            }

            // close Overhead UI
            PatronUI.SetPatienceActive(false);
            PatronUI.SetFulfillmentActive(false);

            // get off position
            if (_desiredTask.ExitLocation != null)
            {
                transform.SetPositionAndRotation(_desiredTask.ExitLocation.position, _desiredTask.ExitLocation.rotation);
            }
            _rb.isKinematic = false;

            _desiredTask.Reserved = false;
            _desiredTask          = null;

            // --- WAIT OVER ---

            if (!_fulfilled)
            {
                //play Sound
                AudioSource.clip = GrumpyClips[Random.Range(0, GrumpyClips.Length)];
                AudioSource.Play();
                break;
            }
        }
        Agent.enabled = true;
        Agent.SetDestination(_spawnLocation);
        Animator.SetTrigger("walk");
        yield return(new WaitUntil(ReachedDestination));

        if (_fulfilled)
        {
            // leave fulfilled
            GameEvents.AddMoney(1);
            //play Sound
            AudioSource.clip = HappyClips[Random.Range(0, HappyClips.Length)];
            AudioSource.Play();
        }
        else
        {
            //play Sound
            AudioSource.clip = GrumpyClips[Random.Range(0, GrumpyClips.Length)];
            AudioSource.Play();
        }

        GameEvents.PatronLeft?.Invoke(this, _fulfilled);
        Destroy(gameObject, .5f);
    }