This is nothing hard to do imho. The thing is, it needs to be kinda generic right? Since we want the developer to have some control over the camera.
Here is some basic cam with smooth movement (not tested against anything, missing methods for stting the world bounds):
Code:
public class Camera {
public Vector2f currentPosition;
public Vector2f targetPosition;
public float normalSpeed;
public Camera() {
currentPosition = new Vector2f();
targetPosition = new Vector2f();
normalSpeed = 0.1f;
}
public void update(World world, int delta) {
currentPosition.x = fadeOut(currentPosition.x, targetPosition.x, normalSpeed);
currentPosition.y = fadeOut(currentPosition.y, targetPosition.y, normalSpeed);
}
public void translate(World world, Graphics g) {
g.translate(-currentPosition.x, -currentPosition.y);
}
public void translateMap(World world, Graphics g) {
g.scale(2, 2);
g.translate(-(currentPosition.x % world.getTileSize()), -(currentPosition.y % world.getTileSize()));
}
private float fadeOut(float current, float target, float ratio) {
return target * (ratio ) + (1 - ratio ) * current;
}
public Vector2f getCurrent() {
return currentPosition;
}
public float getCurrentX() {
return currentPosition.x;
}
public float getCurrentY() {
return currentPosition.y;
}
public Vector2f getTarget() {
return targetPosition;
}
public float getTargetX() {
return targetPosition.x;
}
public float getTargetY() {
return targetPosition.y;
}
public float getSpeed() {
return normalSpeed;
}
public void setCurrent(float x, float y) {}
public void setTarget(float x, float y, int time) {}
public void setSpeed(float speed) {
}
}
It should kinda work because this is a trimmed down version of the camera I use in Mr. Hat I. World could be replaced by some other stuff.
_________________
Current Projects:
Mr. Hat I
Vegan Vs. ZombiesProjects:RadicalFish Engine - Build on top of Slick2D, Ideas, Bugs? Open an Issue ticket!