added maploader
This commit is contained in:
parent
86539f6291
commit
5c5084f211
2 changed files with 125 additions and 2 deletions
|
|
@ -21,7 +21,7 @@ public class InGameState extends GameState{
|
||||||
rootNode = new Node("InGameNode");
|
rootNode = new Node("InGameNode");
|
||||||
|
|
||||||
map = new Map(20,20);
|
map = new Map(20,20);
|
||||||
map.init();
|
map.init("data/map/default");
|
||||||
InGameMouseInput mouse = new InGameMouseInput(map);
|
InGameMouseInput mouse = new InGameMouseInput(map);
|
||||||
super.getInput().addInput(mouse);
|
super.getInput().addInput(mouse);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,16 +1,34 @@
|
||||||
package ei.game.scene;
|
package ei.game.scene;
|
||||||
|
|
||||||
|
import java.io.BufferedReader;
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.FileInputStream;
|
||||||
|
import java.io.FileNotFoundException;
|
||||||
|
import java.io.FileReader;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStreamReader;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.LinkedList;
|
||||||
|
|
||||||
import ei.engine.math.Vector2f;
|
import ei.engine.math.Vector2f;
|
||||||
import ei.engine.math.Vector2i;
|
import ei.engine.math.Vector2i;
|
||||||
import ei.engine.math.Vector3f;
|
import ei.engine.math.Vector3f;
|
||||||
import ei.engine.scene.Node;
|
import ei.engine.scene.Node;
|
||||||
import ei.engine.scene.Sprite;
|
import ei.engine.scene.Sprite;
|
||||||
|
import ei.engine.texture.TextureLoader;
|
||||||
|
import ei.engine.util.MultiPrintStream;
|
||||||
import ei.game.player.GaiaPlayer;
|
import ei.game.player.GaiaPlayer;
|
||||||
import ei.game.player.Player;
|
import ei.game.player.Player;
|
||||||
import ei.game.player.PlayerHandler;
|
import ei.game.player.PlayerHandler;
|
||||||
import ei.game.scene.map.Stone;
|
import ei.game.scene.map.Stone;
|
||||||
|
|
||||||
public class Map {
|
public class Map {
|
||||||
|
public static final int MAP_GRASS = 0;
|
||||||
|
public static final int MAP_SAND = 1;
|
||||||
|
public static final int MAP_REDMUD = 2;
|
||||||
|
|
||||||
|
public static final int OBJ_STONE = 1;
|
||||||
|
|
||||||
private static final int POS_SIZE = 50;
|
private static final int POS_SIZE = 50;
|
||||||
private int width;
|
private int width;
|
||||||
private int hight;
|
private int hight;
|
||||||
|
|
@ -23,6 +41,10 @@ public class Map {
|
||||||
//init();
|
//init();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a default map
|
||||||
|
*
|
||||||
|
*/
|
||||||
public void init(){
|
public void init(){
|
||||||
map = new GameEntity[width][hight];
|
map = new GameEntity[width][hight];
|
||||||
|
|
||||||
|
|
@ -44,6 +66,107 @@ public class Map {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Loads the map from a file
|
||||||
|
*
|
||||||
|
* @param file The map file name without extension
|
||||||
|
*/
|
||||||
|
public void init(String file){
|
||||||
|
int[][] mapData;
|
||||||
|
int[][] objData;
|
||||||
|
try {
|
||||||
|
mapData = getMapData(file+".map");
|
||||||
|
objData = getMapData(file+".obj");
|
||||||
|
} catch (IOException e) {
|
||||||
|
MultiPrintStream.out.println("Error Loading Map: "+e);
|
||||||
|
e.printStackTrace();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
width = mapData.length;
|
||||||
|
hight = mapData[0].length;
|
||||||
|
map = new GameEntity[width][hight];
|
||||||
|
|
||||||
|
// init textures
|
||||||
|
mapNode = new Node("MapNode");
|
||||||
|
for(int i=0; i<width ;i++){
|
||||||
|
for(int j=0; j<hight ;j++){
|
||||||
|
switch(mapData[i][j]){
|
||||||
|
case MAP_GRASS:
|
||||||
|
mapNode.add(getPosSprite(i,j,"data/map/Grass.png"));
|
||||||
|
break;
|
||||||
|
case MAP_SAND:
|
||||||
|
mapNode.add(getPosSprite(i,j,"data/map/sand.jpg"));
|
||||||
|
break;
|
||||||
|
case MAP_REDMUD:
|
||||||
|
mapNode.add(getPosSprite(i,j,"data/map/redmud.jpg"));
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// init the gaia player and stones etc...
|
||||||
|
Player gaia = new GaiaPlayer();
|
||||||
|
PlayerHandler.getInstance().addPlayer(gaia);
|
||||||
|
for(int i=0; i<width ;i++){
|
||||||
|
for(int j=0; j<hight ;j++){
|
||||||
|
switch(objData[i][j]){
|
||||||
|
case OBJ_STONE:
|
||||||
|
gaia.addUnit(new Stone(new Vector2i(i,j),gaia));
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private Sprite getPosSprite(int x, int y, String file){
|
||||||
|
Sprite s = new Sprite("MapPos("+x+","+y+")", file);
|
||||||
|
s.setLocation(new Vector3f(x*POS_SIZE,y*POS_SIZE,0));
|
||||||
|
s.setSize(new Vector2f(POS_SIZE,POS_SIZE));
|
||||||
|
return s;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Loads a map file
|
||||||
|
*
|
||||||
|
* @param file The file to load
|
||||||
|
* @return The data
|
||||||
|
* @throws IOException
|
||||||
|
*/
|
||||||
|
private int[][] getMapData(String file) throws IOException{
|
||||||
|
LinkedList<String> tempData = new LinkedList<String>();
|
||||||
|
|
||||||
|
MultiPrintStream.out.println("Loading Map file: "+file);
|
||||||
|
InputStreamReader isr = new InputStreamReader(
|
||||||
|
getClass().getClassLoader().getResourceAsStream(file));
|
||||||
|
BufferedReader infil = new BufferedReader(isr);
|
||||||
|
|
||||||
|
String rad="";
|
||||||
|
while(rad!=null){
|
||||||
|
rad = infil.readLine();
|
||||||
|
if(rad != null)
|
||||||
|
tempData.addLast(rad);
|
||||||
|
}
|
||||||
|
|
||||||
|
// calculate the size of the map
|
||||||
|
int xSize = 0;
|
||||||
|
int ySize = tempData.size();
|
||||||
|
for(int i=0; i<tempData.size() ;i++){
|
||||||
|
if(xSize < tempData.get(i).length()){
|
||||||
|
xSize = tempData.get(i).length();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// geting the data for every pos
|
||||||
|
int[][] data = new int[xSize][ySize];
|
||||||
|
for(int i=0; i<ySize ;i++){
|
||||||
|
String temp = tempData.get(i);
|
||||||
|
for(int j=0; j<temp.length() ;j++){
|
||||||
|
data[j][i] = Integer.parseInt(""+temp.charAt(j));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return data;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the size of the map
|
* Returns the size of the map
|
||||||
*
|
*
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue