-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtiled.cxs
286 lines (231 loc) · 7.32 KB
/
tiled.cxs
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
Strict
Import brl.json
Import mojo.graphics
Import grid
Global testMessage:String
Class Tiled
Field testGrid:Grid
Method New()
widthMap = 0
heightMap = 0
widthTile = 0
heightTile = 0
tilesets = New List<Tileset>
grid = New Grid
End Method
Method ParseJSON:Void(dataString:String)
If dataString.Length() = 0
Error("Data is empty")
Return
EndIf
Local jsonObject:JsonObject = New JsonObject(dataString)
Self.widthMap = jsonObject.GetInt("width")
Self.heightMap = jsonObject.GetInt("height")
Self.widthTile = jsonObject.GetInt("tilewidth")
Self.heightTile = jsonObject.GetInt("tileheight")
SetGrid(jsonObject)
testGrid = grid
SetTilesets(jsonObject)
End Method
Method GetPropertieTile:String(idTile:Int, namePropertie:String)
For Local tileset:Tileset = EachIn tilesets
If tileset.IncludeTile(idTile)
Return tileset.GetPropertieTile(idTile, namePropertie)
End If
Next
Return ""
End Method
Method GetIdTile:Int(x:Int, y:Int, nameLayer:String)
Return Self.grid.GetValue(x, y, nameLayer)
End Method
Method GetWidthMap:Int()
Return Self.widthMap
End Method
Method GetHeightMap:Int()
Return Self.heightMap
End Method
Method GetWidthTile:Int()
Return Self.widthTile
End Method
Method GetHeightTile:Int()
Return Self.heightTile
End Method
Method GetCountLayers:Int()
Return Self.grid.GetZ()
End Method
Method GetNameLayer:String(index:Int)
Return Self.grid.GetNameLayer(index)
End Method
Method SetIdTile:Void(x:Int, y:Int, nameLayer:String, idTile:Int)
Self.grid.SetValue(x, y, nameLayer, idTile)
End Method
Method RenderTile:Void(idTile:Int, coorX:Int, coorY:Int)
For Local tileset:Tileset = EachIn tilesets
If tileset.IncludeTile(idTile)
tileset.RenderTile(idTile, coorX, coorY)
End If
Next
End Method
Private
Method SetGrid:Void(jsonObject:JsonObject)
Local layers:JsonArray = JsonArray(JsonObject(jsonObject).Get("layers"))
grid = New Grid(widthMap, heightMap, layers.Length())
For Local i:Int = 0 Until layers.Length()
Local layer:JsonObject = JsonObject(layers.Get(i))
Local nameLayer:String = layer.GetString("name")
grid.AddLayer(nameLayer)
Local dataLayer:JsonArray = JsonArray(layer.Get("data"))
Local indexData:Int = 0
For Local coorY:Int = 0 Until widthMap
For Local coorX:Int = 0 Until heightMap
Local value:Int = dataLayer.GetInt(indexData)
grid.SetValue(coorX, coorY, nameLayer, value)
indexData += 1
Next
Next
Next
End Method
Method SetTilesets:Void(jsonObject:JsonObject)
Local jsonArray:JsonArray = JsonArray(JsonObject(jsonObject).Get("tilesets"))
For Local i:Int = 0 Until jsonArray.Length()
Local tilesetJson:JsonObject = JsonObject(jsonArray.Get(i))
Local tileset:Tileset = New Tileset(tilesetJson)
tileset.LoadTilesetImage()
Self.tilesets.AddLast(tileset)
Next
End Method
Field widthMap:Int
Field heightMap:Int
Field widthTile:Int
Field heightTile:Int
Field images:Image[]
Field imagesFrames:Int[]
Field tilesets:List<Tileset>
Field grid:Grid
End Class
Private
Class Tileset
Method New(jsonObject:JsonObject)
Self.tiles = New List<Tile>
Self.name = jsonObject.GetString("name")
Self.pathImage = jsonObject.GetString("image")
Self.widthImage = jsonObject.GetInt("imagewidth")
Self.heightImage = jsonObject.GetInt("imageheidht")
Self.widthTile = jsonObject.GetInt("tilewidth")
Self.heightTile = jsonObject.GetInt("tileheight")
If (Self.widthTile = 0 Or Self.heightTile = 0)
Error("Width or height tile is zero")
End If
Self.firstgid = jsonObject.GetInt("firstgid")
Self.tilecount = jsonObject.GetInt("tilecount")
For Local i:Int = Self.firstgid Until Self.tilecount + Self.firstgid
Local tile:Tile = New Tile(i)
Self.tiles.AddLast(tile)
Next
Local propertiesData:JsonObject = JsonObject(jsonObject.Get("tileproperties"))
If propertiesData
SetProperties(propertiesData)
End If
End Method
Method GetPropertieTile:String(idTile:Int, namePropertie:String)
Local tile:Tile = Self.GetTile(idTile)
Return tile.GetPropertie(namePropertie)
End Method
Method IncludeTile:Bool(idTile:Int)
Return idTile >= Self.firstgid And idTile < Self.firstgid + Self.tilecount
End Method
Method GetTile:Tile(idTile:Int)
For Local tile:Tile = EachIn Self.tiles
If tile.GetId() = idTile
Return tile
End If
Next
Error("Tile " + idTile + " not found!")
Return New Tile
End Method
Method LoadTilesetImage:Void()
Self.tilesetImage = LoadImage(Self.pathImage, Self.widthTile, Self.heightTile, Self.tilecount)
End Method
Method RenderTile:Void(idTile:Int, coorX:Int, coorY:Int)
Local frame:Int = idTile - Self.firstgid
DrawImage(tilesetImage, coorX, coorY, frame)
End Method
Private
Method AddTile:Void(_tile:Tile)
Local idTile:Int = _tile.GetId()
For Local tile:Tile = EachIn Self.tiles
If tile.GetId() = idTile
Error("Tile " + idTile + " already exsist")
Return
End If
Next
Self.tiles.AddLast(_tile)
End Method
Method SetProperties:Void(jsonObject:JsonObject)
For Local tileJson:= EachIn jsonObject.GetData()
Local idTile:Int = Int(tileJson.Key()) + 1
Local tile:Tile = Self.GetTile(idTile)
Local propertiesTile:JsonObject = JsonObject(tileJson.Value())
For Local propertie:= EachIn propertiesTile.GetData()
Local namePropertie:String = propertie.Key()
Local valuePropertie:String = propertiesTile.GetString(namePropertie)
tile.AddPropertie(namePropertie, valuePropertie)
Next
Next
End Method
Field name:String
Field pathImage:String
Field tilesetImage:Image
Field widthImage:Int
Field heightImage:Int
Field widthTile:Int
Field heightTile:Int
Field firstgid:Int
Field tilecount:Int
Field tiles:List<Tile>
End Class
Class Tile
Method New(_id:Int)
Self.id = _id
Self.properties = New List<Propertie>
End Method
Method AddPropertie:Void(_namePropertie:String, _value:String)
For Local propertie:Propertie = EachIn properties
If propertie.GetNamePropertie() = _namePropertie
Error("Propertie " + _namePropertie + " already exist in tile: " + Self.id)
Return
End If
Next
Local nextPropertie:Propertie = New Propertie(_namePropertie, _value)
Self.properties.AddLast(nextPropertie)
End Method
Method GetPropertie:String(_namePropertie:String)
For Local propertie:Propertie = EachIn properties
If propertie.GetNamePropertie() = _namePropertie
Return propertie.GetValuePropertie()
End If
Next
'Error("Propertie " + _namePropertie + " not found in tile: " + Self.id)
Return ""
End Method
Method GetId:Int()
Return Self.id
End Method
Private
Field id:Int
Field properties:List<Propertie>
End Class
Class Propertie
Field namePropertie:String
Field valuePropertie:String
Method New(_namePropertie:String, _valuePropertie:String)
Self.namePropertie = _namePropertie
Self.valuePropertie = _valuePropertie
End Method
Method GetValuePropertie:String()
Return Self.valuePropertie
End Method
Method GetNamePropertie:String()
Return Self.namePropertie
End Method
End Class