Umlagern des Ladens von Bildern, Einführung des (noch sehr kurzen) Ladebildschirmes.

This commit is contained in:
Jan Philipp Timme 2014-10-30 20:21:11 +01:00
parent 8d009c94a6
commit 6b23b3652b
11 changed files with 216 additions and 80 deletions

View File

@ -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);
}
}

View File

@ -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);
}
}

View File

@ -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);

View File

@ -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) {

View File

@ -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)) {

View File

@ -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);
}

View File

@ -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);
}

View File

@ -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));
}
}
}

View File

@ -6,7 +6,6 @@ public class SuperScreen extends Screen {
public SuperScreen(Screen parent) {
super(null);
this.overlay = new MainMenuScreen(this);
}
@Override

View File

@ -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<String> fileList = new ArrayList<String>();
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<String> list = new ArrayList<String>();
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()]);
}
}

View File

@ -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<String, BufferedImage> images;
static {
Loader.images = new Hashtable<String, BufferedImage>();
}
/**
* 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<elements.length; i++) {
String e = elements[i];
if(e.endsWith(".png")) {
if (GameConfig.DEBUG) System.out.println("Creating BufferedImage for: " + e);
Loader.addBufferedImageByFilename(e);
}
loadingScreen.increaseCurrentProcessed();
}
}
/**
* Load a BufferedImage by filename.
*/
private static void addBufferedImageByFilename(String filename) {
try {
BufferedImage image = ImageIO.read(Loader.class.getClassLoader().getResourceAsStream(filename));
Loader.images.put(filename, image);
} catch (IOException e) {
System.err.println("Unable to load BufferedImage: " + filename);
e.printStackTrace();
}
}
}