Ejemplo n.º 1
0
    private bool TestExtending(RoadModule aRoadModule)
    {
        Vector2    closestIntersection = Vector2.one * float.MinValue;
        float      shortestDistance    = float.MaxValue;
        RoadModule intersectedRoad     = null;
        Vector2    start1 = new Vector2(aRoadModule.mPosition.x, aRoadModule.mPosition.z);
        Vector2    end1   = new Vector2(
            aRoadModule.mPosition.x + Mathf.Cos(aRoadModule.mDirection * Mathf.Deg2Rad) * aRoadModule.mLength * 1.5f,
            aRoadModule.mPosition.z + Mathf.Sin(aRoadModule.mDirection * Mathf.Deg2Rad) * aRoadModule.mLength * 1.5f);

        start1 += (end1 - start1).normalized * 0.001f;
        Vector2 intersection = Vector2.zero;

        for (int i = 0; i < mModules.Count; ++i)
        {
            Vector2 start2 = new Vector2(mModules[i].mPosition.x, mModules[i].mPosition.z);
            Vector2 end2   = new Vector2(mModules[i].End.x, mModules[i].End.z);
            if (NeroUtilities.LineLineIntersection(start1, end1, start2, end2, out intersection) == true)
            {
                float distance = Vector2.Distance(start1, intersection);
                if (distance < shortestDistance)
                {
                    closestIntersection = intersection;
                    shortestDistance    = distance;
                    intersectedRoad     = mModules[i];
                }
            }
        }
        if (intersectedRoad != null)
        {
            aRoadModule.mLength = Vector3.Distance(aRoadModule.mPosition, new Vector3(closestIntersection.x, 0.0f, closestIntersection.y));
            mCrossings.Add(aRoadModule.End);
            Debug.Log("TestExtending -> Creating crossing.");
            if (aRoadModule.mLength < 0.000001f)
            {
                Debug.LogWarning("TestExtending -> Road segment length is 0.");
            }

            float originalLength = intersectedRoad.mLength;
            float newLength      = Vector3.Distance(intersectedRoad.mPosition, new Vector3(closestIntersection.x, 0.0f, closestIntersection.y));
            intersectedRoad.mLength = newLength;
            RoadModule newModule = new RoadModule(0, intersectedRoad.End, originalLength - newLength, intersectedRoad.mDirection);
            mModules.Add(newModule);
            return(true);
        }
        return(false);
    }
Ejemplo n.º 2
0
    private void InitLists()
    {
        mQueue.Clear();
        mModules.Clear();
        mCrossings.Clear();
        mNodes.Clear();

        RoadModule initialRoadModule = new RoadModule();

        initialRoadModule.mPriority  = 0;
        initialRoadModule.mPosition  = Vector3.zero;
        initialRoadModule.mLength    = 10.0f;
        initialRoadModule.mDirection = 0.0f;
        mQueue.Add(initialRoadModule);

        mQueue = mQueue.OrderBy(i => i.mPriority).ToList();

        //StopAllCoroutines();
    }
Ejemplo n.º 3
0
    private void GlobalGoals(RoadModule aRoadModule)
    {
        mQueue.Add(new RoadModule(aRoadModule.mPriority + 1, aRoadModule.End, 10.0f, aRoadModule.mDirection + Random.Range(-10.0f, 10.0f)));
        bool crossingCreated = false;
        int  splitChance     = 15;

        if (Random.Range(0, 100) < splitChance)
        {
            mQueue.Add(new RoadModule(aRoadModule.mPriority + 1, aRoadModule.End, 10.0f, aRoadModule.mDirection + 90.0f + Random.Range(-10.0f, 10.0f)));
            crossingCreated = true;
        }
        if (Random.Range(0, 100) < splitChance)
        {
            mQueue.Add(new RoadModule(aRoadModule.mPriority + 1, aRoadModule.End, 10.0f, aRoadModule.mDirection - 90.0f + Random.Range(-10.0f, 10.0f)));
            crossingCreated = true;
        }
        if (crossingCreated == true)
        {
            mCrossings.Add(aRoadModule.End);
            Debug.Log("GlobalGoals -> Creating crossing.");
        }
    }
