public void InitLaneData(MapLane lane) { if (_ActiveBehaviour) { _ActiveBehaviour.InitLaneData(lane); } }
public void copyFromMapLane(MapLane lane) { this.MapWorldPositions.Clear(); this.MapWorldPositions.AddRange(lane.mapWorldPositions); this.LaneCount = lane.laneCount; this.LaneNumber = lane.laneNumber; }
void AddLane(string id, Lane lane) { var curveSegments = lane.central_curve.segment; var lanePoints = GetPointsFromCurve(curveSegments); lanePoints = DownSample(lanePoints); GameObject mapLaneObj = new GameObject("MapLane_" + id); MapLane mapLane = mapLaneObj.AddComponent <MapLane>(); mapLane.mapWorldPositions = ConvertUTMFromDouble3(lanePoints); UpdateObjPosAndLocalPos(mapLaneObj.transform, mapLane); // TODO set self reverse lanes // TODO set direction mapLane.leftLineBoundry = Id2LeftLineBoundary[id]; mapLane.rightLineBoundry = Id2RightLineBoundary[id]; MapLineTypeToBoundType(ref mapLane.leftBoundType, id, true); MapLineTypeToBoundType(ref mapLane.rightBoundType, id); mapLane.laneTurnType = (MapData.LaneTurnType)lane.turn; mapLane.speedLimit = (float)lane.speed_limit; var junctionId = lane.junction_id; if (junctionId != null) { MoveLaneToJunction(new List <string>() { id }, junctionId.id.ToString()); } Id2Lane[id] = mapLane; }
private void OnTriggerExit(Collider other) { if (other.gameObject.layer == LaneLayer) { CurrentMapLane = null; } }
// npc and api public MapLane GetClosestLane(Vector3 position) { MapLane result = null; float minDist = float.PositiveInfinity; // TODO: this should be optimized foreach (var lane in trafficLanes) { if (lane.mapWorldPositions.Count >= 2) { for (int i = 0; i < lane.mapWorldPositions.Count - 1; i++) { var p0 = lane.mapWorldPositions[i]; var p1 = lane.mapWorldPositions[i + 1]; float d = Utility.SqrDistanceToSegment(p0, p1, position); if (d < minDist) { minDist = d; result = lane; } } } } return(result); }
private void GetCinematicFollowMapLane() { var lanesNear = new List <MapLane>(); for (int i = 0; i < SimulatorManager.Instance.MapManager.trafficLanes.Count; i++) { float dist = Vector3.Distance(SimulatorManager.Instance.MapManager.trafficLanes[i].mapWorldPositions[0], targetObject.position); if (dist < 50) { lanesNear.Add(SimulatorManager.Instance.MapManager.trafficLanes[i]); } } if (lanesNear.Count != 0) { var randIndex = Random.Range(0, lanesNear.Count); currentMapLane = lanesNear[randIndex]; cinematicStart = currentMapLane.mapWorldPositions[0]; cinematicStart += cinematicOffset; cinematicEnd = currentMapLane.mapWorldPositions[currentMapLane.mapWorldPositions.Count - 1]; cinematicEnd += cinematicOffset; transform.position = cinematicStart; } else { RandomCinematicState(); // no close lanes just use a different cinematic state } }
private void OnTriggerStay(Collider other) { if (other.gameObject.layer == LaneLayer) { CurrentMapLane = other.GetComponentInParent <MapLane>(); } }
public void ForceLaneChange(bool isLeft) { if (isLeft) { if (currentMapLane.leftLaneForward != null) { if (!isFrontLeftDetect) { currentMapLane = currentMapLane.leftLaneForward; laneSpeedLimit = currentMapLane.speedLimit; aggressionAdjustRate = laneSpeedLimit / 11.176f; // 11.176 m/s corresponds to 25 mph SetChangeLaneData(currentMapLane.mapWorldPositions); controller.Coroutines.Add(FixedUpdateManager.StartCoroutine(DelayOffTurnSignals())); ApiManager.Instance?.AddLaneChange(gameObject); } } } else { if (currentMapLane.rightLaneForward != null) { if (!isFrontRightDetect) { currentMapLane = currentMapLane.rightLaneForward; laneSpeedLimit = currentMapLane.speedLimit; aggressionAdjustRate = laneSpeedLimit / 11.176f; // 11.176 m/s corresponds to 25 mph SetChangeLaneData(currentMapLane.mapWorldPositions); controller.Coroutines.Add(FixedUpdateManager.StartCoroutine(DelayOffTurnSignals())); ApiManager.Instance?.AddLaneChange(gameObject); } } } }
protected void SetLaneChange() { if (currentMapLane == null) // Prevent null if despawned during wait { return; } ApiManager.Instance?.AddLaneChange(gameObject); if (currentMapLane.leftLaneForward != null) { if (!isFrontLeftDetect) { currentMapLane = currentMapLane.leftLaneForward; laneSpeedLimit = currentMapLane.speedLimit; aggressionAdjustRate = laneSpeedLimit / 11.176f; // 11.176 m/s corresponds to 25 mph SetChangeLaneData(currentMapLane.mapWorldPositions); controller.Coroutines.Add(FixedUpdateManager.StartCoroutine(DelayOffTurnSignals())); } } else if (currentMapLane.rightLaneForward != null) { if (!isFrontRightDetect) { currentMapLane = currentMapLane.rightLaneForward; laneSpeedLimit = currentMapLane.speedLimit; aggressionAdjustRate = laneSpeedLimit / 11.176f; // 11.176 m/s corresponds to 25 mph SetChangeLaneData(currentMapLane.mapWorldPositions); controller.Coroutines.Add(FixedUpdateManager.StartCoroutine(DelayOffTurnSignals())); } } }
protected void ResetData() { controller.StopNPCCoroutines(); currentMapLane = null; laneSpeedLimit = 0f; foreach (var intersection in SimulatorManager.Instance.MapManager.intersections) { intersection.ExitStopSignQueue(controller); intersection.ExitIntersectionList(controller); } prevMapLane = null; controller.ResetLights(); currentSpeed = 0f; currentStopTime = 0f; path = 0f; rb.angularVelocity = Vector3.zero; rb.velocity = Vector3.zero; isCurve = false; isLeftTurn = false; isRightTurn = false; isWaitingToDodge = false; isDodge = false; laneChange = true; isStopLight = false; isStopSign = false; hasReachedStopSign = false; isLaneDataSet = false; isForcedStop = false; controller.SetLastPosRot(transform.position, transform.rotation); }
public void setStartLane(MapLane lane) { this.Q.Clear(); // ensure there is nothing left over in the queue (this is the FIRST lane) this.currentLane = lane; this.previousLane = lane; // to avoid having a null previousLane at the start. firstLaneSegmentInit(); fetchStopTargetFromLane(); }
public override void OnInspectorGUI() { DrawDefaultInspector(); MapLane mapLane = (MapLane)target; EditorGUILayout.LabelField("", GUI.skin.horizontalSlider); if (GUILayout.Button("Reverse Lane")) { Undo.RecordObject(mapLane, "change builder"); mapLane.ReversePoints(); } }
protected virtual void OnSceneGUI() { MapLane vmMapLane = (MapLane)target; if (vmMapLane.mapLocalPositions.Count < 1) { return; } if (vmMapLane.displayHandles) { for (int i = 0; i < vmMapLane.mapLocalPositions.Count; i++) { EditorGUI.BeginChangeCheck(); Vector3 newTargetPosition = Handles.PositionHandle(vmMapLane.transform.TransformPoint(vmMapLane.mapLocalPositions[i]), vmMapLane.transform.rotation); if (EditorGUI.EndChangeCheck()) { Undo.RecordObject(vmMapLane, "Line points change"); vmMapLane.mapLocalPositions[i] = vmMapLane.transform.InverseTransformPoint(newTargetPosition); } } } if (vmMapLane.displayLane) { var left = new Vector3[vmMapLane.mapLocalPositions.Count]; var right = new Vector3[vmMapLane.mapLocalPositions.Count]; var halfLaneWidth = vmMapLane.displayLaneWidth / 2; var laneTransform = vmMapLane.transform; var a = laneTransform.TransformPoint(vmMapLane.mapLocalPositions[0]); for (int i = 1; i < vmMapLane.mapLocalPositions.Count; i++) { var b = laneTransform.TransformPoint(vmMapLane.mapLocalPositions[i]); left[i - 1] = (GetPerpPoint(a, b, halfLaneWidth)); right[i - 1] = (GetPerpPoint(a, b, -halfLaneWidth)); if (i == vmMapLane.mapLocalPositions.Count - 1) { left[i] = (GetPerpPoint(b, a, -halfLaneWidth)); right[i] = (GetPerpPoint(b, a, halfLaneWidth)); } a = b; } Handles.DrawAAPolyLine(left); Handles.DrawAAPolyLine(right); } }
public override void InitLaneData(MapLane lane) { ResetData(); laneSpeedLimit = lane.speedLimit; if (laneSpeedLimit > 0) { aggressionAdjustRate = laneSpeedLimit / 11.176f; // give more space at faster speeds stopHitDistance = 12 / aggression * aggressionAdjustRate; } normalSpeed = RandomGenerator.NextFloat(laneSpeedLimit - 3 + aggression, laneSpeedLimit + 1 + aggression); currentMapLane = lane; SetLaneData(currentMapLane.mapWorldPositions); controller.SetLastPosRot(transform.position, transform.rotation); isLaneDataSet = true; }
private void GetNextCinematicMapLane() { if (currentMapLane.nextConnectedLanes == null || currentMapLane.nextConnectedLanes.Count == 0) { currentMapLane = null; return; } int rand = UnityEngine.Random.Range(0, currentMapLane.nextConnectedLanes.Count - 1); currentMapLane = currentMapLane.nextConnectedLanes[rand]; cinematicStart = currentMapLane.mapWorldPositions[0]; cinematicStart += cinematicOffset; cinematicEnd = currentMapLane.mapWorldPositions[currentMapLane.mapWorldPositions.Count - 1]; cinematicEnd += cinematicOffset; transform.position = cinematicStart; }
// Make current lane's start/end point same as predecessor/successor lane's end/start point void AdjustStartOrEndPoint(List <Vector3> positions, string connectLaneId, bool adjustEndPoint) { MapLane connectLane = Id2Lane[connectLaneId]; var connectLaneWorldPositions = connectLane.mapWorldPositions; var connectLaneLocalPositions = connectLane.mapLocalPositions; if (adjustEndPoint) { connectLaneWorldPositions[connectLaneWorldPositions.Count - 1] = positions.First(); connectLaneLocalPositions[connectLaneLocalPositions.Count - 1] = connectLane.transform.InverseTransformPoint(positions.First()); } else { connectLaneWorldPositions[0] = positions.Last(); connectLaneLocalPositions[0] = connectLane.transform.InverseTransformPoint(positions.Last()); } }
protected void GetNextLane() { // last index of current lane data if (currentMapLane?.nextConnectedLanes.Count >= 1) // choose next path and set waypoints { currentMapLane = currentMapLane.nextConnectedLanes[RandomGenerator.Next(currentMapLane.nextConnectedLanes.Count)]; laneSpeedLimit = currentMapLane.speedLimit; aggressionAdjustRate = laneSpeedLimit / 11.176f; // 11.176 m/s corresponds to 25 mph normalSpeed = RandomGenerator.NextFloat(laneSpeedLimit - 3 + aggression, laneSpeedLimit + 1 + aggression); SetLaneData(currentMapLane.mapWorldPositions); SetTurnSignal(); } else // issue getting new waypoints so despawn { // TODO raycast to see adjacent lanes? Need system Despawn(); } }
public Vector3 Dequeue() { if (this.Q.Count == 0) { if (this.currentLane) { // enqueue waypoints from next lane, then dequeue this.previousLane = this.currentLane; // fetch next lane this.currentLane = this.currentLane.nextConnectedLanes[RandomGenerator.Next(this.currentLane.nextConnectedLanes.Count)]; fetchWaypointsFromLane(); } else { Debug.LogError("currentLane is not set for the waypoint queue."); } } return(this.Q.Dequeue()); }
private void RandomCinematicState() { SimulatorManager.Instance?.UIManager?.FadeOutIn(1f); currentMapLane = null; thisCamera.transform.localRotation = Quaternion.identity; thisCamera.transform.localPosition = Vector3.zero; var temp = (CinematicStateType)UnityEngine.Random.Range(0, System.Enum.GetValues(typeof(CinematicStateType)).Length); while (temp == CurrentCinematicState) { temp = (CinematicStateType)UnityEngine.Random.Range(0, System.Enum.GetValues(typeof(CinematicStateType)).Length); } CurrentCinematicState = temp; switch (CurrentCinematicState) { case CinematicStateType.Static: transform.SetParent(SimulatorManager.Instance?.CameraManager.transform); transform.position = (UnityEngine.Random.insideUnitSphere * 20) + targetObject.transform.position; transform.position = new Vector3(transform.position.x, targetObject.position.y + 10f, transform.position.z); break; case CinematicStateType.Follow: transform.SetParent(SimulatorManager.Instance?.CameraManager.transform); GetCinematicFollowMapLane(); break; case CinematicStateType.Rotate: transform.position = targetObject.position + offset; transform.rotation = targetObject.transform.rotation; transform.SetParent(targetObject); break; case CinematicStateType.Stuck: var rando = Random.Range(0, cinematicCameraTransforms.Count); transform.position = cinematicCameraTransforms[rando].transform.position; transform.rotation = cinematicCameraTransforms[rando].transform.rotation; transform.SetParent(cinematicCameraTransforms[rando].transform); break; } }
void StartStoppingCoroutine() { if (currentMapLane?.stopLine != null) // check if stopline is connected to current path { controller.currentIntersection = currentMapLane.stopLine?.intersection; stopTarget = currentMapLane.mapWorldPositions[currentMapLane.mapWorldPositions.Count - 1]; prevMapLane = currentMapLane; if (prevMapLane.stopLine.intersection != null) // null if map not setup right TODO add check to report missing stopline { if (prevMapLane.stopLine.isStopSign) // stop sign { controller.Coroutines.Add(FixedUpdateManager.StartCoroutine(WaitStopSign())); } else { controller.Coroutines.Add(FixedUpdateManager.StartCoroutine(WaitTrafficLight())); } } } }
private void CheckFollow() { if (Vector3.Distance(transform.position, cinematicEnd) < 0.001f) { if (currentMapLane.nextConnectedLanes == null || currentMapLane.nextConnectedLanes.Count == 0) { RandomCinematicState(); } else { int rand = UnityEngine.Random.Range(0, currentMapLane.nextConnectedLanes.Count - 1); currentMapLane = currentMapLane.nextConnectedLanes[rand]; cinematicStart = currentMapLane.mapWorldPositions[0]; cinematicStart += cinematicOffset; cinematicEnd = currentMapLane.mapWorldPositions[currentMapLane.mapWorldPositions.Count - 1]; cinematicEnd += cinematicOffset; transform.position = cinematicStart; } } }
private void CacheLane(MapLane lane) { if (lane.DenySpawn) { return; } if (lane.mapWorldPositions.Count < 2) { return; } var spawnPoint = new SpawnPoint() { position = lane.mapWorldPositions[0], spawnIndex = SpawnPoints.Count, lookAtPoint = lane.mapWorldPositions[1], lane = lane }; SpawnPoints.Add(spawnPoint); }
private void GetStartCinematicMapLane() { for (int i = 0; i < SimulatorManager.Instance.MapManager.trafficLanes.Count; i++) { int rand = UnityEngine.Random.Range(0, SimulatorManager.Instance.MapManager.trafficLanes.Count); float dist = Vector3.Distance(SimulatorManager.Instance.MapManager.trafficLanes[rand].mapWorldPositions[0], targetObject.position); if (SimulatorManager.Instance.MapManager.trafficLanes[rand].Spawnable) { currentMapLane = SimulatorManager.Instance.MapManager.trafficLanes[rand]; cinematicStart = currentMapLane.mapWorldPositions[0]; cinematicStart += cinematicOffset; cinematicEnd = currentMapLane.mapWorldPositions[currentMapLane.mapWorldPositions.Count - 1]; cinematicEnd += cinematicOffset; transform.position = cinematicStart; if (dist < 100f) { break; } } } }
public int GetLaneNextIndex(Vector3 position, MapLane lane) { float minDist = float.PositiveInfinity; int index = -1; for (int i = 0; i < lane.mapWorldPositions.Count - 1; i++) { var p0 = lane.mapWorldPositions[i]; var p1 = lane.mapWorldPositions[i + 1]; var p = Utility.ClosetPointOnSegment(p0, p1, position); float d = Vector3.SqrMagnitude(position - p); if (d < minDist) { minDist = d; index = i + 1; } } return(index); }
public override void OnInspectorGUI() { DrawDefaultInspector(); MapLane mapLane = (MapLane)target; if (mapLane.leftLineBoundry != null) { mapLane.leftLineBoundry.lineType = (MapData.LineType)EditorGUILayout.EnumPopup("Left Boundary Type: ", mapLane.leftLineBoundry?.lineType); } if (mapLane.rightLineBoundry != null) { mapLane.rightLineBoundry.lineType = (MapData.LineType)EditorGUILayout.EnumPopup("Right Boundary Type: ", mapLane.rightLineBoundry?.lineType); } EditorGUILayout.LabelField("", GUI.skin.horizontalSlider); if (GUILayout.Button("Reverse Lane")) { Undo.RecordObject(mapLane, "change builder"); mapLane.ReversePoints(); } }
private void Start() { LaneLayer = LayerMask.NameToLayer("Lane"); CurrentMapLane = null; }
private void OnDisable() { CurrentMapLane = null; }
public override void InitLaneData(MapLane lane) { // Maplane data void in waypoint behavior }
public override void InitLaneData(MapLane lane) { }