--------------------------------------------------- 
-- FBA Tutorials
--
-- Tutorial / 16 / Advanced scrolling
--
--------------------------------------------------- 
--
-- This sample shows how to draw and scroll a map
-- with four different layers. This is done by drawing
-- more than one grid.
--
--------------------------------------------------- 

local PIC = loadimage("tiles.png")

-- create 4 layers
local GRIDZ = creategrid(PIC, 24, 16, 20, 20)
local GRID0 = creategrid(PIC, 24, 16, 20, 20)
local GRID1 = creategrid(PIC, 24, 16, 20, 20)
local GRID2 = creategrid(PIC, 24, 16, 20, 20)


function onmainloop()
        clear(0,0,0)
        
        -- draw three layers
        drawgrid(GRIDZ, 0, 0, screenwidth(), screenheight())
        drawgrid(GRID0, 0, 0, screenwidth(), screenheight())
        drawgrid(GRID1, 0, 0, screenwidth(), screenheight())
        drawgrid(GRID2, 0, 0, screenwidth(), screenheight())
        
        -- move layers if pressing right or left input keys
        if numkeypress(keyleft()) > 0 and gridworldx(GRID1) > 0 then
                gridworldx(GRID0, gridworldx(GRID0) - 2)
                gridworldx(GRID1, gridworldx(GRID1) - 10)
                gridworldx(GRID2, gridworldx(GRID2) - 5)
        elseif numkeypress(keyright()) > 0 and gridworldx(GRID1) < 240 then
                gridworldx(GRID0, gridworldx(GRID0) + 2)
                gridworldx(GRID1, gridworldx(GRID1) + 10)
                gridworldx(GRID2, gridworldx(GRID2) + 5)
        end
        
        print(1,1,"Scroll with left and right", 0, 0, 0)
        print(1,8,"We are using 4 grids", 0, 0, 0)
        print(1,15,"Grid 1: sky", 0, 0, 0)
        print(1,22,"Grid 2: mountains", 0, 0, 0)
        print(1,29,"Grid 3: platforms", 0, 0, 0)
        print(1,36,"Grid 4: clouds", 0, 0, 0)
        
        -- exit if you press the mouse/stylus
        if mousedown() > 0 then quit() end
end

function fillgrids()
        -- fill layers with some tiles...
        -- note: actually this isn't the best way to do that; usually
        -- you store your map definition inside a resource file, created
        -- with external tools, like map editors.
        --
        -- Anyway for tutorial purposes, this sample is acceptable ;)
        --
        fillgrid(GRIDZ, 7)
        
        gridcell(GRID0, 2, 14, 17)
        gridcell(GRID0, 3, 14, 18)
        gridcell(GRID0, 4, 14, 19)
        gridcell(GRID0, 5, 14, 20)
        gridcell(GRID0, 6, 14, 21)
        gridcell(GRID0, 7, 14, 22)
        

        gridcell(GRID0, 9, 14, 23)
        gridcell(GRID0, 10, 14, 24)
        gridcell(GRID0, 11, 14, 25)
        gridcell(GRID0, 12, 14, 26)
        gridcell(GRID0, 13, 14, 27)
        gridcell(GRID0, 14, 14, 28)

        gridcell(GRID1, 0, 15, 0)
        for x=1, 22 do
                if math.mod(x,2) == 0 then
                        id = 1
                else
                        id = 2
                end
                gridcell(GRID1, x, 15, id)
        end
        gridcell(GRID1, 23, 15, 3)
        
        for y = 12, 14 do
                gridcell(GRID1, 4, y, 10)
                gridcell(GRID1, 5, y, 11)
                gridcell(GRID1, 6, y, 13)
        end
        gridcell(GRID1, 4, 11, 0)
        gridcell(GRID1, 5, 11, 1)
        gridcell(GRID1, 6, 11, 3)

        gridcell(GRID1, 8, 4, 5)
        gridcell(GRID1, 9, 4, 6)

        for y = 9, 14 do
                gridcell(GRID1, 11, y, 10)
                gridcell(GRID1, 12, y, 11)
                gridcell(GRID1, 13, y, 12)
                gridcell(GRID1, 14, y, 13)
        end
        gridcell(GRID1, 11, 8, 0)
        gridcell(GRID1, 12, 8, 1)
        gridcell(GRID1, 13, 8, 2)
        gridcell(GRID1, 14, 8, 3)

        for x = 15, 20 do
                gridcell(GRID1, x, 8, 4)
        end

        for y = 9, 14 do
                gridcell(GRID1, 20, y, 10)
                gridcell(GRID1, 21, y, 15)
                gridcell(GRID1, 22, y, 13)
        end
        gridcell(GRID1, 20, 8, 0)
        gridcell(GRID1, 21, 8, 14)
        gridcell(GRID1, 22, 8, 3)
        
        gridcell(GRID2, 5, 6, 8)
        gridcell(GRID2, 6, 6, 9)

        gridcell(GRID2, 11, 3, 8)
        gridcell(GRID2, 12, 3, 9)

        gridcell(GRID2,2, 2, 8)
        gridcell(GRID2,3, 2, 9)
end

fillgrids()
