コード例 #1
0
    private void Update()
    {
        switch (state)
        {
        case EnemyStates.FindTarget:
        {
            _anim.SetBool("IsRunning", false);
            _anim.SetBool("IsReturning", false);
            if (target == null || target.takenByEnemy)
            {
                target = ItemsManager.instance.GetRandomTarget();
            }
            if (target != null)
            {
                state = EnemyStates.ChaseTarget;
            }
            break;
        }

        case EnemyStates.ChaseTarget: {
            if (target == null || target.takenByEnemy)
            {
                state = EnemyStates.FindTarget;
                break;
            }
            _anim.SetBool("IsRunning", true);
            _anim.SetBool("IsReturning", false);
            _agent.SetDestination(target.transform.position);
            // check if reached target
            float distanceFromItem = Vector3.Magnitude(target.transform.position - transform.position);
            if (distanceFromItem <= _agent.stoppingDistance + 0.8f)
            {
                // enemy reached target, take it
                state = EnemyStates.TakeTarget;
                break;
            }
            break;
        }

        case EnemyStates.TakeTarget: {
            if (target == null)
            {
                break;
            }
            // disable interaction of target
            target.DisableInteraction();
            // set target's takenByEnemy
            target.takenByEnemy = true;
            // position and parent target
            target.transform.position = grabPosition.position;
            target.transform.rotation = grabPosition.rotation;
            target.transform.parent   = grabPosition;
            state = EnemyStates.GoCheckout;
            break;
        }

        case EnemyStates.GoCheckout: {
            // animation
            _anim.SetBool("IsRunning", false);
            _anim.SetBool("IsReturning", true);
            // return to checkout area
            Vector3 checkoutPos = checkoutPoint.position;
            _agent.SetDestination(checkoutPos);
            // check if reached checkout area
            float distanceFromCheckout = Vector3.Magnitude(checkoutPos - transform.position);
            if (distanceFromCheckout <= _agent.stoppingDistance + 1f)
            {
                // reached checkout area, drop item
                DropTarget();
                state = EnemyStates.FindTarget;
                break;
            }
            break;
        }

        case EnemyStates.Idle:
        {
            _anim.SetBool("IsRunning", false);
            _anim.SetBool("IsReturning", false);
            _agent.isStopped = true;
            break;
        }
        }
    }