This repository has been archived by the owner on May 1, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
spriteSheetPackerParser.js
87 lines (76 loc) · 2.51 KB
/
spriteSheetPackerParser.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
/**
* Sprite Sheet Packer Parser for Cocos2d-js
* @author Denis Baskovsky
*/
(function(root, factory) {
if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define([], factory);
} else if (typeof module === 'object' && module.exports) {
// Node. Does not work with strict CommonJS, but
// only CommonJS-like environments that support module.exports,
// like Node.
module.exports = factory();
} else {
// Browser globals (root is window)
root.spriteSheetPackerParser = factory();
}
}(this, function() {
/**
* @param txtPath {String}
* @returns {Promise}
*/
return function(txtPath) {
/**
* @return {Promise}
*/
function loadStrokes() {
return new Promise((resolve, reject) => {
cc.loader.loadTxt(txtPath, (error, data) => {
if (error) return reject(error);
const strokes = data.split(/\n/g) || [];
const outData = strokes.map(s => {
/* example: add_energy = 134 354 130 131*/
if (!s.match(/^(\w+).+=.(\d.+)/)) {
return null;
}
const [name, value] = [RegExp.$1, RegExp.$2];
if (!value.match(/(\d+)\s(\d+)\s(\d+)\s(\d+)/)) {
return null;
}
const x = parseFloat(RegExp.$1);
const y = parseFloat(RegExp.$2);
const w = parseFloat(RegExp.$3);
const h = parseFloat(RegExp.$4);
return {name, x, y, w, h};
});
return resolve(outData.filter(oData => !!oData));
});
});
}
/**
* Загрузка спрайтов
* @return {Promise}
*/
function loadSprites(coords) {
return new Promise((resolve, reject) => {
const imgPath = txtPath.replace(/txt$/, 'png');
cc.loader.loadImg(imgPath, (error) => {
if (error) return reject(error);
const texture = cc.textureCache.addImage(imgPath);
const textureAtlas = new cc.TextureAtlas(texture, coords.length);
const spriteNameArray = coords.map(e => {
const sprite = new cc.SpriteFrame();
const rect = cc.rect(e.x, e.y, e.w, e.h);
sprite.setTexture(textureAtlas.getTexture());
sprite.setRect(rect);
cc.spriteFrameCache.addSpriteFrame(sprite, e.name);
return e.name;
});
return resolve(spriteNameArray);
});
});
}
return Promise.resolve(loadStrokes()).then(loadSprites);
}
}));