private void OnStateChanged(MobileBehaviourStates newState) { if (newState == MobileBehaviourStates.Walking) { currentDirection = UnityEngine.Random.value < 0.5 ? -1 : 1; } }
void Start() { player = GameObject.FindGameObjectWithTag("Player").GetComponent <Player>(); healthComponent = GetComponent <Health>(); healthComponent.OnAfterValueChangedEvent += new Health.DamageHandler(OnHealthChanged); healthComponent.OnDeathEvent += new Health.DeathZeroHandler(OnDeath); CurrentState = MobileBehaviourStates.Idling; StartCoroutine(TryChangeState()); }
private void OnHealthChanged(float beforeValue, float afterValue) { if ((CurrentHealth < EscapeHealth) || (IsPeaceful && (beforeValue > afterValue))) { var playerDistance = Math.Abs(player.transform.position.x - transform.position.x); var playerInChaceRange = playerDistance <= ChaseRange; var playerInAttackRange = playerDistance <= AttackRange; if (playerInChaceRange) { CurrentState = MobileBehaviourStates.Escapeing; } } }
public virtual void MakeDecision() { var tempCurrState = CurrentState; var playerInChaceRange = false; var playerInAttackRange = false; // if (!IsPeaceful) // { var playerDistance = Math.Abs(player.transform.position.x - transform.position.x); playerInChaceRange = playerDistance <= ChaseRange; playerInAttackRange = playerDistance <= AttackRange; // } switch (CurrentState) { case MobileBehaviourStates.Idling: if (!playerInChaceRange || IsPeaceful) { CurrentState = UnityEngine.Random.value < StateChangingChance ? MobileBehaviourStates.Walking : MobileBehaviourStates.Idling; } else if (CurrentHealth > EscapeHealth && !IsPeaceful) { CurrentState = MobileBehaviourStates.Chaseing; } break; case MobileBehaviourStates.Walking: if (!playerInChaceRange || IsPeaceful) { CurrentState = UnityEngine.Random.value < StateChangingChance ? MobileBehaviourStates.Walking : MobileBehaviourStates.Idling; } else if (CurrentHealth > EscapeHealth && !IsPeaceful) { CurrentState = MobileBehaviourStates.Chaseing; } break; case MobileBehaviourStates.Chaseing: if (!playerInChaceRange) { CurrentState = MobileBehaviourStates.Idling; } else if (playerInAttackRange && (CurrentHealth > EscapeHealth) && !IsPeaceful) { CurrentState = MobileBehaviourStates.Atacking; } break; case MobileBehaviourStates.Atacking: if (!playerInAttackRange && (CurrentHealth > EscapeHealth) && !IsPeaceful) { CurrentState = MobileBehaviourStates.Chaseing; } break; case MobileBehaviourStates.Escapeing: if (!playerInChaceRange) { CurrentState = MobileBehaviourStates.Walking; } break; default: // Change nothing //CurrentState = CurrentState; break; } if (tempCurrState != CurrentState) { if (OnStateChanged != null) { OnStateChanged(CurrentState); } } }
void Update() { currentBehaviour = mobileStateDecisionMaker.CurrentState; SetMovementSpeed(); }