public override void OnInspectorGUI() { splinePP = target as PolicePath; EditorGUI.BeginChangeCheck(); bool loop = EditorGUILayout.Toggle("Loop", splinePP.Loop); int changePointIndex = EditorGUILayout.IntField("Change Point Index", splinePP.ChangePointIndex); int stopPointIndex = EditorGUILayout.IntField("Stop Point Index", splinePP.StopPointIndex); if (EditorGUI.EndChangeCheck()) { Undo.RecordObject(splinePP, "Toggle Loop"); EditorUtility.SetDirty(splinePP); splinePP.Loop = loop; Undo.RecordObject(splinePP, "IntField Change Point Index"); EditorUtility.SetDirty(splinePP); splinePP.ChangePointIndex = changePointIndex; Undo.RecordObject(splinePP, "IntField Stop Point Index"); EditorUtility.SetDirty(splinePP); splinePP.StopPointIndex = stopPointIndex; } if (selectedIndex >= 0 && selectedIndex < splinePP.ControlPointCount) { DrawSelectedPointInspector(); } if (GUILayout.Button("Add Curve")) { Undo.RecordObject(splinePP, "Add Curve"); splinePP.AddCurve(); EditorUtility.SetDirty(splinePP); } }
/// <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> /// 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; } }
private new void OnSceneGUI() { splinePP = target as PolicePath; handleTransform = splinePP.transform; handleRotation = Tools.pivotRotation == PivotRotation.Local ? handleTransform.rotation : Quaternion.identity; Vector3 p0 = ShowPoint(0); for (int i = 1; i < splinePP.ControlPointCount; i += 3) { Vector3 p1 = ShowPoint(i); Vector3 p2 = ShowPoint(i + 1); Vector3 p3 = ShowPoint(i + 2); Handles.color = Color.gray; Handles.DrawLine(p0, p1); Handles.DrawLine(p2, p3); Handles.DrawBezier(p0, p3, p1, p2, Color.white, null, 2f); p0 = p3; } ShowDirections(); }
/// <summary> /// Changes police path statements (PolicePathMovingEnum) /// </summary> /// <param name="policePath"></param> private void PolicePathMovingStatements(PolicePath policePath) { if (!Stopped) { if (transform.position.z >= policePath.GetControlPoint(policePath.ChangePointIndex - 2).z) { if (transform.position.z >= policePath.GetControlPoint(policePath.ChangePointIndex).z) { if (policePathMoving == PolicePathMovingEnum.Stop) { return; } else { policePathMoving = PolicePathMovingEnum.ExitFromTheRoad; } } else { policePathMoving = PolicePathMovingEnum.SpeedReduction; } } } else { if (transform.position.z > policePath.GetControlPoint(policePath.StopPointIndex + 3).z) { if (didNotLeavePolicemanBefore) { policePath.Occupied = false; policePathMoving = PolicePathMovingEnum.ReturnToTheRoad; didNotLeavePolicemanBefore = false; } } } }
/// <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); } }