Example #1
0
        /// <summary>
        /// Redraws the cities. This is automatically called by Redraw(). Used internally by the Map Editor. You should not need to call this method directly.
        /// </summary>
        public void DrawCities()
        {
            if (!_showCities || !gameObject.activeInHierarchy || cities == null)
            {
                return;
            }

            // Create cities layer
            Transform t = transform.Find("Cities");

            if (t != null)
            {
                DestroyImmediate(t.gameObject);
            }
            citiesLayer = new GameObject("Cities");
            citiesLayer.transform.SetParent(transform, false);
            if (_earthInvertedMode)
            {
                citiesLayer.transform.localScale *= 0.99f;
            }

            // Create cityclass parents
            GameObject countryCapitals = new GameObject("Country Capitals");

            countryCapitals.hideFlags = HideFlags.DontSave;
            countryCapitals.transform.SetParent(citiesLayer.transform, false);
            GameObject regionCapitals = new GameObject("Region Capitals");

            regionCapitals.hideFlags = HideFlags.DontSave;
            regionCapitals.transform.SetParent(citiesLayer.transform, false);
            GameObject normalCities = new GameObject("Normal Cities");

            normalCities.hideFlags = HideFlags.DontSave;
            normalCities.transform.SetParent(citiesLayer.transform, false);
            bool    combineMeshesActive = _combineCityMeshes && Application.isPlaying;
            float   scale     = CityScaler.GetScale(this);
            Vector3 cityScale = new Vector3(scale, scale, 1f);

            // Draw city marks
            _numCitiesDrawn = 0;
            int minPopulation = _minPopulation * 1000;
            int visibleCount  = 0;

            // flip localscale.x to prevent transform issues
            if (_earthInvertedMode)
            {
                transform.localScale = new Vector3(-transform.localScale.x, transform.localScale.y, transform.localScale.z);
            }

            int cityCount = cities.Count;

            for (int k = 0; k < cityCount; k++)
            {
                City    city    = cities [k];
                Country country = countries [city.countryIndex];
                city.isShown = !country.hidden && ((((int)city.cityClass & _cityClassAlwaysShow) != 0) || (minPopulation == 0 || city.population >= minPopulation));
                if (city.isShown)
                {
                    GameObject cityObj, cityParent;
                    switch (city.cityClass)
                    {
                    case CITY_CLASS.COUNTRY_CAPITAL:
                        cityObj = Instantiate(citySpotCapitalCountry);
                        if (!combineMeshesActive)
                        {
                            cityObj.GetComponent <Renderer> ().sharedMaterial = citiesCountryCapitalMat;
                        }
                        cityParent = countryCapitals;
                        break;

                    case CITY_CLASS.REGION_CAPITAL:
                        cityObj = Instantiate(citySpotCapitalRegion);
                        if (!combineMeshesActive)
                        {
                            cityObj.GetComponent <Renderer> ().sharedMaterial = citiesRegionCapitalMat;
                        }
                        cityParent = regionCapitals;
                        break;

                    default:
                        cityObj = Instantiate(citySpot);
                        if (!combineMeshesActive)
                        {
                            cityObj.GetComponent <Renderer> ().sharedMaterial = citiesNormalMat;
                        }
                        cityParent = normalCities;
                        break;
                    }
                    cityObj.hideFlags = HideFlags.DontSave | HideFlags.HideInHierarchy;
                    cityObj.transform.SetParent(cityParent.transform, false);
                    cityObj.transform.localPosition = city.unitySphereLocation;
                    cityObj.transform.localScale    = cityScale;
                    if (_earthInvertedMode)
                    {
                        cityObj.transform.LookAt(transform.TransformPoint(city.unitySphereLocation * 2f));
                    }
                    else
                    {
                        cityObj.transform.LookAt(transform.position);
                    }
                    city.gameObject = cityObj;
                    _numCitiesDrawn++;
                    visibleCount++;
                }
                else
                {
                    city.gameObject = null;
                }
            }

            if (_earthInvertedMode)
            {
                transform.localScale = new Vector3(-transform.localScale.x, transform.localScale.y, transform.localScale.z);
            }

            // Cache visible cities (this is faster than iterate through the entire collection)
            if (visibleCities == null || visibleCities.Length != visibleCount)
            {
                visibleCities = new City[visibleCount];
            }

            for (int k = 0; k < cityCount; k++)
            {
                City city = cities [k];
                if (city.isShown)
                {
                    visibleCities [--visibleCount] = city;
                }
            }

            // Toggle cities layer visibility according to settings
            citiesLayer.SetActive(_showCities);

            CityScaler cityScaler = citiesLayer.GetComponent <CityScaler> () ?? citiesLayer.AddComponent <CityScaler> ();

            cityScaler.map = this;
            cityScaler.ScaleCities();

            if (combineMeshesActive)
            {
                DestroyImmediate(cityScaler);
                CombineMeshes(normalCities, citiesNormalMat);
                CombineMeshes(regionCapitals, citiesRegionCapitalMat);
                CombineMeshes(countryCapitals, citiesCountryCapitalMat);
            }
        }