I have a tutorial on this, maybe it helps you:
http://shockper.com/2012/03/slick2d-mov ... 2/?lang=enI know probably it's not the best way to do it, but well... it works
And here the code I used on it:
(The entity class it's really specific for the tutorial, use your own)
Code:
package game;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Image;
import org.newdawn.slick.Input;
import org.newdawn.slick.geom.Rectangle;
import org.newdawn.slick.geom.Vector2f;
public class Entity {
protected Vector2f pos; // Vector contains a value with components x & y
protected Rectangle box;
protected Image sprite;
public Entity(float x, float y, int width, int height, Image sprite) {
pos = new Vector2f(x,y);
box = new Rectangle(x, y, width, height);
this.sprite = sprite;
}
public void update(GameContainer gc, int mapWidth, int mapHeight, int delta) {
Vector2f trans = new Vector2f(0, 0);
Input input = gc.getInput();
if (input.isKeyDown(Input.KEY_W)) // Delta is used to move things on a frame rate independent way
trans.y = -0.5f * delta;
if (input.isKeyDown(Input.KEY_S))
trans.y = 0.5f * delta;
if (input.isKeyDown(Input.KEY_D))
trans.x = 0.5f * delta;
if (input.isKeyDown(Input.KEY_A))
trans.x = -0.5f * delta;
if (trans.x != 0 && trans.y != 0) { // If both components aren't null, we reduce them to have constant speed on all directions
trans.set(trans.x / 1.5f, trans.y / 1.5f);
}
if(pos.x+trans.x > 32 && pos.x+trans.x < (mapWidth-64)) // Is the player inside the map? (We add (subtract) because of the stone wall)
pos.x += trans.x;
if(pos.y+trans.y > 32 && pos.y+trans.y < (mapHeight-64))
pos.y += trans.y;
}
public void render() {
sprite.draw(pos.x, pos.y);
}
// Getters and Setters
public Vector2f getPos() {
return pos;
}
public float getX() {
return pos.x;
}
public float getY() {
return pos.y;
}
public void setPos(Vector2f pos) {
this.pos = pos;
}
public Rectangle getBox() {
return box;
}
public void setBox(Rectangle box) {
this.box = box;
}
public Image getSprite() {
return sprite;
}
public void setSprite(Image sprite) {
this.sprite = sprite;
}
}
Code:
package game;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.geom.Rectangle;
import org.newdawn.slick.tiled.TiledMap;
public class Camera {
private int transX, transY;
private int mapWidth, mapHeight;
private Rectangle viewPort;
/* We define a rectangle with the size of our screen, this represents our camera
* "range", so everything inside the viewport will be drawn on the screen, we will
* be able to move this rectangle across the map. */
public Camera(TiledMap map, int mapWidth, int mapHeight) {
transX = 0;
transY = 0;
viewPort = new Rectangle(0, 0, Game.WIDTH, Game.HEIGHT);
this.mapWidth = mapWidth;
this.mapHeight = mapHeight;
}
public void translate (Graphics g, Entity entity) {
if(entity.getX()-Game.WIDTH/2+16 < 0)
transX = 0;
else if(entity.getX()+Game.WIDTH/2+16 > mapWidth)
transX = -mapWidth+Game.WIDTH;
else
transX = (int)-entity.getX()+Game.WIDTH/2-16;
if(entity.getY()-Game.HEIGHT/2+16 < 0)
transY = 0;
else if(entity.getY()+Game.HEIGHT/2+16 > mapHeight)
transY = -mapHeight+Game.HEIGHT;
else
transY = (int)-entity.getY()+Game.HEIGHT/2-16;
g.translate(transX, transY);
viewPort.setX(-transX);
viewPort.setY(-transY);
}
}
Code:
package game;
import org.newdawn.slick.AppGameContainer;
import org.newdawn.slick.BasicGame;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.tiled.TiledMap;
public class Game extends BasicGame {
//Constants
static int WIDTH = 1024;
static int HEIGHT = 760;
static boolean fullscreen = false;
static boolean showFPS = true;
static String title = "Advanced Camera tutorial";
static int fpslimit = 60;
//Variables
TiledMap map; //The file that contain the world we are
Entity player; //The moving entity we will follow
Camera camera; //The camera we are going to use
int mapHeight, mapWidth;
public Game(String title) {
super(title);
}
public void init(GameContainer gc) throws SlickException {
map = new TiledMap("res/testmap.tmx");
mapWidth = map.getWidth() * map.getTileWidth(); // Map size = Tile Size * number of Tiles
mapHeight = map.getHeight() * map.getTileHeight();
player = new Entity(50, 50, 32, 32, new Image("res/emo.png"));
camera = new Camera(map, mapWidth, mapHeight);
}
public void update(GameContainer gc, int delta) throws SlickException {
player.update(gc, mapWidth, mapHeight, delta);
}
public void render(GameContainer gc, Graphics g) throws SlickException {
camera.translate(g, player);
map.render(0, 0);
player.render();
}
public static void main(String[] args) throws SlickException {
AppGameContainer app = new AppGameContainer(new Game(title));
app.setDisplayMode(WIDTH, HEIGHT, fullscreen);
app.setTargetFrameRate(fpslimit);
app.setVSync(true);
app.setShowFPS(showFPS);
app.start();
}
}