public void BuildTrafficNodes()
    {
        try
        {
            if (trafficNodeInstance == null)
            {
                EditorUtility.DisplayDialog(
                    "Traffic Node Required",
                    "Please add a Traffic Node Prefab to the script",
                    "Ooops, I forgot.  I will do it now");
            }
            else
            {
                //okay, this is SO bad.
                //Creating an instance of new redirects to exist game object, AND there is no way to connect to existing without calling NEW
                roadNetwork = new ERRoadNetwork();

                random = new System.Random(System.DateTime.Now.Millisecond);

                foreach (Transform child in transform)
                {
                    SearchSubNode(child);
                }
            }
        }
        catch (System.Exception ex)
        {
            HBCLogging.logException(ex);
        }
    }
示例#2
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);
        }
    }
示例#3
0
    private void IndentityMonoAdd()
    {
        /*
         * Set up a unique value for each Road and Connection.
         * This is because we cannot use the Road or Connection name in ECS as strings are not allowed (only bittable data can be used)
         * AND because road and connection names may not be unique.
         * We need the unique value to identify the relationship between road and connection
         */

        ERRoadNetwork roadNetwork = new ERRoadNetwork();

        ERRoad[]       roads       = roadNetwork.GetRoads();
        ERConnection[] connections = roadNetwork.GetConnections();

        int identity = 0;

        foreach (ERRoad road in roads)
        {
            road.gameObject.AddComponent <ERRoadConnectionIdentity>();
            road.gameObject.GetComponent <ERRoadConnectionIdentity>().value = identity;
            identity++;
        }

        foreach (ERConnection connection in connections)
        {
            connection.gameObject.AddComponent <ERRoadConnectionIdentity>();
            connection.gameObject.GetComponent <ERRoadConnectionIdentity>().value = identity;
            identity++;
        }
    }
    public void HideUIElements()
    {
        roadNetwork = new ERRoadNetwork();

        foreach (Transform child in transform)
        {
            HideUISubNode(child);
        }
    }
 // Use this for initialization
 void Start()
 {
     roads                       = new List <ERRoad> ();
     roadNetwork                 = new ERRoadNetwork();
     roadType                    = new ERRoadType();
     roadType.roadWidth          = 6;
     roadType.roadMaterial       = Resources.Load("Materials/roads/single lane") as Material;
     roadType.connectionMaterial = Resources.Load("Materials/crossings/crossing material") as Material;
 }
示例#6
0
    private const byte buildingSizeMultiplier = 3;  //CScape uses a 1 to 3 setting for their buildings, so this is to deal with that.


    public void PlaceBuildings()
    {
        try
        {
            if (buildingTag.Trim().Length == 0 || roadTag.Trim().Length == 0)
            {
                Debug.Log("You must have building and road tags set");
                return;
            }

            //To insure the percentage is never over 100
            if (percentBuildingBack > 100)
            {
                percentBuildingBack = 100;
            }


            //Use only tagged roads and buildings, that way if you have sections the way you want them, this code will not affect the untagged assets
            buildings = GameObject.FindGameObjectsWithTag(buildingTag).ToList();
            List <GameObject> roads       = GameObject.FindGameObjectsWithTag(roadTag).ToList();
            ERRoadNetwork     roadNetwork = new ERRoadNetwork();
            ERRoad[]          ERroads     = roadNetwork.GetRoads();
            string            roadName    = "";


            //Move the buildings out of the way
            foreach (GameObject building in buildings)
            {
                building.transform.position = new Vector3(-100, 0, -100);
            }


            foreach (ERRoad road in ERroads)
            {
                roadName = road.GetName();

                //Make sure the road is in the tagged list
                if (RoadInList(roads, roadName))
                {
                    Vector3[] markersRight  = road.GetSplinePointsRightSide();
                    Vector3[] markersLeft   = road.GetSplinePointsLeftSide();
                    Vector3[] markersCenter = road.GetSplinePointsCenter();
                    Vector3[] markersOffset = markersCenter;

                    PlaceBuildings(markersRight, markersCenter, true, road);

                    PlaceBuildings(markersLeft, markersCenter, false, road);
                }
            }
        }
        catch (System.Exception ex)
        {
            Debug.Log("Place Building Ex: " + ex.Message);
        }
    }
