void AddWay(WaysControl wayController, GameObject node, int wayMode) { switch (wayController.ways) { case 1: { if (wayController.way1 == null) { wayController.way1 = node.transform; wayController.way1Mode = wayMode; } else { wayController.ways++; wayController.way2 = node.transform; wayController.way2Mode = wayMode; } break; } case 2: { if (wayController.way2 == null) { wayController.way2 = node.transform; wayController.way2Mode = wayMode; } else { wayController.ways++; wayController.way3 = node.transform; wayController.way3Mode = wayMode; } break; } case 3: { if (wayController.way3 == null) { wayController.way3 = node.transform; wayController.way3Mode = wayMode; } else { wayController.ways++; wayController.way4 = node.transform; wayController.way4Mode = wayMode; } break; } } }
void RemovePath() { if (Selection.gameObjects.Length != 1) { Debug.Log("Please select only one object"); return; } if (!Selection.gameObjects[0].HasComponent <Node>()) { Debug.Log("Please select a node"); } GameObject parent = Selection.gameObjects[0].transform.parent.gameObject; Node firstNode = parent.transform.GetChild(0).gameObject.GetComponent <Node>(); Node lastNode = parent.transform.GetChild(parent.transform.childCount - 1).gameObject.GetComponent <Node>(); WaysControl waycontroller1 = firstNode.previousNode.gameObject.GetComponent <WaysControl>(); WaysControl waycontroller2 = lastNode.nextNode.gameObject.GetComponent <WaysControl>(); DestroyImmediate(parent); OrganizeWays(waycontroller1); OrganizeWays(waycontroller2); }
public override void OnInspectorGUI() { WaysControl myPlayer = (WaysControl)target; EditorGUILayout.Space(); GUI.color = new Color(0.5f, 1, 0.5f); EditorGUILayout.BeginHorizontal(); EditorGUILayout.PrefixLabel("Stop Distance:"); myPlayer.stopDistance = EditorGUILayout.FloatField(myPlayer.stopDistance); EditorGUILayout.EndHorizontal(); GUI.color = Color.white; EditorGUILayout.Space(); EditorGUILayout.BeginHorizontal(); EditorGUILayout.PrefixLabel("Ways:"); myPlayer.ways = EditorGUILayout.IntSlider(myPlayer.ways, 1, 4); EditorGUILayout.EndHorizontal(); EditorGUILayout.Space(); for (int ways = 1; ways < (myPlayer.ways + 1); ways++) { if (myPlayer.ways > 0) { EditorGUILayout.BeginHorizontal(); EditorGUILayout.PrefixLabel("Way " + ways + ":"); switch (ways) { case 1: myPlayer.way1 = EditorGUILayout.ObjectField(myPlayer.way1, typeof(Transform)) as Transform; myPlayer.way1Mode = EditorGUILayout.Popup(myPlayer.way1Mode, mode); if (myPlayer.way1) { if (myPlayer.way1.GetComponent <Node>().firistNode) { myPlayer.way1.GetComponent <Node>().previousNode = myPlayer.transform; } else if (myPlayer.way1.GetComponent <Node>().lastNode) { myPlayer.way1.GetComponent <Node>().nextNode = myPlayer.transform; } } break; case 2: myPlayer.way2 = EditorGUILayout.ObjectField(myPlayer.way2, typeof(Transform)) as Transform; myPlayer.way2Mode = EditorGUILayout.Popup(myPlayer.way2Mode, mode); if (myPlayer.way2) { if (myPlayer.way2.GetComponent <Node>().firistNode) { myPlayer.way2.GetComponent <Node>().previousNode = myPlayer.transform; } else if (myPlayer.way2.GetComponent <Node>().lastNode) { myPlayer.way2.GetComponent <Node>().nextNode = myPlayer.transform; } } break; case 3: myPlayer.way3 = EditorGUILayout.ObjectField(myPlayer.way3, typeof(Transform)) as Transform; myPlayer.way3Mode = EditorGUILayout.Popup(myPlayer.way3Mode, mode); if (myPlayer.way3) { if (myPlayer.way3.GetComponent <Node>().firistNode) { myPlayer.way3.GetComponent <Node>().previousNode = myPlayer.transform; } else if (myPlayer.way3.GetComponent <Node>().lastNode) { myPlayer.way3.GetComponent <Node>().nextNode = myPlayer.transform; } } break; case 4: myPlayer.way4 = EditorGUILayout.ObjectField(myPlayer.way4, typeof(Transform)) as Transform; myPlayer.way4Mode = EditorGUILayout.Popup(myPlayer.way4Mode, mode); if (myPlayer.way4) { if (myPlayer.way4.GetComponent <Node>().firistNode) { myPlayer.way4.GetComponent <Node>().previousNode = myPlayer.transform; } else if (myPlayer.way4.GetComponent <Node>().lastNode) { myPlayer.way4.GetComponent <Node>().nextNode = myPlayer.transform; } } break; } EditorGUILayout.EndHorizontal(); } } EditorGUILayout.Space(); EditorGUILayout.BeginHorizontal(); EditorGUILayout.PrefixLabel("Traffic Control:"); myPlayer.TCActive = EditorGUILayout.Toggle(myPlayer.TCActive); EditorGUILayout.EndHorizontal(); if (myPlayer.TCActive) { myPlayer.TrafficTime = EditorGUILayout.Slider("Traffic Time:", myPlayer.TrafficTime, 1, 60); EditorGUILayout.Space(); myPlayer.TrafficWays = EditorGUILayout.IntSlider("Traffic Ways:", myPlayer.TrafficWays, 1, 3); EditorGUILayout.Space(); GUI.color = (myPlayer.TrafficNumber == 1) ? new Color(0.5f, 1, 0.5f) : new Color(1, 0.5f, 0.5f); for (int TrafficWay = 1; TrafficWay < (myPlayer.TrafficWays + 1); TrafficWay++) { if (myPlayer.TrafficWays > 0) { EditorGUILayout.BeginHorizontal(); EditorGUILayout.PrefixLabel("TrafficWay " + TrafficWay + ":"); switch (TrafficWay) { case 1: GUI.color = (myPlayer.TrafficNumber == 1) ? new Color(0.5f, 1, 0.5f) : new Color(1, 0.5f, 0.5f); if (myPlayer.TrafficWaitTimer != 1) { GUI.color = Color.yellow; } myPlayer.trafficNumbers[0] = EditorGUILayout.MaskField(myPlayer.trafficNumbers[0], myPlayer.trafficMode); break; case 2: GUI.color = (myPlayer.TrafficNumber == 2) ? new Color(0.5f, 1, 0.5f) : new Color(1, 0.5f, 0.5f); if (myPlayer.TrafficWaitTimer != 1) { GUI.color = Color.yellow; } myPlayer.trafficNumbers[1] = EditorGUILayout.MaskField(myPlayer.trafficNumbers[1], myPlayer.trafficMode); break; case 3: GUI.color = (myPlayer.TrafficNumber == 3) ? new Color(0.5f, 1, 0.5f) : new Color(1, 0.5f, 0.5f); if (myPlayer.TrafficWaitTimer != 1) { GUI.color = Color.yellow; } myPlayer.trafficNumbers[2] = EditorGUILayout.MaskField(myPlayer.trafficNumbers[2], myPlayer.trafficMode); break; } EditorGUILayout.EndHorizontal(); } } } EditorUtility.SetDirty(target); }
//RandomWay////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// Transform RandomWay(Transform node, int maxWays) { WaysControl waysControl = currentNode.GetComponent <WaysControl>(); while (wayActive == false) { if (maxWays == 1) { randomWays = 1; wayActive = true; } else { randomWays = Random.Range(1, maxWays + 1); switch (randomWays) { case 1: if (waysControl.way1 != lastNode) { oneWay = waysControl.way1Mode == 0 ? true : false; if (int.Parse(waysControl.way1.name) > 1) { wayMove = WayMove.Left; } else { wayMove = WayMove.Right; } node = waysControl.way1; myStatue = waysControl.way1.GetComponent <Node>().nodeState; wayActive = true; } break; case 2: if (waysControl.way2 != lastNode) { oneWay = waysControl.way2Mode == 0 ? true : false; if (int.Parse(waysControl.way2.name) > 1) { wayMove = WayMove.Left; } else { wayMove = WayMove.Right; } node = waysControl.way2; myStatue = waysControl.way2.GetComponent <Node>().nodeState; wayActive = true; } break; case 3: if (waysControl.way3 != lastNode) { oneWay = waysControl.way3Mode == 0 ? true : false; if (int.Parse(waysControl.way3.name) > 1) { wayMove = WayMove.Left; } else { wayMove = WayMove.Right; } node = waysControl.way3; myStatue = waysControl.way3.GetComponent <Node>().nodeState; wayActive = true; } break; case 4: if (waysControl.way4 != lastNode) { oneWay = waysControl.way4Mode == 0 ? true : false; if (int.Parse(waysControl.way4.name) > 1) { wayMove = WayMove.Left; } else { wayMove = WayMove.Right; } node = waysControl.way4; myStatue = waysControl.way4.GetComponent <Node>().nodeState; wayActive = true; } break; } } } wayActive = false; maxWidthDistance = maxWidthDistance - 0.1f; if (lastNode != null) { widthDistanceRefrash(lastNode.GetComponent <Node>()); } return(node); }
void LinkIntersections(int wayMode) { WaysControl wayController1 = (WaysControl)inter1Obj; WaysControl wayController2 = (WaysControl)inter2Obj; GameObject inter1 = wayController1.gameObject; GameObject inter2 = wayController2.gameObject; if (!inter1.HasComponent <WaysControl>() || !inter2.HasComponent <WaysControl>()) { Debug.Log("Please select intersections only"); } if ((wayController1.ways == 4 && wayController1.way4 != null) || (wayController2.ways == 4 && wayController2.way4 != null)) { Debug.Log("Intersection cannot have more than 4 links"); return; } GameObject path = new GameObject(); path.transform.position = (inter1.transform.position + inter2.transform.position) / 2; List <GameObject> Nodes = new List <GameObject>(); Vector3 dir = (inter2.transform.position - inter1.transform.position) / (numNodes + 1); for (int i = 0; i < numNodes; i++) { GameObject node = new GameObject(); node.layer = 8; node.transform.SetParent(path.transform); node.name = i.ToString(); node.transform.position = inter1.transform.position + dir * (i + 1); node.AddComponent <Node>(); Node thisNode = node.GetComponent <Node>(); thisNode.widthDistance = widthDistance; thisNode.SpeedLimit = speedLimit; node.AddComponent <BoxCollider>(); Collider nodeCollider = node.GetComponent <BoxCollider>(); nodeCollider.isTrigger = true; Nodes.Add(node); } Node nodeProperties = Nodes[0].GetComponent <Node>(); nodeProperties.previousNode = inter1.transform; if (numNodes > 1) { nodeProperties.nextNode = Nodes[1].transform; } for (int i = 1; i < numNodes - 1; i++) { nodeProperties = Nodes[i].GetComponent <Node>(); nodeProperties.previousNode = Nodes[i - 1].transform; nodeProperties.nextNode = Nodes[i + 1].transform; } nodeProperties = Nodes[numNodes - 1].GetComponent <Node>(); if (numNodes > 1) { nodeProperties.previousNode = Nodes[numNodes - 2].transform; } nodeProperties.nextNode = inter2.transform; if (wayMode == 0) { AddWay(wayController1, Nodes[0], 0); } else if (wayMode == 1) { AddWay(wayController1, Nodes[0], 1); AddWay(wayController2, Nodes[numNodes - 1], 1); } Vector3 parentPos = Vector3.zero; foreach (GameObject node in Nodes) { parentPos += node.transform.position; } parentPos /= Nodes.Count; path.name = "0"; path.AddComponent <VehiclePath>(); }
void OnGUI() { EditorGUILayout.LabelField("Node width"); widthDistance = EditorGUILayout.Slider(widthDistance, 0, 9.0f); if (GUILayout.Button("Set selected Nodes Width")) { foreach (GameObject obj in Selection.gameObjects) { if (obj.HasComponent <Node>()) { Node node = obj.GetComponent <Node>(); node.widthDistance = widthDistance; } } } EditorGUILayout.LabelField("Node speed limit"); speedLimit = EditorGUILayout.Slider(speedLimit, 0, 100.0f); if (GUILayout.Button("Set selected Nodes speed limit")) { foreach (GameObject obj in Selection.gameObjects) { if (obj.HasComponent <Node>()) { Node node = obj.GetComponent <Node>(); node.SpeedLimit = speedLimit; } } } EditorGUILayout.LabelField("Number of nodes"); numNodes = EditorGUILayout.IntSlider(numNodes, 1, 10); EditorGUILayout.LabelField("start wayControl/Node 1"); inter1Obj = EditorGUILayout.ObjectField(inter1Obj, typeof(Object), true); EditorGUILayout.LabelField("end wayControl/Node 2"); inter2Obj = EditorGUILayout.ObjectField(inter2Obj, typeof(Object), true); EditorGUILayout.LabelField(""); EditorGUILayout.LabelField("Node 1 and Node 2 need to be adjacent"); if (GUILayout.Button("Create AIController")) { CreateAIController(); } if (GUILayout.Button("Link Intersections unidirectional")) { LinkIntersections(0); } if (GUILayout.Button("Link Intersections bidirectional")) { LinkIntersections(1); } if (GUILayout.Button("Add Nodes to Intersection as way")) { if (Selection.gameObjects.Length < 2) { Debug.Log("Please select atleast 2 object"); return; } GameObject intersection = null; List <GameObject> nodes = new List <GameObject>(); foreach (GameObject obj in Selection.gameObjects) { if (obj.HasComponent <Node>()) { nodes.Add(obj); } else if (obj.HasComponent <WaysControl>()) { if (intersection == null) { intersection = obj; } else { Debug.Log("Please select only one intersection"); } } } if (intersection == null) { Debug.Log("Please select an intersection"); return; } if (nodes.Count == 0) { Debug.Log("Please select node(s)"); return; } WaysControl wayController = intersection.GetComponent <WaysControl>(); foreach (GameObject node in nodes) { AddWay(wayController, node, 0); } } if (GUILayout.Button("Equally space nodes group straight")) { spaceNodes(); } if (GUILayout.Button("Add nodes between selected nodes")) { AddNodesBetweenNodes(); } if (GUILayout.Button("Organize selected intersection ways")) { foreach (GameObject obj in Selection.gameObjects) { if (obj.HasComponent <WaysControl>()) { OrganizeWays(obj.GetComponent <WaysControl>()); } } } if (GUILayout.Button("Delete Path")) { RemovePath(); } if (GUILayout.Button("Align selected nodes with road below")) { AlignWithRoad(); } }
void OrganizeWays(WaysControl waycontroller) { List <Transform> ways = new List <Transform>(); for (int i = 0; i < waycontroller.ways; i++) { if (i == 0 && waycontroller.way1 != null) { if (!ways.Contains(waycontroller.way1)) { ways.Add(waycontroller.way1); } } else if (i == 1 && waycontroller.way2 != null) { if (!ways.Contains(waycontroller.way2)) { ways.Add(waycontroller.way2); } } else if (i == 2 && waycontroller.way3 != null) { if (!ways.Contains(waycontroller.way3)) { ways.Add(waycontroller.way3); } } else if (i == 3 && waycontroller.way4 != null) { if (!ways.Contains(waycontroller.way4)) { ways.Add(waycontroller.way4); } } } if (ways.Count == 0) { waycontroller.ways = 1; return; } waycontroller.ways = ways.Count; for (int i = 0; i < waycontroller.ways; i++) { switch (i) { case 0: { waycontroller.way1 = ways[i]; break; } case 1: { waycontroller.way2 = ways[i]; break; } case 2: { waycontroller.way3 = ways[i]; break; } case 3: { waycontroller.way4 = ways[i]; break; } } } }