protected override void Start() { base.Start(); fleeState = new State_Flee(this, mask); fleeState.NoLongerFleeing += Idle; idleState = new State_Idle(this); idleState.TargetSpotted += Flee; BehaviourState = idleState; }
void Update() { if (agent.gameObject.GetComponent <Perception>().MemoryMap.ContainsKey(player)) { timeSinceSeen = DateTime.Now - agent.gameObject.GetComponent <Perception>().MemoryMap[player].TimeLastSensed; } else { powerful = 0; //if the player has never been seen then the power is set to 0 by default to stop it from wanting to fight something it doesn't know exists } for (int i = 0; i < allies.Count; i++) { if (hasBeenFoundDead[i] == false) { if (agent.gameObject.GetComponent <Perception>().MemoryMap.ContainsKey(allies[i])) { if (agent.gameObject.GetComponent <Perception>().MemoryMap[allies[i]].targetDead == true) { deadAllyCounter += 0.1f; hasBeenFoundDead[i] = true; } } } } List <float> desireList = new List <float>(); desireList.Add(weaponDesire); desireList.Add(hpDesire); desireList.Add(powerful); desireList.Add(wanderDesire); desireList.Add(postDesire); if (timeSinceSeen.Seconds > timeToForget) { powerful = 0; //if the unit has not seen the player in 50 seconds the power will be set to 0 } if (desireList.Max() == powerful) { if (agent.gameObject.GetComponent <Perception>().MemoryMap != null && agent.gameObject.GetComponent <Perception>().MemoryMap.ContainsKey(player) && agent.gameObject.GetComponent <Perception>().MemoryMap[player].WithinFoV == false) { //if the powerful state has the highest desirability and the AI cannot see the player, the AI will search for the player if (Search_State_On == false) { State_Search_Player pSearchState = new State_Search_Player(); ResetBools(); Search_State_On = true; agent.ChangeState(pSearchState); } } else if (agent.gameObject.GetComponent <Perception>().MemoryMap != null && agent.gameObject.GetComponent <Perception>().MemoryMap.ContainsKey(player) && agent.gameObject.GetComponent <Perception>().MemoryMap[player].WithinFoV == true) { if (player.GetComponent <Player_Gun>().playerPower <= powerful) //checks to see how strong the player is compared to the unit - will make them flee if they feel weaker in comparison { if (Vector3.Distance(agent.transform.position, player.transform.position) > agent.fireRange) { if (Search_State_On == false) { State_Search_Player pSearchState = new State_Search_Player(); ResetBools(); Search_State_On = true; agent.ChangeState(pSearchState); } } else { if (Fight_State_On == false) { State_Fight_Player fightState = new State_Fight_Player(); ResetBools(); Fight_State_On = true; agent.ChangeState(fightState); } } } else if (player.GetComponent <Player_Gun>().playerPower > powerful) { Debug.Log("Epic"); if (Flee_State_On == false) { State_Flee fleeState = new State_Flee(); ResetBools(); Flee_State_On = true; agent.ChangeState(fleeState); } } } } if (desireList.Max() == weaponDesire) { //if the desire to find ammo is the highest it will try to find ammo if (Ammo_State_On == false) { State_Search_Ammo ammoState = new State_Search_Ammo(); ammoState.hasCreatedPath = false; ResetBools(); Ammo_State_On = true; agent.ChangeState(ammoState); } } if (desireList.Max() == hpDesire) { //if the desire to find health is the highest it will try to find health if (HP_State_On == false) { State_Search_Health healthState = new State_Search_Health(); healthState.hasCreatedHealthPath = false; ResetBools(); HP_State_On = true; agent.ChangeState(healthState); } } if (desireList.Max() == wanderDesire) { State_Patrol patrolState = new State_Patrol(); agent.ChangeState(patrolState); } if (desireList.Max() == postDesire) { if (Vector3.Distance(transform.position, postLocation) < 5f) { agent.SB.IsSeekOn = false; State_Idle idleState = new State_Idle(); agent.ChangeState(idleState); } else { if (Post_State_On == false) { State_Return_To_Post postState = new State_Return_To_Post(); ResetBools(); Post_State_On = true; agent.ChangeState(postState); } } } }