2007-03-29 23:21:00 +00:00
|
|
|
package ei.game.gamestate;
|
|
|
|
|
|
2007-04-28 22:09:39 +00:00
|
|
|
import java.io.File;
|
|
|
|
|
import java.net.URISyntaxException;
|
|
|
|
|
import java.util.ArrayList;
|
2007-03-29 23:21:00 +00:00
|
|
|
import java.util.LinkedList;
|
|
|
|
|
import java.util.Queue;
|
|
|
|
|
|
2007-04-23 15:13:23 +00:00
|
|
|
import ei.engine.effects.BitmapProgressBar;
|
2007-03-29 23:21:00 +00:00
|
|
|
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;
|
2007-04-28 22:09:39 +00:00
|
|
|
import ei.engine.util.FileFinderHasher;
|
2007-03-29 23:21:00 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* This class handels the loading of the
|
|
|
|
|
* images and sounds to the memmory.
|
|
|
|
|
*
|
|
|
|
|
* @author Ziver
|
|
|
|
|
*/
|
|
|
|
|
public class LoadingState extends GameState{
|
2007-04-28 22:09:39 +00:00
|
|
|
//The extensions of the files
|
|
|
|
|
private static final String[] TEXTURES = {
|
|
|
|
|
"jpg","png","bmp"
|
|
|
|
|
};
|
|
|
|
|
private static final String[] SOUNDS = {
|
|
|
|
|
"wav","ogg"
|
|
|
|
|
};
|
2007-03-29 23:21:00 +00:00
|
|
|
// 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;
|
2007-04-05 13:30:18 +00:00
|
|
|
|
2007-03-29 23:21:00 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Creates a loadingstate
|
|
|
|
|
* @param name The name of the state
|
|
|
|
|
*/
|
|
|
|
|
public LoadingState(String name,String nextState) {
|
|
|
|
|
super(name);
|
|
|
|
|
this.nextState = nextState;
|
2007-04-28 22:09:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void init() {
|
2007-03-29 23:21:00 +00:00
|
|
|
loadTextures = new LinkedList<String>();
|
|
|
|
|
loadSounds = new LinkedList<String>();
|
2007-04-05 13:30:18 +00:00
|
|
|
|
2007-04-23 15:13:23 +00:00
|
|
|
progress = new BitmapProgressBar("Loading");
|
2007-04-05 13:30:18 +00:00
|
|
|
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"));
|
2007-04-28 22:09:39 +00:00
|
|
|
|
|
|
|
|
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;
|
2007-03-29 23:21:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Add a texture to be loaded
|
|
|
|
|
* @param url
|
|
|
|
|
*/
|
|
|
|
|
public void addTexture(String url){
|
|
|
|
|
loadTextures.add(url);
|
2007-04-05 13:30:18 +00:00
|
|
|
progress.setMax(loadTextures.size()+loadSounds.size());
|
2007-03-29 23:21:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Add a sound to be loaded
|
|
|
|
|
* @param url
|
|
|
|
|
*/
|
|
|
|
|
public void addSound(String url){
|
|
|
|
|
loadSounds.add(url);
|
2007-04-05 13:30:18 +00:00
|
|
|
progress.setMax(loadTextures.size()+loadSounds.size());
|
2007-03-29 23:21:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Loads the things
|
|
|
|
|
*/
|
|
|
|
|
public void update() {
|
|
|
|
|
if(!loadTextures.isEmpty()){
|
|
|
|
|
TextureLoader.getTextureLoaderInstance().getTexture(loadTextures.poll());
|
2007-04-05 13:30:18 +00:00
|
|
|
progress.setValue(progress.getValue()+1);
|
2007-03-29 23:21:00 +00:00
|
|
|
}
|
|
|
|
|
else if(!loadSounds.isEmpty()){
|
|
|
|
|
SoundLoader.getInstnace().loadSound(loadSounds.poll());
|
2007-04-05 13:30:18 +00:00
|
|
|
progress.setValue(progress.getValue()+1);
|
2007-03-29 23:21:00 +00:00
|
|
|
}
|
|
|
|
|
else{
|
2007-04-28 22:09:39 +00:00
|
|
|
//progress.setValue(progress.getValue()+1);
|
2007-04-05 13:30:18 +00:00
|
|
|
if(progress.getProcent() >= 100){
|
2007-03-29 23:21:00 +00:00
|
|
|
//deactivate this state and activate the next one
|
|
|
|
|
GameStateManager.getInstance().removeState(this);
|
|
|
|
|
GameStateManager.getInstance().setActive(nextState);
|
2007-04-05 13:30:18 +00:00
|
|
|
}
|
2007-03-29 23:21:00 +00:00
|
|
|
}
|
2007-04-05 13:30:18 +00:00
|
|
|
progress.update();
|
2007-03-29 23:21:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Render the load bar
|
|
|
|
|
*/
|
|
|
|
|
public void render() {
|
2007-04-05 13:30:18 +00:00
|
|
|
progress.render();
|
2007-03-29 23:21:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|