コード例 #1
0
    public override void OnInspectorGUI()
    {
        rNet = target as RouteNet;
        base.OnInspectorGUI();

        EditorGUI.BeginChangeCheck();

        if (GUILayout.Button("BuildAllRoute"))
        {
            Undo.RecordObject(target, "BuildAllRoute");

            ERRoadNetwork net = new ERRoadNetwork();
            foreach (var r in rNet.allRoutes)
            {
                Debug.Log("Road Name : " + r.__roadName + " Points Count " + r.positions.Count);
                markers = net.GetRoadByName(r.__roadName);
                if (markers == null)
                {
                    Debug.LogError("NO SUCH ROAD.....CANCELED......");
                    continue;
                }

                r.resolution = 50;
                Vector3[] arr = markers.GetSplinePointsCenter();
                r.positions = new List <Vector3>(arr);
                for (int i = 0; i < r.positions.Count; i++)
                {
                    //Debug.Log(r.positions[i]);
                }
            }
            EditorUtility.SetDirty(target);
        }
    }
コード例 #2
0
    /// <summary>
    /// Methode zum Fahren des Autos (Simulation).
    /// </summary>
    public void SimulateCar()
    {
        // Die Collider des Autos holen
        Collider[]      colliders    = Physics.OverlapBox(cameraCar.gameObject.transform.position, (cameraCar.gameObject.transform.localScale / 5f), cameraCar.gameObject.transform.rotation);
        List <Collider> carColliders = new List <Collider>();
        ERRoad          road         = null;

        foreach (Collider collider in colliders)
        {
            if (collider.tag == "Street")
            {
                road = network.GetRoadByName(collider.name);
            }

            if (collider.tag == "Car")
            {
                carColliders.Add(collider);
            }
        }

        Vector3 heading = new Vector3(0, 0, 1);

        if (road != null)
        {
            // Hole den letzten Punkt der Strecke
            Vector3[] markers    = road.GetMarkerPositions();
            Vector3   lastMarker = markers.Last();

            // Die richtige Rotation ausrechnen
            heading   = (lastMarker - markers[markers.Length - 2]).normalized;
            heading.y = 0;
            CustomEasyRoad customEasy = null;
            foreach (CustomEasyRoad customEasyRoad in customEasyRoads)
            {
                if (customEasyRoad.Road.GetName() == road.GetName())
                {
                    customEasy = customEasyRoad;
                    break;
                }
            }

            //Tuple<Vector3, Vector3> markers = customEasy.GetIncludingMarkers(cameraCar.gameObject.transform.position);
            //heading = (markers.Second - markers.First).normalized;
            //heading.y = 0;
        }

        // Geschwindigkeit setzen
        Rigidbody rigidbody = cameraCar.GetComponent <Rigidbody>();

        cameraCar.transform.Translate(Vector3.forward * (carSpeed / 3.6f) * Time.deltaTime);
        //cameraCar.transform.rotation.SetLookRotation(heading);
        cameraCar.transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(heading), 2.5f * Time.deltaTime);

        // Die Autos im Weg entfernen
        foreach (Collider collider in carColliders)
        {
            Destroy(collider.gameObject);
        }
    }
    private void SetNodes(Transform parent)
    {
        try
        {
            //Problem in that the loop is finding itself
            //This avoids recursing a folder that was created by this process
            if (parent.name.Contains(nodeFolderName))
            {
                return;
            }

            //TODO: will need to check for the preexisting nodes, and delete them
            //   "Traffic Nodes Right Lane(0)"


            bool   isRight  = true;
            string sSide    = "Right";
            string roadName = parent.parent.name;

            //Hack: use the name to determine if this is the Right or Left hand side
            if (parent.name.ToLower().Contains("left"))
            {
                isRight = false;
                sSide   = "Left";
            }

            //Get width of road, to get the number of lanes
            ERRoad           erRoad       = roadNetwork.GetRoadByName(roadName);
            ERRoadType       roadType     = erRoad.GetRoadType();
            float            roadWidth    = roadType.roadWidth;
            byte             laneCount    = System.Convert.ToByte((roadType.roadWidth / 2) / laneWidth);
            List <Transform> trafficNodes = new List <Transform>();
            Transform        nodeChild;


            for (byte b = 0; b < laneCount; b++)
            {
                string folderName = nodeFolderName + " " + sSide + " Lane (" + b.ToString() + ")";


                //todo NOT FINDING IT WHEN IT IS INDEED there
                Transform tempFolder = parent.parent.Find(folderName);

                //Check for the existance of this folder, if it already exists, delete it.
                //The road has been adjusted and it is safer to assume that the old nodes are no longer correct
                //...just in case
                if (tempFolder != null)
                {
                    GameObject.DestroyImmediate(tempFolder.gameObject);
                }

                GameObject folder = new GameObject(folderName);

                if (b == 0)
                {
                    //Remove existing Traffic Nodes from the folder that they were created
                    //Moving them insures that they are not moved again, in a forever recursive loop
                    //and to know they are completed, and not readjusted.
                    if (isRight)
                    {
                        for (int i = 0; i < parent.childCount; i++)
                        {
                            //NOTE: Be aware that when creating the Side Object the "Combine Objects" checkbox MUST be unchecked
                            //      otherwise a few of these children will not be TrafficSystemNodes and that will cause all sorts of issues.
                            nodeChild      = parent.GetChild(i);
                            nodeChild.name = roadName + " " + sSide + " node (" + i + ")";
                            trafficNodes.Add(nodeChild);
                        }
                    }
                    else
                    {
                        int iLoop = 0;
                        for (int i = (parent.childCount - 1); i > -1; i--)
                        {
                            nodeChild      = parent.GetChild(i);
                            nodeChild.name = roadName + " " + sSide + " node (" + iLoop + ")";
                            trafficNodes.Add(nodeChild);

                            iLoop++;
                        }
                    }

                    //Add to new parent
                    foreach (Transform trafficNode in trafficNodes)
                    {
                        trafficNode.SetParent(folder.transform);
                    }

                    //Connect node to it's next neighbor
                    ConnectNodes(folder);
                }
                else
                {
                    // PROCESS
                    // * Create new nodes
                    // * Place them
                    // * Connect them to each other - still a bit of an issue here

                    //TODO
                    // * Connect them to the previous Lane
                    // * Connect previous lane to this lane

                    //Create the nodes and place them
                    for (int i = 0; i < trafficNodes.Count; i++)
                    {
                        float nodePosition = this.laneWidth;
                        if (isRight)
                        {
                            nodePosition = -(this.laneWidth);
                        }

                        Transform trafficNode = Instantiate(trafficNodeInstance, trafficNodes[i].position + trafficNodes[i].right * nodePosition, trafficNodes[i].rotation);
                        trafficNode.gameObject.isStatic = true;
                        trafficNode.name = roadName + " " + sSide + " node (" + i + ")";
                        trafficNode.SetParent(folder.transform);



                        //Connect to previous lane

                        //trafficNodes[i] = trafficNode;

                        //TODO: connect to next node, BUT this hasn't been created yet so....
                        //Do I need another LIST?  Do I update the current list with the new Node, then in a separate loop connect them?
                        //if (i < (folder.transform.childCount - 1))
                        //{
                        //    TrafficSystemNode nextNode = folder.transform.GetChild(i + 1).GetComponent<TrafficSystemNode>();

                        //    folder.transform.GetChild(i).GetComponent<TrafficSystemNode>().m_connectedChangeLaneNodes.Clear(); //To insure that previous values are removed
                        //    folder.transform.GetChild(i).GetComponent<TrafficSystemNode>().m_connectedChangeLaneNodes.Add(nextNode);
                        //}
                    }


                    //folder.transform.SetParent(parent.parent);

                    //Connect node to it's next neighbor
                    ConnectNodes(folder);


                    //Connect to Previous nodes to Current nodes
                    folderName = nodeFolderName + " " + sSide + " Lane (" + (b - 1).ToString() + ")";
                    GameObject previousFolder = parent.parent.Find(folderName).gameObject;
                    ConnectLanes(folder, previousFolder);
                }

                folder.transform.SetParent(parent.parent);
            }

            //DestroyImmediate(parent.gameObject);  //Remove the original folder, causes an issue with the loop

            //TODO: intersections (someday)
        }
        catch (System.Exception ex)
        {
            HBCLogging.logException(ex);
        }
    }