void SetupTrains() { // Add trains for (int i = 0; i < totalLines; i++) { if (metroLines[i] != null) { MetroLine _ML = metroLines[i]; float trainSpacing = 1f / _ML.maxTrains; for (int trainIndex = 0; trainIndex < _ML.maxTrains; trainIndex++) { _ML.AddTrain(trainIndex, trainIndex * trainSpacing); } } } // now tell each train who is ahead of them for (int i = 0; i < totalLines; i++) { if (metroLines[i] != null) { MetroLine _ML = metroLines[i]; for (int trainIndex = 0; trainIndex < _ML.maxTrains; trainIndex++) { Train _T = _ML.trains[trainIndex]; _T.trainAheadOfMe = _ML.trains[(trainIndex + 1) % _ML.maxTrains]; } } } }
private void OnDrawGizmos() { if (drawRailBeziers) { for (int i = 0; i < totalLines; i++) { MetroLine _tempLine = metroLines[i]; if (_tempLine != null) { BezierPath _path = _tempLine.bezierPath; if (_path != null) { for (int pointIndex = 0; pointIndex < _path.points.Count; pointIndex++) { BezierPoint _CURRENT_POINT = _path.points[pointIndex]; BezierPoint _NEXT_POINT = _path.points[(pointIndex + 1) % _path.points.Count]; // Link them up Handles.DrawBezier(_CURRENT_POINT.location, _NEXT_POINT.location, _CURRENT_POINT.handle_out, _NEXT_POINT.handle_in, GetLine_COLOUR_FromIndex(i), null, 3f); } } } } } }
Platform GetRandomPlatform() { int _LINE_INDEX = Random.Range(0, metroLines.Length - 1); MetroLine _LINE = metroLines[_LINE_INDEX]; int _PLATFORM_INDEX = Mathf.FloorToInt(Random.Range(0f, (float)_LINE.platforms.Count)); return(_LINE.platforms[_PLATFORM_INDEX]); }
/// <summary> /// 获取当前站点的下一个可直达特殊点 /// </summary> private HashSet <Station> GetNexts(Station current, Station start) { HashSet <Station> nexts = new HashSet <Station>(); foreach (int i in current.PassLineIds) { MetroLine line = _metro.MetroLines.Find((MetroLine l) => l.Id == i); int currentPosition = line.Stations.IndexOf(current); HashSet <int> specialPositionsSet = new HashSet <int>(); specialPositionsSet.Add(0); specialPositionsSet.Add(line.Stations.Count - 1); if (line.Stations.Contains(start)) { specialPositionsSet.Add(line.Stations.IndexOf(start)); } foreach (Station s in line.GetTransferStations()) { specialPositionsSet.Add(line.Stations.IndexOf(s)); } specialPositionsSet.Add(line.Stations.IndexOf(current)); // 排序 List <int> specialPositions = new List <int>(specialPositionsSet); specialPositions.Sort((a, b) => a > b ? 1 : -1); // 寻找下一个可达特殊点 for (int j = 0; j < specialPositions.Count; j++) { if (j == 0) { if (currentPosition == specialPositions[j] && j + 1 <= specialPositions.Count - 1) { nexts.Add(line.Stations[specialPositions[j + 1]]); } } else if (j == specialPositions.Count - 1) { if (currentPosition == specialPositions[j] && j - 1 >= 0) { nexts.Add(line.Stations[specialPositions[j - 1]]); } } else if (currentPosition > specialPositions[j - 1]) { if (currentPosition == specialPositions[j]) { nexts.Add(line.Stations[specialPositions[j - 1]]); nexts.Add(line.Stations[specialPositions[j + 1]]); } else if (currentPosition < specialPositions[j]) { nexts.Add(line.Stations[specialPositions[j - 1]]); nexts.Add(line.Stations[specialPositions[j]]); } } } } return(nexts); }
/// <summary> /// 构造函数。 /// </summary> /// <param name="line">所属线路。</param> /// <param name="from">来源站点。</param> /// <param name="to">目标站点。</param> /// <exception cref="ArgumentNullException">如果from或to为空引用,则抛出该异常。</exception> public MetroLink(MetroLine line, MetroNode from, MetroNode to) { if (line == null) throw new ArgumentNullException("line"); if (from == null) throw new ArgumentNullException("from"); if (to == null) throw new ArgumentNullException("to"); m_line = line; m_from = from; m_to = to; }
/// <summary> /// 获取指定两个线路的中转站。 /// </summary> /// <param name="line1">线路1。</param> /// <param name="line2">线路2。</param> /// <returns>中转站。</returns> /// <exception cref="ArgumentNullException">如果line1或line2为空引用,则抛出该异常。</exception> public IEnumerable<MetroNode> GetTransferNodes(MetroLine line1, MetroLine line2) { if (line1 == null) throw new ArgumentNullException("line1"); if (line2 == null) throw new ArgumentNullException("line2"); if (line1 == line2) yield break; foreach (var node in this.Nodes.Where(c => c.Links.Count > 2 && c.Links.Exists(k => k.Line == line1) && c.Links.Exists(k => k.Line == line2))) { yield return node; } }
public bool RouteisPossible(Platform _A, Platform _B) { MetroLine _lineA = _A.parentMetroLine; MetroLine _lineB = _B.parentMetroLine; if (_lineA == _lineB) { return(true); } return(_lineA.Has_ConnectionToMetroLine(_lineB)); }
public bool Has_ConnectionToMetroLine(MetroLine _targetLine) { foreach (Platform _P in platforms) { foreach (Platform _ADJ in _P.adjacentPlatforms) { if (_ADJ.parentMetroLine == _targetLine) { return(true); } } } return(false); }
public void SetupPlatform(MetroLine _parentMetroLine, BezierPoint _start, BezierPoint _end) { parentMetroLine = _parentMetroLine; point_platform_START = _start; point_platform_END = _end; carriageCount = parentMetroLine.carriagesPerTrain; adjacentPlatforms = new List <Platform>(); SetColour(); // setup queue lists and spacing platformQueues = new Queue <Commuter> [carriageCount]; for (int i = 0; i < carriageCount; i++) { platformQueues[i] = new Queue <Commuter>(); } Setup_Walkways(); }
public Train(int _trainIndex, int _parentLineIndex, float _startPosition, int _totalCarriages) { trainIndex = _trainIndex; parentLineIndex = _parentLineIndex; parentLine = Metro.INSTANCE.metroLines[parentLineIndex]; currentPosition = _startPosition; state = TrainState.EN_ROUTE; totalCarriages = _totalCarriages; SetupCarriages(); passengers = new List <Commuter>(); passengers_to_DISEMBARK = new List <Commuter>(); passengers_to_EMBARK = new List <Commuter>(); speed = 0f; accelerationStrength = Metro.INSTANCE.Train_accelerationStrength * parentLine.speedRatio; railFriction = Metro.INSTANCE.Train_railFriction; ChangeState(TrainState.DEPARTING); }
public MetroStation(int id, string tag, string title, MetroLine line) { new MetroStation(id, tag, title, new MetroLine[] { line }); }
public MetroStation(int id, string tag, string title, MetroLine[] lines) { Id = id; Tag = tag; Title = title; Lines = lines; }
// 两点之间寻路递归算法 private void FindPath(List <Path> result, int startLine, int endLine, Station startStation, Station endStation, List <Station> list, List <int> alreadyPassLines) { if (alreadyPassLines == null) { alreadyPassLines = new List <int>(); } if (startStation.Id == endStation.Id) { return; } // 到同一路线上了,将结果加到list中 if (startLine == endLine) { List <Station> temp = GetPathBetween(startStation, endStation, startLine); // 最后一次使用list了,直接用 if (list == null) // 为null表示一开始就是同路,直接赋值 { list = temp; } else // 不为null表示之前有换线前的数据,在末尾添加 { foreach (Station s in temp) { list.Add(s); } } list.Add(endStation); result.Add(new Path(list, alreadyPassLines)); } // 还没在同一条路线上,寻找下一个换乘点 alreadyPassLines.Add(startLine); // 当前路算已走过了,不要再回来了 MetroLine line = _metro.MetroLines.Find((MetroLine l) => { return(l.Id == startLine); }); foreach (Station s in line.GetTransferStations()) { foreach (int i in s.PassLineIds) { if (!alreadyPassLines.Contains(i)) { List <Station> path; List <Station> temp = GetPathBetween(startStation, s, startLine); // 因为每个循环都会用到之前的list,所以这里不能更改list,传变量path if (list == null) // 为null表示第一次,直接赋值 { path = temp; } else // 不为null表示之前有路径,添加到末尾 { path = new List <Station>(); foreach (Station st in list) { path.Add(st); } foreach (Station st in temp) { path.Add(st); } } List <int> tempAlreadyLines = new List <int>(); foreach (int j in alreadyPassLines) { tempAlreadyLines.Add(j); } FindPath(result, i, endLine, s, endStation, path, tempAlreadyLines); // 递归查找子路径 } } } }
void SetupMetroLines() { totalLines = LineNames.Length; metroLines = new MetroLine[totalLines]; for (int i = 0; i < totalLines; i++) { // Find all of the relevant RailMarkers in the scene for this line List <RailMarker> _relevantMarkers = FindObjectsOfType <RailMarker>().Where(m => m.metroLineID == i) .OrderBy(m => m.pointIndex).ToList(); // Only continue if we have something to work with if (_relevantMarkers.Count > 1) { MetroLine _newLine = new MetroLine(i, maxTrains[i]); _newLine.Create_RailPath(_relevantMarkers); metroLines[i] = _newLine; } else { Debug.LogWarning("Insufficient RailMarkers found for line: " + i + ", you need to add the outbound points"); } } // now destroy all RailMarkers foreach (RailMarker _RM in FindObjectsOfType <RailMarker>()) { Destroy(_RM); } allPlatforms = FindObjectsOfType <Platform>(); for (int i = 0; i < allPlatforms.Length; i++) { Platform _PA = allPlatforms[i]; Vector3 _PA_START = _PA.point_platform_START.location; Vector3 _PA_END = _PA.point_platform_END.location; foreach (Platform _PB in allPlatforms) { if (_PB != _PA) { Vector3 _PB_START = _PB.point_platform_START.location; Vector3 _PB_END = _PB.point_platform_END.location; bool aSTART_to_bSTART = Positions_Are_Adjacent(_PA_START, _PB_START); bool aSTART_to_bEND = Positions_Are_Adjacent(_PA_START, _PB_END); bool aEND_to_bEND = Positions_Are_Adjacent(_PA_END, _PB_END); bool aEND_to_bSTART = Positions_Are_Adjacent(_PA_END, _PB_START); if ((aSTART_to_bSTART && aEND_to_bEND) || (aEND_to_bSTART && aSTART_to_bEND)) { _PA.Add_AdjacentPlatform(_PB); _PA.oppositePlatform.Add_AdjacentPlatform(_PB); } } } } foreach (Platform _P in allPlatforms) { foreach (Platform _ADJ in _P.adjacentPlatforms) { Debug.Log(_P.GetFullName() + " -- " + _ADJ.GetFullName()); } } }
/// <summary> /// 查找指定两个节点之间的最短路径。 /// </summary> /// <param name="startNode">开始节点。</param> /// <param name="endNode">结束节点。</param> /// <param name="line">目标线路。</param> /// <returns>乘车路线列表。</returns> private List<MetroPath> FindShortestPaths(MetroNode startNode, MetroNode endNode, MetroLine line) { List<MetroPath> pathtList = new List<MetroPath>(); if (startNode == endNode) return pathtList; //路径队列,用于遍历路径 Queue<MetroPath> pathQueue = new Queue<MetroPath>(); pathQueue.Enqueue(new MetroPath()); while (pathQueue.Count > 0) { var path = pathQueue.Dequeue(); //如果已经超过最短路径,则直接返回 if (pathtList.Count > 0 && path.Links.Count > pathtList[0].Links.Count) continue; //路径的最后一个节点 MetroNode prevNode = path.Links.Count > 0 ? path.Links[path.Links.Count - 1].From : null; MetroNode lastNode = path.Links.Count > 0 ? path.Links[path.Links.Count - 1].To : startNode; //继续寻找后续节点 foreach (var link in lastNode.Links.Where(c => c.To != prevNode && (line == null || c.Line == line))) { if (link.To == endNode) { MetroPath newPath = path.Append(link); if (pathtList.Count == 0 || newPath.Links.Count == pathtList[0].Links.Count) {//找到一条路径 pathtList.Add(newPath); } else if (newPath.Links.Count < pathtList[0].Links.Count) {//找到一条更短的路径 pathtList.Clear(); pathtList.Add(newPath); } else break;//更长的路径没有意义 } else if (!path.ContainsNode(link.To)) { pathQueue.Enqueue(path.Append(link)); } } } return pathtList; }