protected override IEnumerator Execute() { SetState(NodeState.RUNNING); nearbyEnemies = senses.GetEnemiesInView(); // If no enemy visible exit node if (nearbyEnemies.Count <= 0) { Debug.Log("FAIL"); SetState(NodeState.FAILURE); yield break; } // Delay before agent can attack again yield return(attackTimer); // Attack the nearby enemy for (int i = 0; i < nearbyEnemies.Count; i++) { if (senses.IsInAttackRange(nearbyEnemies[i])) { actions.AttackEnemy(nearbyEnemies[i]); } } SetState(NodeState.SUCCESS); yield return(null); }
// a function that checks to see if the Ai has low health and if they do they will begin to flee public bool LowHealth(AgentData data, AgentActions actions, Sensing sensing, bool Retreated, bool HealthZone) { if (data.CurrentHitPoints < 15) { if (sensing.GetEnemiesInView().Count == 0) { Fleeing(actions, data, sensing, HealthZone); return(true); } } return(false); }
// a function that allows the Ai to check to see if there are enemys near by. if there are then they will begin to move to wards them and attack. slowing down when they get close enough to attack public void Attack(Sensing view, AgentActions action) { //first see if an enemy comes into view foreach (GameObject G in view.GetEnemiesInView()) { action.ResumeMovement(); action.MoveTo(G); action.AttackEnemy(G); //this.GetComponent<AgentActions>().AttackEnemy(G); } //need to resume movement when enemyes are dead }
protected override IEnumerator Execute() { SetState(NodeState.RUNNING); nearbyEnemies = senses.GetEnemiesInView(); //for (int i = 0; i < nearbyEnemies.Count; i++) Debug.Log(nearbyEnemies.Count); if (nearbyEnemies.Count <= 0) { SetState(NodeState.FAILURE); yield break; } SetState(NodeState.SUCCESS); yield return(null); }
private void Battle() { if (!_agentData.HasEnemyFlag) { GameObject checkFlag; if (_agentSenses.GetEnemiesInView().Count > 0) { for (int i = 0; i < _agentSenses.GetEnemiesInView().Count; i++) { if (_agentData.CurrentHitPoints > 50) { if (_agentSenses.IsInAttackRange(_agentSenses.GetEnemiesInView()[i])) { inBattle = true; _agentActions.UseItem(_agentInventory.GetItem(Names.PowerUp)); _agentActions.AttackEnemy(_agentSenses.GetEnemiesInView()[i]); } else if (this.tag == Tags.BlueTeam) { checkFlag = _agentSenses.GetObjectInViewByName(Names.RedFlag); if (checkFlag != null) { if (Vector3.Distance(transform.position, checkFlag.transform.position) < Vector3.Distance(transform.position, _agentSenses.GetEnemiesInView()[i].transform.position)) { _agentActions.MoveTo(checkFlag); } } else { inBattle = true; _agentActions.MoveTo(_agentSenses.GetEnemiesInView()[i]); } } else if (this.tag == Tags.RedTeam) { checkFlag = _agentSenses.GetObjectInViewByName(Names.BlueFlag); if (checkFlag != null) { if (Vector3.Distance(transform.position, checkFlag.transform.position) < Vector3.Distance(transform.position, _agentSenses.GetEnemiesInView()[i].transform.position)) { _agentActions.MoveTo(checkFlag); } } else { inBattle = true; _agentActions.MoveTo(_agentSenses.GetEnemiesInView()[i]); } } } else if (_agentData.CurrentHitPoints <= 10) { if (_agentData.IsPoweredUp) { inBattle = true; _agentActions.AttackEnemy(_agentSenses.GetEnemiesInView()[i]); } else { int RandomChanceToAttack = Random.Range(0, 100) + 1; if (RandomChanceToAttack > 35) { inBattle = true; _agentActions.AttackEnemy(_agentSenses.GetEnemiesInView()[i]); } else { inBattle = false; _agentActions.Flee(_agentSenses.GetEnemiesInView()[i]); } } } else { if (_agentSenses.IsInAttackRange(_agentSenses.GetEnemiesInView()[i])) { int RandomChanceToAttack = Random.Range(0, 2); if (RandomChanceToAttack == 0) { inBattle = true; _agentActions.UseItem(_agentInventory.GetItem(Names.PowerUp)); _agentActions.AttackEnemy(_agentSenses.GetEnemiesInView()[i]); } else { if (_agentData.IsPoweredUp) { inBattle = true; _agentActions.AttackEnemy(_agentSenses.GetEnemiesInView()[i]); } else { int RandomChanceToFlee = Random.Range(0, 2); if (RandomChanceToFlee == 0) { _agentActions.MoveToRandomLocation(); } else { _agentActions.Flee(_agentSenses.GetEnemiesInView()[i]); } inBattle = false; } } } else { inBattle = false; } } } } else { inBattle = false; } } else { inBattle = false; } }