//############################################################################################## // Cache this as the global instance, and setup the component //############################################################################################## void Start() { instance = this; currentTokens = maxTokens; tokenRespawnTimer = new Timer(tokenRespawnTime); recentlyTakenTokens = new Queue <GameObject>(maxTokens); }
//############################################################################################## // 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); } }