Example #1
0
    public void UpdateConstructionCost(BuildingType type)
    {
        BuildingModel model = BuildingModelFactory.Create(type);

        buildingType = model.type;
        string wood       = model.buildCost.TransactionStatusString(ResourceType.WOOD);
        string magicStone = model.buildCost.TransactionStatusString(ResourceType.MAGIC_STONE);
        string buildTime  = (model.constructionTime / 5).ToString() + "s";

        constructionCost.text = string.Format("Wood: {0}\nMagic Stone: {1}\nBuild Time: {2}", wood, magicStone, buildTime);
    }
    // Get highest priority not-already built building, returns null if all are built.
    public BuildingModel PlanBuilding(UnitPriorities unitPriorities)
    {
        // check if all buildings are built?
        // -> prevent unnecessary checks
        Dictionary <UnitType, float> priorities = unitPriorities.priorities;

        List <UnitType> unitTypesSortedByPriority = new List <UnitType>();

        UnitType maxPriorityType  = UnitType.SWORDSMAN;
        float    maxPriorityValue = -1f;

        // sort by priority
        while (unitTypesSortedByPriority.Count < priorities.Count)
        {
            foreach (KeyValuePair <UnitType, float> entry in priorities)
            {
                if (!unitTypesSortedByPriority.Contains(entry.Key) && entry.Value > maxPriorityValue)
                {
                    maxPriorityType  = entry.Key;
                    maxPriorityValue = entry.Value;
                }
            }
            Debug.Log("adding " + maxPriorityType);
            unitTypesSortedByPriority.Add(maxPriorityType);

            maxPriorityValue = -1f;
        }

        // iterate over sorted list
        foreach (UnitType type in unitTypesSortedByPriority)
        {
            UnitPurchaseModel unitModel    = UnitPurchaseModelFactory.Create(type);
            BuildingType      buildingType = unitModel.prerequisite;

            if (!GetBuildPlotController().IsComplete(buildingType))
            {
                BuildingModel buildingModel = BuildingModelFactory.Create(buildingType);

                // Update resource costs
                woodCost       = buildingModel.buildCost.GetResourceAmount(ResourceType.WOOD);
                magicStoneCost = buildingModel.buildCost.GetResourceAmount(ResourceType.MAGIC_STONE);

                return(buildingModel);
            }
        }

        // No building to be built
        return(null);
    }
Example #3
0
    public override bool Execute()
    {
        Debug.Log("build command executing");

        // Get required data on building

        BuildPlotController buildPlotController = GetBuildPlotController();

        buildingModel = BuildingModelFactory.Create(buildingType);
        ResourceTransaction transaction = buildingModel.buildCost;

        // Check if constructing the building is possible

        // Building has already been constructed in another plot
        if (buildPlotController.IsBuilt(buildingType))
        {
            Debug.LogError(string.Format("Error: there is already a {0} on another plot.", buildingType));
            GetGameLogController().Log(string.Format("Error: there is already a {0} on another plot.", buildingType));

            //abort
            return(false);
        }

        // Plot isn't empty
        if (!buildPlotController.IsBuildable(buildPlotLocation))
        {
            Debug.LogError(string.Format("Error: there is already a building on {0}", buildPlotLocation));
            GetGameLogController().Log(string.Format("Error: there is already a building on {0}", buildPlotLocation));


            // abort
            return(false);
        }

        // transaction fails
        if (!PayOutTransaction(transaction))
        {
            // abort
            GetGameLogController().Log(string.Format("Error: not enough resources to construct {0}", buildingType));
            return(false);
        }

        // Success -> Build
        buildPlotController.Build(buildPlotLocation, buildingType, buildingModel);

        return(true);
    }