Exemplo n.º 1
0
    private StartPointClass2 FindNearestStartingPoint(Vector3 point, bool _withNoKind = false)
    {
        double           min_value   = quarterDistance;
        StartPointClass2 returnPoint = startingPoints[0];

        foreach (var startPoint in startingPoints)
        {
            if (_withNoKind && startPoint.kind != 0)
            {
                continue;
            }

            if ((startPoint.position - point).magnitude * (1 / startPoint.sizeKoeff) < min_value)
            {
                min_value   = (startPoint.position - point).magnitude;
                returnPoint = startPoint;
            }
        }

        return(returnPoint);
    }
Exemplo n.º 2
0
    //=================================================
    private void SpawnRoad()
    {
        var rt   = (RectTransform)Map.transform;
        var xMap = Map.transform.position.x;
        var yMap = Map.transform.position.y;

        var     RoadQuat         = Map.transform.rotation;
        var     xPoints          = 5f;
        var     yPoints          = 5f;
        var     streetsFromPoint = 3;
        var     l = 0;
        Vector3 RoadBeg;
        var     neededTryes = 250;

        while (l < neededTryes)
        {
            l = 0;

            var xPlace = Random.Range(width * (1 / xPoints), width * ((xPoints - 1) / xPoints));
            var yPlace = Random.Range(height * (1 / yPoints), height * ((yPoints - 1) / yPoints));
            RoadBeg = new Vector3(xPlace, yPlace);
            while (CheckStartingPointsToThis(RoadBeg))
            {
                xPlace  = Random.Range(Convert.ToSingle(quarterDistance1), Convert.ToSingle(width - quarterDistance1));
                yPlace  = Random.Range(Convert.ToSingle(quarterDistance1), Convert.ToSingle(height - quarterDistance1));
                RoadBeg = new Vector3(xPlace, yPlace);
                l++;
                if (l >= neededTryes)
                {
                    break;
                }
            }

            if (l < neededTryes)
            {
                startingPoints.Add(new StartPointClass2(RoadBeg, 0));
            }
        }

        ///Рассчитываем построения изначальных дорог
        foreach (var startPoint in startingPoints)
        {
            List <StartPointClass2> otherPoints = startingPoints;

            //выбираем ближайшие точки
            List <Vector3> posList = new List <Vector3>();

            posList.Add(startPoint.position);
            otherPoints.Sort((v1, v2) =>
                             (startPoint.position - v1.position).magnitude.CompareTo((startPoint.position - v2.position).magnitude));
            int streetsFrom = Random.Range(streetsFromPoint - 1, streetsFromPoint + 1);
            for (int i = 1, j = 0; j < streetsFrom && i < otherPoints.Count; i++)
            {
                // posList.Add(otherPoints[i].position);

                Quaternion q = new Quaternion();
                Vector3    vectorToTarget = otherPoints[i].position - startPoint.position;
                float      angle          = Mathf.Atan2(vectorToTarget.y, vectorToTarget.x) * Mathf.Rad2Deg;

                q = Quaternion.AngleAxis(angle, Vector3.forward);
                float magn = (otherPoints[i].position - startPoint.position).magnitude;
                if (CheckDistance(startPoint.position, q * Quaternion.Euler(0, 0, -90) * Vector3.up, magn) != true)
                {
                    continue;
                }

                ///я правда не знаю почему все дороги надо поворачивать на 90 градусов.
                startPoint.quat.Add(q * Quaternion.Euler(0, 0, -90));
                j++;
            }

            if (startPoint.kind == 1 && startPoint.quat.Count > 1)
            {
                startPoint.quat.RemoveAt(0);
            }
        }

        StartPointClass2 startPoint1 = FindNearestStartingPoint(new Vector3(width / 2, height / 2), true);

        startPoint1.kind      = 5;
        startPoint1.sizeKoeff = 2;

        foreach (var StartPointer in startingPoints)
        {
            foreach (var quat in StartPointer.quat)
            {
                streetCounter++;
                var _street = new Street();
                spawnedStreets.Add(_street);
                _street.Name = streetNo.ToString();
                streetNo++;
                GenerateRoad(StartPointer.position, quat, rt, _street, true, 0, 1.5f /*, true*/);
            }
        }
    }