view main.lua @ 4:e4263ea87780

Place the sprites on the map
author Radomir Dopieralski <sheep-devel@sheep.art.pl>
date Sat, 15 Dec 2012 13:54:13 +0100
parents f5de6c75c4d0
children 98ab870293bc
line wrap: on
line source

local ATL = require("AdvTiledLoader") 
local anim8 = require("anim8") 
local sprite = require("sprite") 

local sprites = {}
local shadows = {}
local map
local tx, ty = 0, 0
local DX = {0, 1, 0, -1}
local DY = {-1, 0, 1, 0}
local villain

function newVillain(x, y, image)
    local grid = anim8.newGrid(64, 64, 128, 128)
    local villain = sprite.newSprite(
        x, y,
        image,
        anim8.newAnimation('loop', grid('1-2,1'), 0.25)
    )
    function villain:walk(dt, dir)
        local x = self.x + DX[dir] * dt * 250
        local y = self.y + DY[dir] * dt * 250
        local tx, ty = math.floor((x+32)/48), math.floor((y+60)/48)
        local cell = map.layers['control']:get(tx, ty)
        if cell and cell.properties and cell.properties.block == 0 then
            self.x = x
            self.y = y
        end
    end
    return villain
end

function newCivilian(x, y, image)
    local grid = anim8.newGrid(64, 64, 128, 128)
    local civilian = sprite.newSprite(
        x, y,
        image,
        anim8.newAnimation('loop', grid('1-2,1'), 0.25)
    )
    return civilian
end

function love.load()
    local shadow_image = love.graphics.newImage('shadow.png')
    local civilian_image = love.graphics.newImage('civilian.png')
    local villain_image = love.graphics.newImage('villain.png')
    map = ATL.Loader.load('level01.tmx') 
    map.drawObjects = false
    for i, mob in pairs(map.layers['mobs'].objects) do
        if mob.name == 'villain' then
            villain = newVillain(mob.x - 32, mob.y - 60, villain_image)    
            table.insert(sprites, villain)
            table.insert(shadows, sprite.newShadow(shadow_image, villain))
        else
            local civilian = newCivilian(mob.x - 32, mob.y - 60, civilian_image)
            table.insert(sprites, civilian)
            table.insert(shadows, sprite.newShadow(shadow_image, civilian))
        end
    end
end


function love.draw()
    love.graphics.translate(math.floor(tx), math.floor(ty))
    map:autoDrawRange(math.floor(tx), math.floor(ty), 1, 0)
    map:draw()
    for k, shadow in pairs(shadows) do
        shadow:draw()
    end
    for k, sprite in pairs(sprites) do
        sprite:draw()
    end
end


function love.update(dt)
    local scroll_speed = 250
    local scroll_margin = 64
    if love.keyboard.isDown("up") then villain:walk(dt, 1) end
    if love.keyboard.isDown("right") then villain:walk(dt, 2) end
    if love.keyboard.isDown("down") then villain:walk(dt, 3) end
    if love.keyboard.isDown("left") then villain:walk(dt, 4) end
    if villain.x + tx < scroll_margin then
        tx = tx + dt * scroll_speed
    else
        if villain.x + tx > 800 - 64 - scroll_margin then
            tx = tx - dt * scroll_speed
        end
    end
    if villain.y + ty < scroll_margin then
        ty = ty + dt * scroll_speed
    else
        if villain.y + ty > 600 - 64 - scroll_margin then
            ty = ty - dt * scroll_speed
        end
    end
    for k, sprite in pairs(sprites) do
        sprite:update(dt)
    end
    table.sort(sprites, function (a, b) return a.y < b.y end)
end