示例#7
0
    void Start()
    {
        network = new ERRoadNetwork();
        network.BuildRoadNetwork();
        customEasyRoads = new List <CustomEasyRoad>();
        ProjectOnCamera2D projectOnCamera2D = car.GetComponent <ProjectOnCamera2D>();

        projectOnCamera2D.cam = Camera.main;

        culture = CultureInfo.CreateSpecificCulture("en-US");
    }
    void Start()
    {
        Debug.Log("Please read the comments at the top before using the runtime API!");

        // Create Road Network object
        roadNetwork = new ERRoadNetwork();

        // Create road
        //	ERRoad road = roadNetwork.CreateRoad(string name);
        //	ERRoad road = roadNetwork.CreateRoad(string name, Vector3[] markers);
        //	ERRoad road = roadNetwork.CreateRoad(string name, ERRoadType roadType);
        //	ERRoad road = roadNetwork.CreateRoad(string name, ERRoadType roadType, Vector3[] markers);

        ERRoadType roadType = new ERRoadType();

        roadType.roadWidth    = 6;
        roadType.roadMaterial = Resources.Load("Materials/roads/single lane") as Material;

        Vector3[] markers = new Vector3[4];
        markers[0] = new Vector3(200, 5, 200);
        markers[1] = new Vector3(250, 5, 200);
        markers[2] = new Vector3(250, 5, 250);
        markers[3] = new Vector3(300, 5, 250);

        road = roadNetwork.CreateRoad("road 1", roadType, markers);

        // Add Marker: ERRoad.AddMarker(Vector3);
        road.AddMarker(new Vector3(300, 5, 300));

        // Add Marker: ERRoad.InsertMarker(Vector3);
        road.InsertMarker(new Vector3(275, 5, 235));

        // Delete Marker: ERRoad.DeleteMarker(int index);
        road.DeleteMarker(2);

        // Set the road width : ERRoad.SetWidth(float);
        //	road.SetWidth(10);

        // Set the road material : ERRoad.SetMaterial(Material);
        //	Material mat = Resources.Load("Materials/roads/single lane") as Material;
        //	road.SetMaterial(mat);

        // Build Road Network
        roadNetwork.BuildRoadNetwork();

        // Restore Road Network
        //	roadNetwork.RestoreRoadNetwork();


        // create dummy object
        go = GameObject.CreatePrimitive(PrimitiveType.Cube);
    }
示例#9
0
    private void IdentityMonoRemove()
    {
        ERRoadNetwork roadNetwork = new ERRoadNetwork();

        ERRoad[]       roads       = roadNetwork.GetRoads();
        ERConnection[] connections = roadNetwork.GetConnections();

        foreach (ERRoad road in roads)
        {
            Destroy(road.gameObject.GetComponent <ERRoadConnectionIdentity>());
        }

        foreach (ERConnection connection in connections)
        {
            Destroy(connection.gameObject.GetComponent <ERRoadConnectionIdentity>());
        }
    }
        /// <summary>
        /// Place a gameobject on the road at marker 0 looking at marker 1
        /// </summary>
        private void AutoPlaceGameObject()
        {
            if (!editor.extension.roadSettings.autoPlaceGameObject)
            {
                return;
            }

            if (editor.extension.roadSettings.placementGameObject == null)
            {
                Debug.LogError("No GameObject specified");
                return;
            }

            ERRoadNetwork erRoadNetwork = new ERRoadNetwork();
            ERRoad        erRoad        = erRoadNetwork.GetRoadByGameObject(editor.extension.roadSettings.road);

            if (erRoad == null)
            {
                Debug.LogError("No road gameobject specified");
                return;
            }

            if (erRoad.GetMarkerCount() < 2)
            {
                Debug.LogError("At least 2 markers required");
                return;
            }

            GameObject gameObject = editor.extension.roadSettings.placementGameObject;

            // get marker with index 0 and 1
            Vector3 positionA = erRoad.GetMarkerPosition(0);
            Vector3 positionB = erRoad.GetMarkerPosition(1);

            Bounds prefabBounds = BoundsUtils.GetPrefabBounds(gameObject);

            // position at marker position
            gameObject.transform.position = positionA;

            // look at 2nd marker
            gameObject.transform.LookAt(positionB);
        }
        public void OnEnable()
        {
            biomeMaskArea       = editor.FindProperty(x => x.roadSettings.biomeMaskArea);
            road                = editor.FindProperty(x => x.roadSettings.road);
            smoothEnabled       = editor.FindProperty(x => x.roadSettings.smoothEnabled);
            closedTrack         = editor.FindProperty(x => x.roadSettings.closedTrack);
            minDistance         = editor.FindProperty(x => x.roadSettings.minDistance);
            autoPlaceGameObject = editor.FindProperty(x => x.roadSettings.autoPlaceGameObject);
            placementGameObject = editor.FindProperty(x => x.roadSettings.placementGameObject);

#if EASY_ROADS
            // create a reference to the road network in the scene
            erRoadNetwork = new ERRoadNetwork();
            erRoad        = null;

            if (road.objectReferenceValue != null)
            {
                erRoad = erRoadNetwork.GetRoadByGameObject(road.objectReferenceValue as GameObject);
            }
#endif
        }
