public override IEnumerator InState(EnemyAIMachine owner) { while (owner.botMachine.currentState == EnemyTurretSearchState.instance) { if (!owner.disabled) { owner.GetComponent <Animator>().enabled = true; if (owner.canSeeEnemy && owner.enemyObject != null) { owner.botMachine.ChangeState(EnemyAttackState.instance); yield break; } else { owner.canSeeEnemy = false; } } else { owner.thisAgent.SetDestination(owner.transform.position); } yield return(null); } }
public override IEnumerator InState(EnemyAIMachine owner) { float timeWaitingToShoot = 0; bool movingCloserToEnemy = false; while (owner.botMachine.currentState == EnemyAttackState.instance) { if (!owner.disabled) { if (owner.enemyObject == null) { owner.canSeeEnemy = false; owner.health = null; if (!owner.insideTurret) { owner.botMachine.ChangeState(EnemyChaseState.instance); } else { owner.botMachine.ChangeState(EnemyTurretSearchState.instance); } yield break; } if (owner.enemyObject != null) { if (owner.turretBot == false && movingCloserToEnemy == false) { owner.thisAgent.SetDestination(owner.transform.position); } else { owner.GetComponent <Animator>().enabled = false; } //before firing check if there are too many enemies and if the bot should change to the retreat state if (owner.additionalEnemies.Count >= 2 && owner.friendlyBots.Count <= 1 && !owner.turretBot) { for (int i = 0; i < owner.movingBotsOnly.Count; i++) //for all moving bots, check if any of them are close enough to retreat to { if (Vector3.Distance(owner.movingBotsOnly[i].transform.position, owner.transform.position) <= owner.retreatRadius && Vector3.Distance(owner.movingBotsOnly[i].transform.position, owner.transform.position) >= 50) { owner.botMachine.ChangeState(EnemyRetreatState.instance); yield break; } } } int numberOfBotsCalled = 0; for (int i = 0; i < owner.movingBotsOnly.Count; i++) //if there are any friendly bots within the reinforcementRadius, give them this bots target { if (Vector3.Distance(owner.transform.position, owner.movingBotsOnly[i].transform.position) <= owner.reinforcementRadius) { if (owner.movingBotsOnly[i].GetComponent <EnemyAIMachine>().enemyObject == null) //if the current bot doesnt already have a target, set it { owner.movingBotsOnly[i].GetComponent <EnemyAIMachine>().enemyObject = owner.enemyObject; numberOfBotsCalled++; } } } if (numberOfBotsCalled != 0) { owner.logD.RecieveLog(owner.displayName + " called " + numberOfBotsCalled + " bot(s) for help!"); numberOfBotsCalled = 0; } if (Vector3.Distance(owner.transform.position, owner.enemyObject.transform.position) <= owner.attackDistance && owner.canSeeEnemy) { MoveTurret(owner); timeWaitingToShoot += Time.deltaTime; if (timeWaitingToShoot >= 5f) { owner.thisAgent.SetDestination(owner.enemyObject.transform.position); movingCloserToEnemy = true; if (Vector3.Distance(owner.transform.position, owner.enemyObject.transform.position) <= 20) { owner.thisAgent.SetDestination(owner.transform.position); movingCloserToEnemy = false; } } //after calling the MoveTurret method, wait for the onTarget bool to be true if (owner.onTarget == true) { timeWaitingToShoot = 0; owner.laserLine.SetPosition(0, owner.barrelEnd.position); owner.laserLine.SetPosition(1, owner.contactPoint); owner.laserLine.enabled = true; owner.audioS.PlayOneShot(owner.GetComponent <MultipleAudioClips>().clips[1]); yield return(new WaitForSeconds(0.5f)); owner.laserLine.enabled = false; Shoot(owner); bool removing = false; for (int i = 0; i < owner.friendlyBots.Count; i++) //if any of the bots are already removing an enemy, dont do anything { if (owner.friendlyBots[i].GetComponent <EnemyAIMachine>().removingEnemy == true) { removing = true; break; } } if (removing == false) { if (owner.enemyObject != null) { if (owner.health.health <= 0) { owner.removingEnemy = true; OnEnemyDeath(owner); } } } } } else { if (!owner.insideTurret) { owner.botMachine.ChangeState(EnemyChaseState.instance); } else { owner.botMachine.ChangeState(EnemyTurretSearchState.instance); } yield break; } } } else { owner.thisAgent.SetDestination(owner.transform.position); } yield return(null); } }