コード例 #1
0
    private void StateMachine()
    {
        //Global case end check
        if (HP <= 0)
        {
            BoxCollider boxCollider = GFXHeadTrans.GetComponent <BoxCollider>();
            //started from the buttom now we here (enemyGFX => enemyGuy)
            Destroy(this.transform.parent.gameObject);
            return;
        }

        if (lastState != currentState)
        {
            Debug.Log("Switched state! to: " + currentState.ToString() + " ,from: " + lastState.ToString());
            lastState = currentState;
            switch (currentState)
            {
            case EnemyStates.Patroling:
                //Enter state action
                timeSincePatrolled = Time.timeSinceLevelLoad;



                break;

            case EnemyStates.Following:
                //Enter state action

                FollowTarget();
                timerSinceFollow = Time.timeSinceLevelLoad;



                break;

            case EnemyStates.Attacking:
                //Enter state action



                break;

            case EnemyStates.Stunned:
                //Enter state action
                Stunned();

                timerSinceStunned = Time.timeSinceLevelLoad;


                break;
            }
        }



        switch (currentState)
        {
        case EnemyStates.Patroling:
            //Case Update
            if (arrivedAtLocation)
            {
                timerSinceFollow = Time.timeSinceLevelLoad;
            }
            if (Time.timeSinceLevelLoad - timeSincePatrolled >= PatrolUpdateInterval)
            {
                arrivedAtLocation = false;
                Patrol();
            }
            //Case end condition checking
            if (distanceFromPlayer <= playerDetectionDistance)
            {
                currentState = EnemyStates.Following;
            }
            break;

        case EnemyStates.Following:
            //Case Update
            if (Time.timeSinceLevelLoad - timerSinceFollow >= FollowUpdateInterval)
            {
                FollowTarget();
                timerSinceFollow = Time.timeSinceLevelLoad;
            }
            //Case end condition checking
            if (distanceFromPlayer > DetectionDiatance + DetectionDisIncris)
            {
                currentState = EnemyStates.Patroling;
            }
            break;

        case EnemyStates.Attacking:
            //Case Update
            Attacking();
            //Case end condition checking
            currentState = EnemyStates.Following;

            break;

        case EnemyStates.Stunned:
            //Case Update

            //Case end condition checking

            if (Time.timeSinceLevelLoad - timerSinceStunned >= StunUpdateInterval)
            {
                rb.mass        = 1;
                rb.isKinematic = true;
                //transform.position = new Vector3(transform.position.x, 0.5905833f, transform.position.z);
                transform.DORotate(new Vector3(0f, 0f, 0f), 1f).OnComplete(EnablePhysics);
            }
            break;
        }

        //Debug.Log("State: " + currentState.ToString());
    }
コード例 #2
0
 private void OnDrawGizmos()
 {
     Handles.Label(transform.position, state.ToString());
 }