Re: Simple Tiled Implementation
Posted: Tue Jan 14, 2014 9:14 pm
Aye, I plan to implement real-time updating of tile data.
Awesome!Karai17 wrote:Aye, I plan to implement real-time updating of tile data.
If the code grows, I'll probably abstract it into several files. Only time will tell.bekey wrote:Will the library later be expanded into multiple files, or is that out of the intentional scope?
The Lua export feature wasn't implemented yet when I first created ATL or I most likely would have based it on that. Last I looked it actually tosses out some minor map data so be careful. Also it doesn't do any sort of compression so very large maps can actually take up a good chunk of memory. Those reasons, and the convenience of being able to directly load the native Tiled format is the reason I stuck with TMX (also it doesn't have to be nearly as complicated or slow as I made it).Karai17 wrote:You can think of the TMX file as a project file, and the exported JSON/Lua as a program-ready file. I'm not sure if Kadoba was aware that Tiled exported directly to Lua since ATL made use of XML parsing, but STI uses the native Lua spec so hopefully it will load files faster and allow more flexibility when working with the map table.
Code: Select all
local sti = require "libs.sti"
function love.load()
map = sti.new("assets/maps/map01")
local spriteLayer = map.layers["Sprite Layer"]
map:convertToCustomLayer("Sprite Layer")
spriteLayer.sprite = {
x = 64,
y = 64,
image = love.graphics.newImage("sprite.png"),
}
function spriteLayer:update(dt)
self.sprite.r = self.sprite.r + 90 * dt
end
function spriteLayer:draw()
local x = math.floor(self.sprite.x)
local y = math.floor(self.sprite.y)
love.graphics.draw(self.sprite.image, x, y)
end
end
function love.update(dt)
local sprite = map.layers["Sprite Layer"].sprite
local down = love.keyboard.isDown
local speed = 64 -- pixels per second
if down("w") or down("up") then sprite.y = sprite.y - speed * dt end
if down("s") or down("down") then sprite.y = sprite.y + speed * dt end
if down("a") or down("left") then sprite.x = sprite.x - speed * dt end
if down("d") or down("right") then sprite.x = sprite.x + speed * dt end
map:update(dt)
end
function love.draw()
local sprite = map.layers["Sprite Layer"].sprite
love.graphics.push()
local tx = math.floor(-sprite.x + 384)
local ty = math.floor(-sprite.y + 284)
love.graphics.translate(tx, ty)
map:draw()
love.graphics.pop()
end