/// <summary> /// Setting speed via SpeedAdjustmentEnum variable /// </summary> /// <param name="speedAdjustment"></param> private void SetMoveSpeed(SpeedAdjustmentEnum speedAdjustment) { if (speedAdjustment == SpeedAdjustmentEnum.Increase) { if (Stopped) { moveSpeed = Mathf.SmoothStep(moveSpeed, SpeedLimit.SpeedLimitValue, startTime * Time.deltaTime); //Debug.Log(gameObject.name + "=> набираю скорость до допустимой!"); } else { moveSpeed = Mathf.SmoothStep(moveSpeed, initialSpeed, startTime * Time.deltaTime); //Debug.Log(gameObject.name + "=> набираю скорость до изначальной!"); } } else if (speedAdjustment == SpeedAdjustmentEnum.Decrease) { moveSpeed = Mathf.SmoothStep(moveSpeed, MinSpeedForMoving, stopTime * Time.deltaTime); //Debug.Log(gameObject.name + "=> торможу!"); } //else if (speedAdjustment == SpeedAdjustmentEnum.ExtremeDe) else if (speedAdjustment == SpeedAdjustmentEnum.Stop) { moveSpeed = 0f; //Debug.Log(gameObject.name + "=> стою!"); } }
/// <summary> /// Checks whether a car is on a stop point /// </summary> /// <param name="policePath"></param> private void CheckStopPoint(PolicePath policePath) { if (Vector3.Distance(transform.position, policePath.StopPoint) <= StopInfelicity) { speedAdjustment = SpeedAdjustmentEnum.Stop; policePathMoving = PolicePathMovingEnum.Stop; //Debug.Log(gameObject.name + ": стою у полицейского"); StartCoroutine(StoppingCoroutine()); StopCoroutine(StoppingCoroutine()); } }
/// <summary> /// Counts 3 seconds at a stop point after which the car can begin to move /// </summary> /// <returns></returns> private IEnumerator StoppingCoroutine() { //Debug.Log(gameObject.name + "=> Заход в коррутину!"); while (policePathMoving != PolicePathMovingEnum.Stop) { yield return(null); } yield return(new WaitForSeconds(3)); policePathMoving = PolicePathMovingEnum.Starting; speedAdjustment = SpeedAdjustmentEnum.Increase; Stopped = true; }
/// <summary> /// Actions after the machine disappeared from sight /// </summary> /// <param name="other"></param> private void OnTriggerExit(Collider other) { if (other.tag == PoliceCarTag) { return; } GameObject detectedCar = other.gameObject.transform.parent.gameObject; //Debug.Log(gameObject.name + "=>" + detectedCar.name + " больше не вижу личное пространство машины: " + detectedCar.name); detectedCars.Remove(detectedCar); //Debug.Log(gameObject.name + "=>" + " текущее кол-во машин в поле зрения: " + detectedCars.Count); switch (policePathMoving) { case PolicePathMovingEnum.Stop: return; case PolicePathMovingEnum.SpeedReduction: return; case PolicePathMovingEnum.ExitFromTheRoad: return; } foreach (var car in detectedCars) { //если машина, которая осталась в поле зрения сзади if (car.Key.transform.position.z < transform.position.z) { if (policePathMoving == PolicePathMovingEnum.Starting) { return; } } else//если машина, которая осталась в поле зрения спереди { switch (car.Value.policePathMoving) { case PolicePathMovingEnum.None: return; case PolicePathMovingEnum.ReturnToTheRoad: return; } } } speedAdjustment = SpeedAdjustmentEnum.Increase; //Debug.Log(gameObject.name + "=>" + detectedCar.name + " больше не вижу личное пространство машины: " + detectedCar.name + // "\nМоё состояние на полицейском пути: " + policePathMoving + // "\nМоя настройка скорости после ухода: " + speedAdjustment); }
/// <summary> /// Sets actions on a police path /// </summary> private void ActionsOnPolicePath(PolicePath policePath) { //PolicePath policePath = (PolicePath)CurrentPath; PolicePathMovingStatements(policePath); switch (policePathMoving) { case PolicePathMovingEnum.SpeedReduction: speedAdjustment = SpeedAdjustmentEnum.Decrease; //Debug.Log(gameObject.name + "=> сбавляю скорость перед съездом на обочину"); break; case PolicePathMovingEnum.ExitFromTheRoad: CheckStopPoint(policePath); //Debug.Log(gameObject.name + "=> еду по обочине к полицейскому"); break; case PolicePathMovingEnum.ReturnToTheRoad: CheckRemovingCar(); //Debug.Log(gameObject.name + "=> вернулся на дорогу после остановки"); break; } }
/// <summary> /// Actions while cars are in sight /// </summary> /// <param name="detectedCars"></param> private void DetectedCarsChecking(Dictionary <GameObject, CarMovement> detectedCars) { foreach (var car in detectedCars) { if (car.Key.Equals(null)) { //Debug.Log(gameObject.name + "=> машины уже нет на карте!"); detectedCars.Remove(car.Key); if (detectedCars.Count == 0) { switch (policePathMoving) { case PolicePathMovingEnum.None: case PolicePathMovingEnum.Starting: case PolicePathMovingEnum.ReturnToTheRoad: speedAdjustment = SpeedAdjustmentEnum.Increase; break; default: break; } } else { int fwdCarsCount = 0; foreach (var remCar in detectedCars) { if (car.Key.transform.position.z > transform.position.z) { fwdCarsCount++; } } if (fwdCarsCount == 0) { speedAdjustment = SpeedAdjustmentEnum.Increase; } } return; } switch (policePathMoving) { case PolicePathMovingEnum.Starting: //если машина, которая в поле зрения сзади if (car.Key.transform.position.z < transform.position.z) { switch (car.Value.policePathMoving) { case PolicePathMovingEnum.None: speedAdjustment = SpeedAdjustmentEnum.Stop; break; case PolicePathMovingEnum.ReturnToTheRoad: speedAdjustment = SpeedAdjustmentEnum.Stop; break; default: break; } } else //если машина, которая осталась в поле зрения спереди { switch (car.Value.policePathMoving) { case PolicePathMovingEnum.None: speedAdjustment = SpeedAdjustmentEnum.Stop; break; case PolicePathMovingEnum.SpeedReduction: speedAdjustment = SpeedAdjustmentEnum.Decrease; break; case PolicePathMovingEnum.ReturnToTheRoad: speedAdjustment = SpeedAdjustmentEnum.Stop; break; default: break; } } break; case PolicePathMovingEnum.None: { if (car.Key.transform.position.z > transform.position.z) //если машина, которая осталась в поле зрения спереди { if (car.Value.policePathMoving == PolicePathMovingEnum.ExitFromTheRoad) { PolicePath detectedCarPolicePath = car.Value.CurrentPath as PolicePath; if (car.Key.transform.position.z > detectedCarPolicePath.GetControlPoint(detectedCarPolicePath.ChangePointIndex + 3).z) { speedAdjustment = SpeedAdjustmentEnum.Increase; } } } break; } } //Debug.Log(gameObject.name + "=>" + car.Key.name + " нахожусь в личном пространстве машины: " + car.Key.name + // "\nМоё состояние на полицейском пути: " + policePathMoving + // "\nМоя настройка скорости: " + speedAdjustment); } }
/// <summary> /// Detects other machines and takes action in this regard /// </summary> /// <param name="other"></param> private void OnTriggerEnter(Collider other) { if (other.tag == PoliceCarTag) { return; } GameObject detectedCar = other.gameObject.transform.parent.gameObject; CarMovement detectedCarMovement = detectedCar.GetComponent <CarMovement>(); detectedCars.Add(detectedCar, detectedCarMovement); if (detectedCar.transform.position.z < transform.position.z)//личное пространство замеченной машины сзади { if (policePathMoving == PolicePathMovingEnum.Starting) { switch (detectedCarMovement.policePathMoving) { case PolicePathMovingEnum.None: speedAdjustment = SpeedAdjustmentEnum.Stop; break; case PolicePathMovingEnum.ReturnToTheRoad: speedAdjustment = SpeedAdjustmentEnum.Stop; break; } } //Debug.Log(gameObject.name + "=>" + detectedCar.name + " вижу личное пространство машины сзади: " + detectedCar.name + // "\nМоё состояние на полицейском пути: " + policePathMoving + // "\nМоя настройка скорости после обнаружения: " + speedAdjustment); } else if (detectedCar.transform.position.z > transform.position.z) //личное пространство замеченной машины спереди { switch (policePathMoving) { case PolicePathMovingEnum.None: switch (detectedCarMovement.policePathMoving) { case PolicePathMovingEnum.None: speedAdjustment = SpeedAdjustmentEnum.Decrease; break; case PolicePathMovingEnum.SpeedReduction: speedAdjustment = SpeedAdjustmentEnum.Decrease; break; case PolicePathMovingEnum.ReturnToTheRoad: speedAdjustment = SpeedAdjustmentEnum.Decrease; break; } break; case PolicePathMovingEnum.Starting: switch (detectedCarMovement.policePathMoving) { case PolicePathMovingEnum.None: speedAdjustment = SpeedAdjustmentEnum.Stop; break; case PolicePathMovingEnum.SpeedReduction: speedAdjustment = SpeedAdjustmentEnum.Stop; break; case PolicePathMovingEnum.ReturnToTheRoad: speedAdjustment = SpeedAdjustmentEnum.Stop; break; } break; case PolicePathMovingEnum.ReturnToTheRoad: switch (detectedCarMovement.policePathMoving) { case PolicePathMovingEnum.None: speedAdjustment = SpeedAdjustmentEnum.Decrease; break; case PolicePathMovingEnum.SpeedReduction: speedAdjustment = SpeedAdjustmentEnum.Decrease; break; case PolicePathMovingEnum.ReturnToTheRoad: speedAdjustment = SpeedAdjustmentEnum.Decrease; break; } break; } //Debug.Log(gameObject.name + "=>" + detectedCar.name + " вижу личное пространство машины спереди: " + detectedCar.name + // "\nМоё состояние на полицейском пути: " + policePathMoving + // "\nМоя настройка скорости после обнаружения: " + speedAdjustment); } //Debug.Log(gameObject.name + "=>" + " текущее кол-во машин в поле зрения: " + detectedCars.Count); }