public override bool build()
    {
        /*TODO
         * scan the area around the node for other buildings,
         * and weight the random number based on their type.
         * The more buildings of one type around a new node,
         * the more likely it is to instantiate as that type
         */
        if (concreteImplementation == null)
        {
            System.Random rnd = new System.Random();
            SortedDictionary <BuildingClass, float> building_weights = buildingWeights();

            BuildingClass most_frequent    = BuildingClass.Residential;
            float         weighting_factor = 0.0f;
            float         temp_output      = 0.0f;
            foreach (BuildingClass b in building_weights.Keys)
            {
                building_weights.TryGetValue(b, out temp_output);
                if (temp_output > weighting_factor)
                {
                    weighting_factor = temp_output;
                    most_frequent    = b;
                }
            }

            switch ((rnd.NextDouble() <= weighting_factor ? (int)most_frequent : rnd.Next(buildingClasses.Length)))
            {
            case (int)BuildingClass.Residential:
                concreteImplementation = new ResidentialBuildingNode(this.GetComponent <Transform>().position);
                break;

            case (int)BuildingClass.Commercial:
                concreteImplementation = new CommercialBuildingNode(this.GetComponent <Transform>().position);
                break;

            default:
                print("No corosponding building type");
                return(false);
            }

            this.setConstructed(true);
            this.GetComponent <BoxCollider>().enabled = false;
            concreteImplementation.build();
            concreteImplementation.setConstructed(true);
        }
        return(true);
    }
Пример #2
0
    public void expand()
    {
        System.Random rnd = new System.Random();

        surplus += harvest();
        int neededSupplies = (population % surplus);

        surplus    -= neededSupplies;
        gold       += surplus;
        population += surplus;

        int goldDiff    = gold - lastCensusGold;
        int popDiff     = population - lastCensusPopulation;
        int surplusDiff = surplus - lastCensusSurplus;

        int newBuildings = (popDiff / 100);
        int loopCount    = 0;

        while (newBuildings > 0 && loopCount < cityMap.Count)
        {
            BuildingNode b = cityMap[rnd.Next(cityMap.Count)];

            if (b != null && !b.getConstructed())
            {
                if (b.build())
                {
                    newBuildings -= 1;
                }
            }

            loopCount += 1;
        }

        lastCensusGold       = gold;
        lastCensusPopulation = population;
        lastCensusSurplus    = surplus;
    }