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. * The actual LevelConfig.
* It contains all the important details that are required to build the level up * It contains all the important details that are required to build the level up
* and fill it with hot living action. * and fill it with hot living action.
* Its attributes are read by the LevelConfigParser from the files in res/levels/*.level .
*/ */
public class LevelConfig { public class LevelConfig {
/**
* The levels name.
*/
public String name; public String name;
/**
* The background the level will be using.
*/
public String background; public String background;
/**
* The background music this level will use.
*/
public String backgroundMusic; public String backgroundMusic;
public String bossEnemy;
/** /**
* Intervals have a start and an end. * Intervals have a start and an end.
@ -50,8 +61,6 @@ public class LevelConfig {
sb.append(this.background); sb.append(this.background);
sb.append(" backgroundMusic="); sb.append(" backgroundMusic=");
sb.append(this.backgroundMusic); sb.append(this.backgroundMusic);
sb.append(" bossEnemy=");
sb.append(this.bossEnemy);
sb.append("\\\n\tRules:\n"); sb.append("\\\n\tRules:\n");
for(int[] rule : this.spawnRuleList) { for(int[] rule : this.spawnRuleList) {
sb.append("\t"); sb.append("\t");
@ -67,9 +76,17 @@ public class LevelConfig {
* TODO: Catch overlapping intervals and more! * TODO: Catch overlapping intervals and more!
*/ */
public void addIntervalToList(int intervalStart, int intervalEnd) { public void addIntervalToList(int intervalStart, int intervalEnd) {
if(this.getIntervalIndexByBorders(intervalStart, intervalEnd) != -1) { if(intervalStart >= intervalEnd) {
System.err.println("Duplicate interval - not added!"); 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 { } 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}; int[] newInterval= {intervalStart, intervalEnd};
this.intervalList.add(newInterval); this.intervalList.add(newInterval);
} }
@ -106,8 +123,7 @@ public class LevelConfig {
public void addEntitySpawnRule(int intervalStart, int intervalEnd, String entityName, int amount, int spawnRate) { public void addEntitySpawnRule(int intervalStart, int intervalEnd, String entityName, int amount, int spawnRate) {
int intervalIndex = this.getIntervalIndexByBorders(intervalStart, intervalEnd); int intervalIndex = this.getIntervalIndexByBorders(intervalStart, intervalEnd);
if(intervalIndex == -1) { if(intervalIndex == -1) {
System.err.println("No Interval for rule found!"); throw new LevelConfigException("No Interval for rule found!\nRule: " + intervalStart + " to " + intervalEnd + ": " + entityName + ", " + amount + ", " + spawnRate);
System.err.println("Rule: " + intervalStart + " to " + intervalEnd + ": " + entityName + ", " + amount + ", " + spawnRate);
} else { } else {
int enemyNumber = Entity.availableNames.valueOf(entityName).ordinal(); int enemyNumber = Entity.availableNames.valueOf(entityName).ordinal();
int[] newRule = {intervalIndex, enemyNumber, amount, spawnRate}; 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]; this.levelConfig.background = linePieces[1];
} else if (linePieces[0].equals("backgroundMusic")) { } else if (linePieces[0].equals("backgroundMusic")) {
this.levelConfig.backgroundMusic = linePieces[1]; this.levelConfig.backgroundMusic = linePieces[1];
} else if (linePieces[0].equals("bossEnemy")) {
this.levelConfig.bossEnemy = linePieces[1];
} else { } else {
System.err.println("[LevelConfigParser] Unknown attribute in line: '" + line + "'"); throw new LevelConfigException("[LevelConfigParser] Unknown attribute in line: '" + line + "'");
} }
} }
break; break;
@ -91,14 +89,12 @@ public class LevelConfigParser {
String[] entitySpawnRule = rule[1].split(",", 3); String[] entitySpawnRule = rule[1].split(",", 3);
this.levelConfig.addEntitySpawnRule(this.currentIntervalStart, this.currentIntervalEnd, entitySpawnRule[0], Integer.parseInt(entitySpawnRule[1]), Integer.parseInt(entitySpawnRule[2])); this.levelConfig.addEntitySpawnRule(this.currentIntervalStart, this.currentIntervalEnd, entitySpawnRule[0], Integer.parseInt(entitySpawnRule[1]), Integer.parseInt(entitySpawnRule[2]));
} else { } else {
System.err.println("Unknown rule type: '"+rule[0]+"' : '"+line+"'"); throw new LevelConfigException("Unknown rule type: '"+rule[0]+"' : '"+line+"'");
} }
} }
break; break;
default: default:
System.err.println("[LevelConfigParser] Where am i?!"); throw new LevelConfigException("[LevelConfigParser] Where am i?!");
this.printUnknownLine(line);
break;
} }
} }
return this.levelConfig; 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) { protected void paint(Graphics2D g) {
g.setColor(new Color(0,0,120)); g.setColor(new Color(0,0,120));
g.fillRect(0, 0, GameConfig.windowWidth, GameConfig.windowHeight); 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.setFont(new Font("Monospace", 0, 50));
g.drawString("Loading ...", 100, 100); g.drawString("Loading ...", 100, 100);
g.drawString("Progress: " + this.getProgress() + "%", 200, 500); g.drawString("Progress: " + this.getProgress() + "%", 200, 500);