From b9f03a9a657bda047e612921016bd7937fcbeeb9 Mon Sep 17 00:00:00 2001 From: Jan Philipp Timme Date: Fri, 28 Nov 2014 22:04:49 +0100 Subject: [PATCH] Change error behaviour in LevelConfigParser, add interval checks. --- .../spacescooter/level/LevelConfig.java | 30 ++++++++++---- .../level/LevelConfigException.java | 39 +++++++++++++++++++ .../spacescooter/level/LevelConfigParser.java | 19 ++------- .../spacescooter/screen/LoadingScreen.java | 2 +- 4 files changed, 66 insertions(+), 24 deletions(-) create mode 100644 src/de/teamteamteam/spacescooter/level/LevelConfigException.java diff --git a/src/de/teamteamteam/spacescooter/level/LevelConfig.java b/src/de/teamteamteam/spacescooter/level/LevelConfig.java index f99facd..da3d15d 100644 --- a/src/de/teamteamteam/spacescooter/level/LevelConfig.java +++ b/src/de/teamteamteam/spacescooter/level/LevelConfig.java @@ -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}; diff --git a/src/de/teamteamteam/spacescooter/level/LevelConfigException.java b/src/de/teamteamteam/spacescooter/level/LevelConfigException.java new file mode 100644 index 0000000..07a6c79 --- /dev/null +++ b/src/de/teamteamteam/spacescooter/level/LevelConfigException.java @@ -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(); + } + +} diff --git a/src/de/teamteamteam/spacescooter/level/LevelConfigParser.java b/src/de/teamteamteam/spacescooter/level/LevelConfigParser.java index 18e72ae..e5f248d 100644 --- a/src/de/teamteamteam/spacescooter/level/LevelConfigParser.java +++ b/src/de/teamteamteam/spacescooter/level/LevelConfigParser.java @@ -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(); - } - } diff --git a/src/de/teamteamteam/spacescooter/screen/LoadingScreen.java b/src/de/teamteamteam/spacescooter/screen/LoadingScreen.java index 0cc67f1..e2af7c7 100644 --- a/src/de/teamteamteam/spacescooter/screen/LoadingScreen.java +++ b/src/de/teamteamteam/spacescooter/screen/LoadingScreen.java @@ -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);