-
Notifications
You must be signed in to change notification settings - Fork 14
/
TiledLevelImporter.coffee
84 lines (77 loc) · 2.84 KB
/
TiledLevelImporter.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
Crafty.c "TiledLevel",
makeTiles : (ts, drawType) ->
{image: tsImage, firstgid: tNum, imagewidth: tsWidth} =ts
{imageheight: tsHeight, tilewidth: tWidth, tileheight: tHeight} = ts
{tileproperties: tsProperties} = ts
#console.log ts
xCount = tsWidth/tWidth | 0
yCount = tsHeight/tHeight | 0
sMap = {}
#Crafty.load [tsImage], ->
for i in [0...yCount * xCount] by 1
#console.log _ref
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
init: ->
@addComponent(@comp)
@
tNum++
#console.log sMap
Crafty.sprite(tWidth, tHeight, tsImage, sMap)
return null
makeLayer : (layer) ->
#console.log layer
{data: lData, width: lWidth, height: lHeight} = layer
layerDetails = {tiles:[], width:lWidth, height:lHeight}
for tDatum, i in lData
if tDatum
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
tiledLevel : (levelURL, drawType) ->
$.ajax
type: 'GET'
url: levelURL
dataType: 'json'
data: {}
async: false
success: (level) =>
#console.log level
{layers: lLayers, tilesets: tss} = level
drawType = drawType ? "Canvas"
tsImages = for ts in tss
ts.image
Crafty.load tsImages, =>
@makeTiles(ts, drawType) for ts in tss
@makeLayer(layer) for layer in lLayers
@trigger("TiledLevelLoaded", this)
return null
return null
return @
getTile: (r,c,l=0)->
layer = @_layerArray[l]
return null if not layer? or r < 0 or r>=layer.height or c<0 or c>=layer.width
tile = layer.tiles[c + r*layer.width]
if tile
return tile
else
return undefined
init: ->
@_layerArray = []
@