Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for Object Layers #12

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
78 changes: 57 additions & 21 deletions TiledLevelImporter.coffee
Original file line number Diff line number Diff line change
@@ -1,38 +1,78 @@
intRegex = /^\d+$/
floatRegex = /^\d+\.\d+$/
boolRegex = /^(true|false)$/
parseProp = (v) -> switch
when intRegex.test(v) then parseInt v
when floatRegex.test(v) then parseFloat v
when boolRegex.test(v) then v == "true"
else v

Crafty.c "TiledLevel",
makeTiles : (ts, drawType) ->
{image: tsImage, firstgid: tNum, imagewidth: tsWidth} =ts
{image: tsImage, firstgid: firstgid, imagewidth: tsWidth} =ts
{imageheight: tsHeight, tilewidth: tWidth, tileheight: tHeight} = ts
{tileproperties: tsProperties} = ts
#console.log ts
{tileproperties: tsProperties, properties: genericProperties} = ts
@tileHeights ||= {}
tNum = firstgid
xCount = tsWidth/tWidth | 0
yCount = tsHeight/tHeight | 0
sMap = {}
#Crafty.load [tsImage], ->
for i in [0...yCount * xCount] by 1
#console.log _ref
@tileHeights[tNum] = tHeight
posx = i % xCount
posy = i / xCount | 0
sName = "tileSprite#{tNum}"
tName = "tile#{tNum}"
sMap[sName] = [posx, posy]
components = "2D, #{drawType}, #{sName}, MapTile"
if tsProperties
if tsProperties[tNum - 1]
if tsProperties[tNum - 1]["components"]
components += ", #{tsProperties[tNum - 1]["components"]}"
#console.log components
Crafty.c tName,
comp: components
nComp =
comp: "2D, #{drawType}, #{sName}, MapTile"
prop: {}
init: ->
@addComponent(@comp)
@
@attr(@prop)
buildProperties = (props) ->
if props?
for name, value of props
if name == "components"
nComp.comp += ", #{value}"
else
nComp.prop[name] = parseProp value
null
buildProperties genericProperties
buildProperties tsProperties[tNum - firstgid] if tsProperties
Crafty.c tName, nComp
tNum++
#console.log sMap
Crafty.sprite(tWidth, tHeight, tsImage, sMap)
return null

makeLayer : (layer) ->
#console.log layer
layerDetails = switch layer.type
when "tilelayer" then @makeTileLayer layer
when "objectgroup" then @makeObjectLayer layer
else []
@_layerArray.push(layerDetails)
return null

makeObjectLayer: (layer) ->
layerDetails = []
for obj in layer.objects
{gid: gid, width: w, height: h, x: x, y: y, properties: props} = obj
components = if gid then "tile#{gid}" else "MapObject, 2D"
components += ", #{props.components}" if props and props.components
e = Crafty.e(components)
e.x = x
e.y = y
e.y -= @tileHeights[gid] if gid
e.h = h if h > 0
e.w = w if w > 0
for name, value of props
if name != "components"
p = {}
p[name] = parseProp value
e.attr p
layerDetails

makeTileLayer : (layer) ->
{data: lData, width: lWidth, height: lHeight} = layer
layerDetails = {tiles:[], width:lWidth, height:lHeight}

Expand All @@ -41,12 +81,8 @@ Crafty.c "TiledLevel",
tile = Crafty.e "tile#{tDatum}"
tile.x = (i % lWidth) * tile.w
tile.y = (i / lWidth | 0) * tile.h
#tile.attr({x: (i % lWidth) * tile.w, y: (i / lWidth | 0) * tile.h})
#console.log "#{tile.x} #{tile.y}"
layerDetails.tiles[i] = tile

@_layerArray.push(layerDetails)
return null
layerDetails

tiledLevel : (levelURL, drawType) ->
$.ajax
Expand Down
121 changes: 102 additions & 19 deletions TiledLevelImporter.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.