From 420cbd67b114f055b6161986882f4d0dc55f8e49 Mon Sep 17 00:00:00 2001 From: Jan Philipp Timme Date: Tue, 25 Nov 2014 10:45:26 +0100 Subject: [PATCH] Create a basic test level config, make the Loader parse it and do first steps actually implementing the concept. --- .../temp.properties => res/levels/test.level | 6 +-- .../spacescooter/level/Level.java | 33 +++++++++++++ .../spacescooter/level/LevelConfigParser.java | 15 ++---- .../spacescooter/screen/GameOverScreen.java | 2 +- .../spacescooter/screen/GameScreen.java | 29 +++++++++++- .../spacescooter/screen/MainMenuScreen.java | 2 +- .../spacescooter/utility/Loader.java | 46 ++++++++++++++++++- 7 files changed, 114 insertions(+), 19 deletions(-) rename src/de/teamteamteam/spacescooter/level/temp.properties => res/levels/test.level (67%) create mode 100644 src/de/teamteamteam/spacescooter/level/Level.java diff --git a/src/de/teamteamteam/spacescooter/level/temp.properties b/res/levels/test.level similarity index 67% rename from src/de/teamteamteam/spacescooter/level/temp.properties rename to res/levels/test.level index 2b2c261..f26251c 100644 --- a/src/de/teamteamteam/spacescooter/level/temp.properties +++ b/res/levels/test.level @@ -4,7 +4,7 @@ background:FooBackground bossEnemy:TheBigOne - [0-2025] -GegnerOne,2,6 +spawn:GegnerOne,2,6 [2026-2168] -GegnerOne,2,4 -GegnerTwo,5,6 \ No newline at end of file +spawn:GegnerOne,2,4 +spawn:GegnerTwo,5,6 \ No newline at end of file diff --git a/src/de/teamteamteam/spacescooter/level/Level.java b/src/de/teamteamteam/spacescooter/level/Level.java new file mode 100644 index 0000000..c2b5ca7 --- /dev/null +++ b/src/de/teamteamteam/spacescooter/level/Level.java @@ -0,0 +1,33 @@ +package de.teamteamteam.spacescooter.level; + +import de.teamteamteam.spacescooter.utility.Loader; + +public final class Level { + + /** + * Internal LevelConfig containing all the details about how this Level will manage GamePlay. + */ + private LevelConfig config; + + + /** + * Constructor creating a LevelConfig based on a given config file. + */ + public Level(String levelConfig) { + this.config = Loader.getLevelConfigByFilename(levelConfig); + System.out.println(this.config); + } + + + /** + * The magic will happen in here. + * Each time the Level will receive its updateTick, + * it will do some checks, increase an internal counter and do + * evil stuff like looking up what monsters to spawn and whatever else + * is neccessary to torture the player. + */ + public void handleUpdateTick() { + + } + +} diff --git a/src/de/teamteamteam/spacescooter/level/LevelConfigParser.java b/src/de/teamteamteam/spacescooter/level/LevelConfigParser.java index aa22479..e25508b 100644 --- a/src/de/teamteamteam/spacescooter/level/LevelConfigParser.java +++ b/src/de/teamteamteam/spacescooter/level/LevelConfigParser.java @@ -1,17 +1,10 @@ package de.teamteamteam.spacescooter.level; -import java.io.File; +import java.io.InputStream; import java.util.Scanner; public class LevelConfigParser { - /* DEBUG */ - public static void main(String[] args) { - LevelConfigParser lcp = new LevelConfigParser(); - LevelConfig lc = lcp.parse("/home/stud/timmeja/Programmierprojekt/SpaceScooter/src/de/teamteamteam/spacescooter/level/temp.properties"); - System.out.println(lc); - } - /** * Internal LevelConfig instance to fill with data. @@ -50,7 +43,7 @@ public class LevelConfigParser { /** * Takes a given configFile and turns it into a LevelConfig object. */ - public LevelConfig parse(String configFile) { + public LevelConfig parse(InputStream configFile) { this.reset(); this.prepareScanner(configFile); while (this.scanner.hasNextLine()) { @@ -103,9 +96,9 @@ public class LevelConfigParser { /** * Prepares a Scanner for the file to parse. */ - private void prepareScanner(String filename) { + private void prepareScanner(InputStream configStream) { try { - this.scanner = new Scanner(new File(filename)); + this.scanner = new Scanner(configStream); } catch (Exception e) { e.printStackTrace(); } diff --git a/src/de/teamteamteam/spacescooter/screen/GameOverScreen.java b/src/de/teamteamteam/spacescooter/screen/GameOverScreen.java index cbb63fd..b7334d8 100644 --- a/src/de/teamteamteam/spacescooter/screen/GameOverScreen.java +++ b/src/de/teamteamteam/spacescooter/screen/GameOverScreen.java @@ -88,7 +88,7 @@ public class GameOverScreen extends Screen { } else if(this.animationStatus == 2) { switch (this.menuPoint) { case 0: - this.parent.setOverlay(new GameScreen(this.parent)); + this.parent.setOverlay(new GameScreen(this.parent, "levels/test.level")); break; case 1: this.parent.setOverlay(new MainMenuScreen(this.parent)); diff --git a/src/de/teamteamteam/spacescooter/screen/GameScreen.java b/src/de/teamteamteam/spacescooter/screen/GameScreen.java index 899db25..c77513e 100644 --- a/src/de/teamteamteam/spacescooter/screen/GameScreen.java +++ b/src/de/teamteamteam/spacescooter/screen/GameScreen.java @@ -16,6 +16,7 @@ import de.teamteamteam.spacescooter.gui.HealthBar; import de.teamteamteam.spacescooter.gui.InterfaceBar; import de.teamteamteam.spacescooter.gui.ScoreBar; import de.teamteamteam.spacescooter.gui.ShieldBar; +import de.teamteamteam.spacescooter.level.Level; import de.teamteamteam.spacescooter.utility.CollisionHandler; /** @@ -33,8 +34,21 @@ public class GameScreen extends Screen { private static Player player; - public GameScreen(Screen parent) { + /** + * Level instance to handle all the stuff based on its LevelConfig. + */ + private Level level; + + + /** + * GameScreen Constructor. + * Takes the level as its second parameter. + */ + public GameScreen(Screen parent, String levelConfigName) { super(parent); + this.level = new Level(levelConfigName); + + //Old style adding stuff new ItemChance(); points.add(new Point(300,300)); points.add(new Point(600,100)); @@ -50,6 +64,7 @@ public class GameScreen extends Screen { new EnemyBoss(200, 300); } + @Override protected void paint(Graphics2D g) { this.entityPaintIterator.reset(); @@ -58,8 +73,18 @@ public class GameScreen extends Screen { } } + /** + * Trigger level logic, trigger updates on all entities, + * take care of user input such as pressing escape and + * do a little(!) check on the gameover condition. + * TODO: Let the level take care of that. + */ @Override protected void update() { + //The level will take care of whatever happens next. + this.level.handleUpdateTick(); + + //Take care of the usual bussiness this.entityUpdateIterator.reset(); while (this.entityUpdateIterator.hasNext()) { this.entityUpdateIterator.next().update(); @@ -69,7 +94,7 @@ public class GameScreen extends Screen { if (Keyboard.isKeyDown(KeyEvent.VK_ESCAPE)) { this.setOverlay(new GamePausedScreen(this)); } - if (!GameScreen.player.isAlive()) { + if (!GameScreen.player.isAlive()) { //The level shall take this over. this.parent.setOverlay(new GameOverScreen(this.parent)); } } diff --git a/src/de/teamteamteam/spacescooter/screen/MainMenuScreen.java b/src/de/teamteamteam/spacescooter/screen/MainMenuScreen.java index 2a6df92..934075c 100644 --- a/src/de/teamteamteam/spacescooter/screen/MainMenuScreen.java +++ b/src/de/teamteamteam/spacescooter/screen/MainMenuScreen.java @@ -100,7 +100,7 @@ public class MainMenuScreen extends Screen { } else if(animationStatus == 2) { switch (menuPoint) { case 0: - this.parent.setOverlay(new GameScreen(this.parent)); + this.parent.setOverlay(new GameScreen(this.parent, "levels/test.level")); break; case 1: this.parent.setOverlay(new ShopScreen(this.parent)); diff --git a/src/de/teamteamteam/spacescooter/utility/Loader.java b/src/de/teamteamteam/spacescooter/utility/Loader.java index 9291bc4..891ddb4 100644 --- a/src/de/teamteamteam/spacescooter/utility/Loader.java +++ b/src/de/teamteamteam/spacescooter/utility/Loader.java @@ -3,6 +3,7 @@ package de.teamteamteam.spacescooter.utility; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; +import java.io.InputStream; import java.net.URL; import java.util.Hashtable; @@ -10,6 +11,8 @@ import javax.imageio.ImageIO; import javax.sound.sampled.AudioInputStream; import javax.sound.sampled.UnsupportedAudioFileException; +import de.teamteamteam.spacescooter.level.LevelConfig; +import de.teamteamteam.spacescooter.level.LevelConfigParser; import de.teamteamteam.spacescooter.screen.LoadingScreen; import de.teamteamteam.spacescooter.sound.SoundSystem; @@ -19,6 +22,12 @@ import de.teamteamteam.spacescooter.sound.SoundSystem; */ public class Loader { + /** + * Private instance of the LevelConfigParser to parse LevelConfigs. + * Sorry, i know this is kind of bad, but it is a TODO to clean that up. :-/ + */ + private static LevelConfigParser levelConfigParser; + /** * HashTable containing loaded BufferedImages */ @@ -29,12 +38,20 @@ public class Loader { */ private static Hashtable sounds; + /** + * HashTable containing the parsed LevelConfigs. + */ + private static Hashtable levelConfigs; + + /** * Initialize the HashTables on load. */ static { Loader.images = new Hashtable(); Loader.sounds = new Hashtable(); + Loader.levelConfigs = new Hashtable(); + Loader.levelConfigParser = new LevelConfigParser(); } @@ -71,7 +88,19 @@ public class Loader { return Loader.sounds.get(filename.replace("/", File.separator)); } } - + + /** + * Return the LevelConfig by its relative filename. + */ + public static LevelConfig getLevelConfigByFilename(String filename) { + if(CodeEnvironment.isJar()) { + return Loader.levelConfigs.get(filename); + } else { + return Loader.levelConfigs.get(filename.replace("/", File.separator)); + } + } + + /** * Perform the initial loading of everything and show the progress on the * given LoadingScreen. @@ -98,10 +127,25 @@ public class Loader { System.out.println("Creating AudioInputStream for: " + e); Loader.addSoundURLByFilename(e); } + if(e.endsWith(".level")) { + if(GameConfig.DEBUG) { + System.out.println("Creating LevelConfig for: " + e); + } + Loader.addLevelByFilename(e); + } loadingScreen.increaseCurrentProcessed(); } } + /** + * Preload a LevelConfig by simply parsing it into a LevelConfig object. + */ + private static void addLevelByFilename(String levelFilename) { + InputStream foo = Loader.class.getClassLoader().getResourceAsStream(levelFilename); + LevelConfig levelConfig = Loader.levelConfigParser.parse(foo); + Loader.levelConfigs.put(levelFilename, levelConfig); + } + /** * Preload a given class by its filename using the ClassLoader. * This way, we avoid reading out of a jar later.