-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLevel3.java
More file actions
163 lines (145 loc) · 4.72 KB
/
Level3.java
File metadata and controls
163 lines (145 loc) · 4.72 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Level3: Backflip and dash combo.
*/
public class Level3 extends Scrolling {
//Creates String representing the map layout.
private String[] code = { "3", "3", "0" };
private int codeSymbol = 0;
private boolean doorOpen = false;
String[] textMap = {
"eeeeee*****eeeeeeeeeeee",
"eeeeee*ece*eeeeee***eee",
"eeeeee*eee*eeeeee*m*eee",
"*******eee**********eee",
"*mkeeeee*eeeeeeeeee*eee",
"****************eee*eee",
"*bropeeeeeeeeeeeeee*eee",
"*k******************eee",
"*e*eeeeeeeeeeeeeeeeeeee",
"*e*eeeeeeeeeeeeeeeeeeee",
"*e****eeeeeeeeeeeeeeeee",
"*eeee*eeeeeeeeeeeeeeeee",
"****e*eeeeeeeeeeeeeeeee",
"eee*e*************eeeee",
"eee*eebropeeeeee.*eeeee",
"eee***************eeeee",
};
public Level3() {
super(550, 600, 1, 1100, 1200);
setScrollingBackground(new GreenfootImage("sand2.jpg"));
prepare();
drawMap();
}
/**
* Prepare method.
*/
public void prepare() {
Timer.timer = 0;
addObject(new Score(), 75, 575);
addObject(new PackLives(), 480, 575);
addObject(new Pack(), getWidth()/2, getHeight()/2);
}
/**
* Draws map using string with case switch.
*/
private void drawMap() {
for (int i = 0; i < textMap.length; i++) {
String mapLine = textMap[i];
for(int j = 0; j < mapLine.length(); j++) {
char mapChar = mapLine.charAt(j);
int y = 100 + i * 50;
int x = -275 + j * 50;
switch(mapChar) {
case '*':
addObject(new Wall(), x, y);
break;
case '.':
addObject(new SmallYummy(), x, y);
break;
case 'b':
addObject(new BlueGhost(), x, y);
break;
case 'r':
addObject(new RedGhost(), x, y);
break;
case 'o':
addObject(new OrangeGhost(), x, y);
break;
case 'p':
addObject(new PinkGhost(), x, y);
break;
case 'm':
addObject(new BigYummy(), x, y);
break;
case 'c':
addObject(new Clue(), x, y);
break;
case 'k':
addObject(new CodeDoor(), x, y);
break;
default:
break;
}
}
}
}
public void act() {
if (!doorOpen) {
checkDoorCode();
}
checkLives();
checkSounds();
nextLevel();
checkNoPack();
clueTaken();
}
/**
* Checks if Pack is alive or level reset button pressed.
*/
private void checkNoPack() {
if (getObjects(Pack.class).isEmpty() || Greenfoot.isKeyDown("r")) {
stopSounds();
PackLives.lives--;
Greenfoot.setWorld(new Level3());
Greenfoot.delay(5);
}
}
/**
* If SmallYummy empty goes to EndScreenWon.
*/
public void nextLevel() {
if (getObjects(SmallYummy.class).isEmpty()) {
Score.score += (PackLives.lives * 1000);
stopSounds();
Greenfoot.setWorld(new EndScreenWon());
}
}
/**
* Shows a little message with code inside when Clue is taken by Pack.
*/
private void clueTaken() {
if (getObjects(Clue.class).isEmpty()) {
showText("Oh, I think we found a clue!\n15 + 15 * 15 + 10 * 10 - 10", getWidth()/2, getHeight() - 50);
}
}
/**
* Checks if pressed key equals to the code's first symbol.
* If it does -> checks the next one with next pressed key.
* Else -> starts checking keys all over again.
* If they mach -> removes CodeDoor from the world.
*/
private void checkDoorCode() {
String key = Greenfoot.getKey();
if (key != null) {
if (code[codeSymbol].equals(key)) {
codeSymbol++;
if (codeSymbol == code.length) {
removeObjects(getObjects(CodeDoor.class));
doorOpen = true;
return;
}
} else codeSymbol = 0;
}
}
}