示例#12
0
    /// <summary>
    /// Methode zum Aufsetzen des RoadTypes anhand der Lanes.
    /// </summary>
    public void SetUpRoadType()
    {
        // Die Strecke neu holen
        this.network = new ERRoadNetwork();
        this.network.BuildRoadNetwork();

        foreach (ERRoadType roadType in this.network.GetRoadTypes())
        {
            switch (this.numberOfTracks)
            {
            case 8:
                // Falls acht Spuren benötigt werden (roadWidth wurde angepasst)
                roadType.roadMaterial = Resources.Load <Material>("Road8Lanes");
                roadType.roadWidth    = 30;
                roadType.Update();
                break;

            case 6:
                // Falls sechs Spuren benötigt werden (roadWidth wurde angepasst)
                roadType.roadMaterial = Resources.Load <Material>("Road6Lanes");
                roadType.roadWidth    = 20;
                roadType.Update();
                break;

            case 4:
                // Falls vier Spuren benötigt werden
                roadType.roadMaterial = Resources.Load <Material>("Road4Lanes");
                roadType.roadWidth    = 12;
                roadType.Update();
                break;

            case 2:
            default:
                // Falls zwei Spuren benötigt werden (roadMaterial stimmt schon)
                roadType.roadWidth = 6;
                roadType.Update();
                break;
            }
        }
    }
示例#13
0
    /// <summary>
    /// Methode zum Zeichnen einer geraden Straße.
    /// </summary>
    /// <param name="length">Die Länge der Straße.</param>
    /// <param name="minCars">Die minimale Anzahl der Autos auf dem Straßenteil.</param>
    /// <param name="maxCars">Die maximale Anzahl der Autos auf dem Straßenteil.</param>
    /// <param name="heightDifference">Der Höhenunterschied.</param>
    /// <param name="seed">Der Seed.</param>
    /// <returns>Die Straße.</returns>
    public ERRoad CreateStraight(float length, int minCars, int maxCars, float?heightDifference, string seed)
    {
        // Die Strecke neu holen
        this.network = new ERRoadNetwork();
        this.network.BuildRoadNetwork();

        // Den RoadType holen
        ERRoadType roadType = this.GetRandomRoadType();

        // Hole die akutellen Streckenteile
        ERRoad[] currentRoads = network.GetRoads();

        // Hole die Höhe der Strecke
        float fixHeightDifference = heightDifference ?? 0;

        // Lege die Positionen der Strecke an
        Vector3 startPosition  = new Vector3(0, 0, 0);
        Vector3 middlePosition = new Vector3(0, fixHeightDifference / 2, length / 2);
        Vector3 endPosition    = new Vector3(0, fixHeightDifference, length);

        ERRoad lastRoad = null;
        ERRoad road     = null;

        if (currentRoads.Length > 0)
        {
            // Hole die letzte Strecke
            lastRoad = currentRoads.Last();

            // Hole den letzten Punkt der Strecke
            Vector3[] markers    = lastRoad.GetMarkerPositions();
            Vector3   lastMarker = markers.Last();

            // Die richtige Rotation ausrechnen
            Vector3 heading   = (lastMarker - markers[markers.Length - 2]);
            Vector3 direction = heading / heading.magnitude;
            direction.y = 0;

            // Das Verhältnis zwischen x und z-Achse ausrechnen
            float x = direction.x / (direction.magnitude);
            float z = direction.z / (direction.magnitude);

            Vector3[] streetVectors = new Vector3[(int)length];
            float     heightPart    = fixHeightDifference / length;
            for (int lengthPart = 0; lengthPart < length; lengthPart++)
            {
                streetVectors[lengthPart] = lastMarker + new Vector3(x * lengthPart, heightPart * lengthPart, z * lengthPart);
            }

            // Generiere Straße
            road = network.CreateRoad("Straight" + currentRoads.Length, roadType, streetVectors);
        }
        else
        {
            // Generiere erste Straße
            road = network.CreateRoad("Straight" + currentRoads.Length, roadType, new Vector3[] { startPosition, middlePosition, endPosition });
        }

        // Erstelle die Strecke mit einem eindeutigen Namen
        customEasyRoads.Add(new CustomEasyRoad(car, road, minCars, maxCars, numberOfTracks));
        return(road);
    }
