-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexporter_godot.js
406 lines (343 loc) · 14.6 KB
/
exporter_godot.js
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
/*
*
* Tiled to Godot exporter
*
* https://github.com/patwork/tiled-to-godot-exporter-plugin
*
*/
/* global tiled, Tile, TextFile, BinaryFile, Uint8Array */
(function () {
var PLUGIN_NAME = 'Tiled to Godot exporter';
var PLUGIN_VER = 'v0.1 ALPHA ([email protected])';
var EOL = '\n';
var QUOT = '"';
var TILEMAP_OFFSET = 65536;
var TILESET_OFFSET = 65536;
var BIT_FLIP_H = (1 << 29);
var BIT_FLIP_V = (1 << 30);
// -------------------------------------------------------------------
function GodotScene(map) {
this.map = map;
this.ext_count = 0;
this.sub_count = 0;
this.tile_layer_count = 0;
this.object_layer_count = 0;
this.tileset_id = 0;
this.tilesets = {};
this.lines = [];
// -------------------------------------------------------------------
// PNG file width and height
this._image_info = function (filename) {
var i, data;
var pngsig = new Uint8Array([137, 80, 78, 71, 13, 10, 26, 10]);
var pngihdr = new Uint8Array([73, 72, 68, 82]);
var file = new BinaryFile(filename, BinaryFile.ReadOnly);
// png file signature
data = new Uint8Array(file.read(8));
if (data.length != pngsig.length) {
return null;
}
for (i = 0; i < data.length; i++) {
if (data[i] != pngsig[i]) {
return null;
}
}
// chunk size
data = new Uint8Array(file.read(4));
// ihdr
data = new Uint8Array(file.read(4));
if (data.length != pngihdr.length) {
return null;
}
for (i = 0; i < data.length; i++) {
if (data[i] != pngihdr[i]) {
return null;
}
}
// actual header
data = new Uint8Array(file.read(9));
var width = (data[0] << 24) + (data[1] << 16) + (data[2] << 8) + data[3];
var height = (data[4] << 24) + (data[5] << 16) + (data[6] << 8) + data[7];
file.close();
return {
'width': width,
'height': height
};
};
// -------------------------------------------------------------------
// isArray polyfill
this._is_array = function (val) {
return Object.prototype.toString.call(val) === '[object Array]';
};
// -------------------------------------------------------------------
// put quotes around strings
this._quot = function (val) {
return (typeof (val) == 'number') ? val : QUOT + val + QUOT;
};
// -------------------------------------------------------------------
// data formatter
this._key_type_val = function (key, type, val) {
var str;
if (type == null) {
if (this._is_array(val)) {
str = '[ ' + val.join(', ') + ' ]';
} else {
str = this._quot(val);
}
} else {
str = type + '( ' + (this._is_array(val) ? val.join(', ') : this._quot(val)) + ' )';
}
return key + ' = ' + str;
};
// -------------------------------------------------------------------
// resource heading
this._heading = function (resource_type, dictionary) {
var keyval = '';
if (dictionary && Object.keys(dictionary).length) {
var that = this;
var arr = Object.keys(dictionary).map(function (key) {
return key + '=' + that._quot(dictionary[key]);
});
keyval = ' ' + arr.join(' ');
}
return '[' + resource_type + keyval + ']';
};
// -------------------------------------------------------------------
// scene descriptor
this._descriptor = function () {
return this._heading('gd_scene', {
'load_steps': this.ext_count + this.sub_count + 1,
'format': 2
});
};
// -------------------------------------------------------------------
// calculate tile id
this._tile_id = function (tile) {
var region_width = tile.tileset.imageWidth;
var columns = Math.floor(region_width / tile.width);
var tile_row = Math.floor(tile.id / columns);
var tile_col = tile.id - tile_row * columns;
return tile_row * TILESET_OFFSET + tile_col;
};
// -------------------------------------------------------------------
// convert filename to resource path
this._res_path = function (filename) {
var arr = filename.split('/');
return 'res://' + arr[arr.length - 1];
};
// -------------------------------------------------------------------
// export all tiled tilesets into one godot tileset
this._export_tilesets = function (tilesets) {
var i, ts, keys;
// check tilesets compability with godot
keys = [];
for (i = 0; i < tilesets.length; i++) {
ts = tilesets[i];
if (ts.image == '') {
return 'Image collection tilesets are not supported';
}
if (ts.tileSpacing != 0) {
return 'Tile spacing is not supported';
}
if (ts.margin != 0) {
return 'Tile margin is not supported';
}
this.tilesets[ts.name] = [i, ts];
keys.push(ts.name);
}
// workaround because of https://github.com/bjorn/tiled/issues/2733
// this will be hopefully unnecesary after Tiled 1.3.3+
for (i = 0; i < keys.length; i++) {
ts = this.tilesets[keys[i]];
var png = this._image_info(ts[1].image);
if (!png) {
return 'Texture image must be in PNG format';
}
ts[1].imageWidth = png.width;
ts[1].imageHeight = png.height;
}
// headings for external resources
for (i = 0; i < keys.length; i++) {
ts = this.tilesets[keys[i]];
this.lines.push(this._heading('ext_resource', {
'path': this._res_path(ts[1].image),
'type': 'Texture',
'id': this.ext_count + ts[0] + 1
}));
}
this.lines.push('');
// tileset subresource id
this.sub_count++;
this.tileset_id = this.sub_count;
// tileset node heading
this.lines.push(this._heading('sub_resource', {
'type': 'TileSet',
'id': this.tileset_id
}));
// tileset node data
for (i = 0; i < keys.length; i++) {
ts = this.tilesets[keys[i]];
this.lines.push(this._key_type_val(i + '/name', null, ts[1].name + ' ' + i));
this.lines.push(this._key_type_val(i + '/texture', 'ExtResource', this.ext_count + ts[0] + 1));
this.lines.push(this._key_type_val(i + '/tex_offset', 'Vector2', [0, 0]));
this.lines.push(this._key_type_val(i + '/modulate', 'Color', [1, 1, 1, 1]));
this.lines.push(this._key_type_val(i + '/region', 'Rect2', [0, 0, ts[1].imageWidth, ts[1].imageHeight]));
this.lines.push(this._key_type_val(i + '/tile_mode', null, 2));
this.lines.push(this._key_type_val(i + '/autotile/icon_coordinate', 'Vector2', [0, 0]));
this.lines.push(this._key_type_val(i + '/autotile/tile_size', 'Vector2', [ts[1].tileWidth, ts[1].tileHeight]));
this.lines.push(this._key_type_val(i + '/autotile/spacing', null, 0));
this.lines.push(this._key_type_val(i + '/autotile/occluder_map', null, []));
this.lines.push(this._key_type_val(i + '/autotile/navpoly_map', null, []));
this.lines.push(this._key_type_val(i + '/autotile/priority_map', null, []));
this.lines.push(this._key_type_val(i + '/autotile/z_index_map', null, []));
this.lines.push(this._key_type_val(i + '/occluder_offset', 'Vector2', [0, 0]));
this.lines.push(this._key_type_val(i + '/navigation_offset', 'Vector2', [0, 0]));
this.lines.push(this._key_type_val(i + '/shapes', null, []));
this.lines.push(this._key_type_val(i + '/z_index', null, 0));
}
this.lines.push('');
// update external resources counter
this.ext_count += keys.length;
return null;
};
// -------------------------------------------------------------------
// export one tile layer
this._export_tile_layer = function (layer) {
var x, y;
// get all tiles from layer
var tile_data = [];
for (y = 0; y < layer.height; y++) {
var offset = y * TILEMAP_OFFSET;
for (x = 0; x < layer.width; x++) {
var tile = layer.tileAt(x, y);
if (tile != null) {
var ts = tile.tileset;
if (!ts || !this.tilesets[ts.name]) {
return 'Found tile with unknown tileset on layer ' + layer.name;
}
var flags = layer.flagsAt(x, y);
var flip_h = (flags & Tile.FlippedHorizontally) ? BIT_FLIP_H : 0;
var flip_v = (flags & Tile.FlippedVertically) ? BIT_FLIP_V : 0;
tile_data.push(offset + x);
tile_data.push(this.tilesets[ts.name][0] + flip_h + flip_v);
tile_data.push(this._tile_id(tile));
}
}
}
// tilemap node heading
this.tile_layer_count++;
this.lines.push(this._heading('node', {
'name': 'TileMap' + (this.tile_layer_count > 1 ? this.tile_layer_count : ''),
'type': 'TileMap',
'parent': '.'
}));
// tilemap node data
this.lines.push(this._key_type_val('tile_set', 'SubResource', this.tileset_id));
this.lines.push(this._key_type_val('cell_size', 'Vector2', [this.map.tileWidth, this.map.tileHeight]));
this.lines.push(this._key_type_val('format', null, 1));
this.lines.push(this._key_type_val('tile_data', 'PoolIntArray', tile_data));
this.lines.push('');
return null;
};
// -------------------------------------------------------------------
// export one object layer
this._export_object_layer = function (layer) {
// object node name
this.object_layer_count++;
var node_name = 'StaticBody2D' + (this.object_layer_count > 1 ? this.object_layer_count : '');
// object node heading
this.lines.push(this._heading('node', {
'name': node_name,
'type': 'StaticBody2D',
'parent': '.'
}));
// TODO object node data
this.lines.push('; LAYER ' + layer.name);
this.lines.push('');
return null;
};
// -------------------------------------------------------------------
// parse through all layers
this._export_layers = function () {
var err = null;
for (var i = 0; i < this.map.layerCount; i++) {
var layer = this.map.layerAt(i);
if (layer.isTileLayer) {
err = this._export_tile_layer(layer);
} else if (layer.isObjectLayer) {
err = this._export_object_layer(layer);
}
if (err) {
return err;
}
}
return null;
};
// -------------------------------------------------------------------
// export scene
this.export = function () {
var err = null;
this.lines.push('; ' + PLUGIN_NAME + ' ' + PLUGIN_VER);
this.lines.push('; ' + tiled.version + ' @ ' + tiled.platform + ' (' + tiled.arch + ')');
this.lines.push('; ' + new Date().toString());
this.lines.push('');
// check if tilesets are present
var tilesets = this.map.usedTilesets();
if (!tilesets || !tilesets.length) {
return 'Map has no tilesets attached';
}
// export tilesets
err = this._export_tilesets(tilesets);
if (err) {
return err;
}
// root node
this.lines.push(this._heading('node', {
'name': 'Root',
'type': 'Node2D'
}));
this.lines.push('');
// chceck for layers
if (!this.map.layerCount) {
return 'Map has no layers';
}
// export layers
err = this._export_layers();
if (err) {
return err;
}
return null;
};
// -------------------------------------------------------------------
// return scene as string
this.content = function () {
return this._descriptor() + EOL + EOL + this.lines.join(EOL);
};
}
// -------------------------------------------------------------------
// Tiled exporter object
var godot_map_format = {
name: 'Godot TileMap Scene',
extension: 'tscn',
write: function (map, filename) {
var scene = new GodotScene(map);
var err = scene.export();
if (err) {
tiled.alert(err, PLUGIN_NAME);
tiled.error(err);
return;
}
var file = new TextFile(filename, TextFile.WriteOnly);
file.write(scene.content());
file.commit();
file.close();
tiled.log(filename + ' has been written to disk');
},
};
// -------------------------------------------------------------------
// register Tiled exporter
tiled.registerMapFormat('godot', godot_map_format);
tiled.log(godot_map_format.name + ' exporter successfully registered');
})();
// EoF