コード例 #1
0
 //##############################################################################################
 // When the player enters a trigger around the enemy, register for an update to react.
 //##############################################################################################
 public void OnTriggerEnter(Collider other)
 {
     if (other.tag == "Player")
     {
         EnemyManagerComponent.RegisterUpdate(this);
     }
 }
コード例 #2
0
    //##############################################################################################
    // If we ever encounter the player, shoot at them. Otherwise, patrol from A to B, wait, then
    // patrol back.
    //##############################################################################################
    public override void EnemyUpdate()
    {
        base.EnemyUpdate();

        float playerDistance = (transform.position - FirstPersonPlayerComponent.player.transform.position).magnitude;

        // any detection of the player during the patrol stops and shoot
        if (exampleState != ExampleState.Shooting && playerDistance < SHOOT_RADIUS)
        {
            exampleState = ExampleState.Shooting;
            StopMoving();
            rotation.SetAnimationIndex(SHOOT_ANIMATION_INDEX);
        }

        // Pretty simple finite state machine
        if (exampleState == ExampleState.Idle)
        {
            if (patrolIdleTimer.Finished())
            {
                exampleState = ExampleState.Patrolling;
                MoveToPosition(patrollingAToB ? patrolPointB.position : patrolPointA.position);
                rotation.SetAnimationIndex(WALK_ANIMATION_INDEX);
                patrollingAToB = !patrollingAToB;
            }
        }
        else if (exampleState == ExampleState.Patrolling)
        {
            if (AtGoal())
            {
                patrolIdleTimer.Start();
                exampleState = ExampleState.Idle;
                rotation.SetAnimationIndex(IDLE_ANIMATION_INDEX);
            }
        }
        else if (exampleState == ExampleState.Shooting)
        {
            if (playerDistance > SHOOT_RADIUS)
            {
                exampleState = ExampleState.Idle;
                patrolIdleTimer.Start();
            }

            bool hasShootToken = AttackTokenComponent.RequestToken(gameObject);

            if (shootTimer.Finished() && hasShootToken)
            {
                Vector3 toPlayer = FirstPersonPlayerComponent.player.transform.position - transform.position;
                toPlayer.y         = 0.0f;
                transform.rotation = Quaternion.LookRotation(toPlayer);

                gun.Shoot();
                shootTimer.Start();
                rotation.SetAnimationIndex(IDLE_ANIMATION_INDEX);
            }
        }

        // Make sure to re-register for next frames update if we're still near enough!
        if (playerDistance < UDPATE_RADIUS)
        {
            EnemyManagerComponent.RegisterUpdate(this);
        }
    }
コード例 #3
0
 //##############################################################################################
 // If the enemy is damaged, register to update it and react
 //##############################################################################################
 public virtual void PlayDamagedSequence()
 {
     EnemyManagerComponent.RegisterUpdate(this);
 }