private GuiButton.OnClickHandler BuildBuilding(EconomyGrid grid, string buildingName, Type buildingType) { // Return an on click handler return(() => { // That gets the tile the current builder is on Tile t = grid[currentBuilder.PositionInGrid] as Tile; // Then checks if we can build here, and if the player can afford the building if (!t.BuiltOn && currentBuilder.Owner.CanAfford(BuildingRegistry.GetCost(buildingName))) { Tile tile = currentBuilder.Parent as Tile; if (!tile.Terrain.IsMountain && buildingType.Equals(typeof(Mine))) { return; } else if (tile.Terrain != Terrain.Plains && buildingType.Equals(typeof(Town))) { return; } else if ((tile.Terrain == Terrain.Lake || tile.Terrain == Terrain.Mountain || tile.Terrain == Terrain.DesertMountain || tile.Terrain == Terrain.TundraMountain) && !buildingType.Equals(typeof(Mine))) { return; } // And remove the money if possible... currentBuilder.Owner.Buy(BuildingRegistry.GetCost(buildingName)); // ... and tell the grid to build a building here, based on the passed type. // Basically the same as new Building(currentBuilder.PositionInGrid, currentBuilder.Owner), except we don't need to know WHICH building grid.Build(currentBuilder, (Building)Activator.CreateInstance(buildingType, new object[] { currentBuilder.PositionInGrid, currentBuilder.Owner })); currentBuilder.Owner.CalculatePopulation(); currentBuilder.Owner.CalculateMoneyPerTurn(); } }); }