Skip to content

Commit

Permalink
Implement plant remove and undo select.
Browse files Browse the repository at this point in the history
  • Loading branch information
antonrufino committed Jun 2, 2016
1 parent ef758f4 commit 8c569c3
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 5 deletions.
3 changes: 1 addition & 2 deletions src/Banga.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,7 @@ public void run() {
this.kill();
Game.getInstance().getGrid().setPlant(this.getRow(), this.getCol(), null);
} catch (InterruptedException e) {
System.out.println(e.getMessage());
e.printStackTrace();
return;
}
}

Expand Down
20 changes: 20 additions & 0 deletions src/Game.java
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,26 @@ public void placePlant(Point p) {
}
}

public void removePlant(Point p) {
for (int i = 0; i < Grid.ROWS; ++i) {
for (int j = 0; j < Grid.COLS; ++j) {
Rectangle rect = grid.getRectangle(i, j);
if (rect.contains(p)) {
if (grid.hasPlant(i, j)) {
grid.getPlant(i, j).kill();
grid.setPlant(i, j, null);
}
return;
}
}
}
}

public void undoSelectPlant() {
this.setPendingButton(null);
this.selectPlant(null);
}

public void collectEnergy(Point p) {
for (int i = 0; i < energyList.size(); ++i) {
Energy e = energyList.get(i);
Expand Down
12 changes: 10 additions & 2 deletions src/GamePanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,16 @@ public void mousePressed(MouseEvent me){}
public void mouseClicked(MouseEvent me){
// A mouse click can collect energy or place a plant on the grid.
Point p = me.getPoint();
if (game.getSelectedPlant() != null) game.placePlant(p);
game.collectEnergy(p);

if (p == null) return;

if (SwingUtilities.isLeftMouseButton(me)) {
if (game.getSelectedPlant() != null)game.placePlant(p);
else game.collectEnergy(p);
} else if (SwingUtilities.isRightMouseButton(me)) {
if (game.getSelectedPlant() != null) game.undoSelectPlant();
else game.removePlant(p);
}
}
});
}
Expand Down
2 changes: 1 addition & 1 deletion src/Grid.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public boolean hasPlant(int row, int col) {
}

// Get the plant contained by a tile.
public Plant getPlant(int row, int col) {
public synchronized Plant getPlant(int row, int col) {
return plants[row][col];
}

Expand Down

0 comments on commit 8c569c3

Please sign in to comment.