//===================================================== private IEnumerator Escaping(Action onComplete) { // If player is out of range return to normal business if (_player == null) { // *** Escaped player -> GATHER gem *** if (onComplete != null) { onComplete(); } } if (_player != null) { // Set new escape-destination away from player var escapePoint = (_thisTransform.position - _player.position).normalized * _triggerTargetRadius; escapePoint.y = 0.0f; _thisAgent.SetDestination(_thisTransform.position + escapePoint); // Set run speed _thisAgent.speed = _runSpeed; _animation.Run(); // Run away from player while ((_thisTransform.position - _thisAgent.destination).sqrMagnitude > 2.0f) { yield return(null); } } // Stop movement _thisAgent.speed = 0.0f; _animation.Stop(); yield return(new WaitForSeconds(0.25f)); // *** Thinks it's escaped player -> POST_ESCAPE *** if (onComplete != null) { onComplete(); } }
protected virtual void StateMachine() { #region STATE CONFIGS /////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////// var idle = new State <Inputs>(CommonState.IDLE); var onSigth = new State <Inputs>(CommonState.ONSIGHT); var pursuit = new State <Inputs>(CommonState.PURSUIRT); var searching = new State <Inputs>(CommonState.SEARCHING); var attack = new State <Inputs>(CommonState.ATTACKING); var die = new State <Inputs>(CommonState.DIE); StateConfigurer.Create(idle) .SetTransition(Inputs.ON_LINE_OF_SIGHT, onSigth) .SetTransition(Inputs.DIE, die) .Done(); StateConfigurer.Create(onSigth) .SetTransition(Inputs.PROBOCATED, pursuit) .SetTransition(Inputs.OUT_LINE_OF_SIGHT, searching) .SetTransition(Inputs.DIE, die) .Done(); StateConfigurer.Create(pursuit) .SetTransition(Inputs.OUT_LINE_OF_SIGHT, searching) .SetTransition(Inputs.IN_RANGE_TO_ATTACK, attack) .SetTransition(Inputs.DIE, die) .Done(); StateConfigurer.Create(searching) .SetTransition(Inputs.TIME_OUT, idle) .SetTransition(Inputs.ON_LINE_OF_SIGHT, pursuit) .SetTransition(Inputs.DIE, die) .Done(); StateConfigurer.Create(attack) .SetTransition(Inputs.OUT_RANGE_TO_ATTACK, pursuit) .SetTransition(Inputs.OUT_LINE_OF_SIGHT, searching) .SetTransition(Inputs.DIE, die) .Done(); StateConfigurer.Create(die).Done(); /////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////// #endregion #region STATES /////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////// //****************** //*** IDLE //****************** idle.OnEnter += x => { myRb.velocity = Vector2.zero; anim.Idle(); }; idle.OnUpdate += () => { Deb_Estado = "IDLE"; if (LineOfSight()) { Debug.Log("Line of sigth"); SendInputToFSM(Inputs.ON_LINE_OF_SIGHT); } CheckBullet(); }; //****************** //*** ON SIGHT //****************** onSigth.OnEnter += x => { feedbackstate.SetStateGraphic(preset.sprite_OnSight); }; onSigth.OnUpdate += () => { Deb_Estado = "ON SIGTH"; if (LineOfSight()) { LookSmooth(); OnSight_CountDownForProbocate(); } else { timer_to_probocate = 0; SendInputToFSM(Inputs.OUT_LINE_OF_SIGHT); } }; onSigth.OnExit += x => { feedbackstate.SetStateGraphic(); }; //****************** //*** PURSUIT //****************** pursuit.OnEnter += x => { anim.Run(); }; pursuit.OnUpdate += () => { Deb_Estado = "PURSUIT"; if (LineOfSight()) { FollowPlayer(); if (IsInDistanceToAttack()) { SendInputToFSM(Inputs.IN_RANGE_TO_ATTACK); } } else { timer_to_probocate = 0; SendInputToFSM(Inputs.OUT_LINE_OF_SIGHT); } }; pursuit.OnExit += x => { myRb.velocity = Vector2.zero; }; //****************** //*** SEARCH //****************** searching.OnEnter += x => { anim.Walk(); feedbackstate.SetStateGraphic(preset.sprite_Search); }; searching.OnUpdate += () => { CheckBullet(); Deb_Estado = "SEARCH"; if (LineOfSight()) { SendInputToFSM(Inputs.ON_LINE_OF_SIGHT); } else { transform.Rotate(0, 0, 2); OutSight_CountDownForIgnore(); } }; searching.OnExit += x => { feedbackstate.SetStateGraphic(); }; //****************** //*** ATTACK //****************** attack.OnUpdate += () => { Deb_Estado = "ATTACK"; if (LineOfSight()) { LookSmooth(); if (IsInDistanceToAttack()) { anim.Attack(); Attack(); } else { SendInputToFSM(Inputs.OUT_RANGE_TO_ATTACK); } } else { SendInputToFSM(Inputs.OUT_LINE_OF_SIGHT); } }; attack.OnExit += x => { timer = 0; }; //****************** //*** DEATH //****************** die.OnEnter += x => { Deb_Estado = "DEATH"; canMove = false; gameObject.GetComponent <Collider2D>().enabled = false; anim.Die(); lifebar.Off(); del_to_remove(this); }; /////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////// #endregion _myFsm = new EventFSM <Inputs>(idle); }
void Patrol() { navAgent.isStopped = false; navAgent.speed = walkSpeed; patrolTimer += Time.deltaTime; if (patrolTimer > patrolForThisTime) { SetNewRandomDestination(); //Reset Patrol timer patrolTimer = 0f; } if (navAgent.velocity.sqrMagnitude > 0) { enemyAnim.Walk(true); enemyAnim.Run(false); enemyAnim.Idle(false); } else { enemyAnim.Walk(false); enemyAnim.Run(false); } if (Vector3.Distance(transform.position, target.position) <= chaseDistance) { enemyAnim.Walk(false); enemyAnim.Idle(false); enemyAnim.Run(true); enemyState = EnemyState.CHASE; //PLAY SPOTTED AUDIO } if (Vector3.Distance(transform.position, target.position) <= attackDistance) { enemyAnim.Walk(false); enemyAnim.Idle(false); enemyAnim.Run(false); enemyState = EnemyState.ATTACK; if (chaseDistance != currentChaseDistance) { chaseDistance = currentChaseDistance; } else if (Vector3.Distance(transform.position, target.position) > chaseDistance) { enemyAnim.Run(false); enemyState = EnemyState.PATROL; patrolTimer = patrolForThisTime; if (chaseDistance != currentChaseDistance) { chaseDistance = currentChaseDistance; } } } }