コード例 #1
0
    //##############################################################################################
    // Set this as the global instance, and setup the double buffers
    //##############################################################################################
    void Start()
    {
        instance = this;

        updateListA = new List <EnemyBehavior>();
        updateListB = new List <EnemyBehavior>();
    }
コード例 #2
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);
     }
 }
コード例 #3
0
    void SpawnEnemiesOptimized()
    {
        using (new CustomTimer("SpawnEnemies Optimized", _numEnemies)) {
            GameObject            enemyManagerObj = GameObject.Find("EnemyManager");
            EnemyManagerComponent enemyManager    = enemyManagerObj.GetComponent <EnemyManagerComponent>();
            for (int i = 0; i < _numEnemies; ++i)
            {
                GameObject enemy = (GameObject)GameObject.Instantiate(_enemyPrefab, Vector3.zero, Quaternion.identity);

                enemyManager.AddEnemy(enemy);
            }
        }
    }
コード例 #4
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);
        }
    }
コード例 #5
0
 //##############################################################################################
 // If the enemy is damaged, register to update it and react
 //##############################################################################################
 public virtual void PlayDamagedSequence()
 {
     EnemyManagerComponent.RegisterUpdate(this);
 }