Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- #if UNITY_EDITOR
- using UnityEditor;
- [CustomEditor(typeof(NPC_Class))]
- public class NPC_ClassOverride : Editor
- {
- public override void OnInspectorGUI()
- {
- NPC_Class _npcclass = (NPC_Class)target;
- if(GUILayout.Button("OnSpawn"))
- {
- _npcclass.OnSpawn();
- }
- if(GUILayout.Button("FLASH"))
- {
- _npcclass.DEBUG_Flash ();
- }
- if(GUILayout.Button("newtarget left"))
- {
- _npcclass.DEBUG_NPC_NewLane_Left ();
- }
- if(GUILayout.Button("newtarget right"))
- {
- _npcclass.DEBUG_NPC_NewLane_Right ();
- }
- if(GUILayout.Button("newtarget reverse"))
- {
- _npcclass.DEBUG_NPC_NewLane_Reverse ();
- }
- DrawDefaultInspector();
- }
- }
- #endif
- public class NPC_Class : MonoBehaviour {
- private Transform _thisTransform;
- private Simple_CC _thisCC;
- private Pool_Class _thisPoolClass;
- public Class_NPC_Behaviour _thisNPCBehaviour = new Class_NPC_Behaviour(); // how we setup what it does ..
- private float _walkSpeed = 0.6f;
- public float _rayLength = 5f; // distance npc is comfortable being behind something..
- private float _internalRayLength;
- private float _time_Max = 0f;
- private int _Count;
- private int _hash_walkID;
- private int _hash_stopID;
- public Animator _npc_Animator;
- public enum NPCStates
- {
- Stop,
- Stop_Turn,
- Idle,
- Turn,
- Move,
- Blocked,
- // CrossingRoad,
- }
- public NPCStates _current_npcState;
- public NPCStates _next_npcState;
- void OnEnable(){
- _thisTransform = this.transform;
- _thisCC = GetComponent<Simple_CC> ();
- _thisPoolClass = GetComponent<Pool_Class> ();
- _oldDirection = _thisTransform.rotation;
- _newDirection = _thisTransform.rotation;
- _hash_walkID = Animator.StringToHash ("Walk");
- _hash_stopID = Animator.StringToHash ("Stop");
- _npc_Animator.SetTrigger (_hash_stopID);
- }
- private Vector3 _collisionRotation;
- private float _collisionTime = 0f;
- private Vector3 _collisionVect;
- private int _tempRandom;
- private float _timeCollided = 0f;
- private float _speedAdjust = 1f;
- public int _checkTime = 20;
- private Quaternion _newDirection;
- private Quaternion _oldDirection;
- private NPC_Class _tempNpc;
- private float _tempNPC_speedDiff;
- private Transform _tempCar;
- public void Hit_Back( Vector3 _HitPoint , int _type , float _power){ // type:0 = player / type:1 = npc / type:2 = car / type:3 = objects
- _power = Mathf.Clamp (_power, 0f, 1f);
- _npc_Animator.SetTrigger (_hash_stopID);
- _HitPoint.y = _thisTransform.localPosition.y;
- _HitPoint.x = _HitPoint.x + Random.Range (-.2f,.2f);
- _collisionVect = (_thisTransform.localPosition - _HitPoint).normalized;
- _thisTransform.LookAt (_HitPoint);
- _collisionRotation = _thisTransform.localEulerAngles;
- _collisionRotation.x = 0f; _collisionRotation.z = 0f;
- _thisTransform.localEulerAngles = _collisionRotation;
- if (_type == 0) { // player
- _thisCC.SimpleInertiaCC (_collisionVect + Global_Vars._Global_PlayerC.transform.forward * Mathf.Lerp(1f , 5f , Global_Vars._Player_Speed / 30f) , _power);
- Global_Vars._Global_PlayerC.hit_NPC ( _thisTransform );
- _flashProgress = 1f;
- }
- if (_type == 1) { // npc
- _thisCC.SimpleInertiaCC ( _collisionVect * Mathf.Lerp( .5f , 1f , (_tempNPC_speedDiff /10f) ) , Mathf.Lerp( .3f , 1f , (_tempNPC_speedDiff /10f)) );
- _flashProgress = 0.4f;
- }
- if (_type == 2) { // car
- _thisCC.SimpleInertiaCC ( _collisionVect + _tempCar.forward + _tempCar.right , _power );
- _flashProgress = 1f;
- }
- if (_type == 3) { // objects
- _thisCC.SimpleInertiaCC ( _collisionVect * Mathf.Lerp( .5f , 1f , (_thisNPCBehaviour._internalSpeed /10f) ) , Mathf.Lerp( .3f , 1f , (_thisNPCBehaviour._internalSpeed /10f)) );
- }
- NPCState_Change (NPCStates.Stop_Turn);
- _collisionTime = Global_Vars._Global_Difficulty._NPC_RecoveryTime;
- }
- public GameObject _flashObject;
- private float _flashProgress = 0f;
- void OnTriggerEnter( Collider _hitWith ) {
- if(_thisPoolClass._inUse){
- if (_hitWith.CompareTag ("Traffic")) {
- _tempCar = _hitWith.transform;
- Hit_Back ( _hitWith.transform.position , 2 , 1f);
- }
- if (_hitWith.CompareTag ("Player")) {
- Hit_Back (Global_Vars._Player_CurrentPosition, 0 , Global_Vars._Player_Speed / 15f);
- Global_Vars._Global_PlayerC.hit_NPC ( _thisTransform );
- }
- if(Time.time > _timeCollided + .15f){
- _timeCollided = Time.time;
- if (_hitWith.CompareTag ("NPC")) {
- _tempNpc = _hitWith.gameObject.GetComponentInParent<NPC_Class> ();
- _tempNPC_speedDiff = _tempNpc._thisNPCBehaviour._internalSpeed - _thisNPCBehaviour._internalSpeed;
- if ( _tempNPC_speedDiff > 0f ) { // im slower so i bounce ..
- Hit_Back ( _hitWith.transform.localPosition , 1 , 1f);
- } // hitback on ourself because we are slower than what hit us!
- }
- if (_hitWith.CompareTag ("Object") || _hitWith.CompareTag ("Object_Large") ) {
- Hit_Back ( _hitWith.transform.position , 3 , _thisNPCBehaviour._internalSpeed / 10f);
- }
- }
- }
- }
- private void FlashCharacter(){
- if (_flashProgress > 0f) {
- if (Global_Vars._Global_NPCSystem._flashCurve.Evaluate (_flashProgress) > 0.1f) {
- _flashObject.SetActive (true);
- } else {
- _flashObject.SetActive (false);
- }
- _flashProgress -= Global_Vars._TimeDelta;
- } else {
- _flashObject.SetActive (false);
- }
- }
- public void OnSpawn(){
- Global_Vars._Global_NPCSystem.Add_ToNPC_Spawned (this);
- _current_npcState = NPCStates.Idle;
- _thisNPCBehaviour._internalSpeed = 0f;
- _thisNPCBehaviour._internalTimer = 0f;
- _thisNPCBehaviour._internalTick = 1;
- _thisNPCBehaviour._currentOrder = 0;
- _timeWasStuck = 0f;
- _timeDecision = 0f;
- _timeCheck = 0f;
- _flashProgress = 0f;
- FlashCharacter ();
- _isCrossing = false;
- _oldPos = new Vector3();
- _thisNPCBehaviour._currentLane = Global_Vars._Global_Lanes.Return_Lane (_thisTransform.position);
- _thisNPCBehaviour._targetLane = _thisNPCBehaviour._currentLane;
- _time_Max = 0f;
- _collisionTime = 0f;
- _speedAdjust = 1f;
- _speedAdjust = Random.Range (0.75f , 1.1f);
- _internalRayLength = Random.Range (_rayLength * 0.35f , _rayLength * 1.8f);
- if (_thisNPCBehaviour._typeRunner) {
- _internalRayLength = _rayLength * 1.5f;
- }
- NPCState_Change (NPCStates.Stop);
- }
- public void OnDespawn(){
- Global_Vars._Global_NPCSystem.Remove_FromNPC_Spawned (this);
- _thisPoolClass.OnDespawn ();
- }
- // TODO: turn this into a staggered list of actions to do with directions and targets also included!
- // rather than the reactionary model we have now tha only goes 1 action at a time..
- // eg blocked would include the point of blockage..
- // we save that in a history of past actions and if it keeps coming up do a different action?
- public void NPCState_Change( NPCStates _state ){
- if (_next_npcState != NPCStates.Idle) {
- _state = _next_npcState;
- _next_npcState = NPCStates.Idle;
- }
- _thisNPCBehaviour._internalTimer = 0f;
- _time_Max = Random.Range (1f,3f);
- _flashObject.SetActive (false);
- switch (_state){
- case NPCStates.Stop:
- _current_npcState = NPCStates.Stop;
- _npc_Animator.SetTrigger (_hash_stopID);
- break;
- case NPCStates.Stop_Turn:
- _current_npcState = NPCStates.Stop_Turn;
- _npc_Animator.SetTrigger (_hash_stopID);
- break;
- case NPCStates.Idle:
- _current_npcState = NPCStates.Idle;
- _npc_Animator.SetTrigger (_hash_stopID);
- break;
- case NPCStates.Turn:
- _oldDirection = _thisTransform.rotation;
- _current_npcState = NPCStates.Turn;
- _next_npcState = NPCStates.Move;
- _npc_Animator.SetTrigger (_hash_stopID);
- break;
- case NPCStates.Move:
- _thisNPCBehaviour._currentLane = Global_Vars._Global_Lanes.Return_Lane(_thisTransform.position);
- _current_npcState = NPCStates.Move;
- _npc_Animator.SetTrigger (_hash_walkID);
- break;
- case NPCStates.Blocked:
- _current_npcState = NPCStates.Blocked;
- _npc_Animator.SetTrigger (_hash_walkID);
- break;
- //
- // case NPCStates.CrossingRoad:
- // _current_npcState = NPCStates.CrossingRoad;
- // break;
- }
- }
- public void NPCState_Update(){
- if (_thisPoolClass._poolRB != null && _thisPoolClass._poolRB.IsSleeping()) {
- _thisPoolClass._poolRB.WakeUp ();
- }
- if ( _current_npcState.Equals ( NPCStates.Stop ) ) {
- NPC_Stop ();
- }
- if ( _current_npcState.Equals ( NPCStates.Stop_Turn ) ) {
- NPC_Stop ();
- }
- if ( _current_npcState.Equals ( NPCStates.Idle ) ) {
- NPC_Idle ();
- }
- if ( _current_npcState.Equals ( NPCStates.Turn ) ) {
- NPC_Turn ();
- }
- if ( _current_npcState.Equals ( NPCStates.Move ) ) {
- NPC_Move ();
- }
- if ( _current_npcState.Equals ( NPCStates.Blocked ) ) {
- NPC_Blocked ();
- }
- // if ( _current_npcState.Equals ( NPCStates.CrossingRoad ) ) {
- // CrossingRoad ();
- // }
- Check_PlayerDistance(); // distance check //
- _thisNPCBehaviour._internalTick += 1;
- }
- private void Check_PlayerDistance(){
- if (_thisNPCBehaviour._internalTick % 100 == 0) {
- if (_thisTransform.localPosition.z > Global_Vars._Player_CurrentPosition.z) {
- if (_thisTransform.localPosition.z > Global_Vars._Global_Area_TileEndPosition.z) {
- OnDespawn ();
- }
- } else {
- if (_thisTransform.localPosition.z < Global_Vars._Player_CurrentPosition.z - 2.5f) {
- OnDespawn ();
- }
- }
- }
- }
- private void NPC_Stop(){
- FlashCharacter ();
- if (_collisionTime > 0f) {
- _collisionTime -= Global_Vars._TimeDelta;
- _thisCC.SimpleMoveForwardCC ( _collisionTime * -.2f);
- // check if in middle of road here just incase!
- if (MyMath.Get_X_DistanceBasic (_thisTransform.localPosition, Vector3.zero) < 3f) {
- // is in road fix!
- if (_thisTransform.localPosition.x > 0f) {
- NPC_NewLane( 3 , _thisTransform.localPosition.z);
- } else {
- NPC_NewLane( 2 , _thisTransform.localPosition.z);
- }
- }
- } else {
- _thisNPCBehaviour._internalSpeed = Mathf.Lerp(_thisNPCBehaviour._internalSpeed , 0f , Global_Vars._TimeDeltaX5);
- _thisCC.SimpleMoveForwardCC (_thisNPCBehaviour._internalSpeed);
- _thisNPCBehaviour._internalTimer += Global_Vars._TimeDelta;
- if (_thisNPCBehaviour._internalTimer > 1f) {
- if (_current_npcState.Equals(NPCStates.Stop)) {
- NPCState_Change (NPCStates.Idle);
- }
- if (_current_npcState.Equals( NPCStates.Stop_Turn)) {
- NPCState_Change (NPCStates.Turn);
- }
- }
- }
- }
- private void NPC_Idle(){
- _thisNPCBehaviour._internalTimer += Global_Vars._TimeDelta;
- if (_thisNPCBehaviour._internalTimer > _time_Max) {
- NPC_NewLane (_thisNPCBehaviour._currentLane, _thisTransform.localPosition.z + (_directionGoing.z * 5f));
- NPCState_Change (NPCStates.Move);
- }
- }
- private float _directionDiff;
- private Vector3 _directionGoing;
- private Vector3 _directionReverse;
- private void Get_DirectionGoing(){
- _directionDiff = _thisNPCBehaviour._targetPos.z - _thisNPCBehaviour._currentPos.z;
- if ( _directionDiff > 0f) { // facing same way ..
- _directionGoing = Global_Vars._Global_Lanes._forward_rotate.forward;
- _directionReverse = Global_Vars._Global_Lanes._backward_rotate.forward;
- } else {
- _directionGoing = Global_Vars._Global_Lanes._backward_rotate.forward;
- _directionReverse = Global_Vars._Global_Lanes._forward_rotate.forward;
- }
- }
- public void DEBUG_Flash(){
- _flashProgress = 1f;
- NPCState_Change (NPCStates.Stop);
- _collisionTime = Global_Vars._Global_Difficulty._NPC_RecoveryTime;
- }
- public void DEBUG_NPC_NewLane_Left(){ // based on information will get next world target pos to move to..
- NPC_NewLane ( Mathf.Clamp( _thisNPCBehaviour._currentLane - 1 , 0 , 5 ), _thisTransform.localPosition.z + (_directionGoing.z * Global_Vars._Global_Lanes._sideStep));
- }
- public void DEBUG_NPC_NewLane_Right(){ // based on information will get next world target pos to move to..
- NPC_NewLane ( Mathf.Clamp( _thisNPCBehaviour._currentLane + 1 , 0 , 5 ), _thisTransform.localPosition.z + (_directionGoing.z * Global_Vars._Global_Lanes._sideStep));
- }
- public void DEBUG_NPC_NewLane_Reverse(){ // based on information will get next world target pos to move to..
- NPC_NewLane ( _thisNPCBehaviour._currentLane , _thisTransform.localPosition.z + (_directionReverse.z * Global_Vars._Global_Lanes._sideStep));
- }
- private bool _allowNewLane = true;
- private Vector3 _tempPos02;
- public void NPC_NewLane( int _Lane , float _ZPos){ // based on information will get next world target pos to move to..
- _Lane = Mathf.Clamp(_Lane , 0 , 5);
- _isCrossing = false;
- _allowNewLane = true;
- _tempPos02.x = Global_Vars._Global_Lanes._allLanes [_Lane];
- _tempPos02.z = _ZPos;
- if (_thisNPCBehaviour._targetLane == _Lane && _thisNPCBehaviour._isBlocked) {
- if (_tempDistance > 0.2f) {
- if (MyMath.Get_Z_DistanceBasic (_thisTransform.localPosition, _thisNPCBehaviour._targetPos) < MyMath.Get_Z_DistanceBasic (_thisTransform.localPosition, _tempPos02)) {
- _allowNewLane = false;
- }
- }
- }
- _thisNPCBehaviour._currentLane = Global_Vars._Global_Lanes.Return_Lane (_thisTransform.localPosition);
- _thisNPCBehaviour._currentPos = _thisTransform.localPosition;
- if (_thisNPCBehaviour._currentLane == -1) { // problem? maybe keep forced walking?
- NPCState_Change(NPCStates.Move);
- } else {
- if (_allowNewLane) {
- // lane found proceed..
- _thisNPCBehaviour._targetPos = _tempPos02;
- _thisNPCBehaviour._targetLane = _Lane;
- _newDirection = MyMath.Get_LookAtQuat (_thisTransform.localPosition, _thisNPCBehaviour._targetPos);
- _tempDistance = MyMath.Get_XZ_DistanceBasic (_thisTransform.localPosition, _thisNPCBehaviour._targetPos);
- _tempDistanceOld = _tempDistance;
- if (Quaternion.Angle (_thisTransform.rotation, _newDirection) > 50f) {
- NPCState_Change (NPCStates.Stop_Turn); // need to stop and turn!
- } else {
- NPCState_Change (NPCStates.Move);
- }
- Get_DirectionGoing ();
- }
- }
- }
- private void NPC_Turn(){
- _newDirection = MyMath.Get_LookAtQuat (_thisTransform.localPosition, _thisNPCBehaviour._targetPos);
- _thisTransform.rotation = Quaternion.Lerp (_oldDirection, _newDirection, Global_Vars._Global_NPCSystem._turnCurve.Evaluate( _thisNPCBehaviour._internalTimer ) );
- _thisNPCBehaviour._internalTimer += Global_Vars._TimeDeltaX5 * _speedAdjust;
- if ( _thisNPCBehaviour._internalTimer > 1.1f) {
- _thisTransform.rotation = _newDirection;
- _oldDirection = _thisTransform.rotation;
- _tempDistanceOld = MyMath.Get_XZ_DistanceBasic (_thisTransform.localPosition, _thisNPCBehaviour._targetPos);
- NPCState_Change (NPCStates.Move);
- }
- }
- private float _tempDistance;
- private float _tempDistanceOld;
- private Vector3 _targetDiff;
- private void NPC_CheckTarget(){
- if (_thisNPCBehaviour._internalTick % 1 == 0 && _thisNPCBehaviour._internalSpeed > 0.4f ) {
- _targetDiff.x = Mathf.Abs (_thisNPCBehaviour._currentPos.x - _thisNPCBehaviour._targetPos.x);
- _targetDiff.z = Mathf.Abs (_thisNPCBehaviour._currentPos.z - _thisNPCBehaviour._targetPos.z);
- if (_targetDiff.x > 0.5f) {
- _tempDistance = MyMath.Get_X_DistanceBasic (_thisTransform.localPosition, _thisNPCBehaviour._targetPos);
- } else {
- _tempDistance = MyMath.Get_Z_DistanceBasic (_thisTransform.localPosition, _thisNPCBehaviour._targetPos);
- }
- if (_tempDistance < 0.05f) {
- NPC_NewLane (_thisNPCBehaviour._targetLane, _thisTransform.localPosition.z + (_directionGoing.z * 5f));
- } else {
- if (_tempDistance <= _tempDistanceOld) {
- // we are getting closer ..
- _tempDistanceOld = _tempDistance;
- } else {
- // somehings wrong or passed?
- if (_tempDistance <= _tempDistanceOld + 1.5f) {
- NPC_NewLane (_thisNPCBehaviour._targetLane, _thisTransform.localPosition.z + (_directionGoing.z * 5f));
- } else {
- NPCState_Change (NPCStates.Stop_Turn);
- }
- }
- }
- }
- }
- public bool _block_Light; // public for testing TODO
- public bool _block_Npc;
- public bool _block_NpcLane;
- public bool _block_Obstacle;
- public bool _block_ObstacleLarge;
- public bool _block_Player;
- private RaycastHit _tempRay;
- private Vector3 _startPoint;
- private Vector3 _endPoint;
- private void NPC_CheckBlocked(){
- if (_thisNPCBehaviour._internalTick % 5 == 0) {
- _block_Npc = false; _block_NpcLane = false; _block_Obstacle = false; _block_ObstacleLarge = false; _block_Player = false;
- _startPoint = _thisTransform.localPosition + Vector3.up;
- _endPoint = _startPoint + (_directionGoing * _internalRayLength);
- _thisNPCBehaviour._isBlocked = Physics.Linecast ( _startPoint + (_thisTransform.right * 0.2f), _endPoint, out _tempRay, Global_Vars._Global_LayerMasks._for_NPCs);
- if (_thisNPCBehaviour._isBlocked == false) {
- _thisNPCBehaviour._isBlocked = Physics.Linecast ( _startPoint + (_thisTransform.right * -0.2f), _endPoint, out _tempRay, Global_Vars._Global_LayerMasks._for_NPCs);
- }
- if (_thisNPCBehaviour._isBlocked) {
- if (_tempRay.transform.CompareTag ("NPC")) {
- _block_Npc = true;
- }
- if (_tempRay.transform.CompareTag ("NPC_Lane")) {
- _block_NpcLane = true;
- }
- if (_tempRay.transform.CompareTag ("Object")) {
- _block_Obstacle = true;
- }
- if (_tempRay.transform.CompareTag ("Object_Large")) {
- _block_ObstacleLarge = true;
- _isCrossing = true;
- }
- if (_tempRay.transform.CompareTag ("Player")) {
- _block_Player = true;
- }
- }
- }
- }
- private Vector3 _oldPos;
- private float _timeCheck = 0f;
- private float _timeWasStuck = 0f;
- private void NPC_CheckStuck(){
- if (_thisNPCBehaviour._internalTick % 25 == 0) {
- if (Time.time > _timeCheck) {
- _timeCheck = Time.time + 2f;
- if (_thisTransform.localPosition == _oldPos) {
- // possibly blocked,stuck?
- if (Time.time > _timeWasStuck + 4f) {
- _timeWasStuck = Time.time;
- _thisNPCBehaviour._isBlocked = true;
- NPC_GetClearLane ();
- } else {
- // somethings up majorly stuck, reverse out maybe??
- _timeWasStuck = Time.time;
- _thisNPCBehaviour._isBlocked = true;
- NPC_NewLane ( _thisNPCBehaviour._currentLane , _thisTransform.localPosition.z + (_directionReverse.z * Global_Vars._Global_Lanes._sideStep));
- }
- } else {
- _oldPos = _thisTransform.localPosition;
- }
- } else {
- }
- }
- }
- private RaycastHit[] _rayCasts = new RaycastHit[3];
- private float _nearestCollisionZ;
- private int _clearLane;
- private void NPC_Move(){
- NPC_CheckBlocked ();
- NPC_CheckStuck ();
- if (_thisNPCBehaviour._isBlocked) {
- NPCState_Change (NPCStates.Blocked);
- } else {
- if ( _targetDiff.x > 0.5f) {
- if (_tempDistance < 1.5f) {
- // begin slowing down ..
- _thisNPCBehaviour._internalSpeed = Mathf.MoveTowards (_thisNPCBehaviour._internalSpeed, _tempDistance + 0.5f , Global_Vars._TimeDeltaX5 * _thisNPCBehaviour._stopSpeed);
- } else {
- _thisNPCBehaviour._internalSpeed = Mathf.MoveTowards (_thisNPCBehaviour._internalSpeed, _thisNPCBehaviour._maxSpeed * _speedAdjust, Global_Vars._TimeDelta * _thisNPCBehaviour._startSpeed);
- }
- } else {
- _thisNPCBehaviour._internalSpeed = Mathf.MoveTowards (_thisNPCBehaviour._internalSpeed, _thisNPCBehaviour._maxSpeed, Global_Vars._TimeDelta * _thisNPCBehaviour._startSpeed);
- }
- _thisTransform.rotation = Quaternion.Lerp (_thisTransform.rotation, _newDirection, Global_Vars._TimeDeltaX10);
- }
- _thisCC.SimpleMoveForwardCC ( _thisNPCBehaviour._internalSpeed );
- NPC_CheckTarget ();
- }
- private float _timeDecision = 0f;
- private float _distanceBeforeLaneChange = 3f;
- private void NPC_Blocked(){
- NPC_CheckStuck ();
- _thisTransform.rotation = Quaternion.Lerp (_thisTransform.rotation, _newDirection, Global_Vars._TimeDeltaX10);
- if (_thisNPCBehaviour._currentLane == _thisNPCBehaviour._targetLane) {
- _thisNPCBehaviour._internalSpeed = Mathf.MoveTowards (_thisNPCBehaviour._internalSpeed, _tempRay.distance + 1f , Global_Vars._TimeDelta * _thisNPCBehaviour._stopSpeed);
- } else {
- // changing lane so keep moving!
- _thisNPCBehaviour._internalSpeed = Mathf.MoveTowards (_thisNPCBehaviour._internalSpeed, _thisNPCBehaviour._maxSpeed * 0.6f , Global_Vars._TimeDelta );
- }
- _thisCC.SimpleMoveForwardCC (_thisNPCBehaviour._internalSpeed );
- NPC_CheckTarget ();
- if (_thisNPCBehaviour._internalSpeed < _thisNPCBehaviour._maxSpeed * 0.66f) {
- NPC_CheckBlocked (); // check if blocked again?
- }
- if (_thisNPCBehaviour._isBlocked && _thisNPCBehaviour._internalSpeed < _thisNPCBehaviour._maxSpeed * 0.9f ) {
- // still blocked choose a lane ..
- if ( MyMath.Get_Z_DistanceBasic (_thisTransform.localPosition, _tempRay.point) < _distanceBeforeLaneChange ) {
- // keep going to point ..
- NPC_GetClearLane ();
- } // this should walk right upto the target before changing if its more than 1 unit infront..
- } else {
- NPCState_Change (NPCStates.Move);
- }
- }
- private bool _isCrossing;
- // private Vector3 _crossRoad_Pos; // get to road pos..
- // private Vector3 _crossRoad_EndPos; // opposite side of road..
- // private float _crossRoad_Speed = 1f;
- // private float _crossRoad_Wait = 1f; // wait while looking ..
- // private float _crossRoad_WaitInternal;
- //
- // private void CrossingRoad(){
- // get into position by road side..
- // wait and look correct time ..
- // move across correctly ..
- // go back to normal..
- // }
- #if UNITY_EDITOR
- private Vector3 _laneClearPos;
- #endif
- private Vector3 _tempPos;
- private int[] _laneRaySetupOrder = new int[2]; // for deciding which left mid and right position is used..
- private void NPC_GetClearLane(){ // will find and target the empty lane..
- if (Time.time > _timeDecision + 2f ) {
- _timeDecision = Time.time;
- if (_thisNPCBehaviour._currentLane == _thisNPCBehaviour._targetLane) {
- // we need to only do this one time until in target position ..
- if (_isCrossing == false) {
- if (_thisNPCBehaviour._currentLane == 0) {
- _laneRaySetupOrder [0] = 1;
- _laneRaySetupOrder [1] = 2;
- }
- if (_thisNPCBehaviour._currentLane == 1) {
- _laneRaySetupOrder [0] = 0;
- _laneRaySetupOrder [1] = 2;
- }
- if (_thisNPCBehaviour._currentLane == 2) {
- _laneRaySetupOrder [0] = 1;
- _laneRaySetupOrder [1] = 0;
- }
- if (_thisNPCBehaviour._currentLane == 3) {
- _laneRaySetupOrder [0] = 4;
- _laneRaySetupOrder [1] = 5;
- }
- if (_thisNPCBehaviour._currentLane == 4) {
- _laneRaySetupOrder [0] = 5;
- _laneRaySetupOrder [1] = 3;
- }
- if (_thisNPCBehaviour._currentLane == 5) {
- _laneRaySetupOrder [0] = 4;
- _laneRaySetupOrder [1] = 3;
- }
- } else {
- // we are crossing..
- if (_thisNPCBehaviour._currentLane <= 2) {
- NPC_NewLane (3, _thisTransform.localPosition.z); // TODO double check this works for going backwards!
- } else {
- NPC_NewLane (2, _thisTransform.localPosition.z); // TODO double check this works for going backwards!
- }
- return;
- }
- // get nearest z world position of any collision so we go around it .. ..
- _tempDistance = _internalRayLength;
- if (_tempRay.transform != null) {
- _nearestCollisionZ = Mathf.MoveTowards (_thisTransform.localPosition.z, _tempRay.point.z, Global_Vars._Global_Lanes._sideStep);
- } else {
- _nearestCollisionZ = _thisTransform.localPosition.z;
- }
- // now begin raycasts setup ..
- _tempPos = _thisTransform.position + Vector3.up + (_directionReverse * 0.5f);
- for (_Count = 0; _Count < _laneRaySetupOrder.Length; _Count++) {
- _tempPos.x = Global_Vars._Global_Lanes._allLanes [_laneRaySetupOrder [_Count]];
- if (Physics.Raycast (_tempPos, _directionGoing, out _rayCasts [_Count], 4f, Global_Vars._Global_LayerMasks._for_NPCs)) {
- } else {
- // no hit, use this?
- NPC_NewLane (_laneRaySetupOrder [_Count], _nearestCollisionZ);
- #if UNITY_EDITOR
- _laneClearPos = _thisNPCBehaviour._targetPos;
- #endif
- return;
- }
- }
- // now find the clear lane ..
- _tempDistance = 0f;
- _clearLane = 0;
- for (_Count = 0; _Count < _rayCasts.Length -1; _Count++) {
- if (_rayCasts [_Count].transform == null) { // this is clear, just go with this..
- _clearLane = _Count;
- break;
- } else {
- if (_rayCasts [_Count].distance > _tempDistance) {
- _tempDistance = _rayCasts [_Count].distance;
- _clearLane = _Count;
- }
- // 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..
- // Ignore for at lights ... we want npcs to bunch up!
- }
- }
- #if UNITY_EDITOR
- _laneClearPos = _thisNPCBehaviour._targetPos;
- #endif
- NPC_NewLane (_laneRaySetupOrder [_clearLane], _nearestCollisionZ); // TODO double check this works for going backwards!
- }
- }
- }
- #if UNITY_EDITOR
- private Vector3 _tempVect;
- private Color _debug_startColour = new Color(0f, 0.5f, 0f, 1f);
- private Color _debug_endColour = new Color(0f, 1f, 0f, 1f);
- void OnDrawGizmosSelected() {
- if (Global_Vars._globalVarsGO._showDebugs) {
- if (EditorApplication.isPlaying) {
- if (_directionGoing.z > 0f) {
- Gizmos.DrawIcon (_thisTransform.localPosition + (Vector3.up * 1f) + (_thisTransform.forward * 1f), "Icon_Forward.png", true);
- } else {
- Gizmos.DrawIcon (_thisTransform.localPosition + (Vector3.up * 1f) + (_thisTransform.forward * 1f), "Icon_Backward.png", true);
- }
- if ( _current_npcState == NPCStates.Turn) {
- // turning ..
- Gizmos.DrawIcon (_thisTransform.localPosition + (Vector3.up * 1f) + (_thisTransform.forward * 1f), "Icon_Turn.png", true);
- }
- if ( _current_npcState == NPCStates.Stop || _current_npcState == NPCStates.Stop_Turn) {
- // stopping ..
- Gizmos.DrawIcon (_thisTransform.localPosition + (Vector3.up * 1f) + (_thisTransform.forward * 1f), "Icon_Stop.png", true);
- }
- if ( _current_npcState == NPCStates.Idle) {
- // idling ..
- Gizmos.DrawIcon (_thisTransform.localPosition + (Vector3.up * 1f) + (_thisTransform.forward * 1f), "Icon_Talk.png", true);
- }
- if (_current_npcState == NPCStates.Move || _current_npcState == NPCStates.Blocked) {
- // moving ..
- Gizmos.DrawIcon (_thisTransform.localPosition + (Vector3.up * 1f) + (_thisTransform.forward * 1f), "Icon_Run.png", true);
- }
- Gizmos.color = _debug_startColour;
- Gizmos.DrawWireCube (_thisNPCBehaviour._currentPos, new Vector3 (1, 1, 1));
- _tempVect = _thisNPCBehaviour._currentPos;
- for (_Count = 1; _Count < 5; _Count++) {
- Gizmos.color = Color.Lerp (_debug_startColour, _debug_endColour, 1f / 5f * _Count);
- _tempVect = Vector3.MoveTowards (_tempVect, _thisNPCBehaviour._targetPos, 2f);
- Gizmos.DrawCube (_tempVect, new Vector3 (0.3f, 0.3f, 0.3f));
- }
- Gizmos.color = _debug_endColour;
- Gizmos.DrawWireCube (_thisNPCBehaviour._targetPos, new Vector3 (1, 1, 1));
- Gizmos.color = Color.red;
- for (_Count = 0; _Count < _rayCasts.Length; _Count++) {
- if (_rayCasts [_Count].transform != null) {
- Gizmos.DrawCube (_rayCasts [_Count].point, Global_Vars._globalVarsGO._collisionDebugs);
- }
- }
- if (_tempRay.transform != null) {
- Gizmos.color = Color.magenta;
- Gizmos.DrawCube (_tempRay.point, Global_Vars._globalVarsGO._collisionDebugs);
- }
- Gizmos.color = Color.white;
- Gizmos.DrawCube (_laneClearPos, new Vector3 (1, 1, 1));
- Gizmos.DrawLine (_thisNPCBehaviour._currentPos + Vector3.up, _thisNPCBehaviour._targetPos + Vector3.up);
- }
- }
- }
- #endif
- }
Advertisement
Add Comment
Please, Sign In to add comment