Ejemplo n.º 4
0
    private IEnumerator Coroutine_Generate()
    {
        InitLists();
        while (mQueue.Count > 0)
        {
            Debug.Log("Generating");
            mQueryRoad = mQueue[0];
            mQueue.RemoveAt(0);
            eRoadQueryResult result = LocalConstraints(mQueryRoad);
            if (result != eRoadQueryResult.FAIL)
            {
                mModules.Add(mQueryRoad);
                if (result == eRoadQueryResult.SUCCESS)
                {
                    GlobalGoals(mQueryRoad);
                }
            }
            yield return(new WaitForEndOfFrame());
        }
        for (int i = 0; i < mModules.Count; ++i)
        {
            if (mModules[i].mLength <= 0.000001f)
            {
                Debug.Log("Road with no length!");
            }

            GetNode(mModules[i].mPosition).AddConnection(GetNode(mModules[i].End));
            GetNode(mModules[i].End).AddConnection(GetNode(mModules[i].mPosition));
            if (mModules[i].mBadRoad == true)
            {
                GetNode(mModules[i].mPosition).mBadNode = true;
                GetNode(mModules[i].End).mBadNode       = true;
            }
            yield return(new WaitForEndOfFrame());
        }
        GetComponent <ParcelGenerator>().GenerateParcels(mNodes.Values.ToList());
        yield return(new WaitForEndOfFrame());
    }
Ejemplo n.º 5
0
    private bool TestCrossing(RoadModule aRoadModule)
    {
        Vector2 closestPoint     = Vector3.one * float.MinValue;
        float   shortestDistance = float.MaxValue;
        Vector2 start            = new Vector2(aRoadModule.mPosition.x, aRoadModule.mPosition.z);
        Vector2 end               = new Vector2(aRoadModule.End.x, aRoadModule.End.z);
        Vector2 intersection      = Vector2.zero;
        bool    foundIntersection = false;

        for (int i = 0; i < mCrossings.Count; ++i)
        {
            Vector2 crossing = new Vector2(mCrossings[i].x, mCrossings[i].z);
            if (NeroUtilities.PointCircleIntersection(end, crossing, aRoadModule.mLength * 0.75f) == true)
            {
                float distance = Vector2.Distance(end, crossing);
                if (distance < shortestDistance && bgriegebri(start, crossing) == true)
                {
                    foundIntersection = true;
                    closestPoint      = crossing;
                    shortestDistance  = distance;
                }
            }
        }
        if (foundIntersection == true)
        {
            aRoadModule.mLength = Vector2.Distance(start, closestPoint);
            Vector2 delta = closestPoint - start;
            aRoadModule.mDirection = Mathf.Atan2(delta.y, delta.x) * Mathf.Rad2Deg;
            Debug.Log("TestCrossing -> Snapping to crossing.");
            if (aRoadModule.mLength < 0.000001f)
            {
                Debug.LogWarning("TestCrossing -> Road segment length is 0.");
            }
            return(true);
        }
        return(false);
    }
Ejemplo n.º 6
0
    private eRoadQueryResult LocalConstraints(RoadModule aRoadModule)
    {
        eRoadQueryResult result = eRoadQueryResult.SUCCESS;
        bool             valid  = false;
        int debugID             = -1;

        if (aRoadModule.End.x < -mWorldSize || aRoadModule.End.x > mWorldSize || aRoadModule.End.z < -mWorldSize || aRoadModule.End.z > mWorldSize)
        {
            return(eRoadQueryResult.FAIL);
        }

        if (TestIntersection(aRoadModule) == true)
        {
            result  = eRoadQueryResult.SUCCESS_AND_STOP;
            valid   = true;
            debugID = 1;
        }

        if (valid == false && TestCrossing(aRoadModule) == true)
        {
            result  = eRoadQueryResult.SUCCESS_AND_STOP;
            valid   = true;
            debugID = 2;
        }

        if (valid == false && TestExtending(aRoadModule) == true)
        {
            result  = eRoadQueryResult.SUCCESS_AND_STOP;
            valid   = true;
            debugID = 3;
        }

        Vector2 start1 = new Vector2(aRoadModule.mPosition.x, aRoadModule.mPosition.z);
        Vector2 end1   = new Vector2(aRoadModule.End.x, aRoadModule.End.z);

        for (int i = 0; i < mModules.Count; ++i)
        {
            Vector2 start2 = new Vector2(mModules[i].mPosition.x, mModules[i].mPosition.z);
            Vector2 end2   = new Vector2(mModules[i].End.x, mModules[i].End.z);

            if ((start1 == start2 && end1 == end2) || (start1 == end2 && start2 == end1))
            {
                valid = false;
                aRoadModule.mBadRoad = true;
                Debug.Log("OVERLAPPING ROAD");
            }
            else
            {
                ShortenLine(ref start1, ref end1, 0.1f);
                ShortenLine(ref start2, ref end2, 0.1f);
                Vector2 intersection = Vector2.zero;
                if (NeroUtilities.LineLineIntersection(start1, end1, start2, end2, out intersection) == true)
                {
                    valid = false;
                    aRoadModule.mBadRoad = true;
                    Debug.Log("BAD ROAD");
                }
            }
        }

        return(result);
    }