diff --git a/src/de/teamteamteam/spacescooter/Main.java b/src/de/teamteamteam/spacescooter/Main.java index bead95e..3515619 100644 --- a/src/de/teamteamteam/spacescooter/Main.java +++ b/src/de/teamteamteam/spacescooter/Main.java @@ -3,11 +3,14 @@ package de.teamteamteam.spacescooter; import java.awt.EventQueue; import de.teamteamteam.spacescooter.gui.GameFrame; +import de.teamteamteam.spacescooter.screen.LoadingScreen; +import de.teamteamteam.spacescooter.screen.Screen; import de.teamteamteam.spacescooter.screen.SuperScreen; import de.teamteamteam.spacescooter.thread.PaintThread; import de.teamteamteam.spacescooter.thread.UpdateThread; import de.teamteamteam.spacescooter.utility.GameConfig; import de.teamteamteam.spacescooter.utility.GraphicsSettings; +import de.teamteamteam.spacescooter.utility.Loader; /** * Nothing but a class containing the main method. @@ -30,7 +33,7 @@ public class Main { //Initialize SuperScreen and add to GameFrame, so we can call doPaint() on it. final SuperScreen superScreen = new SuperScreen(null); - + //Initialize the GameFrame properly within the AWT EventQueue EventQueue.invokeLater(new Runnable() { public void run() { @@ -51,5 +54,10 @@ public class Main { updateThread.setHz(100); //This shall remain constant across all systems. updateThread.start(); + //Set up the LoadingScreen + superScreen.setOverlay(new LoadingScreen(superScreen)); + + //Start loading and everything will follow up. + Loader.load((LoadingScreen) Screen.currentScreen); } } diff --git a/src/de/teamteamteam/spacescooter/background/StarBackground.java b/src/de/teamteamteam/spacescooter/background/StarBackground.java index f2541a9..72dde15 100644 --- a/src/de/teamteamteam/spacescooter/background/StarBackground.java +++ b/src/de/teamteamteam/spacescooter/background/StarBackground.java @@ -1,28 +1,13 @@ package de.teamteamteam.spacescooter.background; import java.awt.Graphics2D; -import java.awt.image.BufferedImage; -import java.io.IOException; - -import javax.imageio.ImageIO; - -import de.teamteamteam.spacescooter.entity.Player; public class StarBackground extends Background { public StarBackground(int x, int y) { super(x, y); - } - - private static BufferedImage img; - static { - try { - img = ImageIO.read(Player.class.getClassLoader().getResourceAsStream("images/starbackground.png")); - } catch (IOException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } + this.setImage("images/starbackground.png"); } private int offset = 0; @@ -30,14 +15,14 @@ public class StarBackground extends Background { public void update() { this.offset -= 3; //System.out.println(this.offset); - if(Math.abs(this.offset) > StarBackground.img.getWidth()) { - this.offset += StarBackground.img.getWidth(); + if(Math.abs(this.offset) > this.getImage().getWidth()) { + this.offset += this.getImage().getWidth(); } } public void paint(Graphics2D g) { - g.drawImage(StarBackground.img, (0+this.offset), 0, null); - g.drawImage(StarBackground.img, (StarBackground.img.getWidth()+this.offset), 0, null); + g.drawImage(this.getImage(), (0+this.offset), 0, null); + g.drawImage(this.getImage(), (this.getImage().getWidth()+this.offset), 0, null); } } diff --git a/src/de/teamteamteam/spacescooter/entity/EnemyOne.java b/src/de/teamteamteam/spacescooter/entity/EnemyOne.java index 7768516..7c12f4f 100644 --- a/src/de/teamteamteam/spacescooter/entity/EnemyOne.java +++ b/src/de/teamteamteam/spacescooter/entity/EnemyOne.java @@ -1,25 +1,10 @@ package de.teamteamteam.spacescooter.entity; -import java.awt.image.BufferedImage; -import java.io.IOException; - -import javax.imageio.ImageIO; - public class EnemyOne extends Enemy { - private static BufferedImage img; - - static { - try { - img = ImageIO.read(Player.class.getClassLoader().getResourceAsStream("images/nyancat.png")); - } catch (IOException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - } public EnemyOne(int x, int y) { super(x, y); - this.setImage(EnemyOne.img); + this.setImage("images/nyancat.png"); this.setShootSpeed(2); this.setShootDelay(42); this.setShootSpawn(-17, 32); diff --git a/src/de/teamteamteam/spacescooter/entity/Entity.java b/src/de/teamteamteam/spacescooter/entity/Entity.java index e8ba908..b640032 100644 --- a/src/de/teamteamteam/spacescooter/entity/Entity.java +++ b/src/de/teamteamteam/spacescooter/entity/Entity.java @@ -5,6 +5,7 @@ import java.awt.Graphics2D; import java.awt.image.BufferedImage; import de.teamteamteam.spacescooter.utility.GameConfig; +import de.teamteamteam.spacescooter.utility.Loader; public abstract class Entity implements Updateable, Paintable { @@ -49,8 +50,8 @@ public abstract class Entity implements Updateable, Paintable { return this.img; } - public void setImage(BufferedImage img) { - this.img = img; + public void setImage(String filename) { + this.img = Loader.getBufferedImageByFilename(filename); } public void paint(Graphics2D g) { diff --git a/src/de/teamteamteam/spacescooter/entity/Player.java b/src/de/teamteamteam/spacescooter/entity/Player.java index da1d66b..4a192ef 100644 --- a/src/de/teamteamteam/spacescooter/entity/Player.java +++ b/src/de/teamteamteam/spacescooter/entity/Player.java @@ -1,29 +1,14 @@ package de.teamteamteam.spacescooter.entity; import java.awt.event.KeyEvent; -import java.awt.image.BufferedImage; -import java.io.IOException; -import javax.imageio.ImageIO; - import de.teamteamteam.spacescooter.control.Keyboard; import de.teamteamteam.spacescooter.utility.GameConfig; public class Player extends ShootingEntity { - private static BufferedImage img; - - static { - try { - img = ImageIO.read(Player.class.getClassLoader().getResourceAsStream("images/ship.png")); - } catch (IOException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - } - public Player(int x, int y) { super(x, y); - this.setImage(Player.img); + this.setImage("images/ship.png"); this.setShootDelay(40); this.setShootSpawn(50, 16); this.setShootDirection(Shot.RIGHT); @@ -37,13 +22,13 @@ public class Player extends ShootingEntity { if(Keyboard.isKeyDown(KeyEvent.VK_UP) && this.y > 0) { this.y -= off; } - if(Keyboard.isKeyDown(KeyEvent.VK_DOWN) && this.y < (GameConfig.windowHeight - Player.img.getHeight())) { + if(Keyboard.isKeyDown(KeyEvent.VK_DOWN) && this.y < (GameConfig.windowHeight - this.getImage().getHeight())) { this.y += off; } if(Keyboard.isKeyDown(KeyEvent.VK_LEFT) && this.x > 0) { this.x -= off; } - if(Keyboard.isKeyDown(KeyEvent.VK_RIGHT) && this.x < (GameConfig.windowWidth - Player.img.getWidth())) { + if(Keyboard.isKeyDown(KeyEvent.VK_RIGHT) && this.x < (GameConfig.windowWidth - this.getImage().getWidth())) { this.x += off; } if(Keyboard.isKeyDown(KeyEvent.VK_SPACE)) { diff --git a/src/de/teamteamteam/spacescooter/entity/Shot.java b/src/de/teamteamteam/spacescooter/entity/Shot.java index aa777dc..f3d8c8c 100644 --- a/src/de/teamteamteam/spacescooter/entity/Shot.java +++ b/src/de/teamteamteam/spacescooter/entity/Shot.java @@ -1,7 +1,5 @@ package de.teamteamteam.spacescooter.entity; -import java.awt.image.BufferedImage; - public abstract class Shot extends LivingEntity { public static final int RIGHT = 1; @@ -20,8 +18,8 @@ public abstract class Shot extends LivingEntity { this.collisionCount = 1; } - public void setImage(BufferedImage img) { - super.setImage(img); + public void setImage(String filename) { + super.setImage(filename); this.setPosition(this.x - this.getImage().getWidth() / 2, this.y - this.getImage().getHeight() / 2); } diff --git a/src/de/teamteamteam/spacescooter/entity/SingleShot.java b/src/de/teamteamteam/spacescooter/entity/SingleShot.java index bf321d2..182e6a3 100644 --- a/src/de/teamteamteam/spacescooter/entity/SingleShot.java +++ b/src/de/teamteamteam/spacescooter/entity/SingleShot.java @@ -1,27 +1,10 @@ package de.teamteamteam.spacescooter.entity; -import java.awt.image.BufferedImage; -import java.io.IOException; - -import javax.imageio.ImageIO; - public class SingleShot extends Shot { - private static BufferedImage img; - - static { - try { - img = ImageIO.read(Player.class.getClassLoader().getResourceAsStream("images/shot.png")); - } catch (IOException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - } - - public SingleShot(int x, int y, int shootDirection, int shootSpeed) { super(x, y, shootDirection, shootSpeed); - this.setImage(img); + this.setImage("images/shot.png"); this.setDamageValue(5); } diff --git a/src/de/teamteamteam/spacescooter/screen/LoadingScreen.java b/src/de/teamteamteam/spacescooter/screen/LoadingScreen.java new file mode 100644 index 0000000..e089fd5 --- /dev/null +++ b/src/de/teamteamteam/spacescooter/screen/LoadingScreen.java @@ -0,0 +1,49 @@ +package de.teamteamteam.spacescooter.screen; + +import java.awt.Color; +import java.awt.Font; +import java.awt.Graphics2D; +import de.teamteamteam.spacescooter.utility.GameConfig; + +public class LoadingScreen extends Screen { + + private int currentProcessed; + private int totalProcessable; + + public LoadingScreen(Screen parent) { + super(parent); + this.currentProcessed = 0; + this.totalProcessable = 1; //sane default + } + + public void initialize(int currentProcessed, int totalProcessable) { + this.currentProcessed = currentProcessed; + this.totalProcessable = totalProcessable; + } + + public void increaseCurrentProcessed() { + this.currentProcessed++; + } + + public int getProgress() { + return (int) Math.round((100.0 * this.currentProcessed) / this.totalProcessable); + } + + @Override + 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.setFont(new Font("Monospace", 0, 50)); + g.drawString("Loading ...", 100, 100); + g.drawString("Progress: " + this.getProgress() + "%", 200, 500); + } + + @Override + protected void update() { + if(this.getProgress() == 100) { + this.parent.setOverlay(new MainMenuScreen(this.parent)); + } + } + +} diff --git a/src/de/teamteamteam/spacescooter/screen/SuperScreen.java b/src/de/teamteamteam/spacescooter/screen/SuperScreen.java index c1c5468..af0eab5 100644 --- a/src/de/teamteamteam/spacescooter/screen/SuperScreen.java +++ b/src/de/teamteamteam/spacescooter/screen/SuperScreen.java @@ -6,7 +6,6 @@ public class SuperScreen extends Screen { public SuperScreen(Screen parent) { super(null); - this.overlay = new MainMenuScreen(this); } @Override diff --git a/src/de/teamteamteam/spacescooter/utility/CodeEnvironment.java b/src/de/teamteamteam/spacescooter/utility/CodeEnvironment.java new file mode 100644 index 0000000..8e028c5 --- /dev/null +++ b/src/de/teamteamteam/spacescooter/utility/CodeEnvironment.java @@ -0,0 +1,85 @@ +package de.teamteamteam.spacescooter.utility; + +import java.io.File; +import java.io.IOException; +import java.net.URISyntaxException; +import java.net.URL; +import java.util.ArrayList; +import java.util.List; +import java.util.zip.ZipEntry; +import java.util.zip.ZipInputStream; + +public class CodeEnvironment { + + /** + * Returns true if the program is run out of a jar. + */ + public static boolean isJar() { + return (CodeEnvironment.class.getProtectionDomain().getCodeSource().getLocation().toString().endsWith("jar")); + } + + /** + * Returns a String[] containing relative paths to files within the code. + */ + public static String[] getFileList() { + URL codeLocation = CodeEnvironment.class.getProtectionDomain().getCodeSource().getLocation(); + if(CodeEnvironment.isJar()) { + return CodeEnvironment.getFileListFromJar(codeLocation); + } else { + File codeFolder = null; + try { + codeFolder = new File(codeLocation.toURI()); + } catch (URISyntaxException e) { + System.err.println("Could not convert codeLocation ÚRL to File!"); + e.printStackTrace(); + } + return CodeEnvironment.getFileListFromFolder(codeFolder); + } + } + + /** + * Return a list of files based on a given folder. + */ + private static String[] getFileListFromFolder(File folder) { + ArrayList fileList = new ArrayList(); + String rootPath = folder.getAbsolutePath() + "/"; + File[] folderContents = folder.listFiles(); + for(File f : folderContents) { + if(f.isDirectory()) { + String[] filesInDirectory = CodeEnvironment.getFileListFromFolder(f); + for(String entry : filesInDirectory) { + fileList.add(entry.replaceAll(rootPath, "")); + } + } else { + fileList.add(f.toString()); + } + } + return fileList.toArray(new String[fileList.size()]); + } + + /** + * Returns a list of files that are contained within the current jar. + */ + private static String[] getFileListFromJar(URL jar) { + List list = new ArrayList(); + ZipInputStream zip = null; + try { + zip = new ZipInputStream(jar.openStream()); + } catch (IOException e) { + System.err.println("Could not open jar file!"); + e.printStackTrace(); + } + ZipEntry ze = null; + try { + while ((ze = zip.getNextEntry()) != null) { + String entryName = ze.getName(); + list.add(entryName); + } + } catch (IOException e) { + System.err.println("Error reading jar contents!"); + e.printStackTrace(); + } + return list.toArray(new String[list.size()]); + } + +} diff --git a/src/de/teamteamteam/spacescooter/utility/Loader.java b/src/de/teamteamteam/spacescooter/utility/Loader.java new file mode 100644 index 0000000..8dc00e3 --- /dev/null +++ b/src/de/teamteamteam/spacescooter/utility/Loader.java @@ -0,0 +1,58 @@ +package de.teamteamteam.spacescooter.utility; + +import java.awt.image.BufferedImage; +import java.io.IOException; +import java.util.Hashtable; +import javax.imageio.ImageIO; + +import de.teamteamteam.spacescooter.screen.LoadingScreen; + +public class Loader { + + private static Hashtable images; + + static { + Loader.images = new Hashtable(); + } + + + /** + * Return the loaded BufferedImage by its filename. + */ + public static BufferedImage getBufferedImageByFilename(String filename) { + return Loader.images.get(filename); + } + + /** + * Perform the initial loading of everything and show the progress on the given LoadingScreen. + */ + public static void load(LoadingScreen loadingScreen) { + // get a list of stuff to load and create buffered images + // and other things + String[] elements = CodeEnvironment.getFileList(); + loadingScreen.initialize(0, elements.length); + for(int i=0; i