/// <summary> /// Changes label states of points whose name begins with "carDecelerate". This allows the cars /// to move when the light is green and orders them to stop when it is red. /// </summary> /// <param name="newState">New intersection state</param> private void manageDecelerationPoints(int newState) { PointDescriptor carDecelerate1Descriptor = transform.Find("semaphore1").Find("carDecelerate1").GetComponent <PointDescriptor>(); PointDescriptor carDecelerate2Descriptor = transform.Find("semaphore2").Find("carDecelerate2").GetComponent <PointDescriptor>(); PointDescriptor carDecelerate3Descriptor = transform.Find("semaphore3").Find("carDecelerate3").GetComponent <PointDescriptor>(); PointDescriptor carDecelerate4Descriptor = transform.Find("semaphore4").Find("carDecelerate4").GetComponent <PointDescriptor>(); switch (newState) { case VERTICAL: carDecelerate1Descriptor.setLabel(POINT_STATE1); carDecelerate2Descriptor.setLabel(POINT_STATE2); carDecelerate3Descriptor.setLabel(POINT_STATE1); carDecelerate4Descriptor.setLabel(POINT_STATE2); break; case HORIZONTAL: carDecelerate1Descriptor.setLabel(POINT_STATE2); carDecelerate2Descriptor.setLabel(POINT_STATE1); carDecelerate3Descriptor.setLabel(POINT_STATE2); carDecelerate4Descriptor.setLabel(POINT_STATE1); break; default: carDecelerate1Descriptor.setLabel(POINT_STATE2); carDecelerate2Descriptor.setLabel(POINT_STATE2); carDecelerate3Descriptor.setLabel(POINT_STATE2); carDecelerate4Descriptor.setLabel(POINT_STATE2); break; } }
/// <summary> /// Sets the intersection to a new state. (sets appropriate waypoint labels and changes lights). /// </summary> /// <param name="newState">Parameter which sets this intersection either to the new state.</param> private void switchState(int newState) { PointDescriptor point1Descriptor = transform.Find("semaphore1").Find("point1").GetComponent <PointDescriptor>(); PointDescriptor point2Descriptor = transform.Find("semaphore2").Find("point2").GetComponent <PointDescriptor>(); PointDescriptor point3Descriptor = transform.Find("semaphore3").Find("point3").GetComponent <PointDescriptor>(); PointDescriptor point4Descriptor = transform.Find("semaphore4").Find("point4").GetComponent <PointDescriptor>(); switch (newState) { case VERTICAL: case VERTICAL_TO_HORIZONTAL: point1Descriptor.setLabel(POINT_STATE1); point2Descriptor.setLabel(POINT_STATE2); point3Descriptor.setLabel(POINT_STATE2); point4Descriptor.setLabel(POINT_STATE1); break; case HORIZONTAL: case HORIZONTAL_TO_VERTICAL: point1Descriptor.setLabel(POINT_STATE1); point2Descriptor.setLabel(POINT_STATE1); point3Descriptor.setLabel(POINT_STATE2); point4Descriptor.setLabel(POINT_STATE2); break; } currentState = newState; changeLights(currentState); manageDecelerationPoints(currentState); }
/// <summary> /// Links all of the waypoints from the list to the waypoint given as second parameter if at least /// one of their link labels match (it doesn't count if both labels are empty strings). /// </summary> /// <param name="newWaypoints">list of waypoints that will be added to the current waypoint</param> /// <param name="pointScript">current waypoint, it will get references to all the waypoints from the list</param> private static void addNewWaypointsToCurrentOne(List <Transform> newWaypoints, PointDescriptor pointScript) { foreach (Transform newWaypoint in newWaypoints) { string point1FirstLabel = pointScript.firstLinkLabel; string point1SecondLabel = pointScript.secondLinkLabel; string point2FirstLabel = newWaypoint.GetComponent <PointDescriptor>().firstLinkLabel; string point2SecondLabel = newWaypoint.GetComponent <PointDescriptor>().secondLinkLabel; if ((point1FirstLabel.Equals(point2FirstLabel) && !point1FirstLabel.Equals("") && !point2FirstLabel.Equals("")) || (point1SecondLabel.Equals(point2SecondLabel) && !point1SecondLabel.Equals("") && !point2SecondLabel.Equals(""))) { pointScript.addAdjacentWaypoint(newWaypoint); } } }
/// <summary> /// Sets labels of auxiliary waypoints to 1. This means that the cars can reach them only if the /// traffic light is green. /// </summary> private void manageAuxiliaryPoints() { PointDescriptor carExit1Point = transform.Find("AuxiliaryWaypoints").Find("carExit1").GetComponent <PointDescriptor>(); PointDescriptor carExit2Point = transform.Find("AuxiliaryWaypoints").Find("carExit2").GetComponent <PointDescriptor>(); PointDescriptor carExit4Point = transform.Find("AuxiliaryWaypoints").Find("carExit4").GetComponent <PointDescriptor>(); PointDescriptor carRight1Point = transform.Find("semaphore1").Find("carRight1").GetComponent <PointDescriptor>(); PointDescriptor carRight2Point = transform.Find("semaphore2").Find("carRight2").GetComponent <PointDescriptor>(); carExit1Point.setLabel(POINT_STATE1); carExit2Point.setLabel(POINT_STATE1); carExit4Point.setLabel(POINT_STATE1); carRight1Point.setLabel(POINT_STATE1); carRight2Point.setLabel(POINT_STATE1); }
/// <summary> /// Return whether the square distance associated to a is smaller /// than the square distance assocated with b /// </summary> /// <param name="a"></param> /// <param name="b"></param> /// <returns></returns> public static bool DistanceCompare(PointDescriptor a, PointDescriptor b) { return(a._squareDistance < b._squareDistance); }
/// <summary> /// Links all of the waypoints from the list to the waypoint given as second parameter if at least /// one of their link labels match (it doesn't count if both labels are empty strings). /// </summary> /// <param name="newWaypoints">list of waypoints that will be added to the current waypoint</param> /// <param name="pointScript">current waypoint, it will get references to all the waypoints from the list</param> private static void addNewWaypointsToCurrentOne(List<Transform> newWaypoints, PointDescriptor pointScript) { foreach(Transform newWaypoint in newWaypoints) { string point1FirstLabel = pointScript.firstLinkLabel; string point1SecondLabel = pointScript.secondLinkLabel; string point2FirstLabel = newWaypoint.GetComponent<PointDescriptor>().firstLinkLabel; string point2SecondLabel = newWaypoint.GetComponent<PointDescriptor>().secondLinkLabel; if((point1FirstLabel.Equals(point2FirstLabel) && !point1FirstLabel.Equals("") && !point2FirstLabel.Equals("")) || (point1SecondLabel.Equals(point2SecondLabel) && !point1SecondLabel.Equals("") && !point2SecondLabel.Equals(""))) { pointScript.addAdjacentWaypoint(newWaypoint); } } }
/// <summary> /// Rotates the links by requested amount of degrees clockwise. For example, if 90 is provided as an /// argument (map block prefab is rotated by 90 degrees clockwise), north links now become east links, /// east links become south links etc. /// </summary> /// <param name="angle">Map block rotation angle. Can be either 0, 90, 180, 270 or 360.</param> public void rotateLinks(int angle) { Transform waypoints = transform.Find("WayPoints"); foreach (Transform waypoint in waypoints) { PointDescriptor pointScript = waypoint.GetComponent <PointDescriptor>(); bool wasNorthLink = pointScript.isNorthLink; bool wasEastLink = pointScript.isEastLink; bool wasSouthLink = pointScript.isSouthLink; bool wasWestLink = pointScript.isWestLink; pointScript.isNorthLink = false; pointScript.isEastLink = false; pointScript.isSouthLink = false; pointScript.isWestLink = false; switch (angle) { case 90: if (wasNorthLink) { pointScript.isEastLink = true; } if (wasEastLink) { pointScript.isSouthLink = true; } if (wasSouthLink) { pointScript.isWestLink = true; } if (wasWestLink) { pointScript.isNorthLink = true; } break; case 180: if (wasNorthLink) { pointScript.isSouthLink = true; } if (wasEastLink) { pointScript.isWestLink = true; } if (wasSouthLink) { pointScript.isNorthLink = true; } if (wasWestLink) { pointScript.isEastLink = true; } break; case 270: if (wasNorthLink) { pointScript.isWestLink = true; } if (wasEastLink) { pointScript.isNorthLink = true; } if (wasSouthLink) { pointScript.isEastLink = true; } if (wasWestLink) { pointScript.isSouthLink = true; } break; case 0: case 360: break; default: print("Invalid argument, only rotations by 0, 90, 180, 270 or 360 degrees are allowed."); break; } } }
/// <summary> /// This method is invoked once a new map part is generated next to this one. Waypoints on /// the edge of the new part (source part) will be linked to the waypoints on this part if /// their link types match. For example, a new north part (source part) contains waypoints /// of type southLink (newLinkType). Those waypoints are linked to waypoints on this part /// that are of type northLink (thisLinkType). /// </summary> /// <param name="sourcePart">Script that holds references to new waypoints.</param> /// <param name="newLinkType">Waypoints of this type on the source part will get linked to the /// waypoints of thisLinkType on this part.</param> /// <param name="thisLinkType">Waypoints of this type on this map part will be linked to the /// waypoints on source part of type newLinkType.</param> private void link(MapPartController sourcePart, string newLinkType, string thisLinkType) { List <Transform> newWaypoints = new List <Transform>(); foreach (Transform waypoint in sourcePart.transform.Find("WayPoints")) { PointDescriptor pointScript = waypoint.GetComponent <PointDescriptor>(); switch (newLinkType) { case "northLink": if (pointScript.isNorthLink) { newWaypoints.Add(waypoint); } break; case "eastLink": if (pointScript.isEastLink) { newWaypoints.Add(waypoint); } break; case "southLink": if (pointScript.isSouthLink) { newWaypoints.Add(waypoint); } break; case "westLink": if (pointScript.isWestLink) { newWaypoints.Add(waypoint); } break; } } Transform waypoints = transform.Find("WayPoints"); foreach (Transform waypoint in waypoints) { PointDescriptor pointScript = waypoint.GetComponent <PointDescriptor>(); switch (thisLinkType) { case "northLink": if (pointScript.isNorthLink) { addNewWaypointsToCurrentOne(newWaypoints, pointScript); } break; case "eastLink": if (pointScript.isEastLink) { addNewWaypointsToCurrentOne(newWaypoints, pointScript); } break; case "southLink": if (pointScript.isSouthLink) { addNewWaypointsToCurrentOne(newWaypoints, pointScript); } break; case "westLink": if (pointScript.isWestLink) { addNewWaypointsToCurrentOne(newWaypoints, pointScript); } break; } } }