diff --git a/resources/levels/indexes b/resources/levels/indexes index b2e9b5b..f05c5f0 100644 --- a/resources/levels/indexes +++ b/resources/levels/indexes @@ -1,10 +1,10 @@ -0:First Switching -1:Element Mixup -2:Two Players -3:Enemies -4:Parkour -5:Spikes -6:Two Player Puzzle -7:Miniature Level -8:Towers -9:Final Drop +First Switching +Element Mixup +Two Players +Enemies +Parkour +Spikes +Two Player Puzzle +Miniature Level +Towers +Final Drop diff --git a/src/net/earthcomputer/stepfish/Levels.java b/src/net/earthcomputer/stepfish/Levels.java index aef1b8b..07c82c4 100644 --- a/src/net/earthcomputer/stepfish/Levels.java +++ b/src/net/earthcomputer/stepfish/Levels.java @@ -7,99 +7,81 @@ import java.util.HashMap; import java.util.Map; import java.util.Scanner; +import java.util.regex.Pattern; import net.earthcomputer.stepfish.Level.LevelObject; -public class Levels -{ +public class Levels { private static final int CURRENT_LEVEL_VERSION = 0; - + private static final Map levelNamesById = new HashMap(); private static final Map levelIdsByName = new HashMap(); - - static - { + + static { Scanner indexesScanner = new Scanner( - new BufferedInputStream(Stepfish.class.getResourceAsStream("/levels/indexes"))); - while(indexesScanner.hasNextLine()) - { - String line = indexesScanner.nextLine(); - int colonIndex = line.indexOf(':'); - if(colonIndex != -1) - { - String indexString = line.substring(0, colonIndex); - String levelName = line.substring(colonIndex + 1); - int levelIndex; - try - { - levelIndex = Integer.parseInt(indexString); - } - catch (NumberFormatException e) - { - continue; - } - + new BufferedInputStream(Stepfish.class.getResourceAsStream("/levels/indexes"))); + Pattern whitespacePattern = Pattern.compile("\\s*"); + int levelIndex = 0; + while (indexesScanner.hasNextLine()) { + String levelName = indexesScanner.nextLine(); + if (!whitespacePattern.matcher(levelName).matches()) { + levelName = levelName.trim(); levelNamesById.put(levelIndex, levelName); levelIdsByName.put(levelName, levelIndex); } + levelIndex++; } indexesScanner.close(); } - - public static String getNameById(int id) - { + + public static String getNameById(int id) { return levelNamesById.get(id); } - - public static int getIdFromName(String name) - { + + public static int getIdFromName(String name) { return levelIdsByName.get(name); } - - public static Level loadLevel(int id) throws LevelFormatException, IOException - { + + public static Level loadLevel(int id) throws LevelFormatException, IOException { return loadLevel(getNameById(id)); } - - public static Level loadLevel(String name) throws LevelFormatException, IOException - { + + public static Level loadLevel(String name) throws LevelFormatException, IOException { return loadLevel( - new BufferedInputStream(Levels.class.getResourceAsStream(String.format("/levels/%s.gglevel", name)))); + new BufferedInputStream(Levels.class.getResourceAsStream(String.format("/levels/%s.gglevel", name)))); } - - private static Level loadLevel(InputStream input) throws LevelFormatException, IOException - { + + private static Level loadLevel(InputStream input) throws LevelFormatException, IOException { DataInputStream dataInput = new DataInputStream(input); - - if(dataInput.readInt() != 0x4748474D) throw new LevelFormatException(); - + + if (dataInput.readInt() != 0x4748474D) + throw new LevelFormatException(); + int version = dataInput.readUnsignedByte(); - if(version > CURRENT_LEVEL_VERSION) throw new LevelFormatException(); - + if (version > CURRENT_LEVEL_VERSION) + throw new LevelFormatException(); + String levelName = dataInput.readUTF(); int levelWidth = dataInput.readUnsignedShort(); int levelHeight = dataInput.readUnsignedShort(); - + int objectCount = dataInput.readUnsignedShort(); LevelObject[] objects = new LevelObject[objectCount]; - for(int i = 0; i < objectCount; i++) - { + for (int i = 0; i < objectCount; i++) { int x = dataInput.readInt(); int y = dataInput.readInt(); int id = dataInput.readUnsignedShort(); objects[i] = new LevelObject(x, y, id); } - + return new Level(levelName, levelWidth, levelHeight, objects); } - - public static int getLevelCount() - { + + public static int getLevelCount() { return levelNamesById.size(); } - - public static class LevelFormatException extends Exception - { + + public static class LevelFormatException extends Exception { private static final long serialVersionUID = 5901910426359021333L; } }