Пример #1
0
        public GameObject GetBuilding(Vector2 midpoint, Vector2 faceSize)
        {
            if (midpoint.x >= heightMap.Width || midpoint.y >= heightMap.Height || midpoint.x < 0 || midpoint.y < 0) return null;// buildingTypes[0].building;

            float heightValue = heightMap.Values[(int)midpoint.x, (int)midpoint.y];
            int buildingIndex = FindBestTerrain(heightValue);

            GameObject buildingToSpawn = buildingTypes[buildingIndex].building;
            CityBlockGenerator blockGenerator = buildingToSpawn.GetComponent<CityBlockGenerator>();

            if (blockGenerator != null)
            {
                if (blockGenerator.MinLotSize.x < faceSize.x && blockGenerator.MinLotSize.y < faceSize.y)
                {
                    return blockGenerator.gameObject;
                }
            }
            else
            {
                BoxCollider collider = buildingToSpawn.GetComponent<BoxCollider>();
                if (collider != null)
                {
                    if (collider.size.x < faceSize.x && collider.size.z < faceSize.y)
                    {
                        return buildingToSpawn;
                    }
                }
            }

            return null;
        }
Пример #2
0
        void GenerateBuildings()
        {
            Debug.Log("PerlinCityGenerator: Generating Buildings");
            List <GridFace> faces = polyGrid.GetFaces();

            foreach (GridFace face in faces)
            {
                Vector2 bottomLeft = face.GetVertex(0).GetPositionXZ();
                Vector2 midpoint   = face.GetPositionXZ();

                Vector2 faceSize = new Vector2((midpoint.x - bottomLeft.x) * 2, (midpoint.y - bottomLeft.y) * 2);

                //if (CheckSlope(bottomLeft))
                //{
                GameObject district = buildingGenerator.GetBuilding(midpoint, faceSize);
                if (district != null)
                {
                    GameObject instance = GameObject.Instantiate(district, transform);

                    float height = terrainGenerator.terrain.SampleHeight(new Vector3(midpoint.x + transform.root.position.x, 0, midpoint.y + transform.root.position.z));
                    instance.transform.localPosition = new Vector3(midpoint.x, height, midpoint.y);

                    CityBlockGenerator districtBlockGenerator = instance.GetComponent <CityBlockGenerator>();
                    if (districtBlockGenerator != null)
                    {
                        districtBlockGenerator.Dimensions = new Vector3(faceSize.x, 0, faceSize.y);
                        districtBlockGenerator.terrain    = terrainGenerator.terrain;
                        List <Node> verts      = face.GetVertices();
                        GridNode    parentNode = new GridNode(new Vector3(midpoint.x, 0, midpoint.y), ref verts);
                        districtBlockGenerator.districtNode = parentNode;
                        districtBlockGenerator.Setup();
                        districtBlockGenerator.Generate();
                    }


                    //Transform child = districtBlockGenerator.gameObject.transform.GetChild(0);
                    //child.localPosition = Vector3.zero;
                    //instance.transform.localPosition = new Vector3(bottomLeft.x, 0, bottomLeft.y);
                }
                //}
            }
        }