Flynny85

NPC class

Apr 25th, 2017
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 27.31 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. #if UNITY_EDITOR
  5. using UnityEditor;
  6. [CustomEditor(typeof(NPC_Class))]
  7. public class NPC_ClassOverride : Editor
  8. {
  9.     public override void OnInspectorGUI()
  10.     {
  11.         NPC_Class _npcclass = (NPC_Class)target;
  12.         if(GUILayout.Button("OnSpawn"))
  13.         {
  14.             _npcclass.OnSpawn();
  15.         }
  16.         if(GUILayout.Button("FLASH"))
  17.         {
  18.             _npcclass.DEBUG_Flash ();
  19.         }
  20.         if(GUILayout.Button("newtarget left"))
  21.         {
  22.             _npcclass.DEBUG_NPC_NewLane_Left ();
  23.         }
  24.         if(GUILayout.Button("newtarget right"))
  25.         {
  26.             _npcclass.DEBUG_NPC_NewLane_Right ();
  27.         }
  28.         if(GUILayout.Button("newtarget reverse"))
  29.         {
  30.             _npcclass.DEBUG_NPC_NewLane_Reverse ();
  31.         }
  32.  
  33.         DrawDefaultInspector();
  34.     }
  35. }
  36. #endif
  37.  
  38. public class NPC_Class : MonoBehaviour {
  39.  
  40.     private Transform _thisTransform;
  41.     private Simple_CC _thisCC;
  42.     private Pool_Class _thisPoolClass;
  43.     public Class_NPC_Behaviour _thisNPCBehaviour = new Class_NPC_Behaviour(); // how we setup what it does ..
  44.     private float _walkSpeed = 0.6f;
  45.     public float _rayLength = 5f; // distance npc is comfortable being behind something..
  46.     private float _internalRayLength;
  47.     private float _time_Max = 0f;
  48.     private int _Count;
  49.  
  50.     private int _hash_walkID;
  51.     private int _hash_stopID;
  52.     public Animator _npc_Animator;
  53.  
  54.     public enum NPCStates
  55.     {
  56.         Stop,
  57.         Stop_Turn,
  58.         Idle,
  59.         Turn,
  60.         Move,
  61.         Blocked,
  62. //      CrossingRoad,
  63.     }
  64.     public NPCStates _current_npcState;
  65.     public NPCStates _next_npcState;
  66.  
  67.     void OnEnable(){
  68.         _thisTransform = this.transform;
  69.         _thisCC = GetComponent<Simple_CC> ();
  70.         _thisPoolClass = GetComponent<Pool_Class> ();
  71.         _oldDirection = _thisTransform.rotation;
  72.         _newDirection = _thisTransform.rotation;
  73.  
  74.         _hash_walkID = Animator.StringToHash ("Walk");
  75.         _hash_stopID = Animator.StringToHash ("Stop");
  76.         _npc_Animator.SetTrigger (_hash_stopID);
  77.     }
  78.  
  79.  
  80.     private Vector3 _collisionRotation;
  81.     private float _collisionTime = 0f;
  82.     private Vector3 _collisionVect;
  83.     private int _tempRandom;
  84.     private float _timeCollided = 0f;
  85.     private float _speedAdjust = 1f;
  86.     public int _checkTime = 20;
  87.     private Quaternion _newDirection;
  88.     private Quaternion _oldDirection;
  89.  
  90.     private NPC_Class _tempNpc;
  91.     private float _tempNPC_speedDiff;
  92.     private Transform _tempCar;
  93.  
  94.     public void Hit_Back( Vector3 _HitPoint , int _type , float _power){ // type:0 = player / type:1 = npc / type:2 = car / type:3 = objects
  95.         _power = Mathf.Clamp (_power, 0f, 1f);
  96.         _npc_Animator.SetTrigger (_hash_stopID);
  97.         _HitPoint.y = _thisTransform.localPosition.y;
  98.         _HitPoint.x = _HitPoint.x + Random.Range (-.2f,.2f);
  99.         _collisionVect = (_thisTransform.localPosition - _HitPoint).normalized;
  100.  
  101.         _thisTransform.LookAt (_HitPoint);
  102.         _collisionRotation = _thisTransform.localEulerAngles;
  103.         _collisionRotation.x = 0f; _collisionRotation.z = 0f;
  104.         _thisTransform.localEulerAngles = _collisionRotation;
  105.  
  106.         if (_type == 0) { // player
  107.             _thisCC.SimpleInertiaCC (_collisionVect + Global_Vars._Global_PlayerC.transform.forward * Mathf.Lerp(1f , 5f , Global_Vars._Player_Speed / 30f) , _power);
  108.             Global_Vars._Global_PlayerC.hit_NPC ( _thisTransform );
  109.             _flashProgress = 1f;
  110.         }
  111.         if (_type == 1) { // npc
  112.             _thisCC.SimpleInertiaCC ( _collisionVect * Mathf.Lerp( .5f , 1f , (_tempNPC_speedDiff /10f) ) , Mathf.Lerp( .3f , 1f , (_tempNPC_speedDiff /10f)) );
  113.             _flashProgress = 0.4f;
  114.         }
  115.         if (_type == 2) { // car
  116.             _thisCC.SimpleInertiaCC ( _collisionVect + _tempCar.forward + _tempCar.right , _power );
  117.             _flashProgress = 1f;
  118.         }
  119.         if (_type == 3) { // objects
  120.             _thisCC.SimpleInertiaCC ( _collisionVect * Mathf.Lerp( .5f , 1f , (_thisNPCBehaviour._internalSpeed /10f) ) , Mathf.Lerp( .3f , 1f , (_thisNPCBehaviour._internalSpeed /10f)) );
  121.         }
  122.  
  123.         NPCState_Change (NPCStates.Stop_Turn);
  124.         _collisionTime = Global_Vars._Global_Difficulty._NPC_RecoveryTime;
  125.     }
  126.  
  127.     public GameObject _flashObject;
  128.     private float _flashProgress = 0f;
  129.  
  130.     void OnTriggerEnter( Collider _hitWith ) {
  131.         if(_thisPoolClass._inUse){
  132.            
  133.             if (_hitWith.CompareTag ("Traffic")) {
  134.                 _tempCar = _hitWith.transform;
  135.                 Hit_Back ( _hitWith.transform.position , 2 , 1f);
  136.             }
  137.  
  138.             if (_hitWith.CompareTag ("Player")) {
  139.                 Hit_Back (Global_Vars._Player_CurrentPosition, 0 , Global_Vars._Player_Speed / 15f);
  140.                 Global_Vars._Global_PlayerC.hit_NPC ( _thisTransform );
  141.             }
  142.  
  143.             if(Time.time > _timeCollided + .15f){
  144.                 _timeCollided = Time.time;
  145.  
  146.                 if (_hitWith.CompareTag ("NPC")) {
  147.                     _tempNpc = _hitWith.gameObject.GetComponentInParent<NPC_Class> ();
  148.                     _tempNPC_speedDiff = _tempNpc._thisNPCBehaviour._internalSpeed - _thisNPCBehaviour._internalSpeed;
  149.                     if ( _tempNPC_speedDiff > 0f ) { // im slower so i bounce ..
  150.                         Hit_Back ( _hitWith.transform.localPosition , 1 , 1f);
  151.                     } // hitback on ourself because we are slower than what hit us!
  152.                 }
  153.    
  154.                 if (_hitWith.CompareTag ("Object") || _hitWith.CompareTag ("Object_Large")  ) {
  155.                     Hit_Back ( _hitWith.transform.position , 3 , _thisNPCBehaviour._internalSpeed / 10f);
  156.                 }
  157.             }
  158.         }
  159.     }
  160.  
  161.  
  162.     private void FlashCharacter(){
  163.         if (_flashProgress > 0f) {
  164.             if (Global_Vars._Global_NPCSystem._flashCurve.Evaluate (_flashProgress) > 0.1f) {
  165.                 _flashObject.SetActive (true);
  166.             } else {
  167.                 _flashObject.SetActive (false);
  168.             }
  169.             _flashProgress -= Global_Vars._TimeDelta;
  170.         } else {
  171.             _flashObject.SetActive (false);
  172.         }
  173.     }
  174.  
  175.  
  176.     public void OnSpawn(){
  177.         Global_Vars._Global_NPCSystem.Add_ToNPC_Spawned (this);
  178.         _current_npcState = NPCStates.Idle;
  179.         _thisNPCBehaviour._internalSpeed = 0f;
  180.         _thisNPCBehaviour._internalTimer = 0f;
  181.         _thisNPCBehaviour._internalTick = 1;
  182.         _thisNPCBehaviour._currentOrder = 0;
  183.         _timeWasStuck = 0f;
  184.         _timeDecision = 0f;
  185.         _timeCheck = 0f;
  186.         _flashProgress = 0f;
  187.         FlashCharacter ();
  188.         _isCrossing = false;
  189.         _oldPos = new Vector3();
  190.         _thisNPCBehaviour._currentLane = Global_Vars._Global_Lanes.Return_Lane (_thisTransform.position);
  191.         _thisNPCBehaviour._targetLane = _thisNPCBehaviour._currentLane;
  192.  
  193.         _time_Max = 0f;
  194.         _collisionTime = 0f;
  195.         _speedAdjust = 1f;
  196.         _speedAdjust = Random.Range (0.75f , 1.1f);
  197.         _internalRayLength = Random.Range (_rayLength * 0.35f , _rayLength * 1.8f);
  198.  
  199.         if (_thisNPCBehaviour._typeRunner) {
  200.             _internalRayLength = _rayLength * 1.5f;
  201.         }
  202.         NPCState_Change (NPCStates.Stop);
  203.     }
  204.  
  205.     public void OnDespawn(){
  206.         Global_Vars._Global_NPCSystem.Remove_FromNPC_Spawned (this);
  207.         _thisPoolClass.OnDespawn ();
  208.     }
  209.  
  210.     // TODO: turn this into a staggered list of actions to do with directions and targets also included!
  211.     // rather than the reactionary model we have now tha only goes 1 action at a time..
  212.     // eg blocked would include the point of blockage..
  213.     // we save that in a history of past actions and if it keeps coming up do a different action?
  214.  
  215.     public void NPCState_Change( NPCStates _state ){
  216.  
  217.         if (_next_npcState != NPCStates.Idle) {
  218.             _state = _next_npcState;
  219.             _next_npcState = NPCStates.Idle;
  220.         }
  221.  
  222.         _thisNPCBehaviour._internalTimer = 0f;
  223.         _time_Max = Random.Range (1f,3f);
  224.         _flashObject.SetActive (false);
  225.  
  226.         switch (_state){
  227.  
  228.         case NPCStates.Stop:
  229.             _current_npcState = NPCStates.Stop;
  230.             _npc_Animator.SetTrigger (_hash_stopID);
  231.             break;
  232.  
  233.         case NPCStates.Stop_Turn:
  234.             _current_npcState = NPCStates.Stop_Turn;
  235.             _npc_Animator.SetTrigger (_hash_stopID);
  236.             break;
  237.  
  238.         case NPCStates.Idle:
  239.             _current_npcState = NPCStates.Idle;
  240.             _npc_Animator.SetTrigger (_hash_stopID);
  241.             break;
  242.  
  243.         case NPCStates.Turn:
  244.             _oldDirection = _thisTransform.rotation;
  245.             _current_npcState = NPCStates.Turn;
  246.             _next_npcState = NPCStates.Move;
  247.             _npc_Animator.SetTrigger (_hash_stopID);
  248.             break;
  249.  
  250.         case NPCStates.Move:
  251.             _thisNPCBehaviour._currentLane = Global_Vars._Global_Lanes.Return_Lane(_thisTransform.position);
  252.             _current_npcState = NPCStates.Move;
  253.             _npc_Animator.SetTrigger (_hash_walkID);
  254.             break;
  255.  
  256.         case NPCStates.Blocked:
  257.             _current_npcState = NPCStates.Blocked;
  258.             _npc_Animator.SetTrigger (_hash_walkID);
  259.             break;
  260. //
  261. //      case NPCStates.CrossingRoad:
  262. //          _current_npcState = NPCStates.CrossingRoad;
  263. //          break;
  264.         }
  265.     }
  266.  
  267.  
  268.     public void NPCState_Update(){
  269.         if (_thisPoolClass._poolRB != null && _thisPoolClass._poolRB.IsSleeping()) {
  270.             _thisPoolClass._poolRB.WakeUp ();
  271.         }
  272.  
  273.         if ( _current_npcState.Equals ( NPCStates.Stop ) ) {
  274.             NPC_Stop ();
  275.         }
  276.         if ( _current_npcState.Equals ( NPCStates.Stop_Turn ) ) {
  277.             NPC_Stop ();
  278.         }
  279.         if ( _current_npcState.Equals ( NPCStates.Idle ) ) {
  280.             NPC_Idle ();
  281.         }
  282.         if ( _current_npcState.Equals ( NPCStates.Turn ) ) {
  283.             NPC_Turn ();
  284.         }
  285.         if ( _current_npcState.Equals ( NPCStates.Move ) ) {
  286.             NPC_Move ();
  287.         }
  288.         if ( _current_npcState.Equals ( NPCStates.Blocked ) ) {
  289.             NPC_Blocked ();
  290.         }
  291. //      if ( _current_npcState.Equals ( NPCStates.CrossingRoad ) ) {
  292. //          CrossingRoad ();
  293. //      }
  294.  
  295.         Check_PlayerDistance(); // distance check //
  296.  
  297.         _thisNPCBehaviour._internalTick += 1;
  298.     }
  299.  
  300.  
  301.     private void Check_PlayerDistance(){
  302.         if (_thisNPCBehaviour._internalTick % 100 == 0) {
  303.             if (_thisTransform.localPosition.z > Global_Vars._Player_CurrentPosition.z) {
  304.                 if (_thisTransform.localPosition.z > Global_Vars._Global_Area_TileEndPosition.z) {
  305.                     OnDespawn ();
  306.                 }
  307.             } else {
  308.                 if (_thisTransform.localPosition.z < Global_Vars._Player_CurrentPosition.z - 2.5f) {
  309.                     OnDespawn ();
  310.                 }
  311.             }
  312.         }
  313.     }
  314.  
  315.     private void NPC_Stop(){
  316.         FlashCharacter ();
  317.  
  318.         if (_collisionTime > 0f) {
  319.             _collisionTime -= Global_Vars._TimeDelta;
  320.             _thisCC.SimpleMoveForwardCC ( _collisionTime * -.2f);
  321.             // check if in middle of road here just incase!
  322.             if (MyMath.Get_X_DistanceBasic (_thisTransform.localPosition, Vector3.zero) < 3f) {
  323.                 // is in road fix!
  324.                 if (_thisTransform.localPosition.x > 0f) {
  325.                     NPC_NewLane( 3 , _thisTransform.localPosition.z);
  326.                 } else {
  327.                     NPC_NewLane( 2 , _thisTransform.localPosition.z);
  328.                 }
  329.             }
  330.  
  331.         } else {
  332.             _thisNPCBehaviour._internalSpeed = Mathf.Lerp(_thisNPCBehaviour._internalSpeed , 0f , Global_Vars._TimeDeltaX5);
  333.             _thisCC.SimpleMoveForwardCC (_thisNPCBehaviour._internalSpeed);
  334.             _thisNPCBehaviour._internalTimer += Global_Vars._TimeDelta;
  335.             if (_thisNPCBehaviour._internalTimer > 1f) {
  336.                 if (_current_npcState.Equals(NPCStates.Stop)) {
  337.                     NPCState_Change (NPCStates.Idle);
  338.                 }
  339.                 if (_current_npcState.Equals( NPCStates.Stop_Turn)) {
  340.                         NPCState_Change (NPCStates.Turn);
  341.                 }
  342.             }
  343.         }
  344.     }
  345.  
  346.  
  347.     private void NPC_Idle(){
  348.         _thisNPCBehaviour._internalTimer += Global_Vars._TimeDelta;
  349.         if (_thisNPCBehaviour._internalTimer > _time_Max) {
  350.             NPC_NewLane (_thisNPCBehaviour._currentLane, _thisTransform.localPosition.z + (_directionGoing.z * 5f));
  351.             NPCState_Change (NPCStates.Move);
  352.         }
  353.     }
  354.  
  355.     private float _directionDiff;
  356.     private Vector3 _directionGoing;
  357.     private Vector3 _directionReverse;
  358.  
  359.     private void Get_DirectionGoing(){
  360.         _directionDiff = _thisNPCBehaviour._targetPos.z - _thisNPCBehaviour._currentPos.z;
  361.         if ( _directionDiff > 0f) { // facing same way ..
  362.             _directionGoing = Global_Vars._Global_Lanes._forward_rotate.forward;
  363.             _directionReverse = Global_Vars._Global_Lanes._backward_rotate.forward;
  364.         } else {
  365.             _directionGoing = Global_Vars._Global_Lanes._backward_rotate.forward;
  366.             _directionReverse = Global_Vars._Global_Lanes._forward_rotate.forward;
  367.         }
  368.     }
  369.  
  370.     public void DEBUG_Flash(){
  371.         _flashProgress = 1f;
  372.         NPCState_Change (NPCStates.Stop);
  373.         _collisionTime = Global_Vars._Global_Difficulty._NPC_RecoveryTime;
  374.     }
  375.  
  376.     public void DEBUG_NPC_NewLane_Left(){ // based on information will get next world target pos to move to..
  377.         NPC_NewLane ( Mathf.Clamp( _thisNPCBehaviour._currentLane - 1 , 0 , 5 ), _thisTransform.localPosition.z + (_directionGoing.z * Global_Vars._Global_Lanes._sideStep));
  378.     }
  379.     public void DEBUG_NPC_NewLane_Right(){ // based on information will get next world target pos to move to..
  380.         NPC_NewLane ( Mathf.Clamp( _thisNPCBehaviour._currentLane + 1 , 0 , 5 ), _thisTransform.localPosition.z + (_directionGoing.z * Global_Vars._Global_Lanes._sideStep));
  381.     }
  382.     public void DEBUG_NPC_NewLane_Reverse(){ // based on information will get next world target pos to move to..
  383.         NPC_NewLane ( _thisNPCBehaviour._currentLane , _thisTransform.localPosition.z + (_directionReverse.z * Global_Vars._Global_Lanes._sideStep));
  384.     }
  385.  
  386.     private bool _allowNewLane = true;
  387.     private Vector3 _tempPos02;
  388.  
  389.     public void NPC_NewLane( int _Lane , float _ZPos){ // based on information will get next world target pos to move to..
  390.         _Lane = Mathf.Clamp(_Lane , 0 , 5);
  391.         _isCrossing = false;
  392.         _allowNewLane = true;
  393.         _tempPos02.x = Global_Vars._Global_Lanes._allLanes [_Lane];
  394.         _tempPos02.z = _ZPos;
  395.  
  396.         if (_thisNPCBehaviour._targetLane == _Lane && _thisNPCBehaviour._isBlocked) {
  397.             if (_tempDistance > 0.2f) {
  398.                 if (MyMath.Get_Z_DistanceBasic (_thisTransform.localPosition, _thisNPCBehaviour._targetPos) < MyMath.Get_Z_DistanceBasic (_thisTransform.localPosition, _tempPos02)) {
  399.                     _allowNewLane = false;
  400.                 }
  401.             }
  402.         }
  403.  
  404.         _thisNPCBehaviour._currentLane = Global_Vars._Global_Lanes.Return_Lane (_thisTransform.localPosition);
  405.         _thisNPCBehaviour._currentPos = _thisTransform.localPosition;
  406.  
  407.         if (_thisNPCBehaviour._currentLane == -1) { // problem? maybe keep forced walking?
  408.             NPCState_Change(NPCStates.Move);
  409.         } else {
  410.             if (_allowNewLane) {
  411.                 // lane found proceed..
  412.                 _thisNPCBehaviour._targetPos = _tempPos02;
  413.                 _thisNPCBehaviour._targetLane = _Lane;
  414.                 _newDirection = MyMath.Get_LookAtQuat (_thisTransform.localPosition, _thisNPCBehaviour._targetPos);
  415.                 _tempDistance = MyMath.Get_XZ_DistanceBasic (_thisTransform.localPosition, _thisNPCBehaviour._targetPos);
  416.                 _tempDistanceOld = _tempDistance;
  417.                 if (Quaternion.Angle (_thisTransform.rotation, _newDirection) > 50f) {
  418.                     NPCState_Change (NPCStates.Stop_Turn); // need to stop and turn!
  419.                 } else {
  420.                     NPCState_Change (NPCStates.Move);
  421.                 }
  422.                 Get_DirectionGoing ();
  423.             }
  424.         }
  425.     }
  426.  
  427.  
  428.     private void NPC_Turn(){
  429.         _newDirection = MyMath.Get_LookAtQuat (_thisTransform.localPosition, _thisNPCBehaviour._targetPos);
  430.         _thisTransform.rotation = Quaternion.Lerp (_oldDirection, _newDirection, Global_Vars._Global_NPCSystem._turnCurve.Evaluate( _thisNPCBehaviour._internalTimer ) );
  431.         _thisNPCBehaviour._internalTimer += Global_Vars._TimeDeltaX5 * _speedAdjust;
  432.         if ( _thisNPCBehaviour._internalTimer > 1.1f) {
  433.             _thisTransform.rotation = _newDirection;
  434.             _oldDirection = _thisTransform.rotation;
  435.             _tempDistanceOld = MyMath.Get_XZ_DistanceBasic (_thisTransform.localPosition, _thisNPCBehaviour._targetPos);
  436.             NPCState_Change (NPCStates.Move);
  437.         }
  438.     }
  439.  
  440.     private float _tempDistance;
  441.     private float _tempDistanceOld;
  442.     private Vector3 _targetDiff;
  443.  
  444.     private void NPC_CheckTarget(){
  445.         if (_thisNPCBehaviour._internalTick % 1 == 0 && _thisNPCBehaviour._internalSpeed > 0.4f ) {
  446.             _targetDiff.x = Mathf.Abs (_thisNPCBehaviour._currentPos.x - _thisNPCBehaviour._targetPos.x);
  447.             _targetDiff.z = Mathf.Abs (_thisNPCBehaviour._currentPos.z - _thisNPCBehaviour._targetPos.z);
  448.  
  449.             if (_targetDiff.x > 0.5f) {
  450.                 _tempDistance = MyMath.Get_X_DistanceBasic (_thisTransform.localPosition, _thisNPCBehaviour._targetPos);
  451.             } else {
  452.                 _tempDistance = MyMath.Get_Z_DistanceBasic (_thisTransform.localPosition, _thisNPCBehaviour._targetPos);
  453.             }
  454.  
  455.             if (_tempDistance < 0.05f) {
  456.                 NPC_NewLane (_thisNPCBehaviour._targetLane, _thisTransform.localPosition.z + (_directionGoing.z * 5f));
  457.             } else {
  458.                 if (_tempDistance <= _tempDistanceOld) {
  459.                     // we are getting closer ..
  460.                     _tempDistanceOld = _tempDistance;
  461.                 } else {
  462.                     // somehings wrong or passed?
  463.                     if (_tempDistance <= _tempDistanceOld + 1.5f) {
  464.                         NPC_NewLane (_thisNPCBehaviour._targetLane, _thisTransform.localPosition.z + (_directionGoing.z * 5f));
  465.                     } else {
  466.                         NPCState_Change (NPCStates.Stop_Turn);
  467.                     }
  468.                 }
  469.             }
  470.         }
  471.     }
  472.  
  473.     public bool _block_Light; // public for testing TODO
  474.     public bool _block_Npc;
  475.     public bool _block_NpcLane;
  476.     public bool _block_Obstacle;
  477.     public bool _block_ObstacleLarge;
  478.     public bool _block_Player;
  479.     private RaycastHit _tempRay;
  480.  
  481.     private Vector3 _startPoint;
  482.     private Vector3 _endPoint;
  483.  
  484.     private void NPC_CheckBlocked(){
  485.         if (_thisNPCBehaviour._internalTick % 5 == 0) {
  486.             _block_Npc = false; _block_NpcLane = false; _block_Obstacle = false; _block_ObstacleLarge = false; _block_Player = false;
  487.  
  488.             _startPoint = _thisTransform.localPosition + Vector3.up;
  489.             _endPoint = _startPoint + (_directionGoing * _internalRayLength);
  490.  
  491.             _thisNPCBehaviour._isBlocked = Physics.Linecast ( _startPoint + (_thisTransform.right * 0.2f), _endPoint, out _tempRay, Global_Vars._Global_LayerMasks._for_NPCs);
  492.             if (_thisNPCBehaviour._isBlocked == false) {
  493.                 _thisNPCBehaviour._isBlocked = Physics.Linecast ( _startPoint + (_thisTransform.right * -0.2f), _endPoint, out _tempRay, Global_Vars._Global_LayerMasks._for_NPCs);
  494.             }
  495.  
  496.             if (_thisNPCBehaviour._isBlocked) {
  497.  
  498.                 if (_tempRay.transform.CompareTag ("NPC")) {
  499.                     _block_Npc = true;
  500.                 }
  501.                 if (_tempRay.transform.CompareTag ("NPC_Lane")) {
  502.                     _block_NpcLane = true;
  503.                 }
  504.                 if (_tempRay.transform.CompareTag ("Object")) {
  505.                     _block_Obstacle = true;
  506.                 }
  507.                 if (_tempRay.transform.CompareTag ("Object_Large")) {
  508.                     _block_ObstacleLarge = true;
  509.                     _isCrossing = true;
  510.                 }
  511.                 if (_tempRay.transform.CompareTag ("Player")) {
  512.                     _block_Player = true;
  513.                 }
  514.             }
  515.  
  516.         }
  517.     }
  518.  
  519.     private Vector3 _oldPos;
  520.     private float _timeCheck = 0f;
  521.     private float _timeWasStuck = 0f;
  522.  
  523.     private void NPC_CheckStuck(){
  524.         if (_thisNPCBehaviour._internalTick % 25 == 0) {
  525.             if (Time.time > _timeCheck) {
  526.                 _timeCheck = Time.time + 2f;
  527.  
  528.                 if (_thisTransform.localPosition == _oldPos) {
  529.                     // possibly blocked,stuck?
  530.                     if (Time.time > _timeWasStuck + 4f) {
  531.                         _timeWasStuck = Time.time;
  532.                         _thisNPCBehaviour._isBlocked = true;
  533.                         NPC_GetClearLane ();
  534.                     } else {
  535.                         // somethings up majorly stuck, reverse out maybe??
  536.                         _timeWasStuck = Time.time;
  537.                         _thisNPCBehaviour._isBlocked = true;
  538.                         NPC_NewLane ( _thisNPCBehaviour._currentLane , _thisTransform.localPosition.z + (_directionReverse.z * Global_Vars._Global_Lanes._sideStep));
  539.                     }
  540.  
  541.                 } else {
  542.                     _oldPos = _thisTransform.localPosition;
  543.                 }
  544.             } else {
  545.  
  546.             }
  547.         }
  548.     }
  549.  
  550.  
  551.     private RaycastHit[] _rayCasts = new RaycastHit[3];
  552.     private float _nearestCollisionZ;
  553.     private int _clearLane;
  554.  
  555.     private void NPC_Move(){
  556.         NPC_CheckBlocked ();
  557.         NPC_CheckStuck ();
  558.  
  559.         if (_thisNPCBehaviour._isBlocked) {
  560.             NPCState_Change (NPCStates.Blocked);
  561.         } else {
  562.             if ( _targetDiff.x > 0.5f) {
  563.                 if (_tempDistance < 1.5f) {
  564.                     // begin slowing down ..
  565.                     _thisNPCBehaviour._internalSpeed = Mathf.MoveTowards (_thisNPCBehaviour._internalSpeed, _tempDistance + 0.5f , Global_Vars._TimeDeltaX5 * _thisNPCBehaviour._stopSpeed);
  566.                 } else {
  567.                     _thisNPCBehaviour._internalSpeed = Mathf.MoveTowards (_thisNPCBehaviour._internalSpeed, _thisNPCBehaviour._maxSpeed * _speedAdjust, Global_Vars._TimeDelta * _thisNPCBehaviour._startSpeed);
  568.                 }
  569.  
  570.             } else {
  571.                 _thisNPCBehaviour._internalSpeed = Mathf.MoveTowards (_thisNPCBehaviour._internalSpeed, _thisNPCBehaviour._maxSpeed, Global_Vars._TimeDelta * _thisNPCBehaviour._startSpeed);
  572.             }
  573.             _thisTransform.rotation = Quaternion.Lerp (_thisTransform.rotation, _newDirection, Global_Vars._TimeDeltaX10);
  574.         }
  575.  
  576.         _thisCC.SimpleMoveForwardCC ( _thisNPCBehaviour._internalSpeed );
  577.  
  578.         NPC_CheckTarget ();
  579.     }
  580.  
  581.     private float _timeDecision = 0f;
  582.     private float _distanceBeforeLaneChange = 3f;
  583.  
  584.     private void NPC_Blocked(){
  585.         NPC_CheckStuck ();
  586.  
  587.  
  588.         _thisTransform.rotation = Quaternion.Lerp (_thisTransform.rotation, _newDirection, Global_Vars._TimeDeltaX10);
  589.         if (_thisNPCBehaviour._currentLane == _thisNPCBehaviour._targetLane) {
  590.             _thisNPCBehaviour._internalSpeed = Mathf.MoveTowards (_thisNPCBehaviour._internalSpeed, _tempRay.distance + 1f , Global_Vars._TimeDelta * _thisNPCBehaviour._stopSpeed);
  591.         } else {
  592.             // changing lane so keep moving!
  593.             _thisNPCBehaviour._internalSpeed = Mathf.MoveTowards (_thisNPCBehaviour._internalSpeed, _thisNPCBehaviour._maxSpeed * 0.6f , Global_Vars._TimeDelta );
  594.         }
  595.  
  596.         _thisCC.SimpleMoveForwardCC (_thisNPCBehaviour._internalSpeed );
  597.         NPC_CheckTarget ();
  598.  
  599.         if (_thisNPCBehaviour._internalSpeed < _thisNPCBehaviour._maxSpeed * 0.66f) {
  600.             NPC_CheckBlocked (); // check if blocked again?
  601.         }
  602.  
  603.         if (_thisNPCBehaviour._isBlocked && _thisNPCBehaviour._internalSpeed < _thisNPCBehaviour._maxSpeed * 0.9f ) {
  604.             // still blocked choose a lane ..
  605.             if ( MyMath.Get_Z_DistanceBasic (_thisTransform.localPosition, _tempRay.point) < _distanceBeforeLaneChange ) {
  606.                 // keep going to point ..
  607.                 NPC_GetClearLane ();
  608.             } // this should walk right upto the target before changing if its more than 1 unit infront..
  609.         } else {
  610.             NPCState_Change (NPCStates.Move);
  611.         }
  612.     }
  613.  
  614.     private bool _isCrossing;
  615. //  private Vector3 _crossRoad_Pos; // get to road pos..
  616. //  private Vector3 _crossRoad_EndPos; // opposite side of road..
  617. //  private float _crossRoad_Speed = 1f;
  618. //  private float _crossRoad_Wait = 1f; // wait while looking ..
  619. //  private float _crossRoad_WaitInternal;
  620. //
  621. //  private void CrossingRoad(){
  622.         // get into position by road side..
  623.  
  624.         // wait and look correct time ..
  625.  
  626.         // move across correctly ..
  627.  
  628.         // go back to normal..
  629. //  }
  630.  
  631.     #if UNITY_EDITOR
  632.     private Vector3 _laneClearPos;
  633.     #endif
  634.  
  635.     private Vector3 _tempPos;
  636.     private int[] _laneRaySetupOrder = new int[2]; // for deciding which left mid and right position is used..
  637.  
  638.     private void NPC_GetClearLane(){ // will find and target the empty lane..
  639.         if (Time.time > _timeDecision + 2f ) {
  640.             _timeDecision = Time.time;
  641.             if (_thisNPCBehaviour._currentLane == _thisNPCBehaviour._targetLane) {
  642.                 // we need to only do this one time until in target position ..
  643.                 if (_isCrossing == false) {
  644.                     if (_thisNPCBehaviour._currentLane == 0) {
  645.                         _laneRaySetupOrder [0] = 1;
  646.                         _laneRaySetupOrder [1] = 2;
  647.                     }
  648.                     if (_thisNPCBehaviour._currentLane == 1) {
  649.                         _laneRaySetupOrder [0] = 0;
  650.                         _laneRaySetupOrder [1] = 2;
  651.                     }
  652.                     if (_thisNPCBehaviour._currentLane == 2) {
  653.                         _laneRaySetupOrder [0] = 1;
  654.                         _laneRaySetupOrder [1] = 0;
  655.                     }
  656.                     if (_thisNPCBehaviour._currentLane == 3) {
  657.                         _laneRaySetupOrder [0] = 4;
  658.                         _laneRaySetupOrder [1] = 5;
  659.                     }
  660.                     if (_thisNPCBehaviour._currentLane == 4) {
  661.                         _laneRaySetupOrder [0] = 5;
  662.                         _laneRaySetupOrder [1] = 3;
  663.                     }
  664.                     if (_thisNPCBehaviour._currentLane == 5) {
  665.                         _laneRaySetupOrder [0] = 4;
  666.                         _laneRaySetupOrder [1] = 3;
  667.                     }
  668.                 } else {
  669.                     // we are crossing..
  670.                     if (_thisNPCBehaviour._currentLane <= 2) {
  671.                         NPC_NewLane (3, _thisTransform.localPosition.z); // TODO double check this works for going backwards!
  672.                     } else {
  673.                         NPC_NewLane (2, _thisTransform.localPosition.z); // TODO double check this works for going backwards!
  674.                     }
  675.                     return;
  676.                 }
  677.                 // get nearest z world position of any collision so we go around it .. ..
  678.                 _tempDistance = _internalRayLength;
  679.                 if (_tempRay.transform != null) {
  680.                     _nearestCollisionZ = Mathf.MoveTowards (_thisTransform.localPosition.z, _tempRay.point.z, Global_Vars._Global_Lanes._sideStep);
  681.                 } else {
  682.                     _nearestCollisionZ = _thisTransform.localPosition.z;
  683.                 }
  684.  
  685.                 // now begin raycasts setup ..
  686.                 _tempPos = _thisTransform.position + Vector3.up + (_directionReverse * 0.5f);
  687.                 for (_Count = 0; _Count < _laneRaySetupOrder.Length; _Count++) {
  688.                     _tempPos.x = Global_Vars._Global_Lanes._allLanes [_laneRaySetupOrder [_Count]];
  689.                     if (Physics.Raycast (_tempPos, _directionGoing, out _rayCasts [_Count], 4f, Global_Vars._Global_LayerMasks._for_NPCs)) {
  690.                     } else {
  691.                         // no hit, use this?
  692.                         NPC_NewLane (_laneRaySetupOrder [_Count], _nearestCollisionZ);
  693.                         #if UNITY_EDITOR
  694.                         _laneClearPos = _thisNPCBehaviour._targetPos;
  695.                         #endif
  696.                         return;
  697.                     }
  698.                 }
  699.  
  700.                 // now find the clear lane ..
  701.                 _tempDistance = 0f;
  702.                 _clearLane = 0;
  703.  
  704.                 for (_Count = 0; _Count < _rayCasts.Length -1; _Count++) {
  705.                     if (_rayCasts [_Count].transform == null) { // this is clear, just go with this..
  706.                         _clearLane = _Count;
  707.                         break;
  708.                     } else {
  709.                         if (_rayCasts [_Count].distance > _tempDistance) {
  710.                             _tempDistance = _rayCasts [_Count].distance;
  711.                             _clearLane = _Count;
  712.                         }
  713.                         // TODO if we make it through all rays to check, we need to check distance is > 3f so we can make it through, else cross..
  714.                         // Ignore for at lights ... we want npcs to bunch up!
  715.                     }
  716.                 }
  717.  
  718.                 #if UNITY_EDITOR
  719.                 _laneClearPos = _thisNPCBehaviour._targetPos;
  720.                 #endif
  721.                 NPC_NewLane (_laneRaySetupOrder [_clearLane], _nearestCollisionZ); // TODO double check this works for going backwards!
  722.             }
  723.         }
  724.     }
  725.  
  726.  
  727.     #if UNITY_EDITOR
  728.     private Vector3 _tempVect;
  729.     private Color _debug_startColour = new Color(0f, 0.5f, 0f, 1f);
  730.     private Color _debug_endColour = new Color(0f, 1f, 0f, 1f);
  731.     void OnDrawGizmosSelected() {
  732.         if (Global_Vars._globalVarsGO._showDebugs) {
  733.             if (EditorApplication.isPlaying) {
  734.  
  735.                 if (_directionGoing.z > 0f) {
  736.                     Gizmos.DrawIcon (_thisTransform.localPosition + (Vector3.up * 1f) + (_thisTransform.forward * 1f), "Icon_Forward.png", true);
  737.                 } else {
  738.                     Gizmos.DrawIcon (_thisTransform.localPosition + (Vector3.up * 1f) + (_thisTransform.forward * 1f), "Icon_Backward.png", true);
  739.                 }
  740.  
  741.                 if ( _current_npcState == NPCStates.Turn) {
  742.                     // turning ..
  743.                     Gizmos.DrawIcon (_thisTransform.localPosition + (Vector3.up * 1f) + (_thisTransform.forward * 1f), "Icon_Turn.png", true);
  744.                 }
  745.  
  746.                 if ( _current_npcState == NPCStates.Stop || _current_npcState == NPCStates.Stop_Turn) {
  747.                     // stopping ..
  748.                     Gizmos.DrawIcon (_thisTransform.localPosition + (Vector3.up * 1f) + (_thisTransform.forward * 1f), "Icon_Stop.png", true);
  749.                 }
  750.  
  751.                 if ( _current_npcState == NPCStates.Idle) {
  752.                     // idling ..
  753.                     Gizmos.DrawIcon (_thisTransform.localPosition + (Vector3.up * 1f) + (_thisTransform.forward * 1f), "Icon_Talk.png", true);
  754.                 }
  755.  
  756.                 if (_current_npcState == NPCStates.Move || _current_npcState == NPCStates.Blocked) {
  757.                     //  moving ..
  758.                     Gizmos.DrawIcon (_thisTransform.localPosition + (Vector3.up * 1f) + (_thisTransform.forward * 1f), "Icon_Run.png", true);
  759.  
  760.                 }
  761.                 Gizmos.color = _debug_startColour;
  762.                 Gizmos.DrawWireCube (_thisNPCBehaviour._currentPos, new Vector3 (1, 1, 1));
  763.  
  764.                 _tempVect = _thisNPCBehaviour._currentPos;
  765.                 for (_Count = 1; _Count < 5; _Count++) {
  766.                     Gizmos.color = Color.Lerp (_debug_startColour, _debug_endColour, 1f / 5f * _Count);
  767.                     _tempVect = Vector3.MoveTowards (_tempVect, _thisNPCBehaviour._targetPos, 2f);
  768.                     Gizmos.DrawCube (_tempVect, new Vector3 (0.3f, 0.3f, 0.3f));
  769.                 }
  770.  
  771.                 Gizmos.color = _debug_endColour;
  772.                 Gizmos.DrawWireCube (_thisNPCBehaviour._targetPos, new Vector3 (1, 1, 1));
  773.  
  774.                 Gizmos.color = Color.red;
  775.                 for (_Count = 0; _Count < _rayCasts.Length; _Count++) {
  776.                     if (_rayCasts [_Count].transform != null) {
  777.                     Gizmos.DrawCube (_rayCasts [_Count].point, Global_Vars._globalVarsGO._collisionDebugs);
  778.                     }
  779.                 }
  780.                 if (_tempRay.transform != null) {
  781.                     Gizmos.color = Color.magenta;
  782.                 Gizmos.DrawCube (_tempRay.point, Global_Vars._globalVarsGO._collisionDebugs);
  783.                 }
  784.  
  785.                 Gizmos.color = Color.white;
  786.                 Gizmos.DrawCube (_laneClearPos, new Vector3 (1, 1, 1));
  787.  
  788.                 Gizmos.DrawLine (_thisNPCBehaviour._currentPos + Vector3.up, _thisNPCBehaviour._targetPos + Vector3.up);
  789.             }
  790.         }
  791.     }
  792.     #endif
  793.  
  794.  
  795. }
Advertisement
Add Comment
Please, Sign In to add comment