If you're still looking for generic 2d camera suggestions, I had a lot of luck with this method:
Create a class called Camera. Make it look something like this:
Code:
public float XOffset;
public float YOffset;
// How close you'll need to get to the edge of the screen to start moving the camera
// (in pixels)
private float sideBuffer;
public Camera()
{
XOffset = 0;
YOffset = 0;
sideBuffer = 150;
}
public void moveX(float offset)
{
XOffset -= offset;
}
public void moveY(float offset)
{
YOffset -= offset;
}
public boolean isOutsideBoundsX(float x)
{
if (
x + XOffset < sideBuffer
|| x + XOffset > CUtil.Dimensions.width - sideBuffer
)
return true;
return false;
}
public boolean isOutsideBoundsY(float y)
{
if (
y + YOffset < sideBuffer
|| y + YOffset > CUtil.Dimensions.height - sideBuffer
)
return true;
return false;
}
Next, we'll need to move it around. When you move your controlled unit, add this in:
(fXpos and fYpos are the controlled unit's position. the x and yDifs are the amount we are moving this loop.
Code:
fXpos += xDif;
fYpos += yDif;
if ( CUtil.GameCamera.isOutsideBoundsX(fXpos) )
{
CUtil.GameCamera.moveX(xDif);
}
if ( CUtil.GameCamera.isOutsideBoundsY(fYpos) )
{
CUtil.GameCamera.moveY(yDif);
}
So now when we go outside of the buffer, the camera moves with our unit (because it looks weird if your character is locked in the center of the screen)
Lastly, we need to apply the camera to our sprites. Give your sprites a boolean, something like isCamerad. This is true if they are affected by the camera. So in your sprite's draw method, have something like...
Code:
public void draw(Graphics g)
{
mTexture.setRotation(fAngle);
mTexture.draw(
(iXpos - (iWidth * fScale) / 2) + (bIsCamered ? CUtil.GameCamera.XOffset : 0),
(iYpos - (iHeight * fScale) / 2) + (bIsCamered ? CUtil.GameCamera.YOffset : 0),
fScale);
}
Basically the idea is to shift the object by whatever the camera's offset is.