public void GetNextLane() { // TODO move to method sampled earlier? if (currentMapLaneSegmentBuilder?.stopLine != null) // check if stopline is connected to current path { prevMapLaneSegmentBuilder = currentMapLaneSegmentBuilder; if (currentMapLaneSegmentBuilder.stopLine.mapIntersectionBuilder != null) // null if map not setup right TODO add check to report { if (currentMapLaneSegmentBuilder.stopLine.mapIntersectionBuilder.isStopSign) // stop sign { StartCoroutine(WaitStopSign()); } else if (currentMapLaneSegmentBuilder.stopLine.currentState == TrafficLightSetState.Red) // traffic light { StartCoroutine(WaitTrafficLight()); } } } if (currentMapLaneSegmentBuilder?.nextConnectedLanes.Count >= 1) // choose next path and set waypoints { currentMapLaneSegmentBuilder = currentMapLaneSegmentBuilder.nextConnectedLanes[(int)Random.Range(0, currentMapLaneSegmentBuilder.nextConnectedLanes.Count)]; SetLaneData(currentMapLaneSegmentBuilder.segment.targetWorldPositions); } else // issue getting new waypoints so despawn { Despawn(); } }
public MapLaneSegmentBuilder GetClosestLane(Vector3 position, out float distance) { MapLaneSegmentBuilder result = null; float minDist = float.PositiveInfinity; // TODO: this should be optimized foreach (var seg in spawnLaneBldrs) { var segment = seg.segment; if (segment.targetWorldPositions.Count >= 2) { for (int i = 0; i < segment.targetWorldPositions.Count - 1; i++) { var p0 = segment.targetWorldPositions[i]; var p1 = segment.targetWorldPositions[i + 1]; float d = SqrDistanceToSegment(p0, p1, position); if (d < minDist) { minDist = d; result = seg; } } } } distance = minDist; return(result); }
private void ResetData() { StopAllCoroutines(); currentMapLaneSegmentBuilder = null; currentIntersectionComponent = null; if (prevMapLaneSegmentBuilder?.stopLine?.mapIntersectionBuilder != null) { prevMapLaneSegmentBuilder.stopLine.mapIntersectionBuilder.ExitStopSignQueue(this); } prevMapLaneSegmentBuilder = null; currentNPCLightState = NPCLightStateTypes.Off; allRenderers.ForEach(x => SetNPCLightRenderers(x)); currentSpeed = 0f; currentStopTime = 0f; path = 0f; currentSpeed_measured = 0f; rb.angularVelocity = Vector3.zero; rb.velocity = Vector3.zero; isCurve = false; isLeftTurn = false; isRightTurn = false; isStopLight = false; isStopSign = false; hasReachedStopSign = false; isLaneDataSet = false; }
public void setStartLane(MapLaneSegmentBuilder 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 void InitLaneData(MapLaneSegmentBuilder seg) { ResetData(); normalSpeed = Random.Range(normalSpeedRange.x, normalSpeedRange.y); currentMapLaneSegmentBuilder = seg; SetLaneData(currentMapLaneSegmentBuilder.segment.targetWorldPositions); isLaneDataSet = true; }
private void ProcessSpawnLaneData() { foreach (Transform child in spawnLanesHolder) { tempLane = child.GetComponent <MapLaneSegmentBuilder>(); if (tempLane != null) { spawnLaneBldrs.Add(tempLane); } } }
public void SetLaneData(MapLaneSegmentBuilder seg) { currentMapLaneSegmentBuilder = seg; SetLaneData(currentMapLaneSegmentBuilder.segment.targetWorldPositions); currentSpeed = 0f; currentSpeed_measured = currentSpeed; normalSpeed = Random.Range(normalSpeedRange.x, normalSpeedRange.y); targetSpeed = normalSpeed; rb.angularVelocity = Vector3.zero; rb.velocity = Vector3.zero; isLaneDataSet = true; }
private void Despawn() { StopAllCoroutines(); isLaneDataSet = false; isStop = false; if (prevMapLaneSegmentBuilder?.stopLine?.mapIntersectionBuilder != null) { prevMapLaneSegmentBuilder.stopLine.mapIntersectionBuilder.ExitQueue(this); } currentMapLaneSegmentBuilder = null; prevMapLaneSegmentBuilder = null; NPCManager.Instance.DespawnNPC(gameObject); }
private void EvaluateTarget() { distanceToCurrentTarget = Vector3.Distance(new Vector3(frontCenter.position.x, 0f, frontCenter.position.z), new Vector3(currentTarget.x, 0f, currentTarget.z)); distanceToStopTarget = Vector3.Distance(new Vector3(frontCenter.position.x, 0f, frontCenter.position.z), new Vector3(stopTarget.x, 0f, stopTarget.z)); if (Vector3.Dot(frontCenter.forward, (currentTarget - frontCenter.position).normalized) < 0 || distanceToCurrentTarget < 1f) { if (currentIndex == laneData.Count - 2) // reached 2nd to last target index see if stop line is present { if (currentMapLaneSegmentBuilder?.stopLine != null) // check if stopline is connected to current path { currentIntersectionComponent = currentMapLaneSegmentBuilder.stopLine?.mapIntersectionBuilder?.intersectionC; stopTarget = currentMapLaneSegmentBuilder.segment.targetWorldPositions[currentMapLaneSegmentBuilder.segment.targetWorldPositions.Count - 1]; prevMapLaneSegmentBuilder = currentMapLaneSegmentBuilder; if (prevMapLaneSegmentBuilder.stopLine.mapIntersectionBuilder != null) // null if map not setup right TODO add check to report missing stopline { if (prevMapLaneSegmentBuilder.stopLine.mapIntersectionBuilder.isStopSign) // stop sign { StartCoroutine(WaitStopSign()); } else { StartCoroutine(WaitTrafficLight()); } } GetTurnSignal(); } } if (currentIndex < laneData.Count - 2) { currentIndex++; currentTarget = laneData[currentIndex]; CurrentPath[0] = CurrentPath[1]; CurrentPath[1] = CurrentPath[2]; CurrentPath[2] = laneData[currentIndex + 1]; } else if (currentIndex < laneData.Count - 1) // reached target dist and is not at last index of lane data { currentIndex++; currentTarget = laneData[currentIndex]; CurrentPath[0] = CurrentPath[1]; CurrentPath[1] = CurrentPath[2]; CurrentPath[2] = CurrentPath[2]; } else { GetNextLane(); } } }
private void GetNextLane() { // last index of current lane data if (currentMapLaneSegmentBuilder?.nextConnectedLanes.Count >= 1) // choose next path and set waypoints { currentMapLaneSegmentBuilder = currentMapLaneSegmentBuilder.nextConnectedLanes[(int)Random.Range(0, currentMapLaneSegmentBuilder.nextConnectedLanes.Count)]; SetLaneData(currentMapLaneSegmentBuilder.segment.targetWorldPositions); GetTurnSignal(); } 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[(int)UnityEngine.Random.Range(0, this.currentLane.nextConnectedLanes.Count)]; fetchWaypointsFromLane(); } else { Debug.LogError("currentLane is not set for the waypoint queue."); } } return(this.Q.Dequeue()); }
public void GetNextLane() { // last index of current lane data if (currentMapLaneSegmentBuilder?.nextConnectedLanes.Count >= 1) // choose next path and set waypoints { MapLaneSegmentBuilder tempMSB = currentMapLaneSegmentBuilder; currentMapLaneSegmentBuilder = currentMapLaneSegmentBuilder.nextConnectedLanes[(int)Random.Range(0, currentMapLaneSegmentBuilder.nextConnectedLanes.Count)]; SetLaneData(currentMapLaneSegmentBuilder.segment.targetWorldPositions); if (tempMSB.stopLine != null) { path = transform.InverseTransformPoint(currentMapLaneSegmentBuilder.segment.targetWorldPositions[currentMapLaneSegmentBuilder.segment.targetWorldPositions.Count - 1]).x; if (path < -1f) { isLeftTurn = true; isRightTurn = false; } else if (path > 1f) { isLeftTurn = false; isRightTurn = true; } else { isLeftTurn = false; isRightTurn = false; } } else { isLeftTurn = false; isRightTurn = false; } } else // issue getting new waypoints so despawn { // TODO raycast to see adjacent lanes? Need system Despawn(); } }
private void SetNPCOnMap() { for (int i = 0; i < currentPooledNPCs.Count; i++) { if (!currentPooledNPCs[i].activeInHierarchy) { seg = MapManager.Instance.GetRandomLane(); if (seg.segment.targetWorldPositions == null || seg.segment.targetWorldPositions.Count == 0) { continue; } if (seg.segment.targetWorldPositions.Count < 2) { continue; } var start = seg.segment.targetWorldPositions[0]; //var end = seg.segment.targetWorldPositions[seg.segment.targetWorldPositions.Count - 1]; //var estAvgPoint = (start + end) * 0.5f; if (IsPositionWithinSpawnArea(start)) // || IsPositionWithinSpawnArea(estAvgPoint) || IsPositionWithinSpawnArea(end)) { if (!Physics.CheckSphere(seg.segment.targetWorldPositions[0], checkRadius, NPCSpawnCheckBitmask)) { spawnPos = seg.segment.targetWorldPositions[0]; currentPooledNPCs[i].transform.position = spawnPos; if (!IsVisible(currentPooledNPCs[i])) { currentPooledNPCs[i].GetComponent <NPCControllerComponent>().SetLaneData(seg); currentPooledNPCs[i].SetActive(true); currentPooledNPCs[i].transform.LookAt(seg.segment.targetWorldPositions[1]); // TODO check if index 1 is valid activeNPCCount++; } } } } } }
public void SetLaneData() { lanes = new List <MapLaneSegmentBuilder>(); lanes.AddRange(GetComponentsInChildren <MapLaneSegmentBuilder>()); lanesForward = new List <MapLaneSegmentBuilder>(); lanesReverse = new List <MapLaneSegmentBuilder>(); isOneWay = true; // for laneSections with branching lanes, all lanes should have at least 3 waypoints for (var i = 0; i < lanes.Count; i++) { var lane = lanes[i]; var idx = 1; // index to compute vector from lane to otherLane and distance between those two lanes var laneDir = (lane.segment.targetWorldPositions[1] - lane.segment.targetWorldPositions[0]).normalized; var minDistLeft = 50f; var minDistRight = 50f; for (var j = 0; j < lanes.Count; j++) { var otherLane = lanes[j]; if (lane == otherLane) { continue; } // Check if these two lanes have same directions by check the dist between 1st pos in lane and (the 1st and last pos in otherLane). var isSameDirection = true; var distFirstToFirst = Vector3.Distance(lane.segment.targetWorldPositions[0], otherLane.segment.targetWorldPositions[0]); var distFirstToLast = Vector3.Distance(lane.segment.targetWorldPositions[0], otherLane.segment.targetWorldPositions[otherLane.segment.targetWorldPositions.Count - 1]); if (distFirstToFirst > distFirstToLast) { isSameDirection = false; } if (isSameDirection) // same direction { var cross = Vector3.Cross(laneDir, (otherLane.segment.targetWorldPositions[idx] - lane.segment.targetWorldPositions[idx]).normalized).y; var dist = Mathf.RoundToInt(Vector3.Distance(lane.segment.targetWorldPositions[idx], otherLane.segment.targetWorldPositions[idx])); if (cross < 0) // otherLane is left of lane { if (dist < minDistLeft) // closest lane left of lane is otherLane { minDistLeft = dist; lane.leftForward = otherLane; } } else if (cross > 0) // otherLane is right of lane { if (dist < minDistRight) // closest lane right of lane is otherLane { minDistRight = dist; lane.rightForward = otherLane; } } if (!lanesForward.Contains(lane) && !lanesReverse.Contains(lane)) { lanesForward.Add(lane); } if (!lanesForward.Contains(otherLane) && !lanesReverse.Contains(otherLane)) { lanesForward.Add(otherLane); } } else // opposite direction { isOneWay = false; var cross = Vector3.Cross(laneDir, (otherLane.segment.targetWorldPositions[otherLane.segment.targetWorldPositions.Count - 1 - idx] - lane.segment.targetWorldPositions[idx]).normalized).y; var dist = Mathf.RoundToInt(Vector3.Distance(lane.segment.targetWorldPositions[idx], otherLane.segment.targetWorldPositions[otherLane.segment.targetWorldPositions.Count - 1 - idx])); if (cross < 0) // otherLane is left of lane { if (dist < minDistLeft) // closest lane left of lane is otherLane { minDistLeft = dist; lane.leftReverse = otherLane; } } else if (cross > 0) // otherLane is right of lane { if (dist < minDistRight) // closest lane right of lane is otherLane { minDistRight = dist; lane.rightReverse = otherLane; } } if (!lanesForward.Contains(lane) && !lanesReverse.Contains(lane)) { lanesForward.Add(lane); } if (!lanesReverse.Contains(otherLane) && !lanesForward.Contains(otherLane)) { lanesReverse.Add(otherLane); } } if (lane.leftForward != null) { lane.leftReverse = null; // null lane left reverse if not inside lane TODO right side } } } int wayCount = isOneWay ? 1 : 2; for (int i = 0; i < wayCount; i++) { MapLaneSegmentBuilder currentLane = null; List <MapLaneSegmentBuilder> edited = new List <MapLaneSegmentBuilder>(); List <MapLaneSegmentBuilder> currentLanes = i == 0 ? lanesForward : lanesReverse; foreach (var lane in currentLanes) { if (lane.rightForward == null) { currentLane = lane; lane.rightBoundType = HD.LaneBoundaryType.Types.Type.Curb; break; } } while (currentLane != null) { edited.Add(currentLane); currentLane.laneNumber = edited.Count; currentLane.laneCount = currentLanes.Count; currentLane = currentLane.leftForward; } // set left boundary type for the left most lane if (isOneWay) { edited[edited.Count - 1].leftBoundType = HD.LaneBoundaryType.Types.Type.Curb; } else { edited[edited.Count - 1].leftBoundType = HD.LaneBoundaryType.Types.Type.DoubleYellow; } } }
public void SetLaneData() { lanes = new List <MapLaneSegmentBuilder>(); lanes.AddRange(GetComponentsInChildren <MapLaneSegmentBuilder>()); lanesForward = new List <MapLaneSegmentBuilder>(); lanesReverse = new List <MapLaneSegmentBuilder>(); isOneWay = true; foreach (var lane in lanes) { var laneDir = (lane.segment.targetWorldPositions[0] - lane.segment.targetWorldPositions[1]).normalized; var minDistLeft = 50f; var minDistRight = 50f; foreach (var otherLane in lanes) { if (lane == otherLane) { continue; } var otherDir = (otherLane.segment.targetWorldPositions[0] - otherLane.segment.targetWorldPositions[1]).normalized; var dot = Mathf.RoundToInt(Vector3.Dot(laneDir, otherDir)); if (dot == 1) // same direction { var cross = Vector3.Cross(laneDir, (lane.segment.targetWorldPositions[0] - otherLane.segment.targetWorldPositions[0]).normalized).y; var dist = Mathf.RoundToInt(Vector3.Distance(lane.segment.targetWorldPositions[0], otherLane.segment.targetWorldPositions[0])); if (cross < 0) // otherLane is left of lane { if (dist < minDistLeft) // closest lane left of lane is otherLane { minDistLeft = dist; lane.leftForward = otherLane; } } else if (cross > 0) // otherLane is right of lane { if (dist < minDistRight) // closest lane right of lane is otherLane { minDistRight = dist; lane.rightForward = otherLane; } } if (!lanesForward.Contains(lane) && !lanesReverse.Contains(lane)) { lanesForward.Add(lane); } if (!lanesForward.Contains(otherLane) && !lanesReverse.Contains(otherLane)) { lanesForward.Add(otherLane); } } else if (dot == -1) // opposite direction { isOneWay = false; var cross = Vector3.Cross(laneDir, (lane.segment.targetWorldPositions[0] - otherLane.segment.targetWorldPositions[otherLane.segment.targetWorldPositions.Count - 1]).normalized).y; var dist = Mathf.RoundToInt(Vector3.Distance(lane.segment.targetWorldPositions[0], otherLane.segment.targetWorldPositions[otherLane.segment.targetWorldPositions.Count - 1])); if (cross < 0) // otherLane is left of lane { if (dist < minDistLeft) // closest lane left of lane is otherLane { minDistLeft = dist; lane.leftReverse = otherLane; } } else if (cross > 0) // otherLane is right of lane { if (dist < minDistRight) // closest lane right of lane is otherLane { minDistRight = dist; lane.rightReverse = otherLane; } } if (!lanesForward.Contains(lane) && !lanesReverse.Contains(lane)) { lanesForward.Add(lane); } if (!lanesReverse.Contains(otherLane) && !lanesForward.Contains(otherLane)) { lanesReverse.Add(otherLane); } } if (lane.leftForward != null) { lane.leftReverse = null; // null lane left reverse if not inside lane TODO right side } } } int wayCount = isOneWay ? 1 : 2; for (int i = 0; i < wayCount; i++) { MapLaneSegmentBuilder currentLane = null; List <MapLaneSegmentBuilder> edited = new List <MapLaneSegmentBuilder>(); List <MapLaneSegmentBuilder> currentLanes = i == 0 ? lanesForward : lanesReverse; foreach (var lane in currentLanes) { if (lane.rightForward == null) { currentLane = lane; break; } } while (currentLane != null) { edited.Add(currentLane); currentLane.laneNumber = edited.Count; currentLane.laneCount = currentLanes.Count; currentLane = currentLane.leftForward; } } }