private void ShowRoadElement(RoadSettings element) { EditorGUILayout.BeginVertical("box"); { RoadType saveKind = element.Type; element.Type = (RoadType)EditorGUILayout.EnumPopup("Type Road:", element.Type); if (GUI.changed && saveKind != element.Type) { if (UsingType.Contains("rt_" + saveKind.ToString())) { UsingType.Remove("rt_" + saveKind.ToString()); } UsingType.Add("rt_" + element.Type.ToString()); } if (element.Type == RoadType.Rail) { element.TypeRail = (RailwayType)EditorGUILayout.EnumPopup("Type Rail:", element.TypeRail); } element.Material = (Material)EditorGUILayout.ObjectField("Material", element.Material, typeof(Material)); if (element.Material == null) { DisplayErrorMEssage("Not setting material"); } element.Width = EditorGUILayout.FloatField("Road Width", element.Width); } EditorGUILayout.EndVertical(); }
void AddNewPath() { Camera cam = Camera.current; if (cam == null) { cam = SceneView.lastActiveSceneView.camera; } RoadSettings me = target as RoadSettings; Vector3 spawnPos = cam.ViewportToWorldPoint(new Vector3(0.5f, 0.5f, 32f)); GameObject go = new GameObject("Road"); go.transform.position = spawnPos; go.transform.rotation = cam.transform.rotation; go.transform.parent = me.transform; go.AddComponent <Path>(); Selection.activeGameObject = go; Undo.RegisterCreatedObjectUndo(go, "Create New Traffic Path"); }
public void createPath() { roadPath = new RoadPath(transform.position); roadSettings = new RoadSettings(); meshCreator = new RoadMeshCreator(); }
protected RoadSettings createRoadSettings(RoadType type, int width, string material) { var rs = new RoadSettings(); rs.Type = type; rs.Material = (Material)Resources.Load(material, typeof(Material)); rs.Width = width; return(rs); }
private void CreateMesh(List <Vector3> list, RoadSettings settings, MeshData md) { var vertsStartCount = md.Vertices.Count; Vector3 lastPos = Vector3.zero; var norm = Vector3.zero; for (int i = 1; i < list.Count; i++) { var p1 = list[i - 1]; var p2 = list[i]; var p3 = p2; if (i + 1 < list.Count) { p3 = list[i + 1]; } if (lastPos == Vector3.zero) { lastPos = Vector3.Lerp(p1, p2, 0f); norm = GetNormal(p1, lastPos, p2) * settings.Width; md.Vertices.Add(lastPos + norm); md.Vertices.Add(lastPos - norm); } lastPos = Vector3.Lerp(p1, p2, 1f); norm = GetNormal(p1, lastPos, p3) * settings.Width; md.Vertices.Add(lastPos + norm); md.Vertices.Add(lastPos - norm); } for (int j = vertsStartCount; j <= md.Vertices.Count - 3; j += 2) { var clock = Vector3.Cross(md.Vertices[j + 1] - md.Vertices[j], md.Vertices[j + 2] - md.Vertices[j + 1]); if (clock.y < 0) { md.Indices.Add(j); md.Indices.Add(j + 2); md.Indices.Add(j + 1); md.Indices.Add(j + 1); md.Indices.Add(j + 2); md.Indices.Add(j + 3); } else { md.Indices.Add(j + 1); md.Indices.Add(j + 2); md.Indices.Add(j); md.Indices.Add(j + 3); md.Indices.Add(j + 2); md.Indices.Add(j + 1); } } }
Queue <Pool <GateSpace.Gate> > _gatePools; //road will pick up random gate from pool public RoadSpawner(RoadSettings settings) { _blocksOnLevel = settings.RoadBlocksOnLevel; _startRoadPref = settings.StartingRoad; _roadBlockPref = settings.RoadBlock; _finishBlockPref = settings.FinishBlock; _numStartSpawn = settings.NumStartSpawn; _blockLength = _roadBlockPref.GetComponent <Renderer>().bounds.size.x; _roadPool = new Pool <Road>(new PrefabFactory <Road>(_roadBlockPref.gameObject)); }
public void OnEnable() { editingSettings = (RoadSettings)target; }
protected override void OnUpdate() { Entities.ForEach((Path path) => { var entity = GetPrimaryEntity(path); int numPathNodes = path.GetNumNodes(); PathMap.Add(path.GetInstanceID(), RIndex); RoadSettings roadSettings = path.gameObject.GetComponentInParent <RoadSettings>(); uint spawnPool = 0; if (roadSettings != null) { spawnPool = roadSettings.vehicleSelection; } for (int n = 1; n < numPathNodes; n++) { var rs = new RoadSection(); path.GetSplineSection(n - 1, out rs.p0, out rs.p1, out rs.p2, out rs.p3); rs.arcLength = CatmullRom.ComputeArcLength(rs.p0, rs.p1, rs.p2, rs.p3, 1024); rs.vehicleHalfLen = Constants.VehicleLength / rs.arcLength; rs.vehicleHalfLen /= 2; rs.sortIndex = RIndex; rs.linkNext = RIndex + 1; if (n == numPathNodes - 1) { rs.linkNext = -1; MergeMap[math.round((path.GetReversibleRawPosition(n) + new float3(path.transform.position)) * Constants.NodePositionRounding) / Constants.NodePositionRounding] = RIndex; } rs.linkExtraChance = 0.0f; rs.linkExtra = -1; rs.width = path.width; rs.height = path.height; rs.minSpeed = path.minSpeed; rs.maxSpeed = path.maxSpeed; rs.occupationLimit = math.min(Constants.RoadOccupationSlotsMax, (int)math.round(rs.arcLength / Constants.VehicleLength)); var sectionEnt = CreateAdditionalEntity(path); IdxMap[RIndex] = sectionEnt; if (!path.isOnRamp) { RampMap[math.round((path.GetReversibleRawPosition(n - 1) + new float3(path.transform.position)) * Constants.NodePositionRounding) / Constants.NodePositionRounding] = RIndex; if (n == 1) { int x = 2; // Only spawn in right lane float t = rs.vehicleHalfLen; float pathTime = (n - 1) + t; var spawner = new Spawner(); spawner.Time = math.frac(pathTime); spawner.Direction = math.normalize(path.GetTangent(pathTime)); float3 rightPos = (x - 1) * math.mul(spawner.Direction, Vector3.right) * ((rs.width - Constants.VehicleWidth) / 2.0f); spawner.Position = path.GetWorldPosition(pathTime) + rightPos; spawner.RoadIndex = RIndex; spawner.minSpeed = path.minSpeed; spawner.maxSpeed = path.maxSpeed; var speedInverse = 1.0f / spawner.minSpeed; spawner.random = new Unity.Mathematics.Random((uint)RIndex + 1); spawner.delaySpawn = (int)Constants.VehicleLength + spawner.random.NextInt((int)(speedInverse * 60.0f), (int)(speedInverse * 120.0f)); spawner.LaneIndex = x; spawner.poolSpawn = spawnPool; // Each path will only have one spawner, so use the Primary Entity DstEntityManager.AddComponentData(entity, spawner); } } RIndex++; DstEntityManager.AddComponentData(sectionEnt, rs); } }); // Loop over the paths again to start building the ramps and merges Entities.ForEach((Path path) => { int numPathNodes = path.GetNumNodes(); // Handle On Ramp roads if (path.isOnRamp) { int rsRampIdx = PathMap[path.GetInstanceID()]; float3 rampEntry = math.round((path.GetReversibleRawPosition(0) + new float3(path.transform.position)) * Constants.NodePositionRounding) / Constants.NodePositionRounding; if (RampMap.ContainsKey(rampEntry)) { int rsIndex = RampMap[rampEntry]; if (rsIndex > 0) { rsIndex -= 1; if (IdxMap.ContainsKey(rsIndex)) { Entity ramp = IdxMap[rsIndex]; RoadSection rs = DstEntityManager.GetComponentData <RoadSection>(ramp); if (rs.linkNext == rsIndex + 1) { rs.linkExtra = rsRampIdx; rs.linkExtraChance = path.percentageChanceForOnRamp / 100.0f; DstEntityManager.SetComponentData(ramp, rs); } } } } } // Handle merging roads { int n = 0; float3 pos = math.round((path.GetReversibleRawPosition(n) + new float3(path.transform.position)) * Constants.NodePositionRounding) / Constants.NodePositionRounding; if (MergeMap.ContainsKey(pos)) { int mergeFromIndex = MergeMap[pos]; int mergeToBase = PathMap[path.GetInstanceID()]; if (mergeFromIndex > 0 && mergeFromIndex != mergeToBase + n) { Entity merge = IdxMap[mergeFromIndex]; RoadSection rs = DstEntityManager.GetComponentData <RoadSection>(merge); if (rs.linkNext == -1) { rs.linkNext = mergeToBase + n; DstEntityManager.SetComponentData(merge, rs); } } } } }); RIndex = 0; PathMap.Clear(); RampMap.Clear(); MergeMap.Clear(); IdxMap.Clear(); }