private void ConstructFSM() { PatrolState patrol = new PatrolState(this); patrol.AddTransition(Transition.SawPlayer, FSMStateID.Chasing); patrol.AddTransition(Transition.ReachPlayer, FSMStateID.Attacking); patrol.AddTransition(Transition.NoHealth, FSMStateID.Dead); patrol.AddTransition(Transition.GotBored, FSMStateID.Bored); patrol.AddTransition(Transition.WantsTimeOff, FSMStateID.OffDuty); patrol.AddTransition(Transition.Hurt, FSMStateID.Repairing); patrol.AddTransition(Transition.WantsToHide, FSMStateID.Hiding); ChaseState chase = new ChaseState(this); chase.AddTransition(Transition.LostPlayer, FSMStateID.Patrolling); chase.AddTransition(Transition.ReachPlayer, FSMStateID.Attacking); chase.AddTransition(Transition.NoHealth, FSMStateID.Dead); chase.AddTransition(Transition.Hurt, FSMStateID.Repairing); AttackState attack = new AttackState(this); attack.AddTransition(Transition.LostPlayer, FSMStateID.Patrolling); attack.AddTransition(Transition.SawPlayer, FSMStateID.Chasing); attack.AddTransition(Transition.NoHealth, FSMStateID.Dead); attack.AddTransition(Transition.Hurt, FSMStateID.Repairing); OffDutyState offduty = new OffDutyState(rigidbody.transform, this); offduty.AddTransition(Transition.SawPlayer, FSMStateID.Chasing); offduty.AddTransition(Transition.RestedLongEnough, FSMStateID.Patrolling); offduty.AddTransition(Transition.NoHealth, FSMStateID.Dead); offduty.AddTransition(Transition.WantsToHide, FSMStateID.Hiding); RepairState repair = new RepairState(this); repair.AddTransition(Transition.Healed, FSMStateID.Patrolling); repair.AddTransition(Transition.NoHealth, FSMStateID.Dead); HideState hide = new HideState(this); hide.AddTransition(Transition.ReachPlayer, FSMStateID.Attacking); hide.AddTransition(Transition.RestedLongEnough, FSMStateID.Patrolling); DeadState dead = new DeadState(); dead.AddTransition(Transition.NoHealth, FSMStateID.Dead); AddFSMState(patrol); AddFSMState(chase); AddFSMState(attack); AddFSMState(offduty); AddFSMState(repair); AddFSMState(hide); AddFSMState(dead); }
private void ConstructFSM() { pointList = GameObject.FindGameObjectsWithTag("WayPoint"); // //Creating a wapoint transform array for each state // Transform[] waypoints = new Transform[pointList.Length]; int i = 0; foreach (GameObject obj in pointList) { waypoints[i] = obj.transform; i++; } // //Create States // HideState hide = new HideState(waypoints); AttackState attack = new AttackState(waypoints); //Create the Dead state DeadState dead = new DeadState(waypoints); //there are no transitions out of the dead state PatrolState patrol = new PatrolState(waypoints); FleeState flee = new FleeState(waypoints); //add transitions OUT of the attack state attack.AddTransition(Transition.NoHealth, FSMStateID.Dead); if (AI1) { attack.AddTransition(Transition.LostPlayer, FSMStateID.Patrolling); attack.AddTransition(Transition.LowHealth, FSMStateID.Fleeing); } if (AI2) { attack.AddTransition(Transition.LostPlayer, FSMStateID.Hiding); } attack.AddTransition(Transition.LowHealth, FSMStateID.Fleeing); //add transitions OUT of the hide state hide.AddTransition(Transition.SawPlayer, FSMStateID.Attacking); hide.AddTransition(Transition.NoHealth, FSMStateID.Dead); //add transitions OUT of the patrol state patrol.AddTransition(Transition.SawPlayer, FSMStateID.Attacking); patrol.AddTransition(Transition.NoHealth, FSMStateID.Dead); //add transitions OUT of the flee state if (AI1) { flee.AddTransition(Transition.LostPlayer, FSMStateID.Patrolling); } if (AI2) { flee.AddTransition(Transition.LostPlayer, FSMStateID.Hiding); } flee.AddTransition(Transition.NoHealth, FSMStateID.Dead); //Add state to the state list if (AI1) { AddFSMState(patrol); } if (AI2) { AddFSMState(hide); } AddFSMState(attack); AddFSMState(dead); }
public override void Init() { base.Init(); wanderState = new WanderState(this); hideState = new HideState(this); retreatState = new Retreat(this, enemyObj.health.maxHealth); refillAmmoState = new RefifllAmmo(this); stunnedState = new StunnedState(this); hideState.AddTransition(() => { if (hideState.doneHiding) { Diagnostic.Instance.AddLog(enemyObj.gameObject, "Ok, done hiding"); return(true); } return(false); }, wanderState); //Will look for healthpack if health is not at max wanderState.AddTransition(() => { if (enemyObj.health.GetHealth() < enemyObj.health.maxHealth) { var packs = GameManager.Instance.GetAvailableHealthPacks(); if (packs.Count > 0) { if (stateMachine.GetCurrentState() != retreatState) { retreatState.allPacks = packs.ToArray(); Diagnostic.Instance.AddLog(enemyObj.gameObject, "Low health, got nothing to do, going to get some"); return(true); } } } return(false); }, retreatState); //Will look for healthpack if health is not at max wanderState.AddTransition(() => { if (enemyObj.rifle.Ammo < enemyObj.rifle.MaxAmmo) { var packs = GameManager.Instance.GetAvailableAmmoPacks(); if (packs.Count > 0) { if (stateMachine.GetCurrentState() != refillAmmoState) { refillAmmoState.allPacks = packs.ToArray(); Diagnostic.Instance.AddLog(enemyObj.gameObject, "Low ammo, got nothing to do, going to get some"); return(true); } } } return(false); }, refillAmmoState); retreatState.AddTransition(() => { var packs = GameManager.Instance.GetAvailableHealthPacks(); //If the health was restored, or no packs available go back wandering if (enemyObj.health.GetHealth() >= enemyObj.health.maxHealth || enemyObj.health.GetHealth() < enemyObj.health.maxHealth && packs.Count == 0) { Diagnostic.Instance.AddLog(enemyObj.gameObject, "Feeling better, going to stroll around..."); return(true); } return(false); }, wanderState); refillAmmoState.AddTransition(() => { var packs = GameManager.Instance.GetAvailableAmmoPacks(); //If the health was restored, or no packs available go back wandering if (enemyObj.rifle.Ammo >= enemyObj.rifle.MaxAmmo || enemyObj.rifle.Ammo < enemyObj.rifle.MaxAmmo && packs.Count == 0) { Diagnostic.Instance.AddLog(enemyObj.gameObject, "Feeling better, going to stroll around..."); return(true); } return(false); }, wanderState); stunnedState.AddTransition(() => { if (!stunnedState.isStunned) { return(true); } return(false); }, hideState); stateMachine.SetState(wanderState); }
public override void Init() { base.Init(); wanderState = new WanderState(this); shootState = new ShootState(this, enemyObj.shootRate); investigationState = new InvestigateState(this); retreatState = new Retreat(this, minHealthForRetreat); hideState = new HideState(this); bombAvoidState = new BombAvoid(this); refillAmmoState = new RefifllAmmo(this); stunnedState = new StunnedState(this); shootState.AddTransition(() => { if (!enemyObj.enemySight.IsPlayerInSight()) { investigationState.waitBeforeGoingToPoint = 0; investigationState.investigationPoint = Player.Instance.transform.position; Diagnostic.Instance.AddLog(enemyObj.gameObject, "Lost sight of player, going to check last known position"); return(true); } return(false); }, investigationState); shootState.AddTransition(() => { if (enemyObj.rifle.Ammo <= 0) { var allPacks = GameManager.Instance.GetAvailableAmmoPacks(); if (allPacks.Count == 0) { Diagnostic.Instance.AddLog(enemyObj.gameObject, "Out of ammo, no packs around, going to hide!"); return(true); } } return(false); }, hideState); shootState.AddTransition(() => { if (enemyObj.rifle.Ammo <= 0) { var allPacks = GameManager.Instance.GetAvailableAmmoPacks(); if (allPacks.Count > 0) { refillAmmoState.allPacks = allPacks.ToArray(); refillAmmoState.playerLastKnowsPosition = Player.Instance.transform.position; Diagnostic.Instance.AddLog(enemyObj.gameObject, "Out of ammo, going to refill!"); return(true); } } return(false); }, refillAmmoState); investigationState.AddTransition(() => { return(investigationState.done); }, wanderState); refillAmmoState.AddTransition(() => { if (enemyObj.rifle.Ammo <= 0) { var allPacks = GameManager.Instance.GetAvailableAmmoPacks(); if (allPacks.Count == 0) { Diagnostic.Instance.AddLog(enemyObj.gameObject, "No ammo boxe available, will hide!"); return(true); } } return(false); }, hideState); refillAmmoState.AddTransition(() => { if (enemyObj.rifle.Ammo > 0) { if (refillAmmoState.playerLastKnowsPosition != null) { Diagnostic.Instance.AddLog(enemyObj.gameObject, "Go ammo, going to check last player known position"); investigationState.investigationPoint = refillAmmoState.playerLastKnowsPosition.Value; return(true); } } return(false); }, investigationState); refillAmmoState.AddTransition(() => { if (enemyObj.rifle.Ammo > 0) { if (refillAmmoState.playerLastKnowsPosition == null) { Diagnostic.Instance.AddLog(enemyObj.gameObject, "Go ammo, don't know where player is, will wander around"); return(true); } } return(false); }, wanderState); //If health is ok and retreat has a player known pos, go investigate retreatState.AddTransition(() => { if (enemyObj.health.GetHealth() >= minHealthForRetreat) { if (retreatState.playerLastKnowsPosition != null) { investigationState.investigationPoint = retreatState.playerLastKnowsPosition.Value; } Diagnostic.Instance.AddLog(enemyObj.gameObject, "Feeling better, go to check last investigation point"); return(true); } return(false); }, investigationState); //If health is ok and retreat has no player known pos, go patrol retreatState.AddTransition(() => { if (enemyObj.health.GetHealth() >= minHealthForRetreat) { if (retreatState.playerLastKnowsPosition == null) { Diagnostic.Instance.AddLog(enemyObj.gameObject, "Feeling better, go to back to wander"); return(true); } } return(false); }, wanderState); retreatState.AddTransition(() => { if (enemyObj.health.GetHealth() < minHealthForRetreat) { var packs = GameManager.Instance.GetAvailableHealthPacks(); if (packs.Count == 0 && !hideState.isHiding) { Diagnostic.Instance.AddLog(enemyObj.gameObject, "Go a healthpack, but still feel crap, will go hide"); } return(packs.Count == 0 && !hideState.isHiding); } return(false); }, hideState); //Will come out of hiding only when a health pack is available again hideState.AddTransition(() => { var packs = GameManager.Instance.GetAvailableHealthPacks(); if (packs.Count > 0 && enemyObj.health.GetHealth() < retreatState.minHealthForRetreat) { retreatState.allPacks = packs.ToArray(); Diagnostic.Instance.AddLog(enemyObj.gameObject, "Healthpack is available, going to get it!"); return(true); } return(false); }, retreatState); //Will come out of hiding only when a ammo pack is available again hideState.AddTransition(() => { var packs = GameManager.Instance.GetAvailableAmmoPacks(); if (packs.Count > 0 && enemyObj.health.GetHealth() >= retreatState.minHealthForRetreat) { refillAmmoState.allPacks = packs.ToArray(); Diagnostic.Instance.AddLog(enemyObj.gameObject, "Ammo box is available, going to get it!"); return(true); } return(false); }, refillAmmoState); bombAvoidState.AddTransitionDynamicState(() => { return(bombAvoidState.readyToGo); }, () => { return(stateMachine.previousState); }); stunnedState.AddTransition(() => { if (!stunnedState.isStunned) { investigationState.investigationPoint = enemyObj.transform.position; return(true); } return(false); }, investigationState); //Soldier will respond to alarm Enemy.OnAlarmSent += GoInvestigateSOS; stateMachine.SetState(wanderState); }