I've been following freeaks tutorial for tiled maps and have managed to get a 600x480 map to work with colllisions. So I decided to try to make a map which is 1200x960, the collisions work if the map is rendered at 0,0. I tried to move the map to 0,-480 to check if the collision on the bottom of the map worked and it doesn't, as this is my first time using Tiled Maps I am unsure how to resolve this problem, any help will be much appreciated.
Code:
package run.game;
import org.newdawn.slick.Animation;
import org.newdawn.slick.AppGameContainer;
import org.newdawn.slick.BasicGame;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.SpriteSheet;
import org.newdawn.slick.geom.Polygon;
public class Run extends BasicGame{
private float playerX=32;
private float playerY=416;
private Animation player;
private Polygon playerPoly;
public BlockMap map;
public Run() {
super("one class barebone game");
}
public void init(GameContainer container) throws SlickException {
container.setVSync(true);
SpriteSheet sheet = new SpriteSheet("data/karbonator.png",32,32);
map = new BlockMap("data/mapborder.tmx");
player = new Animation();
player.setAutoUpdate(false);
for (int frame=0;frame<3;frame++) {
player.addFrame(sheet.getSprite(frame,0), 150);
}
playerPoly = new Polygon(new float[]{
playerX,playerY,
playerX+32,playerY,
playerX+32,playerY+32,
playerX,playerY+32
});
}
public void update(GameContainer container, int delta) throws SlickException {
if (container.getInput().isKeyDown(Input.KEY_LEFT)) {
playerX--;
playerPoly.setX(playerX);
if (entityCollisionWith()){
playerX++;
playerPoly.setX(playerX);
}
}
if (container.getInput().isKeyDown(Input.KEY_RIGHT)) {
playerX++;
playerPoly.setX(playerX);
if (entityCollisionWith()){
playerX--;
playerPoly.setX(playerX);
}
}
if (container.getInput().isKeyDown(Input.KEY_SPACE)){
playerY--;
playerPoly.setY(playerY);
if (entityCollisionWith()){
playerY++;
playerPoly.setY(playerY);
}
}
if (!container.getInput().isKeyDown(Input.KEY_SPACE)){
playerY++;
playerPoly.setY(playerY);
/**if(playerY>400){
mapY--;
if(mapY<-479){
mapY=-480;
}
**/
if (entityCollisionWith()){
playerY--;
playerPoly.setY(playerY);
}
}
}
public boolean entityCollisionWith() throws SlickException {
for (int i = 0; i < BlockMap.entities.size(); i++) {
Block entity1 = (Block) BlockMap.entities.get(i);
if (playerPoly.intersects(entity1.poly)) {
return true;
}
}
return false;
}
public void render(GameContainer container, Graphics g) {
BlockMap.tmap.render(0, -480);
g.drawAnimation(player, playerX, playerY);
g.draw(playerPoly);
}
public static void main(String[] argv) throws SlickException {
AppGameContainer container =
new AppGameContainer(new Run(), 640, 480, false);
container.start();
}
}
Code:
package run.game;
import java.util.ArrayList;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.tiled.TiledMap;
public class BlockMap {
public static TiledMap tmap;
public int mapWidth;
public int mapHeight;
private int square[] = {1,1,15,1,15,15,1,15}; //square shaped tile
public static ArrayList<Object> entities;
public BlockMap(String ref) throws SlickException {
entities = new ArrayList<Object>();
tmap = new TiledMap(ref, "data");
mapWidth = tmap.getWidth() * tmap.getTileWidth() ;
mapHeight = tmap.getHeight() * tmap.getTileHeight();
for (int x = 0; x < tmap.getWidth(); x++) {
for (int y = 0; y < tmap.getHeight(); y++) {
int tileID = tmap.getTileId(x, y, 0);
if (tileID == 1) {
entities.add(
new Block(x * 16, y * 16, square, "square")
);
}
}
}
}
}
Code:
package run.game;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.geom.Polygon;
public class Block {
public Polygon poly;
public Block(int x, int y, int test[],String type) {
poly = new Polygon(new float[]{
x+test[0], y+test[1],
x+test[2], y+test[3],
x+test[4], y+test[5],
x+test[6], y+test[7],
});
}
public void update(int delta) {
}
public void draw(Graphics g) {
g.draw(poly);
}
}