示例#14
0
    /// <summary>
    /// Erstellt eine Kurve anhand eines Winkels, der Länge der Kurve und den Positionen des aktuellen und vorherigen Straßen Elementes.
    /// </summary>
    /// <param name="angle">Der Winkel.</param>
    /// <param name="length">Die Länge des Straßenelementes.</param>
    /// <param name="heightDifference">Die Höhendifferenz für den Streckenabschnitt.</param>
    /// <param name="minCars">Die minimale Anzahl an Autos auf diesem Streckenabschnitt.</param>
    /// <param name="maxCars">Die maximale Anzahl an Autos auf diesem Streckenabschnitt.</param>
    /// <param name="seed">Der Seed des Random-Generators.</param>
    /// <returns>Die Kurve.</returns>
    public ERRoad CreateCurve(float angle, float length, float?heightDifference, int minCars, int maxCars, string seed)
    {
        // Die Strecke neu holen
        this.network = new ERRoadNetwork();
        this.network.BuildRoadNetwork();

        // hole die Höhendifference
        float fixHeightDifference = heightDifference ?? 0f;

        // Die StartPosition initialisieren.
        Vector3 startPosition = new Vector3(0, 0, 0);

        // Die Ausrichtung initialisieren (default ist z-Richtung).
        Vector3 heading = new Vector3(0, 0, 1);

        // Den RoadType holen
        ERRoadType roadType = this.GetRandomRoadType();

        // Hole die Position des letzten Streckenabschnitts, wenn vorhanden.
        ERRoad lastRoad = null;

        if (network.GetRoads().Length > 0)
        {
            lastRoad = network.GetRoads().Last();
            Vector3[] markers      = lastRoad.GetMarkerPositions();
            Vector3   lastPosition = markers.Last();

            // Die Startposition an den letzten Streckenabschnitt anpassen.
            startPosition = lastPosition;

            // Die Ausrichtung in Bezug auf den vorherigen Streckenabschnitt holen.
            Vector3 secondToLast = markers[markers.Count() - 2];
            heading   = lastPosition - secondToLast;
            heading.y = 0;
        }

        // Den (geraden) Richtungsvektor berechnen.
        Vector3 direction = heading / heading.magnitude;

        // Der Vektor der y-Achse
        Vector3 yAxis = new Vector3(0, 1, 0);

        // Die Anzahl an zu berechnenden Positionen für die Kurve
        int   numbPositions      = Convert.ToInt32(Math.Abs(angle));
        float positionPercentage = numbPositions * percentageEven;

        // Das Array mit den neuen Positionen.
        Vector3[] curvePositions = new Vector3[numbPositions];
        curvePositions[0] = startPosition;

        // es werden in 1-Grad-Schritten Positionen berechnet.
        float anglePart  = angle / Math.Abs(angle);
        float lengthPart = length / numbPositions;
        float heightPart = fixHeightDifference / (numbPositions - (2 * positionPercentage));

        // Die Positionen berechnen.
        for (int i = 1; i < numbPositions; i++)
        {
            // Die direction für den nächsten Schritt berechnen
            if (i > 1)
            {
                heading   = curvePositions[i - 1] - curvePositions[i - 2];
                heading.y = 0;
                direction = heading / heading.magnitude;
            }

            // Die letzte Position holen.
            Vector3 oldPosition = curvePositions[i - 1];

            // innerhalb des Prozent-Bereiches die Höhe anwenden.
            if (i > positionPercentage && i < (numbPositions - positionPercentage))
            {
                oldPosition.y += heightPart.Truncate(5);
            }

            // Die neue Position berechnen.
            curvePositions[i] = oldPosition + Quaternion.AngleAxis(anglePart, yAxis) * direction * lengthPart;
        }

        // Die Kurve erzeugen.
        ERRoad thisRoad = this.network.CreateRoad("Curve" + network.GetRoads().Count(), roadType, curvePositions);

        customEasyRoads.Add(new CustomEasyRoad(car, thisRoad, minCars, maxCars, numberOfTracks));
        return(thisRoad);
    }
