I'm making an rts with Java and swing and have come across a problem with ordering my units to build. My unit can only build the last item of a [b]Building[/b] placed in an ArrayList. The last Building works fine as far as being set as the current construction task, but any before call both the [b]if[/b] and the [b]else[/b] statements that prevent that from happening. The way I have my game working ; A button is pressed to create a building, you place the building, the currently selected builder teleports to it and begins building. The problem happens when multiple buildings are placed since the previous Buildings are not buildable since the [b]else[/b] statement is triggering for the other buildings.
Here I have the code that sets the current building task of my Builder Entity :
public void mousePressed(MouseEvent e) {
for(Iterator<Building> buildingIter = buildinglist.iterator();buildingIter.hasNext();){
Building building = buildingIter.next();
for(Iterator<game.Entity> entityIter = EntityManager.entitylist.iterator(); entityIter.hasNext();){
game.Entity entity = entityIter.next();
float mouseX = e.getX();
float mouseY = e.getY();
if(e.getButton() == MouseEvent.BUTTON3 && entity.getSelected() && entity instanceof EntityBuilder){
if(building.getRect().contains(mouseX, mouseY)){
entity.setCurrentConstruction(building);
System.out.println("Engineer : SIR!");
}
else{
entity.setCurrentConstruction(null);
}
}
}
}
}
Here is the mousePressed() method of my EntityBuilder:
public void mousePressed(MouseEvent e) {
// To change body of generated methods, choose Tools | Templates.
int mousex = e.getX();
int mousey = e.getY();
int speed = 3;
if (soldierrectangle.contains(mousex, mousey) && e.getButton() == MouseEvent.BUTTON1 && selected == false) {
selected = true;
EntityManager.selectedEntity = this;
}
if (!soldierrectangle.contains(mousex, mousey) && e.getButton() == MouseEvent.BUTTON1 && selected == true && BuildingManager.currentBuilding == null) {
speed = 0;
selected = false;
setPressed(false);
}
if (!soldierrectangle.contains(mousex, mousey) && e.getButton() == MouseEvent.BUTTON3 && selected == true) {
double distance = Math
.sqrt(Math.pow(mousex - soldierrectangle.x, 2) + Math.pow(mousey - soldierrectangle.y, 2));
velocityx = ((mousex - soldierrectangle.x) / distance) * speed;
velocityy = ((mousey - soldierrectangle.y) / distance) * speed;
targetx = mousex;
targety = mousey;
}
}
Here is the Update() method of my EntityBuilder, but this should not be relevant I believe:
public void update() {
/* Walk towards Current Construction Assignment */
if(currentConstruction != null ){
if(currentConstruction.isPlaced()){
x = currentConstruction.getPositionX();
y = currentConstruction.getPositionY();
}
}
if (!soldierrectangle.contains(destinationrectangle)) {
x = x + velocityx;
y = y + velocityy;
}
// If the player is in the radius of the building, begin building
if (currentConstruction != null) {
if(isBuilding == false){
if (Math.abs(x - currentConstruction.getPositionX()) < 20
&& Math.abs(y - currentConstruction.getPositionY()) < 20) {
System.out.println("Engineer : I'm in position!");
isBuilding = true;
}
}
}
// Begin building
if (isBuilding) {
targetx = x;
targety = y;
deltaBuildTime += .01f;
if(currentConstruction!= null){
if(deltaBuildTime >= currentConstruction.getBuildTime()){
currentConstruction.setBuilt(true);
isBuilding = false;
currentConstruction = null;
deltaBuildTime -= deltaBuildTime;
System.out.println("Engineer: It's not much but it'll do");
}
}
}
// Deselect building on movement
if (currentConstruction == null && isBuilding == true) {
isBuilding = false;
System.out.println("Engineer : It was a damn prank call!");
}
soldierrectangle.setLocation((int) x, (int) y);
destinationrectangle.setLocation((int) targetx, (int) targety);
attack();
}