protected void AddRecruitingButton(GuiList buildingActions, string unitToRecruit) { StringBuilder labelNextToButtonText = new StringBuilder(); labelNextToButtonText.Append(Translations.GetTranslation(unitToRecruit)).Append(" ("); // Soldier ( labelNextToButtonText.Append(SoldierRegistry.GetSoldierCost(unitToRecruit)).Append("G): "); // Soldier ('cost'G) buildingActions.addElement(ElementBuildButton.CreateBuildButton(buildingActions.Bounds.Location, labelNextToButtonText.ToString(), () => TrySpawnUnit(unitToRecruit), "Recruit")); }
protected void TrySpawnUnit(string soldierType) { // Check if the player can afford this soldier int cost = SoldierRegistry.GetSoldierCost(soldierType); if (!owner.CanAfford(cost) || owner.Population <= 0) { return; } // Set this soldier in the world if possible Tile currentTile = ((GameWorld as Grid)[positionInGrid] as Tile); if (!currentTile.Occupied) { // Nothing here, just place it in this square Army army = new Army(positionInGrid.X, positionInGrid.Y, owner); army.AddSoldier(SoldierRegistry.GetSoldier(soldierType, owner)); currentTile.SetUnit(army); } else if (currentTile.Unit.Owner == owner && currentTile.Unit is Army) { // Our own army is here, just place it in there :) Army a = currentTile.Unit as Army; if (a.GetTotalUnitCount() >= 20) { return; } a.AddSoldier(SoldierRegistry.GetSoldier(soldierType, owner)); } else { // We can't place it, just stop this whole function return; } // Buy the soldier, as we placed it. owner.Buy(cost); owner.CalculatePopulation(); }