public static int FindUnitIndexInUnitProduction(UnitType unitType, SUnitBuilding unitProduction) { string typeName; switch (unitType) { case UnitType.Worker: typeName = "Goblin"; break; case UnitType.DonkeyGun: typeName = "DonkeyGun"; break; case UnitType.WraithRaider: typeName = "WraithRaider"; break; default: throw new Exception("Unknown Unit Type name"); } for (var unitIndex = 0; unitIndex < unitProduction.units.Length; unitIndex++) { if (typeName == unitProduction.units[unitIndex].customName) { return(unitIndex); } } throw new Exception( "Cannot determine " + unitType + "'s unit index for the Unit Production from its custom name"); }
private static bool IsUnitOfType(string unitName, UnitType unitType) { switch (unitType) { case UnitType.Worker: return(unitName.Equals("Goblin")); case UnitType.DonkeyGun: return(unitName.Equals("DonkeyGun")); case UnitType.WraithRaider: return(unitName.Equals("WraithRaiderStarship")); default: return(false); } }
public static Type DetermineUnitType(UnitType unitType) { switch (unitType) { case UnitType.Worker: return(typeof(Worker)); case UnitType.DonkeyGun: return(typeof(DonkeyGun)); case UnitType.WraithRaider: return(typeof(WraithRaider)); default: throw new InvalidEnumArgumentException(); } }
/// <summary> /// Finds a Unit representation of a chosen UnitType within current Player's Faction. /// </summary> /// <param name="unitType">Type that the Unit has to represent.</param> /// <param name="buildingController">If not null, will assert that the building can produce the Unit.</param> /// <returns>Unit representation index in the Faction.</returns> /// <exception cref="InvalidOperationException">Unit of the chosen type isn't valid in the context.</exception> public static int FindUnitIndexInFaction(UnitType unitType, BuildingController buildingController) { for (var index = 0; index < Player.CurrentPlayer.Faction.UnitList.Length; index++) { var unit = Player.CurrentPlayer.Faction.UnitList[index]; if (!IsUnitOfType(unit.obj.name, unitType)) { continue; } if (buildingController != null && !buildingController.unitProduction.canProduce && !buildingController.unitProduction.units[index].canProduce) { throw new InvalidOperationException( "Unit " + unitType + " cannot be produced by this Building"); } return(index); } throw new InvalidOperationException( "Unit " + unitType + " is not available in your Faction"); }