Example #1
0
        void UseResourcesFromFaction(string faction, int usedResources)
        {
            foreach (Building building in manager.GetBuildingsByFaction(faction))
            {
                if (building is ResourceBuilding && !building.IsDestroyed)
                {
                    ResourceBuilding resourceBuilding = (ResourceBuilding)building;

                    //this buildings has not resources left, let's skip it
                    if (resourceBuilding.Generated <= 0)
                    {
                        continue;
                    }

                    //determine how many resources can be used from this buildings
                    int resourcesToUse = Math.Min(usedResources, resourceBuilding.Generated);

                    //subtract resources from total used by faction
                    usedResources -= resourcesToUse;
                    //subtract resources from actual building
                    resourceBuilding.Generated -= resourcesToUse;

                    //if we've subtracted all the resources use by this faction
                    if (usedResources <= 0)
                    {
                        return;
                    }
                }
            }
        }
Example #2
0
 void AddToResourcePoolByFaction(string faction)
 {
     foreach (Building building in manager.GetBuildingsByFaction(faction))
     {
         if (building is ResourceBuilding && !building.IsDestroyed)
         {
             ResourceBuilding resourceBuilding = (ResourceBuilding)building;
             resourceBuilding.Pool += 1;
         }
     }
 }
Example #3
0
        //calculate how much resources a faction has at it's disposal
        int GetResourcesTotalByFaction(string faction)
        {
            int totalResources = 0;

            foreach (Building building in manager.GetBuildingsByFaction(faction))
            {
                //we are interested in resource buildings that have not been destroyed
                if (building is ResourceBuilding && !building.IsDestroyed)
                {
                    ResourceBuilding resourceBuilding = (ResourceBuilding)building;
                    totalResources += resourceBuilding.Generated;
                }
            }
            return(totalResources);
        }
Example #4
0
        void UpdateBuildings()
        {
            foreach (string faction in factions)
            {
                //new resources are only considered at the beginning of the next round
                int resources     = GetResourcesTotalByFaction(faction);
                int usedResources = 0;

                foreach (Building building in manager.GetBuildingsByFaction(faction))
                {
                    //ignore destroyed buildings
                    if (building.IsDestroyed)
                    {
                        building.CheckHide();
                        continue;
                    }

                    if (building is FactoryBuilding)
                    {
                        FactoryBuilding factoryBuilding = (FactoryBuilding)building;

                        if (factoryBuilding.CanProduce(round) && factoryBuilding.SpawnCost <= resources)
                        {
                            resources     -= factoryBuilding.SpawnCost;
                            usedResources += factoryBuilding.SpawnCost;

                            Unit newUnit = factoryBuilding.SpawnUnit(round);
                            manager.AddUnit(newUnit);
                        }
                    }
                    else if (building is ResourceBuilding)
                    {
                        ResourceBuilding resourceBuilding = (ResourceBuilding)building;
                        resourceBuilding.GenerateResources();
                    }
                }

                //remove used resources from faction's available resource buildings
                UseResourcesFromFaction(faction, usedResources);
            }
        }
Example #5
0
        private void AddBuildings(int numBuildings, string faction)
        {
            for (int i = 0; i < numBuildings; i++)
            {
                int x            = random.Next(0, map.width);
                int y            = random.Next(0, map.height);
                int buildingType = random.Next(0, 2);

                Building building;
                if (buildingType == 0)
                {
                    building = new FactoryBuilding(x, y, faction, map.height);
                }
                else
                {
                    building = new ResourceBuilding(x, y, faction);
                }

                manager.AddBuilding(building);
            }
        }