Change error behaviour in LevelConfigParser, add interval checks.

This commit is contained in:
Jan Philipp Timme 2014-11-28 22:04:49 +01:00
parent 7a9766788a
commit b9f03a9a65
4 changed files with 66 additions and 24 deletions

View File

@ -9,13 +9,24 @@ import de.teamteamteam.spacescooter.entity.Entity;
* The actual LevelConfig.
* It contains all the important details that are required to build the level up
* and fill it with hot living action.
* Its attributes are read by the LevelConfigParser from the files in res/levels/*.level .
*/
public class LevelConfig {
/**
* The levels name.
*/
public String name;
/**
* The background the level will be using.
*/
public String background;
/**
* The background music this level will use.
*/
public String backgroundMusic;
public String bossEnemy;
/**
* Intervals have a start and an end.
@ -50,8 +61,6 @@ public class LevelConfig {
sb.append(this.background);
sb.append(" backgroundMusic=");
sb.append(this.backgroundMusic);
sb.append(" bossEnemy=");
sb.append(this.bossEnemy);
sb.append("\\\n\tRules:\n");
for(int[] rule : this.spawnRuleList) {
sb.append("\t");
@ -67,9 +76,17 @@ public class LevelConfig {
* TODO: Catch overlapping intervals and more!
*/
public void addIntervalToList(int intervalStart, int intervalEnd) {
if(this.getIntervalIndexByBorders(intervalStart, intervalEnd) != -1) {
System.err.println("Duplicate interval - not added!");
if(intervalStart >= intervalEnd) {
throw new LevelConfigException("Interval borders are invalid! intervalStart must be < intervalEnd!");
} else if(this.getIntervalIndexByBorders(intervalStart, intervalEnd) != -1) {
throw new LevelConfigException("Duplicate interval - not added!");
} else {
if(this.intervalList.size() > 0) {
int[] lastIntervalInList = this.intervalList.get(this.intervalList.size() - 1);
if(intervalStart < lastIntervalInList[1]) {
throw new LevelConfigException("Intervals must be added in order!");
}
}
int[] newInterval= {intervalStart, intervalEnd};
this.intervalList.add(newInterval);
}
@ -106,8 +123,7 @@ public class LevelConfig {
public void addEntitySpawnRule(int intervalStart, int intervalEnd, String entityName, int amount, int spawnRate) {
int intervalIndex = this.getIntervalIndexByBorders(intervalStart, intervalEnd);
if(intervalIndex == -1) {
System.err.println("No Interval for rule found!");
System.err.println("Rule: " + intervalStart + " to " + intervalEnd + ": " + entityName + ", " + amount + ", " + spawnRate);
throw new LevelConfigException("No Interval for rule found!\nRule: " + intervalStart + " to " + intervalEnd + ": " + entityName + ", " + amount + ", " + spawnRate);
} else {
int enemyNumber = Entity.availableNames.valueOf(entityName).ordinal();
int[] newRule = {intervalIndex, enemyNumber, amount, spawnRate};

View File

@ -0,0 +1,39 @@
package de.teamteamteam.spacescooter.level;
/**
* RuntimeException thrown when parsing (or subsequent actions concerning the LevelConfig)
* go horribly wrong.
*/
public class LevelConfigException extends RuntimeException {
/**
* Default serial Version UID.
*/
private static final long serialVersionUID = 1L;
/**
* Message the Exception carries with it.
*/
private String message;
/**
* Constructor passing on the message.
*/
public LevelConfigException(String msg) {
this.message = msg;
}
/**
* Add our custom message to the Exceptions representation.
*/
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append(this.message);
sb.append("\n");
sb.append(super.toString());
return sb.toString();
}
}

View File

@ -70,10 +70,8 @@ public class LevelConfigParser {
this.levelConfig.background = linePieces[1];
} else if (linePieces[0].equals("backgroundMusic")) {
this.levelConfig.backgroundMusic = linePieces[1];
} else if (linePieces[0].equals("bossEnemy")) {
this.levelConfig.bossEnemy = linePieces[1];
} else {
System.err.println("[LevelConfigParser] Unknown attribute in line: '" + line + "'");
throw new LevelConfigException("[LevelConfigParser] Unknown attribute in line: '" + line + "'");
}
}
break;
@ -91,14 +89,12 @@ public class LevelConfigParser {
String[] entitySpawnRule = rule[1].split(",", 3);
this.levelConfig.addEntitySpawnRule(this.currentIntervalStart, this.currentIntervalEnd, entitySpawnRule[0], Integer.parseInt(entitySpawnRule[1]), Integer.parseInt(entitySpawnRule[2]));
} else {
System.err.println("Unknown rule type: '"+rule[0]+"' : '"+line+"'");
throw new LevelConfigException("Unknown rule type: '"+rule[0]+"' : '"+line+"'");
}
}
break;
default:
System.err.println("[LevelConfigParser] Where am i?!");
this.printUnknownLine(line);
break;
throw new LevelConfigException("[LevelConfigParser] Where am i?!");
}
}
return this.levelConfig;
@ -129,13 +125,4 @@ public class LevelConfigParser {
}
}
/**
* Debugging method - print an unknown line read.
*/
private void printUnknownLine(String line) {
System.err.println("[LevelConfigParser] Unknown line?");
System.err.println(line);
System.err.println();
}
}

View File

@ -42,7 +42,7 @@ public class LoadingScreen extends Screen {
protected void paint(Graphics2D g) {
g.setColor(new Color(0,0,120));
g.fillRect(0, 0, GameConfig.windowWidth, GameConfig.windowHeight);
g.setColor(new Color(255,255,255));
g.setColor(Color.WHITE);
g.setFont(new Font("Monospace", 0, 50));
g.drawString("Loading ...", 100, 100);
g.drawString("Progress: " + this.getProgress() + "%", 200, 500);