|
7 | 7 | import java.util.HashMap; |
8 | 8 | import java.util.Map; |
9 | 9 | import java.util.Scanner; |
| 10 | +import java.util.regex.Pattern; |
10 | 11 |
|
11 | 12 | import net.earthcomputer.stepfish.Level.LevelObject; |
12 | 13 |
|
13 | | -public class Levels |
14 | | -{ |
| 14 | +public class Levels { |
15 | 15 | private static final int CURRENT_LEVEL_VERSION = 0; |
16 | | - |
| 16 | + |
17 | 17 | private static final Map<Integer, String> levelNamesById = new HashMap<Integer, String>(); |
18 | 18 | private static final Map<String, Integer> levelIdsByName = new HashMap<String, Integer>(); |
19 | | - |
20 | | - static |
21 | | - { |
| 19 | + |
| 20 | + static { |
22 | 21 | 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(); |
42 | 29 | levelNamesById.put(levelIndex, levelName); |
43 | 30 | levelIdsByName.put(levelName, levelIndex); |
44 | 31 | } |
| 32 | + levelIndex++; |
45 | 33 | } |
46 | 34 | indexesScanner.close(); |
47 | 35 | } |
48 | | - |
49 | | - public static String getNameById(int id) |
50 | | - { |
| 36 | + |
| 37 | + public static String getNameById(int id) { |
51 | 38 | return levelNamesById.get(id); |
52 | 39 | } |
53 | | - |
54 | | - public static int getIdFromName(String name) |
55 | | - { |
| 40 | + |
| 41 | + public static int getIdFromName(String name) { |
56 | 42 | return levelIdsByName.get(name); |
57 | 43 | } |
58 | | - |
59 | | - public static Level loadLevel(int id) throws LevelFormatException, IOException |
60 | | - { |
| 44 | + |
| 45 | + public static Level loadLevel(int id) throws LevelFormatException, IOException { |
61 | 46 | return loadLevel(getNameById(id)); |
62 | 47 | } |
63 | | - |
64 | | - public static Level loadLevel(String name) throws LevelFormatException, IOException |
65 | | - { |
| 48 | + |
| 49 | + public static Level loadLevel(String name) throws LevelFormatException, IOException { |
66 | 50 | 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)))); |
68 | 52 | } |
69 | | - |
70 | | - private static Level loadLevel(InputStream input) throws LevelFormatException, IOException |
71 | | - { |
| 53 | + |
| 54 | + private static Level loadLevel(InputStream input) throws LevelFormatException, IOException { |
72 | 55 | 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 | + |
76 | 60 | 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 | + |
79 | 64 | String levelName = dataInput.readUTF(); |
80 | 65 | int levelWidth = dataInput.readUnsignedShort(); |
81 | 66 | int levelHeight = dataInput.readUnsignedShort(); |
82 | | - |
| 67 | + |
83 | 68 | int objectCount = dataInput.readUnsignedShort(); |
84 | 69 | LevelObject[] objects = new LevelObject[objectCount]; |
85 | | - for(int i = 0; i < objectCount; i++) |
86 | | - { |
| 70 | + for (int i = 0; i < objectCount; i++) { |
87 | 71 | int x = dataInput.readInt(); |
88 | 72 | int y = dataInput.readInt(); |
89 | 73 | int id = dataInput.readUnsignedShort(); |
90 | 74 | objects[i] = new LevelObject(x, y, id); |
91 | 75 | } |
92 | | - |
| 76 | + |
93 | 77 | return new Level(levelName, levelWidth, levelHeight, objects); |
94 | 78 | } |
95 | | - |
96 | | - public static int getLevelCount() |
97 | | - { |
| 79 | + |
| 80 | + public static int getLevelCount() { |
98 | 81 | return levelNamesById.size(); |
99 | 82 | } |
100 | | - |
101 | | - public static class LevelFormatException extends Exception |
102 | | - { |
| 83 | + |
| 84 | + public static class LevelFormatException extends Exception { |
103 | 85 | private static final long serialVersionUID = 5901910426359021333L; |
104 | 86 | } |
105 | 87 | } |
0 commit comments