Skip to content

Commit 7ba4244

Browse files
committed
Levels indexes are now calculated automatically
1 parent 0ff6217 commit 7ba4244

File tree

2 files changed

+48
-66
lines changed

2 files changed

+48
-66
lines changed

resources/levels/indexes

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
0:First Switching
2-
1:Element Mixup
3-
2:Two Players
4-
3:Enemies
5-
4:Parkour
6-
5:Spikes
7-
6:Two Player Puzzle
8-
7:Miniature Level
9-
8:Towers
10-
9:Final Drop
1+
First Switching
2+
Element Mixup
3+
Two Players
4+
Enemies
5+
Parkour
6+
Spikes
7+
Two Player Puzzle
8+
Miniature Level
9+
Towers
10+
Final Drop

src/net/earthcomputer/stepfish/Levels.java

Lines changed: 38 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -7,99 +7,81 @@
77
import java.util.HashMap;
88
import java.util.Map;
99
import java.util.Scanner;
10+
import java.util.regex.Pattern;
1011

1112
import net.earthcomputer.stepfish.Level.LevelObject;
1213

13-
public class Levels
14-
{
14+
public class Levels {
1515
private static final int CURRENT_LEVEL_VERSION = 0;
16-
16+
1717
private static final Map<Integer, String> levelNamesById = new HashMap<Integer, String>();
1818
private static final Map<String, Integer> levelIdsByName = new HashMap<String, Integer>();
19-
20-
static
21-
{
19+
20+
static {
2221
Scanner indexesScanner = new Scanner(
23-
new BufferedInputStream(Stepfish.class.getResourceAsStream("/levels/indexes")));
24-
while(indexesScanner.hasNextLine())
25-
{
26-
String line = indexesScanner.nextLine();
27-
int colonIndex = line.indexOf(':');
28-
if(colonIndex != -1)
29-
{
30-
String indexString = line.substring(0, colonIndex);
31-
String levelName = line.substring(colonIndex + 1);
32-
int levelIndex;
33-
try
34-
{
35-
levelIndex = Integer.parseInt(indexString);
36-
}
37-
catch (NumberFormatException e)
38-
{
39-
continue;
40-
}
41-
22+
new BufferedInputStream(Stepfish.class.getResourceAsStream("/levels/indexes")));
23+
Pattern whitespacePattern = Pattern.compile("\\s*");
24+
int levelIndex = 0;
25+
while (indexesScanner.hasNextLine()) {
26+
String levelName = indexesScanner.nextLine();
27+
if (!whitespacePattern.matcher(levelName).matches()) {
28+
levelName = levelName.trim();
4229
levelNamesById.put(levelIndex, levelName);
4330
levelIdsByName.put(levelName, levelIndex);
4431
}
32+
levelIndex++;
4533
}
4634
indexesScanner.close();
4735
}
48-
49-
public static String getNameById(int id)
50-
{
36+
37+
public static String getNameById(int id) {
5138
return levelNamesById.get(id);
5239
}
53-
54-
public static int getIdFromName(String name)
55-
{
40+
41+
public static int getIdFromName(String name) {
5642
return levelIdsByName.get(name);
5743
}
58-
59-
public static Level loadLevel(int id) throws LevelFormatException, IOException
60-
{
44+
45+
public static Level loadLevel(int id) throws LevelFormatException, IOException {
6146
return loadLevel(getNameById(id));
6247
}
63-
64-
public static Level loadLevel(String name) throws LevelFormatException, IOException
65-
{
48+
49+
public static Level loadLevel(String name) throws LevelFormatException, IOException {
6650
return loadLevel(
67-
new BufferedInputStream(Levels.class.getResourceAsStream(String.format("/levels/%s.gglevel", name))));
51+
new BufferedInputStream(Levels.class.getResourceAsStream(String.format("/levels/%s.gglevel", name))));
6852
}
69-
70-
private static Level loadLevel(InputStream input) throws LevelFormatException, IOException
71-
{
53+
54+
private static Level loadLevel(InputStream input) throws LevelFormatException, IOException {
7255
DataInputStream dataInput = new DataInputStream(input);
73-
74-
if(dataInput.readInt() != 0x4748474D) throw new LevelFormatException();
75-
56+
57+
if (dataInput.readInt() != 0x4748474D)
58+
throw new LevelFormatException();
59+
7660
int version = dataInput.readUnsignedByte();
77-
if(version > CURRENT_LEVEL_VERSION) throw new LevelFormatException();
78-
61+
if (version > CURRENT_LEVEL_VERSION)
62+
throw new LevelFormatException();
63+
7964
String levelName = dataInput.readUTF();
8065
int levelWidth = dataInput.readUnsignedShort();
8166
int levelHeight = dataInput.readUnsignedShort();
82-
67+
8368
int objectCount = dataInput.readUnsignedShort();
8469
LevelObject[] objects = new LevelObject[objectCount];
85-
for(int i = 0; i < objectCount; i++)
86-
{
70+
for (int i = 0; i < objectCount; i++) {
8771
int x = dataInput.readInt();
8872
int y = dataInput.readInt();
8973
int id = dataInput.readUnsignedShort();
9074
objects[i] = new LevelObject(x, y, id);
9175
}
92-
76+
9377
return new Level(levelName, levelWidth, levelHeight, objects);
9478
}
95-
96-
public static int getLevelCount()
97-
{
79+
80+
public static int getLevelCount() {
9881
return levelNamesById.size();
9982
}
100-
101-
public static class LevelFormatException extends Exception
102-
{
83+
84+
public static class LevelFormatException extends Exception {
10385
private static final long serialVersionUID = 5901910426359021333L;
10486
}
10587
}

0 commit comments

Comments
 (0)