コード例 #1
0
    private void stateMachine()
    {
        Debug.Log("Sucking: " + healthManager.IsSucking());
        switch (myState)
        {
        //IMPORTANT:
        //every state MUST set stationary and scared
        case EnemyState.chasing:
            stationary = false;
            scared     = false;
            if (faceSeen && !healthManager.IsSucking())
            {
                fearTimer = timeSpentAfraid;
                myState   = EnemyState.afraid;
                //important to remember to disable these transition variables
                faceSeen = false;
            }
            if (Vector2.Distance(player.transform.position, transform.position) < weaponRange)
            {
                myState = EnemyState.attacking;
            }
            break;

        case EnemyState.afraid:
            stationary = false;
            scared     = true;
            if (fearTimer <= 0 || healthManager.IsSucking())
            {
                if (!faceSeen)
                {
                    myState = EnemyState.chasing;
                }
                else
                {
                    faceSeen  = false;
                    fearTimer = timeSpentAfraid;
                }
            }

            break;

        case EnemyState.resting:
            stationary = true;
            scared     = false;
            //can set to this state from the speech controller
            // OR this happens every time they're done being afraid
            break;

        case EnemyState.attacking:
            stationary = true;
            scared     = false;
            if (attackCooldownTimer <= 0)
            {
                attackCooldownTimer = attackCooldownTime;
                weapon.attack();
                enemyAnimator.SetBool("Attacking", true);
                Invoke("StopAttacking", .5f);
            }
            if (Vector2.Distance(player.transform.position, transform.position) > weaponRange)
            {
                myState = EnemyState.chasing;
            }
            if (faceSeen)
            {
                myState = EnemyState.afraid;
            }

            //check distance to player, exit if further than attack range
            break;

        case EnemyState.gettingSucked:
            stationary = true;
            scared     = false;
            if (!bloodSpawned)
            {
                bloodSpawned = true;
                GameObject particles = GameObject.Instantiate(suckedParticles, transform);
                particles.transform.localPosition = new Vector3(0, 0, -1);
            }

            //whatever state caused this transition (afraid, chasing, resting or non aggro)
            //will need to set a succ timer

            // this state queries it and waits until it runs out or the player stops
            // the succ to transition to the dead state

            //OR we could just have the player tell the enemy they're done succin
            break;

        case EnemyState.dead:
            stationary = true;
            scared     = false;
            break;

        case EnemyState.speaking:
            stationary = false;
            scared     = false;
            //transition: very dependent on entry reason. Should have a speach controller
            // script that will manually change this state from its context


            //we may not need this state at all if we dont want speech to affect enemy movement
            break;

        case EnemyState.notAggressive:
            stationary = true;
            scared     = false;
            //transition: when player is within certain range, could transition to a speaking
            // state and then to chasing, or go straight to chasing
            //TO IMPLEMENT: random movement for idling
            break;

        default:
            break;
        }
        if (!(myState == EnemyState.gettingSucked))
        {
            bloodSpawned = false;
        }
        enemyMovementController.setStationary(stationary);
        enemyMovementController.setScared(scared);
    }