evil-inside/src/ei/game/scene/Map.java

295 lines
6.7 KiB
Java
Raw Normal View History

package ei.game.scene;
2007-04-23 13:49:48 +00:00
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;
2007-04-04 17:36:11 +00:00
import ei.engine.math.Vector2f;
2007-04-04 16:00:09 +00:00
import ei.engine.math.Vector2i;
import ei.engine.math.Vector3f;
import ei.engine.scene.Node;
import ei.engine.scene.Sprite;
2007-04-23 13:49:48 +00:00
import ei.engine.texture.TextureLoader;
import ei.engine.util.MultiPrintStream;
import ei.game.player.GaiaPlayer;
import ei.game.player.Player;
import ei.game.player.PlayerHandler;
import ei.game.scene.map.Stone;
public class Map {
2007-04-23 13:49:48 +00:00
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 int width;
private int hight;
private GameEntity[][] map;
private Node mapNode;
public Map(int w, int h){
this.width = w;
this.hight = h;
//init();
}
2007-04-23 13:49:48 +00:00
/**
* Creates a default map
*
*/
public void init(){
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++){
Sprite s = new Sprite("MapPos("+i+","+j+")","data/map/sand.jpg");
s.setLocation(new Vector3f(i*POS_SIZE,j*POS_SIZE,0));
s.setSize(new Vector2f(POS_SIZE,POS_SIZE));
mapNode.add(s);
}
}
Player gaia = new GaiaPlayer();
PlayerHandler.getInstance().addPlayer(gaia);
for(int i=0; i<5 ;i++){
gaia.addUnit(new Stone(new Vector2i((int)(Math.random()*width),(int)(Math.random()*hight)),gaia));
}
}
2007-04-23 13:49:48 +00:00
/**
* 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;
}
2007-04-16 15:52:24 +00:00
/**
* Returns the size of the map
*
* @return The size of the map
*/
public Vector2i getSize(){
return new Vector2i(width,hight);
}
2007-04-04 17:39:10 +00:00
/**
* Returns the pos of the map by the pixel
* @param x x pixels
* @param y y pixels
* @return The pos thats in the coordinates
*/
2007-04-16 13:25:25 +00:00
public static Vector2i getPosByPixel(float x, float y){
2007-04-04 16:00:09 +00:00
x = x/POS_SIZE;
y = y/POS_SIZE;
2007-04-16 13:25:25 +00:00
return new Vector2i((int)x, (int)y);
2007-04-04 16:00:09 +00:00
}
2007-04-04 17:39:10 +00:00
/**
* The coordinate of the map pos
* @param x x pos
* @param y y pos
* @return The coordinate of the pos
*/
2007-04-16 13:25:25 +00:00
public static Vector2f getPixelByPos(int x, int y){
2007-04-15 19:27:36 +00:00
float xf = (POS_SIZE*x);
float yf = (POS_SIZE*y);
return new Vector2f(xf,yf);
2007-04-04 16:00:09 +00:00
}
/**
* Returns if the pos inthe map is empty
*
* @param x The x pos
* @param y The y pos
* @return True if empty else false
*/
public boolean isPosEmpty(int x, int y){
2007-04-18 16:06:44 +00:00
if(posExist(x,y) && map[x][y] == null){
return true;
}
return false;
}
2007-04-18 16:06:44 +00:00
/**
* Returns true if the given pos exists on the map
* @param x The x pos
* @param y The y pos
* @return True if the given pos exists on the map
*/
public boolean posExist(int x, int y){
if(x < 0 || y < 0 || x >= width || y >= hight)
return false;
return true;
}
/**
* Returns the object at the pos
*
* @param x The x pos
* @param y The y pos
* @return The object in that pos
*/
public GameEntity getPos(int x, int y){
return map[x][y];
}
/**
* Sets an object at the pos
*
* @param e The object
* @param x The x pos
* @param y The y pos
*/
public void setPos(GameEntity e, int x, int y){
map[x][y] = e;
}
/**
* Removes a object from that pos
*
* @param x The x pos
* @param y The y pos
*/
public void removePos(int x, int y){
map[x][y] = null;
}
/**
* Returns the map node
*
* @return The map node
*/
public Node getMapNode(){
return mapNode;
}
2007-04-04 18:28:59 +00:00
2007-04-19 12:41:38 +00:00
public Vector2i getPosIndex(GameEntity u){
for(int i=0; i<width ;i++){
for(int j=0; j<hight ;j++){
if(map[i][j] == u){
return new Vector2i(i,j);
}
}
}
return null;
}
2007-04-04 18:28:59 +00:00
/**
* Prints all the units on the map
*
*/
public void printAllUnits(){
for(int i=0; i<width ;i++){
for(int j=0; j<hight ;j++){
if(map[i][j] != null){
2007-04-16 21:47:07 +00:00
System.out.println("Unit("+map[i][j].getNode().getName()+"): "+i+" "+j);
2007-04-04 18:28:59 +00:00
}
}
}
}
}