2007-03-29 23:21:00 +00:00
|
|
|
package ei.game.scene;
|
|
|
|
|
|
2007-04-23 13:49:48 +00:00
|
|
|
import java.io.BufferedReader;
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
import java.io.InputStreamReader;
|
|
|
|
|
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;
|
2007-03-29 23:21:00 +00:00
|
|
|
import ei.engine.math.Vector3f;
|
2007-05-06 20:35:09 +00:00
|
|
|
import ei.engine.scene.Box;
|
|
|
|
|
import ei.engine.scene.Entity;
|
2007-03-29 23:21:00 +00:00
|
|
|
import ei.engine.scene.Node;
|
|
|
|
|
import ei.engine.scene.Sprite;
|
2007-04-23 13:49:48 +00:00
|
|
|
import ei.engine.util.MultiPrintStream;
|
2007-04-21 21:18:27 +00:00
|
|
|
import ei.game.player.GaiaPlayer;
|
|
|
|
|
import ei.game.player.Player;
|
|
|
|
|
import ei.game.player.PlayerHandler;
|
|
|
|
|
import ei.game.scene.map.Stone;
|
2007-03-29 23:21:00 +00:00
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
2007-04-24 21:41:31 +00:00
|
|
|
public static final int POS_SIZE = 50;
|
2007-03-29 23:21:00 +00:00
|
|
|
private int width;
|
|
|
|
|
private int hight;
|
|
|
|
|
private GameEntity[][] map;
|
|
|
|
|
private Node mapNode;
|
|
|
|
|
|
|
|
|
|
public Map(int w, int h){
|
|
|
|
|
this.width = w;
|
|
|
|
|
this.hight = h;
|
2007-04-21 21:18:27 +00:00
|
|
|
//init();
|
2007-03-29 23:21:00 +00:00
|
|
|
}
|
2007-04-23 13:49:48 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Creates a default map
|
|
|
|
|
*
|
|
|
|
|
*/
|
2007-04-21 21:18:27 +00:00
|
|
|
public void init(){
|
2007-03-29 23:21:00 +00:00
|
|
|
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));
|
2007-04-15 18:52:42 +00:00
|
|
|
s.setSize(new Vector2f(POS_SIZE,POS_SIZE));
|
2007-03-29 23:21:00 +00:00
|
|
|
mapNode.add(s);
|
|
|
|
|
}
|
|
|
|
|
}
|
2007-04-21 21:18:27 +00:00
|
|
|
|
|
|
|
|
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-03-29 23:21:00 +00:00
|
|
|
}
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2007-05-06 20:35:09 +00:00
|
|
|
private Entity getPosSprite(int x, int y, String file){
|
|
|
|
|
Box s = new Box("MapPos("+x+","+y+")", file);
|
2007-04-23 13:49:48 +00:00
|
|
|
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
|
|
|
}
|
2007-04-23 13:58:47 +00:00
|
|
|
public static Vector2f getPixelByPos(int x, int y, int size){
|
|
|
|
|
if(size%2!=0) {
|
|
|
|
|
size = (size/2)+1;
|
|
|
|
|
float xf = (POS_SIZE)*(x+size);
|
|
|
|
|
float yf = (POS_SIZE)*(y+size);
|
|
|
|
|
return new Vector2f(xf,yf);
|
|
|
|
|
}
|
|
|
|
|
size = (size/2)-1;
|
|
|
|
|
float xDiff = Math.abs((getPixelByPos(x+size, 0).getX())-(getPixelByPos(size+x+1, 0).getX()))/2;
|
|
|
|
|
float yDiff = Math.abs((getPixelByPos(0, size+y).getY())-(getPixelByPos(0, size+y+1).getY()))/2;
|
|
|
|
|
|
|
|
|
|
return new Vector2f(getPixelByPos(x+size, 0).getX()+xDiff, getPixelByPos(0, y+size).getY()+yDiff);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
2007-04-04 16:00:09 +00:00
|
|
|
|
2007-03-29 23:21:00 +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){
|
2007-03-29 23:21:00 +00:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2007-04-23 13:58:47 +00:00
|
|
|
/**
|
|
|
|
|
* Returns true if the pos in the map is empty
|
|
|
|
|
* @param x pos
|
|
|
|
|
* @param y pos
|
|
|
|
|
* @param size, size to check
|
|
|
|
|
* @return true if pos is empty, else false
|
|
|
|
|
*/
|
|
|
|
|
public boolean isPosEmpty(int x, int y, int size) {
|
|
|
|
|
for(int i=x; i<size+x; i++) {
|
|
|
|
|
for(int j=y; j<size+y; j++) {
|
|
|
|
|
if(posExist(i,j) && map[i][j] == null){
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2007-03-29 23:21:00 +00:00
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2007-03-29 23:21:00 +00:00
|
|
|
/**
|
|
|
|
|
* 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;
|
|
|
|
|
}
|
2007-04-23 13:58:47 +00:00
|
|
|
/**
|
|
|
|
|
* Set an object in a rectangle of size*size, starting at x,y
|
|
|
|
|
* @param e object
|
|
|
|
|
* @param x pos
|
|
|
|
|
* @param y pos
|
|
|
|
|
* @param size
|
|
|
|
|
*/
|
|
|
|
|
public void setBuildPos(GameEntity e, int x, int y, int size) {
|
|
|
|
|
if(isPosEmpty(x, y, size)) {
|
|
|
|
|
for(int i=x; i<size+x; i++) {
|
|
|
|
|
for(int j=y; j<size+y; j++) {
|
|
|
|
|
map[i][j] = e;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
/**
|
|
|
|
|
* Remove an object from this position
|
|
|
|
|
* @param e, the object
|
|
|
|
|
* @param x pos of object
|
|
|
|
|
* @param y pos of object
|
|
|
|
|
* @param size of object
|
|
|
|
|
*/
|
|
|
|
|
public void removeBuildPos(int x, int y, int size) {
|
|
|
|
|
for(int i=x; i<size+x; i++) {
|
|
|
|
|
for(int j=y; j<size+y; j++) {
|
|
|
|
|
map[i][j] = null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2007-03-29 23:21:00 +00:00
|
|
|
/**
|
|
|
|
|
* 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
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2007-03-29 23:21:00 +00:00
|
|
|
}
|