Ok, that seems to be the easiest in the long run. Just a quick question
collisionBoxArray.add (new CollisionControl(x,y,mapX,mapY));
where does the mapX and mapY values come from?
Ok, that seems to be the easiest in the long run. Just a quick question
collisionBoxArray.add (new CollisionControl(x,y,mapX,mapY));
where does the mapX and mapY values come from?
It’s the current location of the exact top left corner of the entire map in relation to the screen/camera (not just the tiles you’re rendering).
OK, i have the players collision box set up with a bit of modifying because of the camera code, and i can see it being drawn round the characters feet. I am having trouble working out how to find the mapX and mapY values though.
Ok, i have the collisions working from every direction. Stopping the player in the correct place and moving it backwards to stop it getting stuck. I just need help on how to get the maps coordinates, not the screens, at the moment the collisions are being drawn in the center of the screen. The tile x/y values are correct when i check the array
Ok, this is what i have so far for the map class
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package pokemon.statebased;
import java.util.HashMap;
import org.newdawn.slick.Animation;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.tiled.TiledMap;
/**
*
* @author Ceri
*/
public class MapClass {
public TiledMap map;
private static Camera camera;
private final GameContainer gc;
private HashMap<String, Animation> autos = new HashMap<>();
public String mapName;
public MapClass(String mapName, String file, GameContainer g) throws SlickException {
this.mapName = mapName;
map = new TiledMap(file);
gc = g;
//SpriteSheet flower = new SpriteSheet("Images/Animations/Flower.png", 16, 16);
Animation flowerAnimation = new Animation();
flowerAnimation.setAutoUpdate(true);
/*flowerAnimation.addFrame(flower.getSprite(0, 0), 300);
flowerAnimation.addFrame(flower.getSprite(1, 0), 300);
flowerAnimation.addFrame(flower.getSprite(2, 0), 300);
autos.put("Flower", flowerAnimation);*/
}
public int getMapX() {
int tileOffset = camera.tileOffsetX;
int x = tileOffset * 16;
return x;
}
public int getMapY() {
int tileOffset = camera.tileOffsetY;
int y = tileOffset * 16;
return y;
}
public void updateCollisions() throws SlickException
{
for (int i = 0; i < DataSingleton.getInstance().collisionBoxArray.size(); i++) {
DataSingleton.getInstance().collisionBoxArray.get(i).update(getMapX(), getMapY());
}
}
public void updateMap(TiledMap map) throws SlickException {
this.map = map;
this.updateCollisions();
}
public void drawAnimations() {
Animation a = autos.get("Flower");
a.drawFlash(210f, 222f, 16f, 16f);
}
public void setCamera(Camera c) throws SlickException {
camera = c;
this.collisions();
}
public void collisions() throws SlickException {
for (int x = 0; x < map.getWidth(); x++) {
for (int y = 0; y < map.getHeight(); y++) {
int tileID = map.getTileId(x, y, 0);
String value = map.getTileProperty(tileID, "blocked", "false");
if ("true".equals(value)) {
DataSingleton.getInstance().collisionBoxArray.add(new CollisionControl(x, y, this.getMapX(), this.getMapY()));
}
}
}
}
public void drawMap(int layer, Graphics g) throws SlickException {
map.render(
(camera.tileOffsetX + camera.offSetX),
(camera.tileOffsetY + camera.offSetY),
camera.tileIndexX,
camera.tileIndexY,
(gc.getWidth() - camera.tileOffsetX) / camera.tileWidth + 1,
(gc.getHeight() - camera.tileOffsetY) / camera.tileHeight + 1, layer, false);
//drawAnimations();
this.updateCollisions();
for (int i = 0; i < DataSingleton.getInstance().collisionBoxArray.size(); i++) {
DataSingleton.getInstance().collisionBoxArray.get(i).render(g, this.getMapX(), this.getMapY());
}
}
}
The code for updating the collisions when the player moves
Now whats happening is the collision boxes are being drawn in the right place, but when the player moves they’re not updating correctly, and if the player steps on the box its being ignored
This is what i mean