-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGateTileEnter.java
More file actions
87 lines (71 loc) · 2.36 KB
/
Copy pathGateTileEnter.java
File metadata and controls
87 lines (71 loc) · 2.36 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
import greenfoot.*;
import java.util.List;
public class GateTileEnter extends Tile {
private boolean activated = false;
private boolean wasPlayerOnTileLastFrame = false;
public GateTileEnter() {
super("tile_gate0.png", 'g'); // initially open/passable
setIsPassable(true);
}
public void act() {
super.act();
if (activated){
if (isInTopHalfOfBoard()){
if (player.getKilled() == 8){
deactivate();
}
} else{
if (player.getKilled() == 24){
deactivate();
}
}
}
Player player = (Player) getOneIntersectingObject(Player.class);
if (player != null) {
if (!wasPlayerOnTileLastFrame) {
wasPlayerOnTileLastFrame = true;
}
} else {
if (wasPlayerOnTileLastFrame) {
wasPlayerOnTileLastFrame = false;
boolean topHalf = isInTopHalfOfBoard();
activateGatesInHalf(topHalf);
}
}
}
private void activateGatesInHalf(boolean topHalf) {
MyWorld world = (MyWorld) getWorld();
Board board = getBoard();
int midRow = board.getTileRowCount() / 2;
for (GateTileEnter gate : world.getObjects(GateTileEnter.class)) {
int row = board.getTileRow(gate);
if ((topHalf && row < midRow) || (!topHalf && row >= midRow)) {
gate.activate();
}
}
for (GateTileExit gate : world.getObjects(GateTileExit.class)) {
int row = board.getTileRow(gate);
if ((topHalf && row < midRow) || (!topHalf && row >= midRow)) {
gate.activate();
}
}
}
private Board getBoard() {
List<Board> boards = getWorld().getObjects(Board.class);
return boards.isEmpty() ? null : boards.get(0);
}
public void activate() {
if (activated) return;
activated = true;
setIsPassable(false);
setImage(new GreenfootImage("tile_gate1.png"));
}
public void deactivate() {
activated = false;
setIsPassable(true);
setImage(new GreenfootImage("tile_gate0.png"));
}
public boolean isActivated(){
return activated;
}
}