-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTiledLevelImporter.coffee
106 lines (91 loc) · 3.47 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
console.log("map tile incoming")
Crafty.c "MapTile",
init: ()-> @
setMapInfo: (@map_c, @map_r, @tiledLevel, @mapTileType)-> @
findRelativeTile: (a, b)->
if typeof a is "string"
[dr, dc] = switch a
when "above" then [-1, 0]
when "below" then [1, 0]
when "right" then [0, 1]
when "left" then [0, -1]
else
[dr, dc] = [a, b]
@tiledLevel.getTile(@map_r+dr, @map_c+dc)
Crafty.c "TiledLevel",
makeTiles : (ts, drawType) ->
console.log("Making tiles")
{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?[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.addComponent("MapTile")
tile.setMapInfo(i % lWidth, (i / lWidth | 0), this, tDatum)
layerDetails.tiles[i] = tile
@_layerArray.push(layerDetails)
#console.log("Layer!!!! \n \n " + layerDetails.tiles)
return null
tiledLevel : (levelURL, drawType) ->
console.log("Starting things off!")
console.log(levelURL)
$.ajax
type: 'GET'
url: levelURL
dataType: 'json'
data: {}
async: true
success: (level) =>
console.log "loaded #{levelURL}"
{layers: lLayers, tilesets: tss} = level
drawType = drawType ? "Canvas"
tsImages = for ts in tss
ts.image
#console.log(tsImages)
Crafty.load {"sprites":tsImages}, =>
@makeTiles(ts, drawType) for ts in tss
@makeLayer(layer) for layer in lLayers
@trigger("TiledLevelLoaded")
console.log("Finished")
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]
return tile if tile?
init: ->
@_layerArray = []