/// <summary> /// Shows an attacking overlay for armies. /// </summary> /// <param name="a">The army which needs an attacking overlay to show.</param> protected void SetArmyAttackingOverlay(Army a) { // Use a swordsman, as it has a range of 1. Soldier swordsman = SoldierRegistry.GetSoldier("unit.swordsman.1", a.Owner); swordsman.PositionInGrid = a.PositionInGrid; SetUnitAttackingOverlay(swordsman); }
protected void AddUpgradeButton(GuiList buildingActions, string unitToUpgrade, int tier, Player player) { StringBuilder buttonText = new StringBuilder(); buttonText.Append("Upgrade to tier ").Append(tier + 1).Append(": ("); buttonText.Append(SoldierRegistry.GetUpgradeCost(unitToUpgrade, player.soldierTiers[unitToUpgrade])).Append("G): "); // Soldier ('cost'G) buildingActions.addElement(ElementBuildButton.CreateBuildButton(buildingActions.Bounds.Location, buttonText.ToString(), () => { TryUpgradeUnit(unitToUpgrade); }, "Upgrade")); }
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 TryUpgradeUnit(string soldierType) { int cost = SoldierRegistry.GetUpgradeCost(soldierType, owner.soldierTiers[soldierType]); if (!owner.CanAfford(cost)) { return; } owner.Buy(cost); owner.soldierTiers[soldierType] += 1; }
// Creates a puppet unit to check if every tile is accessible from a certain unobstructed tile private bool CheckIfEveryTileIsAccessible(Configuration file, int gridWidth, int gridHeight) { // Puts a unit on the first available tile int startx = 0; int starty = 0; while ((this[startx, starty] as Tile).Terrain == Terrain.Mountain || (this[startx, starty] as Tile).Terrain == Terrain.Lake) { if (startx < gridWidth - 1) { startx++; } else { startx = 0; starty++; } } Unit u = SoldierRegistry.GetSoldier("unit.swordsman.1", new Player("", "Blue", Color.Blue, 1)); u.Parent = this; u.MovesLeft = u.MoveSpeed = int.MaxValue; (this[startx, starty] as Tile).SetUnit(u); Point[] reachableTiles = Pathfinding.ReachableTiles(u, gridWidth, gridHeight); // Tests if every other nonobstructed tile is accessible by this unit for (int gridX = 0; gridX < gridWidth; ++gridX) { for (int gridY = 0; gridY < gridHeight; ++gridY) { Tile t = this[gridX, gridY] as Tile; Terrain terrain = t.Terrain; if (u.Passable(terrain) && !(reachableTiles.Contains(t.PositionInGrid) || t.PositionInGrid.Equals(u.PositionInGrid))) { mountainArray = ResetMountains(file, gridWidth, gridHeight); lakeArray = ResetLakes(file, gridWidth, gridHeight); t.SetUnit(null); return(false); } else { t.SetUnit(null); } } } return(true); }
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(); }
protected void LoadConfiguration() { // Initialize main configuration file mainConfiguration = FileManager.LoadConfig("Main"); // Initialize buildings Configuration buildingConfiguration = mainConfiguration.GetPropertySection("building"); BuildingRegistry.InitBuildings(buildingConfiguration); Building.LoadFromConfig(buildingConfiguration); // Initialize units Configuration unitConfiguration = mainConfiguration.GetPropertySection("unit"); SoldierRegistry.Init(unitConfiguration); // Initialize language file Translations.LoadLanguage("en_US"); // Initialize keys InitializeKeys(FileManager.LoadConfig("Keys").GetPropertySection("key")); }
/// <summary> /// Populates the field with the armies of both attacker and defender. /// </summary> /// <param name="attacker">The attacking Army.</param> /// <param name="defender">The defending Army.</param> public void PopulateField(Army attacker, Army defender) { // Initialize which players are the attacker and the defender attackingPlayer = attacker.Owner; defendingPlayer = defender.Owner; // Initialize the defender's field int currentX = 0; int currentY = 0; // Iterate over every type of Soldier in the defending Army foreach (string s in defender.UnitsAndCounts.Keys) { // Get the amount of this kind of Soldier int soldierCount = defender.UnitsAndCounts[s]; // Place them in a position based on how many soldiers we have passed so far. for (; soldierCount > 0; --soldierCount) { //only places soldier if terrain is passable Soldier soldier = SoldierRegistry.GetSoldier(s, defender.Owner); while (!(this[currentX, currentY] as Tile).Passable(soldier)) { ++currentX; if (currentX >= Width) { currentX = 0; ++currentY; } } (this[currentX, currentY] as Tile).SetUnit(soldier); ++currentX; // Make sure we don't create a line of soldiers longer than the field. if (currentX >= Width) { currentX = 0; ++currentY; } } } // Do the same for the attacking Army, except start bottom right and go left->up for each Soldier. currentX = Width - 1; currentY = Height - 1; foreach (string s in attacker.UnitsAndCounts.Keys) { int soldierCount = attacker.UnitsAndCounts[s]; for (; soldierCount > 0; --soldierCount) { //only places soldier if terrain is passable Soldier soldier = SoldierRegistry.GetSoldier(s, attacker.Owner); while (!(this[currentX, currentY] as Tile).Passable(soldier)) { --currentX; if (currentX < 0) { currentX = Width - 1; --currentY; } } (this[currentX, currentY] as Tile).SetUnit(soldier); --currentX; if (currentX < 0) { currentX = Width - 1; --currentY; } } } // And clear all target positions after we populated the field. ClearAllTargetPositions(); CurrentPlayer = attacker.Owner; }