// Use this for initialization protected virtual void Start() { model = GetComponent <GenericShipModel>(); view = GetComponent <GenericShipView>(); myDetector = GetComponent <SphereDetector>(); if (myDetector == null) { Debug.LogError("No Detector attached to " + gameObject + "!"); } // Check if initial state is specified. If not, and there is no waypoint manager, then begin in square patrol mode. // Otherwise, begin in waypoint patrol mode. if (startState == GenericShipAIState.Idle && waypointManager == null) { currentState = GenericShipAIState.SquarePatrolling; } if (startState == GenericShipAIState.Idle && waypointManager != null) { currentState = GenericShipAIState.WaypointPatrolling; } // If we are patrolling and have no waypoints, then begin patrolling in square formation. if (currentState == GenericShipAIState.SquarePatrolling) { StartCoroutine(SquareFormationPatrol()); } // Begin custom update loop for AI. InvokeRepeating("UpdateAIController", 0.0f, behaviorChangeRate); }
// Updates the AI controller. private void UpdateAIController() { if (currentState == GenericShipAIState.SquarePatrolling) { // If we are in square formation patrol and have reached our current forward point, then turn left and generate a new forward point. if (transform.position == forwardSquarePatrolPoint) { // Turn some number of degrees left. transform.Rotate(new Vector3(0, 0, Random.Range(0, 90))); StartCoroutine(SquareFormationPatrol()); } } // Check if there is an enemy within range. GameObject closestEnemy = myDetector.ClosestEnemy(); // If we have an enemy, then chase and attack it. Otherwise, resume patrolling. // Don't execute this if we're in lock chase if (closestEnemy != null && !lockedInChase) { currentState = GenericShipAIState.Chasing; StartCoroutine(LockChase()); StartCoroutine(ChaseAttackEnemyVariance()); StartCoroutine(ChaseAttackEnemy(closestEnemy)); } else if (closestEnemy == null) { if (waypointManager != null) { currentState = GenericShipAIState.WaypointPatrolling; } currentState = GenericShipAIState.SquarePatrolling; } }