public bool GetToWork(int xUnits = 0, string type = "farm") { bool canWork = true; foreach (GameEntity entity in entities) { if (type == "farm" && entity is Farm) { coins += (entity as Farm).Work(); } else if (type == "barrack" && entity is Barrack) { Barrack barrack = (entity as Barrack); if (barrack.Work(xUnits) <= coins) { if (xUnits <= barrack.Capacity) { barrack.Capacity -= xUnits; n_Infantry += xUnits; coins -= barrack.Work(xUnits); canWork = true; } else { canWork = false; } } else { canWork = false; } } else if (type == "stable" && entity is Stable) { Stable stable = (entity as Stable); if (stable.Work(xUnits) <= coins) { if (xUnits <= stable.Capacity) { stable.Capacity -= xUnits; n_Cavalry += xUnits; coins -= stable.Work(xUnits); canWork = true; } else { canWork = false; } } else { canWork = false; } } else if (type == "artillery_factory" && entity is ArtilleryFactory) { ArtilleryFactory artilleryFactory = (entity as ArtilleryFactory); if (artilleryFactory.Work(xUnits) <= coins) { if (xUnits <= artilleryFactory.Capacity) { artilleryFactory.Capacity -= xUnits; n_Artillery += xUnits; coins -= artilleryFactory.Work(xUnits); canWork = true; } else { canWork = false; } } else { canWork = false; } } } return(canWork); }