protected override void BuildFSM() { //Other States Here PatrolState patrol = new PatrolState(this); patrol.AddTransitionState(FSMStateID.Investigate, FSMTransitions.HeardPlayer); patrol.AddTransitionState(FSMStateID.Shoot, FSMTransitions.SawPlayer); AddFSMState(patrol); InvestigateState investigate = new InvestigateState(this); investigate.AddTransitionState(FSMStateID.Patrol, FSMTransitions.FoundNothing); investigate.AddTransitionState(FSMStateID.Shoot, FSMTransitions.SawPlayer); AddFSMState(investigate); ShootState shoot = new ShootState(); shoot.AddTransitionState(FSMStateID.Investigate, FSMTransitions.PlayerOutOfRange); AddFSMState(shoot); DeadState dead = new DeadState(); AddFSMState(dead); }
void Start() { fromAttr = GetComponent <BasicObjectAttr>(); this.originalPos.transform.parent = null; FSMManager manager = GetComponent <FSMManager>(); NavMeshAgent agent = GetComponent <NavMeshAgent>(); RandomState search = new RandomState(); FollowState follow = new FollowState(); IdleState idle = new IdleState(); idle.isInfinite = true; InvestigateState investigateState = new InvestigateState(); manager.GetBasicState().configure("Player", (o) => { return(null); }, (o) => { investigateState.setTargetPos(o.transform.position); investigateState.SetDoWhenArrive((s) => { Collider[] coll = Physics.OverlapSphere(gameObject.transform.position, fromAttr.dangerViewAreaRadius); Debug.Log("Clll" + coll.Length); foreach (Collider c in coll) { if (c.tag.Equals("Player")) { var go = Camera.main.GetComponent <GameOverCameraControl>(); go.showDeadEffect = true; return(null); } } idle.time = fromAttr.investigateWaitTime; idle.isInfinite = false; idle.onComplete((s1) => { investigateState.setTargetPos(this.originalPos.transform.position); investigateState.SetDoWhenArrive((s2) => { idle.isInfinite = true; transform.rotation = this.originalPos.transform.rotation; agent.destination = this.transform.position; return(idle); }); return(investigateState); }); return(idle); }); return(investigateState); }); manager.setCurrentState(idle); }
private void Awake() { instance = this; //ADD NEW STATE PATTERNS HERE patrolState = this.GetComponent <PatrolState>(); attackState = this.GetComponent <AttackState>(); blockShurikenState = this.GetComponent <BlockShurikenState>(); chaseState = this.GetComponent <ChaseState>(); blockSwordState = this.GetComponent <BlockSwordState>(); investigateState = this.GetComponent <InvestigateState>(); engardeState = this.GetComponent <EngardeState>(); enemyAudio = GetComponent <AudioSource>(); }
// Use this for initialization protected override void Awake() { base.Awake(); //Setup states PatrollingState patrollingState = gameObject.AddComponent <PatrollingState>(); patrollingState.SetNodes(m_patrollingNodes); patrollingState.SetMovementVelocity(m_forwardVelocity); MoveTowardsState moveTowardsState = gameObject.AddComponent <MoveTowardsState>(); moveTowardsState.SetMovementVelocity(m_forwardVelocity); InvestigateState investigateState = gameObject.AddComponent <InvestigateState>(); investigateState.SetMovementVelocity(m_forwardVelocity); IdleState idleState = gameObject.AddComponent <IdleState>(); idleState.SetRandomTime(m_idleMinTime, m_idleMaxTime); AttackingState attackingState = gameObject.AddComponent <AttackingState>(); FleeingState fleeingState = gameObject.AddComponent <FleeingState>(); fleeingState.SetMovementVelocity(m_forwardVelocity); fleeingState.SetFleeingMaxTime(m_fleeingMaxTime); //Assigning transitons patrollingState.AddInterruptTransition(TargetSightTransition.CreateComponent(gameObject, m_detectionRange), moveTowardsState); moveTowardsState.AddInterruptTransition(NotTransition.CreateComponent(gameObject, TargetSightTransition.CreateComponent(gameObject, m_detectionRange)), investigateState); moveTowardsState.AddInterruptTransition(TargetWithinRangeTransition.CreateComponent(gameObject, m_attackingRange), attackingState); investigateState.AddEndTransition(BaseTransition.CreateComponent(gameObject), idleState); investigateState.AddInterruptTransition(TargetSightTransition.CreateComponent(gameObject, m_detectionRange), moveTowardsState); idleState.AddEndTransition(BaseTransition.CreateComponent(gameObject), patrollingState); idleState.AddInterruptTransition(TargetSightTransition.CreateComponent(gameObject, m_detectionRange), moveTowardsState); attackingState.AddEndTransition(BaseTransition.CreateComponent(gameObject), fleeingState); fleeingState.AddEndTransition(BaseTransition.CreateComponent(gameObject), moveTowardsState); fleeingState.AddInterruptTransition(NotTransition.CreateComponent(gameObject, TargetWithinRangeTransition.CreateComponent(gameObject, m_fleeingDistance)), moveTowardsState); //Setup intial state GetComponent <StateManager>().SetInitialState(patrollingState); }
void Start() { fromAttr = GetComponent <BasicObjectAttr>(); FSMManager manager = GetComponent <FSMManager>(); RandomState search = new RandomState(); FollowState follow = new FollowState(); IdleState idle = new IdleState(); PatrolState patrol = new PatrolState(); InvestigateState investigateState = new InvestigateState(); patrol.patrolRoute = patrolRoute; patrol.setAgent(GetComponent <NavMeshAgent>()); manager.GetBasicState().configure("Player", (o) => { investigateState.setTargetPos(o.transform.position).SetDoWhenArrive(null); return(investigateState); }, (o) => { investigateState.setTargetPos(o.transform.position); investigateState.SetDoWhenArrive((s) => { Collider[] coll = Physics.OverlapSphere(gameObject.transform.position, fromAttr.dangerViewAreaRadius); foreach (Collider c in coll) { if (c.tag.Equals("Player")) { var go = Camera.main.GetComponent <GameOverCameraControl>(); go.showDeadEffect = true; return(null); } } return(patrol); }); return(investigateState); }); manager.setCurrentState(patrol); }
private void MakeFSM() { PatrolState patrol = new PatrolState(patrolPath, this); patrol.AddTransition(Transition.Clue, StateID.Investigate); //Add transition to chase InvestigateState investigate = new InvestigateState(this); investigate.AddTransition(Transition.AllClear, StateID.ResumePatrol); //add transition to investigate again? //add transition to chase ResumePatrolState resume = new ResumePatrolState(this); resume.AddTransition(Transition.Return, StateID.Patrol); resume.AddTransition(Transition.Clue, StateID.Investigate); fsm = new FSMSystem(); fsm.AddState(patrol); fsm.AddState(investigate); fsm.AddState(resume); }
private void ConstructFSM() { //Get the list of points Transform[] waypoints = new Transform[pointList.Length]; int i = 0; foreach (GameObject obj in pointList) { waypoints[i] = obj.transform; i++; } //Add transitions PatrolState patrol = new PatrolState(waypoints); patrol.AddTransition(Transition.NoHealth, FSMStateID.Dead); patrol.AddTransition(Transition.SmelledSardine, FSMStateID.Investigating); InvestigateState investigate = new InvestigateState(waypoints); investigate.AddTransition(Transition.LostPlayer, FSMStateID.Patrolling); investigate.AddTransition(Transition.NoHealth, FSMStateID.Dead); DeadState dead = new DeadState(); dead.AddTransition(Transition.NoHealth, FSMStateID.Dead); //Add states to the fsmstates list AddFSMState(patrol); AddFSMState(dead); AddFSMState(investigate); }
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 Enter() { Debug.Log("Entering alert state"); investigationState = this.GetComponent <InvestigateState> (); stateMachine = this.GetComponent <StateMachine> (); }
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); }