if (GameData.getSelectedColony().isCommandCenterUnderConstruction()) {
this.developButton.enable();
}
if(selectedAction != null){
if(selectedAction instanceof BuildingAction){
BuildingAction ba = (BuildingAction) selectedAction;
if(!BuildingType.COMMAND_CENTER.equals(ba.getBuilding().getType())){
// The selected action is not a command center construction
selectedAction = null;
}
}else{
// The action is not allowed yet
selectedAction = null;
}
}
this.buildWindow.setCommandCenterBuilt(false);
}
this.buildWindow.update(input);
// Handle action drop
if (this.selectedAction != null) {
if (input.isMousePressed(Input.MOUSE_LEFT_BUTTON)
&& this.mouseY > TOP_BAR_HEIGHT
&& this.mouseY < Settings.HEIGHT - BOTTOM_BAR_HEIGHT) {
final int tileX = this.squareXOnMap;
final int tileY = this.squareYOnMap;
if (this.selectedAction instanceof PreparationAction) {
// Preparation action
PreparationAction pa = (PreparationAction) this.selectedAction;
boolean canBuild = false;
if (TileHelper.tileIsPreparable(tileX, tileY)) {
// Tile is OK
if (ResourceHelper
.enoughResourcesFor(SectorState.this.selectedAction
.getCostMap())) {
// Resources are OK
if (GameData.getSelectedColony()
.getAvailableRobots() > 0) {
canBuild = true;
}
}
}
if (canBuild) {
// Preparation can be done
pa.setX(tileX);
pa.setY(tileY);
Map<Resource, BigDecimal> map = pa.getCostMap();
for (Resource r : resources) {
if (map.get(r) == null) {
// No value defined for this resource
continue;
}
GameData.getSelectedColony().updateResource(r,
map.get(r).negate());
}
GameData.getSelectedColony().addPreparationAction(
pa.clone());
} else {
// TODO play an error sound
}
} else if (this.selectedAction instanceof BuildingAction) {
BuildingAction ba = (BuildingAction) this.selectedAction;
if (BuildingType.COMMAND_CENTER.equals(ba.getBuilding()
.getType())) {
// Command center construction is handled in a submethod
handleCommandCenterConstruction(ba, tileX, tileY);
} else if (TileHelper.tileIsBuildable(tileX, tileY, ba
.getBuilding().getType())) {
// Is there already a building there?
List<Building> buildings = GameData.getSelectedColony()
.getBuildings();
boolean canBuild = true;
for (Building b : buildings) {
if (b.getX() == tileX && b.getY() == tileY) {
// Oops, there is already a building here.
// Construction is impossible
canBuild = false;
break;
}
}
// No building, but is there a building under
// construction?
List<BuildingAction> bactions = GameData
.getSelectedColony().getBuildingActions();
for (BuildingAction cba : bactions) {
Building cb = cba.getBuilding();
if (cb.getX() == tileX && cb.getY() == tileY) {
// Construction is impossible
canBuild = false;
break;
}
}
if (!ResourceHelper
.enoughResourcesFor(SectorState.this.selectedAction
.getCostMap())) {
canBuild = false;
}
if (canBuild) {
ba.getBuilding().setX(tileX);
ba.getBuilding().setY(tileY);
final BuildingAction nba = ba.clone();
if (!BuildingType.ROAD.equals(nba.getBuilding()
.getType())) {
nba.setCallback(new Runnable() {
@Override
public void run() {
if (nba.getBuilding()
.equals(SectorState.this.selectedBuilding)) {
SectorState.this.selectBuilding(nba
.getBuilding());
}
}
});
}
GameData.getSelectedColony().addBuildingAction(nba);
// Remove from the stock the building cost
BuildingData data = BuildingIO.getBuildingData(nba
.getBuilding().getType());
Map<Resource, BigDecimal> map = data
.getLevelData(0).getUpgradeCost();
for (Resource r : resources) {
GameData.getSelectedColony().updateResource(r,