luaz wrote:Could you give an example of how to implement this with Advanced Tile Loader?
Sure. I'm going to assume that you already have something working - you can draw stuff, but you are not "scrolling" yet.
Step 1 is to download the lib into your game's folder. You can download it in the root folder if you want, or you can download it into a lib/ folder.
Step 2 is requiring the library. You will want to do this in the file that has the love.load function and the love.draw function.
Code: Select all
local camera = require 'camera' -- or require 'lib.camera' if you downloaded camera.lua to a lib/ folder
Step 3 is initializing the camera with the size of your world. This can be done inside love.load() like this:
Code: Select all
function love.load()
... -- your stuff
camera.setBoundary(0,0, WORLD_WIDTH, WORLD_HEIGHT) -- Width and height are in pixels
end
Step 4 is optional. If you never change the screen dimensions from within the game, you don't need it. If you
do change it, (for example, via a Grapical Options menu), you must inform camera like this:
Code: Select all
function changeScreenResoultion()
... -- your stuff
camera.setBoundary(0,0, love.graphics.getWidth(), love.graphics.getHeight())
end
The library does this once at the beginning automatically, so if you don't change the screen size, you don't need this step.
Step 5 is making the camera look at the player. Usually you do this inside love.update(), after moving the player. Or in love.draw(), at the very beginning.
Code: Select all
... -- inside love.update(dt) after moving the player, or at the beginning of love.draw()
camera.lookAt(player.x, player.y) -- you can choose any other thing for the camera to look at, player is just the most common one
Step 6 is modifying the code that draws the "part that you want to scroll" (this is usually the code that draws the tiles, the player and the monsters) so that it uses the camera lib. It is also worth mentioning that usually you want some drawing code to stay out of the scrolling - for example the game HUD (the score, the "hearts", etc).
So, if your drawing code looks like this:
Code: Select all
function love.draw()
drawTiles()
drawEnemies()
drawPlayer()
drawHUD()
end
You "wrap" the scrollable part inside camera.draw, like this:
Code: Select all
function love.draw()
camera.draw(function()
drawTiles()
drawEnemies()
drawPlayer()
end)
drawHUD() -- HUD is left out of camera because it doesn't scroll
end
That's pretty much it.
Bonus step: The camera.draw lib has 4 parameters - left, top, width and height. This is the rectangle that the camera is using. You can use that rectangle to limit the amount of tiles that you draw. The exact way to do this depends on how your data is structured, but the code should look similar to this:
Code: Select all
function love.draw()
camera.draw(function(left, top, width, height)
drawTilesInsideRectangle(left, top, width, height)
drawEnemiesInsideRectangle(left, top, width, height)
drawPlayerIfInsideRectangle(left, top, width, height)
end)
drawHUD() -- HUD is left out of camera because it doesn't scroll
end
I hope this helps. Regards, and good luck!