evil-inside/src/ei/game/gamestate/LoadingState.java

136 lines
3.6 KiB
Java
Raw Normal View History

package ei.game.gamestate;
import java.io.File;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;
2007-04-23 15:13:23 +00:00
import ei.engine.effects.BitmapProgressBar;
import ei.engine.scene.Sprite;
import ei.engine.sound.SoundLoader;
import ei.engine.state.GameState;
import ei.engine.state.GameStateManager;
import ei.engine.texture.TextureLoader;
import ei.engine.util.FileFinderHasher;
/**
* This class handels the loading of the
* images and sounds to the memmory.
*
* @author Ziver
*/
public class LoadingState extends GameState{
//The extensions of the files
private static final String[] TEXTURES = {
"jpg","png","bmp"
};
private static final String[] SOUNDS = {
"wav","ogg"
};
// The things to load
private Queue<String> loadTextures;
private Queue<String> loadSounds;
// The name of the next state to activate after loading
private String nextState;
2007-04-23 15:13:23 +00:00
private BitmapProgressBar progress;
/**
* Creates a loadingstate
* @param name The name of the state
*/
public LoadingState(String name,String nextState) {
super(name);
this.nextState = nextState;
}
@Override
public void init() {
loadTextures = new LinkedList<String>();
loadSounds = new LinkedList<String>();
2007-04-23 15:13:23 +00:00
progress = new BitmapProgressBar("Loading");
progress.centerToScreen();
progress.setBarTexture(new Sprite("ProgressBar","data/loadbar_front.png"));
progress.setProgressTexture(new Sprite("Progress","data/loadbar.png"));
progress.setBackgroundTexture(new Sprite("progressBackground","data/loadbar_back.png"));
try {
File dir = new File(getClass().getClassLoader().getResource("data/").toURI());
ArrayList<File> files = FileFinderHasher.Search(dir);
for(int i=0; i<files.size() ;i++){
String ext = files.get(i).getName();
ext = ext.substring(ext.lastIndexOf ('.')+1,ext.length());
if(contains(TEXTURES,ext)){
addTexture(files.get(i).getPath().substring(files.get(i).getPath().lastIndexOf("data"), files.get(i).getPath().length()));
}
else if(contains(SOUNDS,ext)){
addSound(files.get(i).getPath().substring(files.get(i).getPath().lastIndexOf("data"), files.get(i).getPath().length()));
}
}
} catch (URISyntaxException e) {
e.printStackTrace();
}
}
private boolean contains(String[] array, String search){
for(int i=0; i<array.length ;i++){
if(array[i].equals(search)){
return true;
}
}
return false;
}
/**
* Add a texture to be loaded
* @param url
*/
public void addTexture(String url){
loadTextures.add(url);
progress.setMax(loadTextures.size()+loadSounds.size());
}
/**
* Add a sound to be loaded
* @param url
*/
public void addSound(String url){
loadSounds.add(url);
progress.setMax(loadTextures.size()+loadSounds.size());
}
/**
* Loads the things
*/
public void update() {
if(!loadTextures.isEmpty()){
TextureLoader.getTextureLoaderInstance().getTexture(loadTextures.poll());
progress.setValue(progress.getValue()+1);
}
else if(!loadSounds.isEmpty()){
SoundLoader.getInstnace().loadSound(loadSounds.poll());
progress.setValue(progress.getValue()+1);
}
else{
//progress.setValue(progress.getValue()+1);
if(progress.getProcent() >= 100){
//deactivate this state and activate the next one
GameStateManager.getInstance().removeState(this);
GameStateManager.getInstance().setActive(nextState);
}
}
progress.update();
}
/**
* Render the load bar
*/
public void render() {
progress.render();
}
}