68 lines
1.8 KiB
C#
68 lines
1.8 KiB
C#
using UnityEngine;
|
|
using System.Collections;
|
|
|
|
public class AiChase : AiBehavior {
|
|
public float chaseTimeout = 5;
|
|
private GameObject _obj;
|
|
private HumanController _selfController;
|
|
private bool _chasing;
|
|
public RandomSoundPlayer endChasingSound;
|
|
|
|
protected override void OnBegin(AiBlackboard blackboard) {
|
|
base.OnBegin(blackboard);
|
|
if (_obj == null) {
|
|
_obj = blackboard.Obj;
|
|
_selfController = _obj.GetComponent<HumanController>();
|
|
}
|
|
}
|
|
|
|
private void OnEndChasing() {
|
|
Debug.Log("Begin chasing");
|
|
if (endChasingSound != null) {
|
|
endChasingSound.Play();
|
|
}
|
|
}
|
|
|
|
private void OnBeginChasing() {
|
|
Debug.Log("End chasing");
|
|
}
|
|
|
|
protected override BehaviorStatus OnExecute(AiBlackboard blackboard) {
|
|
float time;
|
|
if (!blackboard.GetValue(BlackboardVariables.LastSuspiciousTime, out time)) {
|
|
return BehaviorStatus.Fail;
|
|
}
|
|
|
|
//check if chasing too long
|
|
if (Time.time - time >= chaseTimeout) {
|
|
if (_chasing) {
|
|
_chasing = false;
|
|
OnEndChasing();
|
|
}
|
|
return BehaviorStatus.Fail;
|
|
}
|
|
|
|
|
|
|
|
//do chase!
|
|
if (!_chasing) {
|
|
_chasing = true;
|
|
OnBeginChasing();
|
|
}
|
|
|
|
|
|
Vector3 chaseTarget;
|
|
if (blackboard.GetValue(BlackboardVariables.LastEnemyPosition, out chaseTarget)) {
|
|
if (_selfController != null) {
|
|
float dir = Mathf.Sign(chaseTarget.x - _obj.transform.position.x);
|
|
|
|
if (Mathf.Abs(chaseTarget.x - _obj.transform.position.x) > 0.3f) {
|
|
_selfController.UpdateMovement(dir * _selfController.maxWalkSpeed);
|
|
}
|
|
|
|
}
|
|
}
|
|
return BehaviorStatus.Success;
|
|
}
|
|
}
|