This repository was archived by the owner on May 28, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNode.pde
87 lines (69 loc) · 1.65 KB
/
Node.pde
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
class Node extends Being {
public static final int SIZE = 50;
private Game game;
private PostOffice po;
private Tower tower = null;
private NodeCtrls ctrls;
private boolean hovered = false;
private boolean active = false;
Node(float x, float y, Game game) {
super(new HRectangle(x - SIZE * 0.5, y - SIZE * 0.5, SIZE, SIZE));
this.game = game;
this.po = game.getPostOffice();
this.ctrls = new NodeCtrls(this, game);
this.game.register(this);
this.game.subscribe(this, POCodes.Button.LEFT);
}
void receive(MouseMessage m) {
if (m.getAction() == POCodes.Click.PRESSED) {
if (hovered
|| active && po.isMouseInRegion(ctrls.getShape())) {
ctrls.show();
activate();
} else {
deactivate();
}
}
}
void update() {
hovered = po.isMouseInRegion(getShape());
}
void draw() {
stroke(hovered || active
? #00aa00
: #00dd00
);
fill(active
? #00bb00
: #00ff00
);
_shape.draw();
}
public void setTower(Tower tower) {
this.tower = tower;
}
public void assertVacancy() throws NodeIsOccupiedException {
if (tower instanceof Tower) {
throw new NodeIsOccupiedException();
}
}
public NodeCtrls getCtrls() {
return ctrls;
}
public boolean isActive() {
return active;
}
public void activate() {
ctrls.show();
active = true;
}
public void deactivate() {
ctrls.hide();
active = false;
}
public void unregister() {
game.delete(this);
deactivate();
}
}
class NodeIsOccupiedException extends Exception {}