-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCursorImage.java
More file actions
111 lines (93 loc) · 3.42 KB
/
Copy pathCursorImage.java
File metadata and controls
111 lines (93 loc) · 3.42 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import greenfoot.Actor;
import greenfoot.Greenfoot;
import greenfoot.GreenfootImage;
import greenfoot.MouseInfo;
/**
* Supports the players UX by displaying what he currently has selected the
* mouse to do
*/
public class CursorImage extends Actor {
public static final String MOUSE_CURSER_IMAGE_NAME = "mouse-button.png";
public static final String ARCHER_TOWER_LOGO_IMAGE_NAME = "archer-tower-logo.png";
public static final String BOMBER_TOWER_LOGO_IMAGE_NAME = "bomb-tower-logo.png";
public static final String SNIPER_TOWER_LOGO_IMAGE_NAME = "sniper-tower-logo.png";
public static final String MINE_FIELD_LOGO_IMAGE_NAME = "land-mine-logo.png";
public static final String SLIME_FIELD_LOGO_IMAGE_NAME = "slime-ball-logo.png";
public static final String DELETE_TOOL_IMAGE_NAME = "delete-tool-logo.png";
private static final GreenfootImage NONE_MOUSE_IMAGE = new GreenfootImage(2, 2);
/**
* in which mode the mouse currently is (Placing, Removing, None)
*/
public enum MouseState {
NONE(CursorImage.MOUSE_CURSER_IMAGE_NAME, 0), PLACE_ARCHER_TOWER(CursorImage.ARCHER_TOWER_LOGO_IMAGE_NAME, ArcherTower.PRICE),
PLACE_BOMB_TOWER(CursorImage.BOMBER_TOWER_LOGO_IMAGE_NAME, BombTower.PRICE),
PLACE_SNIPER_TOWER(CursorImage.SNIPER_TOWER_LOGO_IMAGE_NAME, SniperTower.PRICE),
PLACE_MINE_FIELD(CursorImage.MINE_FIELD_LOGO_IMAGE_NAME, MineField.PRICE),
PLACE_SLIME_FIELD(CursorImage.SLIME_FIELD_LOGO_IMAGE_NAME, SlimeField.PRICE), DELETE_TOOL(CursorImage.DELETE_TOOL_IMAGE_NAME, 0);
private String imageName;
private int toolUseCost;
MouseState(String imageName, int toolUseCost) {
this.imageName = imageName;
this.toolUseCost = toolUseCost;
}
public String getImageName() {
return imageName;
}
public int getToolUseCost() {
return toolUseCost;
}
}
private MouseState mouseState;
private MouseInfo mouse = Greenfoot.getMouseInfo();
public CursorImage() {
this.setMouseState(MouseState.NONE);
}
@Override
public void act() {
mouse = Greenfoot.getMouseInfo();
if(mouse != null && mouseState != MouseState.NONE) {
if(getImage().equals(NONE_MOUSE_IMAGE)) {
setMouseState(mouseState); // replaces image with right image
}
if(isMouseStateImageInsideWorld()) {
setLocation(calcMouseStateImageLocationX(false), calcMouseStateImageLocationY(false));
} else {
setLocation(calcMouseStateImageLocationX(true), calcMouseStateImageLocationY(true));
}
} else {
if(getX() != 0 && getY() != 0) {
setImage(NONE_MOUSE_IMAGE);
setLocation(0, 0);
}
}
}
private boolean isMouseStateImageInsideWorld() {
boolean result = false;
result = calcMouseStateImageLocationX(false) < getWorld().getWidth();
result &= calcMouseStateImageLocationY(false) < getWorld().getHeight();
return result;
}
private int calcMouseStateImageLocationX(boolean above) {
return mouse.getX() + getFactor(above) * (getImage().getWidth() / 2 + 2);
}
private int calcMouseStateImageLocationY(boolean above) {
return mouse.getY() + getFactor(above) * (getImage().getHeight() / 2 + 2);
}
private int getFactor(boolean above) {
return above ? -1 : 1;
}
public MouseState getMouseState() {
return mouseState;
}
public void setMouseState(MouseState mouseState) {
this.mouseState = mouseState;
if(mouseState == MouseState.NONE) {
setImage(NONE_MOUSE_IMAGE);
} else {
setImage(getMouseStateImageName());
}
}
public String getMouseStateImageName() {
return getMouseState().getImageName();
}
}