Hi, I am currently working on my own little game utility library and the first thing I am getting down is a camera that has built in functions that can be useful for 2D side scrolling and top down games.
Basically, what happens currently this method:
public void follow(float delta, Vector2 position, float offsetX,
float offsetY)
Which basically takes a vector2 you want the camera to follow, and offset if needed in each direction. But what if your world/level has bounds? Say a certain size, if the camera is centred on the character and goes towards the left edge for instance, the camera will go out of bounds, showing an un-rendered area of the screen.
So basically my logic was, allow the user to specify the size of the world, then I would use some checks to see if the given vector2 was in a position that if the camera was hit the edge, stop following the player and sit still until they move out of that range.
Here is basically what I have, it looks cheap and nasty so looking for a better way if possible, if not, the method needs cleaned anyway so it will look less shit:
if (shakeEnabled) {
if (shaking) {
processShake(delta);
} else {
if (position.x < viewportWidth / 2
|| viewportWidth / 2 > position
.dst(worldDimension.x, 0)) {
lerp.x = super.position.x;
super.position.lerp(lerp, smoothing);
return;
}
lerp.x = position.x;
super.position.lerp(lerp, smoothing);
}
}
Ignore the first logic checks, the main part I am about is line 5 to 10, this part here calculates the distance between the given vector, the camera bounds and the screen width.
Any tidier ideas?