/**
* TimeHero - http://timehero.sf.net
*
* @license See LICENSE
*/
package it.timehero.map;
import it.timehero.entities.Entity;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.tiled.TiledMap;
/**
* Gestisce la mappa
*/
public class Mappa {
/** The collision map indicating which tiles block movement - generated based on tile properties */
private boolean[][] blocked;
/** The map that we're going to drive around */
private TiledMap map;
private int tileSize;
/**
* Percorso della mappa, memorizza la grandezza del tileset della mappa (16x16, 32x32, ecc..)
* @param percorso
* @throws SlickException - se la mappa non � leggibile/non trovata
*/
public Mappa(String percorso) throws SlickException{
this.map = new TiledMap(percorso);
// build a collision map based on tile properties in the TileD map
blocked = getCollisionMatrix(map,"blocked","false");
tileSize = map.getTileHeight();
}
/**
* Check if a specific location of the tank would leave it
* on a blocked tile
*
* @param x The x coordinate of the tank's location
* @param y The y coordinate of the tank's location
* @return True if the location is blocked
*/
public boolean blocked(float x, float y) {
return blocked[(int) x][(int) y];
}
/**
* @param map
* @param key nome propriet� da valutare
* @param value valore della propriet� che rende bloccante quel tile
* @return matrice di collisione della mappa
*/
private boolean[][] getCollisionMatrix(TiledMap map, String key, String value){
boolean [][] matrix = new boolean[map.getWidth()][map.getHeight()];
for (int x=0;x<map.getWidth();x++) {
for (int y=0;y<map.getHeight();y++) {
int tileID = map.getTileId(x, y, 0);
String temp = map.getTileProperty(tileID, key, value);
if ("true".equals(temp)) {
matrix[x][y] = true;
}
}
}
return matrix;
}
public int getHeight() {
return map.getHeight();
}
public int getTileSize() {
return tileSize;
}
public int getWidth() {
return map.getWidth();
}
public void render(int arg0,int arg1,int arg2,int arg3,int arg4, int arg5){
map.render(arg0,arg1,arg2,arg3,arg4,arg5);
}
public void setTileSize(int tileSize) {
this.tileSize = tileSize;
}
/**
* Try to move in the direction specified. If it's blocked, try sliding. If that
* doesn't work just don't bother
*
* @param ent the entity to move
* @param x The amount on the X axis to move
* @param y The amount on the Y axis to move
* @return True if we managed to move
*/
public boolean tryToMoveEntity(Entity ent, float x, float y){
float newx = ent.getX() + x;
float newy = ent.getY() + y;
if (blocked(newx,newy)) {
return false;
}
return true;
}
}