示例#15
0
    public override void OnInspectorGUI()
    {
        _easyRoadNodes = (EasyRoadNodes)target;

        ERRoadNetwork network = new ERRoadNetwork();

        _easyRoadNodes.ERRoads = network.GetRoads().ToList();

        if (_easyRoadNodes.DontBuild.Count == 0)
        {
            for (int i = 0; i < _easyRoadNodes.ERRoads.Count; i++)
            {
                _easyRoadNodes.DontBuild.Add(false);
            }
        }

        if (_easyRoadNodes.Inverted.Count == 0)
        {
            for (int i = 0; i < _easyRoadNodes.ERRoads.Count; i++)
            {
                _easyRoadNodes.Inverted.Add(false);
            }
        }

        if (_easyRoadNodes.LinesCount.Count == 0)
        {
            for (int i = 0; i < _easyRoadNodes.ERRoads.Count; i++)
            {
                _easyRoadNodes.LinesCount.Add(2);
            }
        }

        if (_easyRoadNodes.DontBuild.Count != _easyRoadNodes.ERRoads.Count)
        {
            bool[] bools = new bool[_easyRoadNodes.DontBuild.Count];
            _easyRoadNodes.DontBuild.CopyTo(bools);
            _easyRoadNodes.DontBuild.Clear();

            for (int i = 0; i < _easyRoadNodes.ERRoads.Count; i++)
            {
                if (bools.Length > i)
                {
                    _easyRoadNodes.DontBuild.Add(bools[i]);
                }
                else
                {
                    _easyRoadNodes.DontBuild.Add(false);
                }
            }
        }

        if (_easyRoadNodes.Inverted.Count != _easyRoadNodes.ERRoads.Count)
        {
            bool[] bools = new bool[_easyRoadNodes.Inverted.Count];
            _easyRoadNodes.Inverted.CopyTo(bools);
            _easyRoadNodes.Inverted.Clear();

            for (int i = 0; i < _easyRoadNodes.ERRoads.Count; i++)
            {
                if (bools.Length > i)
                {
                    _easyRoadNodes.Inverted.Add(bools[i]);
                }
                else
                {
                    _easyRoadNodes.Inverted.Add(false);
                }
            }
        }

        if (_easyRoadNodes.OneWay.Count != _easyRoadNodes.ERRoads.Count)
        {
            bool[] bools = new bool[_easyRoadNodes.OneWay.Count];
            _easyRoadNodes.OneWay.CopyTo(bools);
            _easyRoadNodes.OneWay.Clear();

            for (int i = 0; i < _easyRoadNodes.ERRoads.Count; i++)
            {
                if (bools.Length > i)
                {
                    _easyRoadNodes.OneWay.Add(bools[i]);
                }
                else
                {
                    _easyRoadNodes.OneWay.Add(false);
                }
            }
        }

        if (_easyRoadNodes.LinesCount.Count != _easyRoadNodes.ERRoads.Count)
        {
            int[] ints = new int[_easyRoadNodes.LinesCount.Count];
            _easyRoadNodes.LinesCount.CopyTo(ints);
            _easyRoadNodes.LinesCount.Clear();

            for (int i = 0; i < _easyRoadNodes.ERRoads.Count; i++)
            {
                if (ints.Length > i)
                {
                    _easyRoadNodes.LinesCount.Add(ints[i]);
                }
                else
                {
                    _easyRoadNodes.LinesCount.Add(2);
                }
            }
        }

        GUILayout.BeginHorizontal();
        GUILayout.Label("Nodes count:", GUILayout.MaxWidth(230f));
        _easyRoadNodes.Count = EditorGUILayout.IntField(_easyRoadNodes.Count, GUILayout.MaxWidth(200f));
        GUILayout.FlexibleSpace();
        GUILayout.EndHorizontal();


        GUILayout.BeginHorizontal();
        GUILayout.Label("Range between nodes (in meters):", GUILayout.MaxWidth(230f));
        _easyRoadNodes.NodeDefinition = EditorGUILayout.FloatField(_easyRoadNodes.NodeDefinition, GUILayout.MaxWidth(200f));
        GUILayout.FlexibleSpace();
        GUILayout.EndHorizontal();


        GUILayout.BeginHorizontal();
        GUILayout.Label("Count of Bezier curve's points:", GUILayout.MaxWidth(230f));
        _easyRoadNodes.Definition = EditorGUILayout.IntField(_easyRoadNodes.Definition, GUILayout.MaxWidth(200f));
        GUILayout.FlexibleSpace();
        GUILayout.EndHorizontal();


        GUILayout.BeginHorizontal();
        GUILayout.Label("Range for connected nodes search:", GUILayout.MaxWidth(230f));
        _easyRoadNodes.MaximumDistance = EditorGUILayout.FloatField(_easyRoadNodes.MaximumDistance, GUILayout.MaxWidth(200f));
        GUILayout.FlexibleSpace();
        GUILayout.EndHorizontal();

        GUILayout.Space(10f);

        if (_easyRoadNodes.CrossRoads != null)
        {
            GUILayout.BeginHorizontal();
            GUILayout.Label("Crossroads: " + _easyRoadNodes.CrossRoads.Count, GUILayout.MaxWidth(230f));
            GUILayout.EndHorizontal();

            if (GUILayout.Button("Regenerate crossroads"))
            {
                _easyRoadNodes.RegenerateCrossroads();
            }
        }

        GUILayout.Space(10f);

        for (int i = 0; i < _easyRoadNodes.ERRoads.Count; i++)
        {
            GUILayout.BeginHorizontal();
            if (Selection.activeGameObject == _easyRoadNodes.ERRoads[i].roadScript.transform.gameObject)
            {
                GUILayout.Label("=>");
            }
            GUILayout.Label(_easyRoadNodes.ERRoads[i].roadScript.transform.name, GUILayout.MaxWidth(200f));
            _easyRoadNodes.DontBuild[i] = GUILayout.Toggle(_easyRoadNodes.DontBuild[i], " Ignore", GUILayout.MaxWidth(70f));
            GUILayout.Label("Lines count: ");
            _easyRoadNodes.LinesCount[i] = EditorGUILayout.IntField(_easyRoadNodes.LinesCount[i], GUILayout.MaxWidth(50f));
            _easyRoadNodes.OneWay[i]     = GUILayout.Toggle(_easyRoadNodes.OneWay[i], " One way", GUILayout.MaxWidth(70f));
            GUI.enabled = _easyRoadNodes.OneWay[i];
            _easyRoadNodes.Inverted[i] = GUILayout.Toggle(_easyRoadNodes.Inverted[i], " Invert", GUILayout.MaxWidth(70f));
            GUI.enabled = true;
            GUILayout.Space(5f);

            if (GUILayout.Button("Rebuild", GUILayout.MaxWidth(100f)))
            {
                _easyRoadNodes.GenerateOneRoad(i);
            }

            GUILayout.FlexibleSpace();
            GUILayout.EndHorizontal();
        }

        if (GUILayout.Button(_easyRoadNodes.Generate ? "Generating..." : "Build waypoint network"))
        {
            _easyRoadNodes.GenerateNetwork();
        }
    }
