-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLevel.java
executable file
·244 lines (220 loc) · 6.47 KB
/
Level.java
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
import greenfoot.*;
import java.io.*;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.StringTokenizer;
/**
* Level Superclass
*
* @author Adrian, Jimmy
* @version June 13, 2024
*/
public class Level extends World {
private static final GreenfootSound orbSound = new GreenfootSound("levelup.wav");
protected static int totalCoins = 0;
protected static int totalHP = 5;
protected static int numOfCrown = 0;
private static int level = 0;
private final Font font = new Font("Arial", 18);
private final GreenfootImage saveButtonImage = new GreenfootImage("saveButtonImage.png");
protected ImgScroll scroll;
protected Player player;
protected SuperDisplayLabel coinLabel = new SuperDisplayLabel(Color.BLACK, Color.WHITE, font);
protected Button saveButton = new Button();
private Orb orb;
/**
* Constructor
*/
public Level() {
super(1280, 720, 1, false);
saveButtonImage.scale(150, 60);
saveButton.setImage(saveButtonImage);
setPaintOrder(Button.class, SuperDisplayLabel.class, Tile.class);
Collection.init();
}
/**
* Constructor
*
* @param level The level to go to
*/
public Level(int level) {
super(1280, 720, 1, false);
saveButtonImage.scale(150, 60);
saveButton.setImage(saveButtonImage);
setPaintOrder(Button.class, SuperDisplayLabel.class, Tile.class);
setLevel(level);
}
/**
* Resets coins
*/
public static void resetCoin() {
totalCoins = 0;
}
/**
* Adds a singular coin to the total
*/
public static void addToTotalCoin() {
totalCoins++;
}
/**
* Adds one crown to the total
*/
public static void addCrown() {
numOfCrown++;
}
/**
* Spawns the floor of the world
*/
public void spawnFloor(ImgScroll sc) {
for (int i = 0; i < sc.getScrollWidth() + 64; i += 64) {
addObject(new Brick(), i, 700);
}
}
/**
* Check if touching Orb.
* If it is touching orb, go to the next level
*/
public void checkNext() {
if (orb.isBeingTouched()) {
orbSound.play();
if (level == 0) {
levelUp();
Level1 world = new Level1();
Greenfoot.setWorld(world);
return;
}
if (level == 1) {
EndScreen end = new EndScreen();
Greenfoot.setWorld(end);
}
}
}
/**
* Sets the current level to the desired level
*
* @param level The desired level to go to
*/
public void setLevel(int level) {
Level.level = level;
}
/**
* Follows the Player around the map
*/
public void followPlayer(Player p) {
if (p != null) {
scroll.scroll(getWidth() / 2 - p.getX(), getHeight() / 2 - p.getY());
}
}
/**
* Updates the coin and hp and sets the location of it
*/
public void updateCoin() {
coinLabel.update("Coins: " + totalCoins + " HP: " + totalHP);
coinLabel.setLocation(getWidth() / 2, 20);
}
/**
* Loads level from a csv file
*
* @param level The level to load
* @return int[][] The 2d array with the location of the blocks
*/
public int[][] loadLevel(int level) {
ArrayList<String> data = new ArrayList<>();
Scanner scan;
try {
scan = new Scanner(new File("levels/" + level + ".csv"));
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
}
while (scan.hasNextLine()) {
data.add(scan.nextLine());
}
int[][] blocks = new int[120][20];
for (String line : data) {
StringTokenizer st = new StringTokenizer(line, ",");
ArrayList<String> lineData = new ArrayList<>();
while (st.hasMoreTokens()) {
lineData.add(st.nextToken());
}
blocks[Integer.parseInt(lineData.get(0))][Integer.parseInt(lineData.get(1))] = Integer.parseInt(lineData.get(2));
}
return blocks;
}
/**
* Spawns the blocks
*
* @param identifier The 2d array of blocks to be loaded
* <p>
* <p>
* NOTE - Use a 2d array of [40][10] for this to work as intended
* Each value in the array represents 64x and 64y
*/
public void spawnTerrain(int[][] identifier) {
for (int i = 0; i < identifier.length; i++) {
for (int j = 0; j < identifier[i].length; j++) {
Actor a = switch (identifier[i][j]) {
case 1 -> new Brick();
case 2 -> orb = new Orb();
case 3 -> new Coin();
case 4 -> new Mites();
case 5 -> new BlueBee();
case 6 -> new RedBee();
case 7 -> new GreenBee();
case 8 -> new Spider();
case 9 -> new Crown();
case 10 -> new JumpBooster();
case 11 -> new Spike();
case 12 -> new FloorHole();
default -> null;
};
if (a != null) {
addObject(a, i * 64, j * 64);
}
}
}
}
/**
* Saves the values to a csv file
*/
public void checkToSave() {
try {
FileWriter out = new FileWriter("saveFile1.csv");
PrintWriter output = new PrintWriter(out);
output.println(totalHP + "," + totalCoins + "," + level);
output.close();
} catch (IOException e) {
System.out.println("Error writing to file");
}
}
/**
* Checks if saveButton has been clicked
* If clicked, runs the checkToSave() method
*/
public void checkSaveButton() {
if (Greenfoot.mouseClicked(saveButton)) {
checkToSave();
}
}
/**
* Increases level by one
*/
public void levelUp() {
level++;
}
/**
* Sets current HP to desired HP
*
* @param hp The desired HP
*/
public void setHP(int hp) {
totalHP = hp;
}
/**
* Sets coins to the desired coins
*
* @param coins The desired coins
*/
public void setCoins(int coins) {
totalCoins = coins;
}
}