public void Generate()
    {
        float step = CityConfig.WIDTH / RESOLUTION;

        for (float i = CityConfig.X_START;
             i < CityConfig.X_START + CityConfig.WIDTH;
             i += step)
        {
            for (float j = CityConfig.Y_START;
                 j < CityConfig.Y_START + CityConfig.HEIGHT;
                 j += step)
            {
                Transform heatSquare = Instantiate(prefab);
                heatSquare.localScale    = new Vector3(0.1f * step, 1f, 0.1f * step);
                heatSquare.localPosition = new Vector3(i + step * 0.5f, -0.1f, j + step * 0.5f);

                float pop          = Heatmap.Value(heatSquare.localPosition);
                Color color        = Color.Lerp(minColor, maxColor, pop);
                var   meshRenderer = heatSquare.GetComponent <MeshRenderer>();
                meshRenderer.material.color = color;
            }
        }
    }
示例#2
0
    public void CreateLot(List <Vector3> corners)
    {
        for (int i = 0; i < corners.Count; i++)
        {
            corners[i] -= Vector3.up * CityConfig.BUILDING_UNDEGROUND_DEPTH;
        }
        Lot   lot    = Instantiate(lotPrefab);
        float height = Heatmap.Value(corners[0]);

        height -= CityConfig.STREET_BRANCH_POPULATION_THRESHOLD;
        if (height < 0)
        {
            height = 0;
        }
        height /= (1f - CityConfig.STREET_BRANCH_POPULATION_THRESHOLD);
        height *= CityConfig.BUILDING_HEIGHT_MAX;
        height += CityConfig.BUILDING_HEIGHT_VARIANCE * (UnityEngine.Random.value - .5f);
        height += CityConfig.BUILDING_UNDEGROUND_DEPTH;
        lot.Initialize(corners, height, sharedMaterial);
        if (printDebug)
        {
            print("found");
        }
    }
示例#3
0
    public List <Road> Generate(Road prevSegment, RoadMap roadMap)
    {
        var newRoads = new List <Road>();

        if (prevSegment.severed)
        {
            return(newRoads);
        }

        if (segFactory == null)
        {
            Initialize();
        }
        Road continueStraight = segFactory.CreateRoad(
            prevSegment.end,
            prevSegment.transform.localRotation,
            prevSegment.length,
            prevSegment.t + 1,
            prevSegment.type
            );
        float straightPop = Heatmap.Value(continueStraight.end);

        if (prevSegment.type == RoadType.Highway)
        {
            // generate a random deviation and compare to going straight,
            // chosing the road that leads to the highest population
            float newAngle = prevSegment.transform.localEulerAngles.y + CityConfig.RandomStraightAngle();

            Road randomStraight = segFactory.CreateRoad(
                prevSegment.end,
                Quaternion.Euler(prevSegment.transform.localEulerAngles.x, newAngle, 0),
                prevSegment.length,
                prevSegment.t + 1,
                prevSegment.type
                );

            float randomPop = Heatmap.Value(randomStraight.end);
            float roadPop;
            Road  straight;
            if (randomPop > straightPop)
            {
                newRoads.Add(randomStraight);
                roadPop  = randomPop;
                straight = randomStraight;
                DestroyImmediate(continueStraight.gameObject);
            }
            else
            {
                newRoads.Add(continueStraight);
                roadPop  = straightPop;
                straight = continueStraight;
                DestroyImmediate(randomStraight.gameObject);
            }

            // highway branches off highway
            if (roadPop > CityConfig.HIGHWAY_BRANCH_POPULATION_THRESHOLD)
            {
                Junction j = null;
                if (Random.value < CityConfig.HIGHWAY_BRANCH_PROBABILITY)
                {
                    Road leftRoad = AddBranch(
                        prevSegment,
                        straight,
                        j,
                        prevSegment.transform.localEulerAngles.y - 90 + CityConfig.RandomBranchAngle(),
                        prevSegment.length,
                        prevSegment.t + 1,
                        prevSegment.type
                        );
                    newRoads.Add(leftRoad);
                }
                if (Random.value < CityConfig.HIGHWAY_BRANCH_PROBABILITY)
                {
                    Road rightRoad = AddBranch(
                        prevSegment,
                        straight,
                        j,
                        prevSegment.transform.localEulerAngles.y + 90 + CityConfig.RandomBranchAngle(),
                        prevSegment.length,
                        prevSegment.t + 1,
                        prevSegment.type
                        );
                    newRoads.Add(rightRoad);
                }
            }
        }
        else if (straightPop > CityConfig.STREET_BRANCH_POPULATION_THRESHOLD)
        {
            // do not delete continueStraight
            newRoads.Add(continueStraight);
        }
        else
        {
            DestroyImmediate(continueStraight.gameObject);
        }

        // street branches off either highway or streets
        if (straightPop > CityConfig.STREET_BRANCH_POPULATION_THRESHOLD)
        {
            int t = (prevSegment.type == RoadType.Highway) ? CityConfig.STREET_FROM_HIGHWAY_DELAY : 1;

            Junction j = null;
            if (Random.value < CityConfig.STREET_BRANCH_PROBABILITY)
            {
                Road leftRoad = AddBranch(
                    prevSegment,
                    continueStraight,
                    j,
                    prevSegment.transform.localEulerAngles.y - 90 + CityConfig.RandomBranchAngle(),
                    CityConfig.STREET_SEGMENT_LENGTH,
                    prevSegment.t + t,
                    RoadType.Street
                    );
                newRoads.Add(leftRoad);
            }
            if (Random.value < CityConfig.STREET_BRANCH_PROBABILITY)
            {
                Road leftRoad = AddBranch(
                    prevSegment,
                    continueStraight,
                    j,
                    prevSegment.transform.localEulerAngles.y + 90 + CityConfig.RandomBranchAngle(),
                    CityConfig.STREET_SEGMENT_LENGTH,
                    prevSegment.t + t,
                    RoadType.Street
                    );
                newRoads.Add(leftRoad);
            }
        }

        // set up links between roads
        foreach (Road r in newRoads)
        {
            r.Parent = prevSegment;
            prevSegment.next.Add(new Road.Neighbour(r, true));
            foreach (Road r_other in newRoads)
            {
                if (r_other.id != r.id)
                {
                    r.prev.Add(new Road.Neighbour(r_other, false));
                }
            }
        }

        return(newRoads);
    }