示例#16
0
    void Start()
    {
        Debug_Log.Call_WriteLog("Please read the comments at the top of the runtime script (/Assets/EasyRoads3D/Scripts/runtimeScript) before using the runtime API!");

        // Create Road Network object
        roadNetwork = new ERRoadNetwork();

        // Create road
        //	ERRoad road = roadNetwork.CreateRoad(string name);
        //	ERRoad road = roadNetwork.CreateRoad(string name, Vector3[] markers);
        //	ERRoad road = roadNetwork.CreateRoad(string name, ERRoadType roadType);
        //	ERRoad road = roadNetwork.CreateRoad(string name, ERRoadType roadType, Vector3[] markers);

        // get exisiting road types
        //  ERRoadType[] roadTypes = roadNetwork.GetRoadTypes();
        //  ERRoadType roadType = roadNetwork.GetRoadTypeByName(string name);


        // create a new road type
        ERRoadType roadType = new ERRoadType();

        roadType.roadWidth    = 6;
        roadType.roadMaterial = Resources.Load("Materials/roads/road material") as Material;
        // optional
        roadType.layer = 1;
        roadType.tag   = "Untagged";
        //   roadType.hasMeshCollider = false; // default is true

//		roadType = roadNetwork.GetRoadTypeByName("Train Rail");
//		Debug_Log.Call_WriteLog(roadType.roadMaterial);

        // create a new road
        Vector3[] markers = new Vector3[4];
        markers[0] = new Vector3(200, 5, 200);
        markers[1] = new Vector3(250, 5, 200);
        markers[2] = new Vector3(250, 5, 250);
        markers[3] = new Vector3(300, 5, 250);

        road = roadNetwork.CreateRoad("road 1", roadType, markers);

        // road.SetResolution(float value):void;


        // Add Marker: ERRoad.AddMarker(Vector3);
        road.AddMarker(new Vector3(300, 5, 300));

        // Add Marker: ERRoad.InsertMarker(Vector3);
        road.InsertMarker(new Vector3(275, 5, 235));
        //  road.InsertMarkerAt(Vector3 pos, int index): void;

        // Delete Marker: ERRoad.DeleteMarker(int index);
        road.DeleteMarker(2);


        // Set the road width : ERRoad.SetWidth(float width);
        //	road.SetWidth(10);

        // Set the road material : ERRoad.SetMaterial(Material path);
        //	Material mat = Resources.Load("Materials/roads/single lane") as Material;
        //	road.SetMaterial(mat);

        // add / remove a meshCollider component
        //   road.SetMeshCollider(bool value):void;

        // set the position of a marker
        //   road.SetMarkerPosition(int index, Vector3 position):void;
        //   road.SetMarkerPositions(Vector3[] position):void;
        //   road.SetMarkerPositions(Vector3[] position, int index):void;

        // get the position of a marker
        //   road.GetMarkerPosition(int index):Vector3;

        // get the position of a marker
        //   road.GetMarkerPositions():Vector3[];

        // Set the layer
        //   road.SetLayer(int value):void;

        // Set the tag
        //   road.SetTag(string value):void;

        // set marker control type
        //  road.SetMarkerControlType(int marker, ERMarkerControlType type) : bool; // Spline, StraightXZ, StraightXZY, Circular

        // find a road
        //  public static function ERRoadNetwork.GetRoadByName(string name) : ERRoad;

        // get all roads
        //  public static function ERRoadNetwork.GetRoads() : ERRoad[];

        // snap vertices to the terrain (no terrain deformation)
//		road.SnapToTerrain(true);

        // Build Road Network
        roadNetwork.BuildRoadNetwork();

        // remove script components
//		roadNetwork.Finalize();

        // Restore Road Network
        //	roadNetwork.RestoreRoadNetwork();

        // Show / Hide the white surfaces surrounding roads
        //     public function roadNetwork.HideWhiteSurfaces(bool value) : void;

        //   road.GetConnectionAtStart(): GameObject;
        //   road.GetConnectionAtStart(out int connection): GameObject; // connections: 0 = bottom, 1= tip, 2 = left, 3 = right (the same for T crossings)

        //   road.GetConnectionAtEnd(): GameObject;
        //   road.GetConnectionAtEnd(out int connection): GameObject; // connections: 0 = bottom, 1= tip, 2 = left, 3 = right (the same for T crossings)

        // Snap the road vertices to the terrain following the terrain shape (no terrain deformation)
        //   road.SnapToTerrain(bool value): void;
        //   road.SnapToTerrain(bool value, float yOffset): void;

        // get the road length
        //   road.GetLength() : float;

        // create dummy object
        go = GameObject.CreatePrimitive(PrimitiveType.Cube);
    }
