I can’t get my Tiled map to work with the current work. Is there anyway to make a better collision system? This is the code I have so far.
public class WizardGame extends BasicGame
{
private TiledMap grassMap;
private Animation sprite, up, down, left, right;
private float x = 60f, y = 100f;
/** The collision map indicating which tiles block movement - generated based on tile properties */
private boolean[][] blocked;
private static final int SIZE = 16;
public WizardGame()
{
super("Wizard game");
}
public static void main(String [] arguments)
{
try
{
AppGameContainer app = new AppGameContainer(new WizardGame());
app.setDisplayMode(240, 160, false);
app.start();
}
catch (SlickException e)
{
e.printStackTrace();
}
}
@Override
public void init(GameContainer container) throws SlickException
{
grassMap = new TiledMap("C:/Users/user/workspace/LWJGL/data/housemap.tmx");
Image [] movementUp = {new Image("C:/Users/user/Desktop/ZELDA/up1.png"), new Image("C:/Users/user/Desktop/ZELDA/up2.png") , new Image("C:/Users/user/Desktop/ZELDA/up3.png")};
Image [] movementDown = {new Image ("C:/Users/user/Desktop/ZELDA/down1.png"), new Image("C:/Users/user/Desktop/ZELDA/down2.png"), new Image("C:/Users/user/Desktop/ZELDA/down3.png")};
Image [] movementRight = {new Image ("C:/Users/user/Desktop/ZELDA/right1.png"), new Image("C:/Users/user/Desktop/ZELDA/right2.png"), new Image("C:/Users/user/Desktop/ZELDA/right3.png")};
Image [] movementLeft = {new Image ("C:/Users/user/Desktop/ZELDA/left1.png"), new Image ("C:/Users/user/Desktop/ZELDA/left2.png"), new Image("C:/Users/user/Desktop/ZELDA/left3.png")};
int [] duration = {85, 85, 85};
/*
* false variable means do not auto update the animation.
* By setting it to false animation will update only when
* the user presses a key.
*/
up = new Animation(movementUp, duration, false);
down = new Animation(movementDown, duration, false);
left = new Animation(movementLeft, duration, false);
right = new Animation(movementRight, duration, false);
// Original orientation of the sprite. It will look right.
sprite = right;
// build a collision map based on tile properties in the TileD map
blocked = new boolean[grassMap.getWidth()][grassMap.getHeight()];
for (int xAxis=0;xAxis<grassMap.getWidth(); xAxis++)
{
for (int yAxis=0;yAxis<grassMap.getHeight(); yAxis++)
{
int tileID = grassMap.getTileId(xAxis, yAxis, 0);
String value = grassMap.getTileProperty(tileID, "blocked", "false");
if ("true".equals(value))
{
blocked[xAxis][yAxis] = true;
}
}
}
}
@Override
public void update(GameContainer container, int delta) throws SlickException
{
Input input = container.getInput();
float fdelta=delta*0.1f;
if (input.isKeyDown(Input.KEY_UP))
{
sprite = up;
if (!(isBlocked(x, y - fdelta) || isBlocked(x+SIZE-1, y - fdelta)))
{
sprite.update(delta);
// The lower the delta the slowest the sprite will animate.
y -= fdelta;
}
}
else if (input.isKeyDown(Input.KEY_DOWN))
{
sprite = down;
if (!(isBlocked(x, y + SIZE + fdelta) || isBlocked(x+SIZE-1, y + SIZE + fdelta)))
{
sprite.update(delta);
y += fdelta;
}
}
else if (input.isKeyDown(Input.KEY_LEFT))
{
sprite = left;
if (!(isBlocked(x - fdelta, y) || isBlocked(x - fdelta, y+SIZE-1)))
{
sprite.update(delta);
x -= fdelta;
}
}
else if (input.isKeyDown(Input.KEY_RIGHT))
{
sprite = right;
if (!(isBlocked(x + SIZE + fdelta, y) || isBlocked(x + SIZE + fdelta, y+SIZE-1)))
{
sprite.update(delta);
x += fdelta;
}
}
}
public void render(GameContainer container, Graphics g) throws SlickException
{
grassMap.render(0, 0);
sprite.draw((int)x, (int)y);
}
private boolean isBlocked(float x, float y)
{
int xBlock = (int)x / SIZE;
int yBlock = (int)y / SIZE;
return blocked[xBlock][yBlock];
}
}
Which is taken from this tutorial http://thejavablog.wordpress.com/2008/06/08/using-slick-2d-to-write-a-game/
This is my current tiled map. Unless the tutorial mine uses more than 2 tiles… I can’t figure a way to make this collision system to work with my map.
http://gyazo.com/133cb2f82a93ee7c9970818f91d6de42.png?1346977992
<?xml version="1.0" encoding="UTF-8"?>
<map version="1.0" orientation="orthogonal" width="15" height="10" tilewidth="16" tileheight="16">
<tileset firstgid="1" name="tilehouse" tilewidth="16" tileheight="16">
<image source="ZELDA/tilehouse.png" width="240" height="160"/>
</tileset>
<layer name="Tile Layer 1" width="15" height="10">
<data>
<tile gid="1"/>
<tile gid="2"/>
<tile gid="3"/>
<tile gid="4"/>
<tile gid="5"/>
<tile gid="6"/>
<tile gid="7"/>
<tile gid="8"/>
<tile gid="9"/>
<tile gid="10"/>
<tile gid="11"/>
<tile gid="12"/>
<tile gid="13"/>
<tile gid="14"/>
<tile gid="15"/>
<tile gid="16"/>
<tile gid="17"/>
<tile gid="18"/>
<tile gid="19"/>
<tile gid="20"/>
<tile gid="21"/>
<tile gid="22"/>
<tile gid="23"/>
<tile gid="24"/>
<tile gid="25"/>
<tile gid="26"/>
<tile gid="27"/>
<tile gid="28"/>
<tile gid="29"/>
<tile gid="30"/>
<tile gid="31"/>
<tile gid="32"/>
<tile gid="33"/>
<tile gid="34"/>
<tile gid="35"/>
<tile gid="36"/>
<tile gid="37"/>
<tile gid="38"/>
<tile gid="39"/>
<tile gid="40"/>
<tile gid="41"/>
<tile gid="42"/>
<tile gid="43"/>
<tile gid="44"/>
<tile gid="45"/>
<tile gid="46"/>
<tile gid="47"/>
<tile gid="48"/>
<tile gid="49"/>
<tile gid="50"/>
<tile gid="51"/>
<tile gid="52"/>
<tile gid="53"/>
<tile gid="54"/>
<tile gid="55"/>
<tile gid="56"/>
<tile gid="57"/>
<tile gid="58"/>
<tile gid="59"/>
<tile gid="60"/>
<tile gid="61"/>
<tile gid="62"/>
<tile gid="63"/>
<tile gid="64"/>
<tile gid="65"/>
<tile gid="66"/>
<tile gid="67"/>
<tile gid="68"/>
<tile gid="69"/>
<tile gid="70"/>
<tile gid="71"/>
<tile gid="72"/>
<tile gid="73"/>
<tile gid="74"/>
<tile gid="75"/>
<tile gid="76"/>
<tile gid="77"/>
<tile gid="78"/>
<tile gid="79"/>
<tile gid="80"/>
<tile gid="81"/>
<tile gid="82"/>
<tile gid="83"/>
<tile gid="84"/>
<tile gid="85"/>
<tile gid="86"/>
<tile gid="87"/>
<tile gid="88"/>
<tile gid="89"/>
<tile gid="90"/>
<tile gid="91"/>
<tile gid="92"/>
<tile gid="93"/>
<tile gid="94"/>
<tile gid="95"/>
<tile gid="96"/>
<tile gid="97"/>
<tile gid="98"/>
<tile gid="99"/>
<tile gid="100"/>
<tile gid="101"/>
<tile gid="102"/>
<tile gid="103"/>
<tile gid="104"/>
<tile gid="105"/>
<tile gid="106"/>
<tile gid="107"/>
<tile gid="108"/>
<tile gid="109"/>
<tile gid="110"/>
<tile gid="111"/>
<tile gid="112"/>
<tile gid="113"/>
<tile gid="114"/>
<tile gid="115"/>
<tile gid="116"/>
<tile gid="117"/>
<tile gid="118"/>
<tile gid="119"/>
<tile gid="120"/>
<tile gid="121"/>
<tile gid="122"/>
<tile gid="123"/>
<tile gid="124"/>
<tile gid="125"/>
<tile gid="126"/>
<tile gid="127"/>
<tile gid="128"/>
<tile gid="129"/>
<tile gid="130"/>
<tile gid="131"/>
<tile gid="132"/>
<tile gid="133"/>
<tile gid="134"/>
<tile gid="135"/>
<tile gid="136"/>
<tile gid="137"/>
<tile gid="138"/>
<tile gid="139"/>
<tile gid="140"/>
<tile gid="141"/>
<tile gid="142"/>
<tile gid="143"/>
<tile gid="144"/>
<tile gid="145"/>
<tile gid="146"/>
<tile gid="147"/>
<tile gid="148"/>
<tile gid="149"/>
<tile gid="150"/>
</data>
</layer>
</map>
I also tried this with two layers, but that means changing completely the current collision method am I right?
http://gyazo.com/4bafeadc9fbf73160f8ac65fd84df337.png?1346978096