public override void Init() { base.Init(); wanderState = new WanderState(this); shootState = new ShootState(this, enemyObj.shootRate); investigationState = new InvestigateState(this); stunnedState = new StunnedState(this); meleeAttackState = new MeleeAttackState(this); refillAmmoState = new RefifllAmmo(this); retreatState = new Retreat(this, minHealthForRetreat); qMark = Resources.Load <Sprite>("StateIcons\\what"); wanderState.AddTransition(() => { if (enemyObj.enemySight.IsPlayerInSight() && enemyObj.rifle.Ammo > 0) { Diagnostic.Instance.AddLog(enemyObj.gameObject, "See the player, shooting!"); return(true); } return(false); }, shootState); wanderState.AddTransition(() => { if (enemyObj.rifle.Ammo <= 0) { var packs = GameManager.Instance.GetAvailableAmmoPacks(); if (packs.Count > 0) { if (stateMachine.GetCurrentState() != refillAmmoState) { refillAmmoState.allPacks = packs.ToArray(); Diagnostic.Instance.AddLog(enemyObj.gameObject, "No ammo, got nothing to do, going to get some"); return(true); } } } return(false); }, refillAmmoState); wanderState.AddTransition(() => { if (enemyObj.health.GetHealth() < minHealthForRetreat) { 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); wanderState.AddTransition(() => { if (enemyObj.rifle.Ammo <= 0) { var packs = GameManager.Instance.GetAvailableAmmoPacks(); if (packs.Count > 0) { if (stateMachine.GetCurrentState() != refillAmmoState) { Diagnostic.Instance.AddLog(enemyObj.gameObject, "No ammo, got nothing to do, going to get some"); return(true); } } } return(false); }, refillAmmoState); wanderState.AddTransition(() => { if (enemyObj.enemySight.IsPlayerInSight() && enemyObj.rifle.Ammo <= 0) { Diagnostic.Instance.AddLog(enemyObj.gameObject, "See the player, no ammo, will attack with hands!"); return(true); } return(false); }, meleeAttackState); 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); refillAmmoState.AddTransition(() => { if (enemyObj.enemySight.IsPlayerInSight()) { Diagnostic.Instance.AddLog(enemyObj.gameObject, "Saw player, no time for refill ammo, attack!"); return(true); } return(false); }, meleeAttackState); retreatState.AddTransition(() => { if (enemyObj.enemySight.IsPlayerInSight()) { Diagnostic.Instance.AddLog(enemyObj.gameObject, "Saw player, no time for get healthpack, attack!"); return(true); } return(false); }, shootState); retreatState.AddTransition(() => { //If the health was restored OR health is low but no packs available, go back wandering if (enemyObj.health.GetHealth() > minHealthForRetreat || (enemyObj.health.GetHealth() <= minHealthForRetreat && GameManager.Instance.GetAvailableHealthPacks().Count == 0)) { Diagnostic.Instance.AddLog(enemyObj.gameObject, "Feeling better, going to stroll around..."); return(true); } return(false); }, wanderState); shootState.AddTransition(() => { if (!enemyObj.enemySight.IsPlayerInSight()) { investigationState.waitBeforeGoingToPoint = 0; investigationState.investigationPoint = Player.Instance.transform.position; shootTimer = 15.0f; Diagnostic.Instance.AddLog(enemyObj.gameObject, "Lost sight of player, going to check last known psition (and keep shooting, why not)"); return(true); } return(false); }, investigationState); shootState.AddTransition(() => { return(enemyObj.rifle.Ammo <= 0); }, meleeAttackState); meleeAttackState.AddTransition(() => { if (meleeAttackState.playerLastKnownPosition != null) { investigationState.investigationPoint = meleeAttackState.playerLastKnownPosition.Value; return(true); } return(false); }, investigationState); investigationState.AddTransition(() => { if (enemyObj.enemySight.IsPlayerInSight()) { Diagnostic.Instance.AddLog(enemyObj.gameObject, "See the player, shooting!"); return(true); } return(false); }, shootState); investigationState.AddTransition(() => { return(investigationState.done); }, wanderState); stunnedState.AddTransition(() => { if (!stunnedState.isStunned) { investigationState.investigationPoint = enemyObj.transform.position; return(true); } return(false); }, investigationState); //Beserk will respond to alarm Enemy.OnAlarmSent += GoInsestigateSOS; 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); }