Skip to content

Commit

Permalink
Levels indexes are now calculated automatically
Browse files Browse the repository at this point in the history
  • Loading branch information
Earthcomputer committed Oct 11, 2016
1 parent 0ff6217 commit 7ba4244
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 66 deletions.
20 changes: 10 additions & 10 deletions resources/levels/indexes
Original file line number Diff line number Diff line change
@@ -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
94 changes: 38 additions & 56 deletions src/net/earthcomputer/stepfish/Levels.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<Integer, String> levelNamesById = new HashMap<Integer, String>();
private static final Map<String, Integer> levelIdsByName = new HashMap<String, Integer>();

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;
}
}

0 comments on commit 7ba4244

Please sign in to comment.