示例#17
0
    private void CreateRoadEntities()
    {
        var roadEntityArchetype = entityManager.CreateArchetype(
            typeof(ERRoadTag),
            typeof(RoadDetails),
            typeof(LanePoints)
            );

        var connectionEntityArchetype = entityManager.CreateArchetype(
            typeof(ERConnectionTag),
            typeof(ConnectionDetails),
            typeof(LanePoints)
            );

        int           nextRoadIdentity = 0;
        int           nextLaneIndex    = 0;
        int           connectionIndexEnd;
        int           connectionIndexStart;
        int           connectionIdentityEnd   = 0;
        int           connectionIdentityStart = 0;
        ERConnection  erConnectionEnd;
        ERConnection  erConnectionStart;
        ERRoadNetwork roadNetwork = new ERRoadNetwork();

        ERRoad[]       roads                = roadNetwork.GetRoads();
        List <Vector3> allLanePoints        = new List <Vector3>();
        List <Vector3> connectionLanePoints = new List <Vector3>();
        ConnectedTo    connectedTo;


        //Make entities for each of these objects
        //Roads first.  They need a collection of Lanes and each lane has an input and output connection identity.
        //This collection of Road Identity & Lane Identity, or Connection Identity & Lane Identity
        //will be used in the JOB to get the next load of data
        foreach (ERRoad road in roads)
        {
            int roadIdentity = road.gameObject.GetComponent <ERRoadConnectionIdentity>().value;
            int lanes        = road.GetLaneCount();


            for (int lane = 0; lane < lanes; lane++)
            {
                ERLaneData erLaneData     = road.GetLaneData(lane);
                Vector3[]  roadLanePoints = erLaneData.points;


                if (erLaneData.direction == ERLaneDirection.Right)
                {
                    erConnectionEnd   = road.GetConnectionAtEnd(out connectionIndexEnd);
                    erConnectionStart = road.GetConnectionAtStart(out connectionIndexStart);
                }
                else
                {
                    erConnectionEnd   = road.GetConnectionAtStart(out connectionIndexEnd);
                    erConnectionStart = road.GetConnectionAtEnd(out connectionIndexStart);
                }

                connectionIdentityStart = RoadConnectionIdentity(erConnectionStart.gameObject);
                connectionIdentityEnd   = RoadConnectionIdentity(erConnectionEnd.gameObject);

                //Create the Road/Lane entity
                Entity roadEntity = entityManager.CreateEntity(roadEntityArchetype);
                entityManager.SetComponentData(
                    roadEntity,
                    new RoadDetails
                {
                    RoadIdentity            = roadIdentity,
                    LaneIndex               = erLaneData.laneIndex,
                    ConnectionIdentityEnd   = connectionIdentityEnd,
                    ConnectionIdentityStart = connectionIdentityStart,
                    ConnectionIndexEnd      = connectionIndexEnd,
                    ConnectionIndexStart    = connectionIndexStart
                });

                //Get and store the road's lane points
                var roadLanePointsBuffer = entityManager.GetBuffer <LanePoints>(roadEntity);
                var roadLanePoint        = new LanePoints();

                for (int point = 0; point < roadLanePoints.Length; point++)
                {
                    roadLanePoint.value = roadLanePoints[point];
                    roadLanePointsBuffer.Add(roadLanePoint);
                }
                //---------------------------------------------------

                //Create this Road/Lane/Connection combo
                ERLaneConnector[] laneConnectors = erConnectionEnd.GetLaneData(connectionIndexEnd, erLaneData.laneIndex);

                for (int i = 0; i < laneConnectors.Length; i++)
                {
                    var laneConnector = laneConnectors[i];

                    var endRoad         = erConnectionEnd.GetConnectedRoad(laneConnector.endConnectionIndex, out connectedTo);
                    var roadIdentityEnd = RoadConnectionIdentity(endRoad.gameObject);

                    Entity connectionEntity = entityManager.CreateEntity(connectionEntityArchetype);
                    entityManager.SetComponentData(
                        connectionEntity,
                        new ConnectionDetails
                    {
                        ConnectionIdentity   = connectionIdentityEnd,
                        LaneIndexStart       = erLaneData.laneIndex,
                        LaneIndexEnd         = laneConnector.endLaneIndex,
                        RoadIdentityStart    = roadIdentity,
                        RoadIdentityEnd      = roadIdentityEnd,
                        ConnectionIndexEnd   = laneConnector.endConnectionIndex,
                        ConnectionIndexStart = connectionIndexStart
                    });

                    var connectionLanePointsBuffer = entityManager.GetBuffer <LanePoints>(connectionEntity);
                    var connectionLanePoint        = new LanePoints();

                    for (int x = 0; x < laneConnector.points.Length; x++)
                    {
                        connectionLanePoint.value = laneConnector.points[x];
                        connectionLanePointsBuffer.Add(connectionLanePoint);
                    }
                }
                //---------------------------------------------------

                //Pick a connection and one of the output values to give to the cars that are about to be created
                int randomValue = UnityEngine.Random.Range(0, laneConnectors.Length);
                var laneConnect = laneConnectors[randomValue];
                var exitRoad    = erConnectionEnd.GetConnectedRoad(laneConnect.endConnectionIndex, out connectedTo);

                nextLaneIndex      = laneConnect.endLaneIndex;
                nextRoadIdentity   = RoadConnectionIdentity(exitRoad.gameObject);
                connectionIndexEnd = laneConnect.endConnectionIndex;

                connectionLanePoints = laneConnect.points.ToList();

                allLanePoints.Clear();
                allLanePoints.AddRange(roadLanePoints);
                allLanePoints.AddRange(connectionLanePoints);
                connectionLanePoints.Clear();

                //Option to not fill every lane
                int random = UnityEngine.Random.Range(0, 100);

                if (random <= percentageLanesPopulated)
                {
                    CreateAutoEntity(
                        allLanePoints,
                        nextLaneIndex,
                        nextRoadIdentity,
                        connectionIdentityEnd,
                        connectionIndexEnd);
                }
            }
        }

        allLanePoints.Clear();
        connectionLanePoints.Clear();
    }