For small isometric map there is able to use GO.factory and .lua file from Tiled.
I tried this way here: http://dragosha.com/adventure/
Map Go contains Factory block.go and this script:
Map loader script:
local tiles={}
local WIDTH = tonumber(sys.get_config("display.width"))
local HEIGHT = tonumber(sys.get_config("display.height"))
-- Get info about flipping of tile. Based on object GID in the object layer
-- @param gid GID from .tmx
-- @return realGID (int), flipX (bool), flipY (bool)
local function flippedGID(gid)
local bit31 = 2147483648
local bit30 = 1073741824
local bit29 = 536870912
local flipX = false
local flipY = false
local flipD = false
local realgid = gid
if realgid >= bit31 then
realgid = realgid - bit31
flipX = true
end
if realgid >= bit30 then
realgid = realgid - bit30
flipY = true
end
if realgid >= bit29 then
realgid = realgid - bit29
flipD = not flipD
end
return realgid, flipX, flipY
end
-- @return file name without path and extension
local function pureName(name)
name=string.gsub(name,"%w+/+","")
name=name:gsub("../+","")
name=string.gsub(name,".png","")
return name
end
local isoToScreen = function(ix,iy,tw,th)
local x = (ix-1)*tw/2 - (iy-1)*tw/2
local y = (ix-1)*th/2 + (iy-1)*th/2
return x,y
end
--- Project isometric position to cartesian position
function convert_isometric_to_screen(map, x, y)
local mapH = map.height
local tileW = map.tilewidth
local tileH = map.tileheight
local tileX = x / tileH
local tileY = y / tileH
local offsetX = mapH * tileW / 2
return
(tileX - tileY) * tileW / 2 + offsetX,
mapH*tileH-(tileX + tileY) * tileH / 2
end
--
local function build_board(self, level)
local pos = vmath.vector3()
local layers=#level.layers
local tileset=level.tilesets[1]
local scale = vmath.vector3()
local rot
for i=1,layers do
local layer=level.layers[i]
for j,obj in pairs(layer.objects) do
local gid,flipX,flipY=flippedGID(obj.gid)
local tileInfo=tileset.tiles[gid]
local img=pureName(tileInfo.image)
if level.orientation=="isometric" then
pos.x,pos.y=convert_isometric_to_screen(level, obj.x, obj.y)
--pos.x=200+obj.x+tileInfo.width/2-obj.y/2
--pos.y=(level.height*level.tileheight)-obj.y+tileInfo.height/2
pos.z=0
else
pos.x=obj.x+tileInfo.width/2
pos.y=(level.height*level.tileheight)-obj.y+tileInfo.height/2
pos.z=0
end
if obj.width ~= tileInfo.width then
scale.x= obj.width/tileInfo.width
scale.y= obj.height/tileInfo.height
scale.z= 1
else
scale.x=1 scale.y=1 scale.z=1
end
if obj.rotation ~=0 then
local angle=-obj.rotation * math.pi/180
rot=vmath.quat_rotation_z(angle)
-- print(rot)
else
rot=nil
end
local y=pos.y
if layer.name=="floor" then
pos.y=-obj.height
else
pos.y=HEIGHT+obj.height
end
local id=factory.create("#factory",pos,rot,{img=hash(img),flipX=flipX,flipY=flipY},scale)
local duration=y/HEIGHT
--duration = math.random(duration * 0.9, duration * 1.1)
local delay = j*.01
go.animate(id, "position.y", go.PLAYBACK_ONCE_FORWARD, y, go.EASING_OUTBACK, duration, delay)
table.insert(tiles,{id=id,x=pos.x,y=y,layer=layer.name, height=obj.height})
end
end
end
local function boardAnimation(self)
local pos=vmath.vector3()
for i,t in pairs(tiles) do
local duration=t.y/HEIGHT
local delay = i*.01
local y
if t.layer=="floor" then
y=-t.height
else
y=HEIGHT+t.height
end
go.animate(t.id, "position.y", go.PLAYBACK_ONCE_FORWARD, y, go.EASING_INBACK, duration, delay)
end
end
local function boardAnimation2(self)
local pos=vmath.vector3()
for i,t in pairs(tiles) do
local duration=t.y/HEIGHT
local delay = i*.01
go.animate(t.id, "position.y", go.PLAYBACK_ONCE_FORWARD, t.y, go.EASING_OUTCUBIC, duration, delay)
end
end
function init(self)
build_board(self, require "main/Tiles/map2")
msg.post(".","acquire_input_focus")
end
function final(self)
-- Add finalization code here
-- Remove this function if not needed
msg.post(".","release_input_focus")
end
function update(self, dt)
-- Add update code here
-- Remove this function if not needed
end
function on_message(self, message_id, message, sender)
-- Add message-handling code here
-- Remove this function if not needed
end
function on_input(self, action_id, action)
if action_id==hash("click") and action.pressed then
if self.anim then
boardAnimation2(self)
self.anim=false
else
boardAnimation(self)
self.anim=true
end
end
end
in this implementation method ‘convert_isometric_to_screen’ works not correct, need to fix, i used not isometric map in Tiled in example
Block GO contains “sprite” component and this script:
Block.script:
go.property("img",hash("none"))
go.property("flipX", false)
go.property("flipY", false)
function init(self)
-- Add initialization code here
-- Remove this function if not needed
--go.set_scale(1)
if self.flipX then
sprite.set_hflip("#sprite", true)
end
if self.flipY then
sprite.set_vflip("#sprite", true)
end
if self.img ~= hash("none") then
msg.post("#sprite","play_animation", {id=self.img})
else
msg.post("#sprite","disable")
end
end
Sprite component uses atlas with images that have same names as in Tiled editor.