Declare an x value for your camera, and then offset everything (projectile, scenery, etc.) by that camera's X. For example, If I want my camera to be 500 pixels to the right, but projectile is 600 pixels to the right, I draw the projectile on the screen at projectileX - cameraX = 600 - 500 = 100 pixels.
There are three possible states for your camera:
- The target of the camera (the projectile) is less that half the window width away from the left edge, where we want to lock the camera to the left edge
- The target is less that half the window width away from the right edge, where we want to lock the camera to the right edge.
- The target is somewhere in the middle, so it should be centered in the window
Now, lets say your game window is
camWidth wide, your stage is
stageWidth wide, and the projectiles x relative to the stage is
tarX, and the camera's left edge x relative to the stage is
camX. Your code will look something like this:
halfcamWidth = camWidth / 2
if tarX < halfcamWidth:
camX = 0
else if tarX > stageWidth - halfcamWidth:
camX = stageWidth - camWidth
camX = tarX - halfcamWidth
Then each drawing loop, draw the projectile at
tarX - camX.