1 /**
  2  * @fileOverview
  3  * extendMap.enchant.js
  4  * @version 1.0
  5  * @require enchant.js v0.4 or later
  6  * @author rtsan
  7  *
  8  * @description
  9  * enchantMapEditor:
 10  * http://github.com/wise9/enchantMapEditor
 11  */
 12 
 13 /**
 14  * plugin namespace object
 15  * @type {Object}
 16  */
 17 enchant.extendMap = {};
 18 
 19 /**
 20  * scope enchant.extendMap.ExMap.prototype
 21  * @type {*}
 22  */
 23 enchant.extendMap.ExMap = enchant.Class.create(enchant.Map, {
 24     /**
 25      * @extends enchant.Map
 26      */
 27 
 28     type2data: function() {
 29         var len = this._typeData.length;
 30         var xlen = this._typeData[0][0].length;
 31         var ylen = this._typeData[0].length;
 32         for (var index = 0; index < len; index++) {
 33             this._data[index] = new Array();
 34             for (var y = 0; y < ylen; y++) {
 35                 this._data[index][y] = new Array();
 36                 for (var x = 0; x < xlen; x++) {
 37                     this._data[index][y][x] = this.searchPattern(index, x, y);
 38                 }
 39             }
 40         }
 41     },
 42     data2type: function() {
 43         var len = this._data.length;
 44         var xlen = this._data[0][0].length;
 45         var ylen = this._data[0].length;
 46         this._typeData = new Array();
 47         for (var index = 0; index < len; index++) {
 48             this._typeData[index] = new Array();
 49             for (var y = 0; y < ylen; y++) {
 50                 this._typeData[index][y] = new Array();
 51                 for (var x = 0; x < xlen; x++) {
 52                     this._typeData[index][y][x] = Math.floor(this._data[index][y][x] / 68);
 53                     if (this._data[index][y][x] % 17 > 12) {
 54                         this._typeData[index][y][x] = -1;
 55                     }
 56                 }
 57             }
 58         }
 59     },
 60     isOwn: function(index, x, y, own) {
 61         var data = this._typeData[index][y][x];
 62         if (data == own
 63             || data == -1
 64             || data > 7
 65             || (typeof this._types != 'undefined')
 66             && this._types[data].parentNum == this._types[own].baseNum) {
 67             return true;
 68         } else {
 69             return false;
 70         }
 71     },
 72     searchPattern: function(index, x, y) {
 73         var patternTable = {
 74             0: 42, 2: 54, 8: 3, 10: 53, 11: 57, 16: 1, 18: 51, 22: 55, 24: 2, 26: 52,
 75             27: 44, 30: 45, 31: 56, 64: 20, 66: 37, 72: 19, 74: 36, 75: 10, 80: 17, 82: 34,
 76             86: 11, 88: 18, 90: 35, 91: 60, 94: 58, 95: 59, 104: 23, 106: 27, 107: 40, 120: 61,
 77             122: 26, 123: 43, 126: 9, 127: 6, 208: 21, 210: 28, 214: 38, 216: 62, 218: 24, 222: 41,
 78             223: 7, 219: 8, 248: 22, 250: 25, 251: 4, 254: 5, 255: 39
 79         };
 80         var patternNumber = 0;
 81         var own = this._typeData[index][y][x];
 82         var xlen = this._typeData[index][y].length - 1;
 83         var ylen = this._typeData[index].length - 1;
 84         if (own == -1) {
 85             return -1;
 86         } else if (own > 7) {
 87             return this._data[index][y][x];
 88         }
 89         if (x == 0) {
 90             patternNumber |= 41;
 91         } else if (x == xlen) {
 92             patternNumber |= 148;
 93         }
 94         if (y == 0) {
 95             patternNumber |= 7;
 96         } else if (y == ylen) {
 97             patternNumber |= 224;
 98         }
 99 
100         if (x > 0) {
101             if (this.isOwn(index, x - 1, y, own)) {
102                 patternNumber += 8;
103             }
104 
105         }
106         if (x < xlen) {
107             if (this.isOwn(index, x + 1, y, own)) {
108                 patternNumber += 16;
109             }
110         }
111         if (y > 0) {
112             if (this.isOwn(index, x, y - 1, own)) {
113                 patternNumber += 2;
114             }
115         }
116         if (y < ylen) {
117             if (this.isOwn(index, x, y + 1, own)) {
118                 patternNumber += 64;
119             }
120         }
121         if (x > 0 && y > 0) {
122             if (this.isOwn(index, x - 1, y - 1, own)) {
123                 patternNumber += 1;
124             }
125         }
126         if (x < xlen && y > 0) {
127             if (this.isOwn(index, x + 1, y - 1, own)) {
128                 patternNumber += 4;
129             }
130         }
131         if (x > 0 && y < ylen) {
132             if (this.isOwn(index, x - 1, y + 1, own)) {
133                 patternNumber += 32;
134             }
135         }
136         if (x < xlen && y < ylen) {
137             if (this.isOwn(index, x + 1, y + 1, own)) {
138                 patternNumber += 128;
139             }
140         }
141 
142         if (!((patternNumber & 1) && (patternNumber & 2) && (patternNumber & 8))) {
143             patternNumber &= 254;
144         }
145         if (!((patternNumber & 4) && (patternNumber & 2) && (patternNumber & 16))) {
146             patternNumber &= 251;
147         }
148         if (!((patternNumber & 32) && (patternNumber & 64) && (patternNumber & 8))) {
149             patternNumber &= 223;
150         }
151         if (!((patternNumber & 128) && (patternNumber & 64) && (patternNumber & 16))) {
152             patternNumber &= 127;
153         }
154 
155         if (patternTable.hasOwnProperty(patternNumber)) {
156             var ret = own * 68 + patternTable[patternNumber];
157         } else {
158             var ret = -1;
159             console.log('this._typeData[' + index + '][' + y + '][' + x + '] = ' + patternNumber + ' // undefined');
160         }
161         return ret;
162     },
163     match: function(ind1, ind2) {
164         var i = 0;
165         while (i < 1024) {
166             if (this._types[ind1].baseType[i] ^ this._types[ind2].parentType[i]) {
167                 return false;
168             } else {
169                 i++;
170             }
171         }
172         return true;
173     },
174     loadTypeData: function(data) {
175         this._typeData = Array.prototype.slice.apply(arguments);
176         this._dirty = true;
177         this.type2data();
178         var c = 0;
179         for (var index = 0, l = this._data.length; index < l; index++) {
180             for (var y = 0, ll = this._data[0].length; y < ll; y++) {
181                 for (var x = 0, lll = this._data[0][0].length; x < lll; x++) {
182                     if (this._typeData[index][y][x] > 7) {
183                         this._typeData[index][y][x] = -1;
184                     }
185                     if (this._data[index][y][x] >= 0) {
186                         c++;
187                     }
188                 }
189             }
190         }
191         if (c / (data.length * data[0].length) > 0.2) {
192             this._tight = true;
193         } else {
194             this._tight = false;
195         }
196     },
197     loadData: function(data) {
198         this._data = Array.prototype.slice.apply(arguments);
199         this._dirty = true;
200         this.data2type();
201         var c = 0;
202         for (var index = 0, l = this._data.length; index < l; index++) {
203             for (var y = 0, ll = this._data[0].length; y < ll; y++) {
204                 for (var x = 0, lll = this._data[0][0].length; x < lll; x++) {
205                     if (this._data[index][y][x] >= 0) {
206                         c++;
207                     }
208                 }
209             }
210         }
211         if (c / (data.length * data[0].length) > 0.2) {
212             this._tight = true;
213         } else {
214             this._tight = false;
215         }
216     },
217     image: {
218         get: function() {
219             return this._image;
220         },
221         set: function(image) {
222             var img = image.clone();
223             var core = enchant.Core.instance;
224             var surface = new Surface(272, 512);
225             var Type = function(image, left, top, tileWidth, tileHeight) {
226                 this.baseType = [];
227                 this.parentType = [];
228                 this.baseType = image.context.getImageData(left, top, tileWidth, tileHeight).data;
229                 this.parentType = image.context.getImageData(left + tileWidth, top, tileWidth, tileHeight).data;
230             };
231             var extract = function(left, top, sx, sy) {
232                 var params = [
233                     [  0, 16, 48, 8, 16, 0, 48, 8 ],
234                     [  0, 56, 48, 8, 16, 8, 48, 8 ],
235                     [  0, 16, 8, 48, 48, 16, 8, 48 ],
236                     [ 40, 16, 8, 48, 56, 16, 8, 48 ],
237                     [  0, 16, 48, 48, 0, 16, 48, 48 ],
238                     [ 40, 8, 8, 8, 8, 24, 8, 8 ],
239                     [ 40, 0, 8, 8, 8, 48, 8, 8 ],
240                     [ 32, 8, 8, 8, 32, 24, 8, 8 ],
241                     [ 32, 0, 8, 8, 32, 48, 8, 8 ],
242                     [ 40, 0, 8, 8, 8, 32, 8, 8 ],
243                     [ 40, 8, 8, 8, 8, 40, 8, 8 ],
244                     [ 32, 0, 8, 8, 32, 32, 8, 8 ],
245                     [ 32, 8, 8, 8, 32, 40, 8, 8 ],
246                     [ 32, 8, 8, 8, 16, 24, 8, 8 ],
247                     [ 40, 8, 8, 8, 24, 24, 8, 8 ],
248                     [ 40, 0, 8, 8, 24, 48, 8, 8 ],
249                     [ 32, 0, 8, 8, 16, 48, 8, 8 ],
250                     [ 32, 0, 16, 16, 112, 16, 16, 16 ],
251                     [ 32, 0, 16, 16, 112, 32, 16, 16 ],
252                     [ 32, 0, 16, 16, 112, 48, 16, 16 ],
253                     [ 32, 0, 16, 16, 128, 16, 16, 16 ],
254                     [ 32, 0, 16, 16, 128, 48, 16, 16 ],
255                     [ 32, 0, 16, 16, 144, 16, 16, 16 ],
256                     [ 32, 0, 16, 16, 144, 32, 16, 16 ],
257                     [ 32, 0, 16, 16, 144, 48, 16, 16 ],
258                     [ 24, 32, 8, 8, 120, 48, 8, 8 ],
259                     [ 24, 40, 8, 8, 120, 24, 8, 8 ],
260                     [ 16, 32, 8, 8, 144, 48, 8, 8 ],
261                     [ 16, 40, 8, 8, 144, 24, 8, 8 ],
262                     [ 16, 40, 16, 8, 128, 24, 16, 8 ],
263                     [ 16, 32, 16, 8, 128, 48, 16, 8 ],
264                     [ 24, 32, 8, 16, 120, 32, 8, 16 ],
265                     [ 16, 32, 8, 16, 144, 32, 8, 16 ],
266                     [  0, 16, 8, 8, 128, 32, 8, 8 ],
267                     [ 40, 16, 8, 8, 136, 32, 8, 8 ],
268                     [  0, 56, 8, 8, 128, 40, 8, 8 ],
269                     [ 40, 56, 8, 8, 136, 40, 8, 8 ],
270                     [ 16, 32, 16, 16, 64, 0, 16, 16 ],
271                     [ 40, 0, 8, 8, 72, 0, 8, 8 ],
272                     [ 16, 32, 16, 16, 80, 0, 16, 16 ],
273                     [ 32, 0, 8, 8, 80, 0, 8, 8 ],
274                     [ 16, 32, 16, 16, 96, 0, 16, 16 ],
275                     [ 40, 8, 8, 8, 104, 8, 8, 8 ],
276                     [ 16, 32, 16, 16, 112, 0, 16, 16 ],
277                     [ 32, 8, 8, 8, 112, 8, 8, 8 ],
278                     [ 16, 32, 16, 16, 128, 0, 16, 16 ],
279                     [ 40, 0, 8, 8, 136, 0, 8, 8 ],
280                     [ 32, 8, 8, 8, 128, 8, 8, 8 ],
281                     [ 16, 32, 16, 16, 144, 0, 16, 16 ],
282                     [ 32, 0, 8, 8, 144, 0, 8, 8 ],
283                     [ 40, 8, 8, 8, 152, 8, 8, 8 ],
284                     [ 32, 32, 16, 16, 160, 0, 16, 16 ],
285                     [ 32, 32, 16, 16, 160, 16, 16, 16 ],
286                     [ 32, 8, 8, 8, 160, 8, 8, 8 ],
287                     [ 32, 0, 8, 8, 160, 16, 8, 8 ],
288                     [  0, 32, 16, 16, 176, 0, 16, 16 ],
289                     [  0, 32, 16, 16, 176, 16, 16, 16 ],
290                     [ 40, 8, 8, 8, 184, 8, 8, 8 ],
291                     [ 40, 0, 8, 8, 184, 16, 8, 8 ],
292                     [  8, 48, 16, 16, 160, 32, 16, 16 ],
293                     [  8, 48, 16, 16, 176, 32, 16, 16 ],
294                     [ 40, 0, 8, 8, 168, 32, 8, 8 ],
295                     [ 32, 0, 8, 8, 176, 32, 8, 8 ],
296                     [  8, 16, 16, 16, 160, 48, 16, 16 ],
297                     [  8, 16, 16, 16, 176, 48, 16, 16 ],
298                     [ 40, 8, 8, 8, 168, 56, 8, 8 ],
299                     [ 32, 8, 8, 8, 176, 56, 8, 8 ],
300                     [  0, 0, 16, 16, 0, 0, 16, 16 ],
301                     [  0, 16, 48, 48, 64, 16, 48, 48 ],
302                     [ 32, 0, 16, 16, 16, 32, 16, 16 ]
303                 ];
304                 for (var i = 0, l = params.length; i < l; i++) {
305                     params[i][0] += left;
306                     params[i][1] += top;
307                     params[i][4] += sx;
308                     params[i][5] += sy;
309                     params[i].unshift(image);
310                     surface.draw.apply(surface, params[i]);
311                 }
312             };
313 
314             // イメージの展開
315             surface.draw(image, 96, 0, 80, 256, 192, 0, 80, 256);
316             surface.draw(image, 176, 0, 80, 256, 192, 256, 80, 256);
317             for (var y = 0; y < 4; y++) {
318                 for (var x = 0; x < 2; x++) {
319                     var left = x * 48;
320                     var top = y * 64;
321                     extract(left, top, 0, (x + y * 2) * 64);
322                 }
323             }
324 
325             this._image = surface;
326             this._types = new Array();
327 
328             for (var y = 0; y < 4; y++) {
329                 for (var x = 0; x < 2; x++) {
330                     var left = x * 48;
331                     var top = y * 64;
332                     this._types[x + y * 2] = new Type(img, left, top, this.tileWidth, this.tileHeight);
333                 }
334             }
335             for (var i = 0; i < 8; i++) {
336                 for (var j = 0; j < 8; j++) {
337                     if (this.match(i, j)) {
338                         this._types[j].parentNum = i;
339                     }
340                 }
341                 if (this._types[i].parentNum == undefined) {
342                     this._types[i].parentNum = i;
343                 }
344                 this._types[i].baseNum = i;
345             }
346 
347             /*
348              if (RETINA_DISPLAY && core.scale == 2) {
349              var img = new Surface(image.width * 2, image.height * 2);
350              var tileWidth = this._tileWidth || image.width;
351              var tileHeight = this._tileHeight || image.height;
352              var row = image.width / tileWidth | 0;
353              var col = image.height / tileHeight | 0;
354              for (var y = 0; y < col; y++) {
355              for (var x = 0; x < row; x++) {
356              img.draw(image, x * tileWidth, y * tileHeight, tileWidth, tileHeight,
357              x * tileWidth * 2, y * tileHeight * 2, tileWidth * 2, tileHeight * 2);
358              }
359              }
360              this._doubledImage = img;
361              }
362              this._dirty = true;
363              */
364         }
365     }
366 });
367