Esempio n. 1
0
        /// <summary>
        /// Attempts to force all buildings in a district to meet the district's minimum or maximum target level, as specified.
        /// </summary>
        /// <param name="districtID">Target district</param>
        /// <param name="upgrade">True if we want to upgrade all buildings (below the minimum) to the minimum for this district, false if we want to downgrade (all buildings above the maximum) to the maximum</param>
        private void LevelDistrict(ushort districtID, bool upgrade)
        {
            // Instances and arrays.
            Array16 <Building> buildings       = Singleton <BuildingManager> .instance.m_buildings;
            DistrictManager    districtManager = Singleton <DistrictManager> .instance;

            // Iterate through all buildings in map.
            for (ushort i = 0; i < buildings.m_size; ++i)
            {
                // Get local reference.
                Building thisBuilding = buildings.m_buffer[i];

                // Skip non-existent buildings or non-Private AI buildings.
                if (thisBuilding.m_flags != Building.Flags.None || !(thisBuilding.Info?.GetAI() is PrivateBuildingAI))
                {
                    // Building exists; get its district and see if it matches the target district.
                    if (districtManager.GetDistrict(thisBuilding.m_position) == districtID)
                    {
                        // It's in our district.
                        // Are we upgrading or downgrading?
                        if (upgrade)
                        {
                            // Upgrading.
                            byte minLevel = (thisBuilding.Info?.GetService() == ItemClass.Service.Residential ? DistrictsABLC.minResLevel[districtID] : DistrictsABLC.minWorkLevel[districtID]);

                            // It's in our district; check if its level is less than the relevant minimum.
                            if (buildings.m_buffer[i].m_level < minLevel)
                            {
                                // It needs to be upgraded; store copy of current index for action queue.
                                ushort buildingID = i;

                                // Upgrade.
                                LevelUtils.ForceLevel(buildingID, minLevel);
                            }
                        }
                        else
                        {
                            // Downgrading.
                            byte maxLevel = (thisBuilding.Info?.GetService() == ItemClass.Service.Residential ? DistrictsABLC.maxResLevel[districtID] : DistrictsABLC.maxWorkLevel[districtID]);

                            // It's in our district; check if its level is less than the relevant minimum.
                            if (buildings.m_buffer[i].m_level > maxLevel)
                            {
                                // It needs to be upgraded; store copy of current index for action queue.
                                ushort buildingID = i;

                                // Downgrade.
                                LevelUtils.ForceLevel(buildingID, maxLevel);
                            }
                        }
                    }
                }
            }
        }
        /// <summary>
        /// Performs initial setup for the panel; we don't use Start() as that's not sufficiently reliable (race conditions), and is not needed with the dynamic create/destroy process.
        /// </summary>
        /// <param name="parentTransform">Transform to attach to</param>
        internal override void Setup(Transform parentTransform)
        {
            try
            {
                base.Setup(parentTransform);

                // Set initial building.
                BuildingChanged();

                // Add event handlers.
                minLevelDropDown.eventSelectedIndexChanged += (control, index) =>
                {
                    // Don't do anything if events are disabled.
                    if (!disableEvents)
                    {
                        // Set minimum level of building in dictionary.
                        UpdateMinLevel((byte)index);

                        // If the minimum level is now greater than the maximum level, increase the maximum to match the minimum.
                        if (index > maxLevelDropDown.selectedIndex)
                        {
                            maxLevelDropDown.selectedIndex = index;
                        }
                    }
                };

                maxLevelDropDown.eventSelectedIndexChanged += (control, index) =>
                {
                    // Don't do anything if events are disabled.
                    if (!disableEvents)
                    {
                        // Update maximum level.
                        UpdateMaxLevel((byte)index);

                        // If the maximum level is now less than the minimum level, reduce the minimum to match the maximum.
                        if (index < minLevelDropDown.selectedIndex)
                        {
                            minLevelDropDown.selectedIndex = index;
                        }
                    }
                };

                upgradeButton.eventClick += (control, clickEvent) =>
                {
                    LevelUtils.ForceLevel(targetID, upgradeLevel);

                    // Check to see if we should increase this buildings maximum level.
                    byte newLevel = Singleton <BuildingManager> .instance.m_buildings.m_buffer[targetID].m_level;
                    if (BuildingsABLC.levelRanges.ContainsKey(targetID) && BuildingsABLC.levelRanges[targetID].maxLevel < newLevel)
                    {
                        //BuildingsABLC.levelRanges[targetID].maxLevel = newLevel;
                        maxLevelDropDown.selectedIndex = newLevel;
                    }

                    // Update the panel once done.
                    UpdatePanel();
                };

                downgradeButton.eventClick += (control, clickEvent) =>
                {
                    LevelUtils.ForceLevel(targetID, downgradeLevel);

                    // Check to see if we should increase this buildings maximum level.
                    byte newLevel = Singleton <BuildingManager> .instance.m_buildings.m_buffer[targetID].m_level;
                    if (BuildingsABLC.levelRanges.ContainsKey(targetID) && BuildingsABLC.levelRanges[targetID].minLevel > newLevel)
                    {
                        //BuildingsABLC.levelRanges[targetID].minLevel = newLevel;
                        minLevelDropDown.selectedIndex = newLevel;
                    }

                    // Update the panel once done.
                    UpdatePanel();
                };

                // Close button.
                UIButton closeButton = AddUIComponent <UIButton>();
                closeButton.relativePosition = new Vector3(width - 35, 2);
                closeButton.normalBgSprite   = "buttonclose";
                closeButton.hoveredBgSprite  = "buttonclosehover";
                closeButton.pressedBgSprite  = "buttonclosepressed";

                // Close button event handler.
                closeButton.eventClick += (component, clickEvent) =>
                {
                    BuildingPanelManager.Close();
                };
            }
            catch (Exception e)
            {
                Logging.LogException(e, "exception setting up building panel");
            }
        }