-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTowerCell.java
More file actions
61 lines (54 loc) · 1.95 KB
/
TowerCell.java
File metadata and controls
61 lines (54 loc) · 1.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import greenfoot.Greenfoot;
import greenfoot.GreenfootImage;
/**
* A special cell where towers can only be placed on.
*/
public class TowerCell extends Cell {
private static final String CELL_BACKGROUND_OVERLAY_IMAGE_NAME = "tower-cell-overlay.png";
/**
* Creates an tower cell object and sets its image.
*
* @param gridX - number of the cell in horizontal direction
* @param gridY - number of the cell in vertical direction
*/
public TowerCell(int gridX, int gridY) {
super(gridX, gridY);
constructImage();
}
/**
* Adds an overlay to the image of the tower cell.
*/
private void constructImage() {
GreenfootImage img = getImage();
img.drawImage(new GreenfootImage(CELL_BACKGROUND_OVERLAY_IMAGE_NAME), 0, 0);
setImage(img);
}
/**
* @see checkAddTowerClick()
*/
@Override
public void act() {
checkAddTowerClick();
}
/**
* Checks which type of tower is going to be placed on the tower cell and adds
* the tower onto itself.
*/
private void checkAddTowerClick() {
if(Greenfoot.mouseClicked(this) && Greenfoot.getMouseInfo().getButton() == 1) {
if(!(getWorld().getObjectsAt(getX(), getY(), Tower.class).size() > 0)) {
CursorImage.MouseState mouseState = getWorld().getCursorImage().getMouseState();
if(mouseState == CursorImage.MouseState.PLACE_ARCHER_TOWER && getWorld().haveEnoughCoins(ArcherTower.PRICE)) {
getWorld().addObject(new ArcherTower(), getX(), getY());
getWorld().getCoinsCounter().add(-ArcherTower.PRICE);
} else if(mouseState == CursorImage.MouseState.PLACE_BOMB_TOWER && getWorld().haveEnoughCoins(BombTower.PRICE)) {
getWorld().addObject(new BombTower(), getX(), getY());
getWorld().getCoinsCounter().add(-BombTower.PRICE);
} else if(mouseState == CursorImage.MouseState.PLACE_SNIPER_TOWER && getWorld().haveEnoughCoins(SniperTower.PRICE)) {
getWorld().addObject(new SniperTower(), getX(), getY());
getWorld().getCoinsCounter().add(-SniperTower.PRICE);
}
}
}
}
}