1 /**
  2  * @fileOverview
  3  * nineleap.enchant.js
  4  * @version 0.3.4 (2013/04/03)
  5  * @requires enchant.js v0.6.3 or later
  6  *
  7  * @description
  8  * enchant.js extension for 9leap.net
  9  * 9leap.net 向けの enchant.js 拡張プラグイン。
 10  * core.end の引数にスコアと結果の文字列を渡すことで、ランキングに登録できる。
 11  * (9leapにアップロードした後のみランキング画面にジャンプする)
 12  *
 13  * @usage
 14  * 
 15  * var core = new Core(320, 320);
 16  * 
 17  * core.onload = function(){
 18  * // executed after player pushed "START"
 19  * // ...
 20  * if(some.condition)core.end(score, result);
 21  * };
 22  * 
 23  * core.start();
 24  */
 25 
 26 (function() {
 27 
 28     /**
 29      * @type {Object}
 30      */
 31     enchant.nineleap = { assets: ['start.png', 'end.png'] };
 32 
 33     /**
 34      * @scope enchant.nineleap.Core.prototype
 35      */
 36     enchant.nineleap.Core = enchant.Class.create(enchant.Core, {
 37         /**
 38          * start, gameover の画像を表示し、
 39          * 最後にスコアを送信するように拡張された Core クラス
 40          * @param width
 41          * @param height
 42          * @constructs
 43          */
 44         initialize: function(width, height) {
 45             enchant.Core.call(this, width, height);
 46             this.addEventListener('load', function() {
 47                 var core = this;
 48                 this.startScene = new enchant.nineleap.SplashScene();
 49                 this.startScene.image = this.assets['start.png'];
 50                 this.startScene.addEventListener('touchend', function() {
 51                     if (core.started === false) {
 52                         if (core.onstart != null) {
 53                             core.onstart();
 54                         }
 55                         core.started = true;
 56                         coreStart = true;   // deprecated
 57                     }
 58                     if (core.currentScene === this) {
 59                         core.popScene();
 60                     }
 61                     this.removeEventListener('touchend', arguments.callee);
 62                 });
 63                 this.addEventListener('keydown', function() {
 64                     if (this.started === false) {
 65                         if (this.onstart != null) {
 66                             this.onstart();
 67                         }
 68                         this.started = true;
 69                     }
 70                     if (core.currentScene === core.startScene) {
 71                         core.popScene();
 72                     }
 73                     this.removeEventListener('keydown', arguments.callee);
 74                 });
 75                 this.pushScene(this.startScene);
 76 
 77                 this.endScene = new enchant.nineleap.SplashScene();
 78                 this.endScene.image = this.assets['end.png'];
 79                 this.removeEventListener('load', arguments.callee);
 80             });
 81             this.scoreQueue = false;
 82             this.started = false;
 83             coreStart = false; // deprecated
 84         },
 85 
 86         _requestPreload: function() {
 87             var o = {};
 88             var loaded = 0,
 89                 len = 0,
 90                 loadFunc = function() {
 91                     var e = new enchant.Event('progress');
 92                     e.loaded = ++loaded;
 93                     e.total = len;
 94                     enchant.Core.instance.loadingScene.dispatchEvent(e);
 95                 };
 96             this._assets
 97                 .concat(this._twitterAssets || [])
 98                 .concat(this._netpriceData || [])
 99                 .concat(this._memoryAssets || [])
100                 .reverse()
101                 .forEach(function(asset) {
102                     var src, name;
103                     if (asset instanceof Array) {
104                         src = asset[0];
105                         name = asset[1];
106                     } else {
107                         src = name = asset;
108                     }
109                     if (!o[name]) {
110                         o[name] = this.load(src, name, loadFunc);
111                         len++;
112                     }
113                 }, this);
114 
115             this.pushScene(this.loadingScene);
116             return enchant.Deferred.parallel(o);
117         },
118 
119         end: function(score, result, img) {
120             if (img !== undefined) {
121                 this.endScene.image = img;
122             }
123             this.pushScene(this.endScene);
124             if (location.hostname === 'r.jsgames.jp') {
125                 var submit = function() {
126                     var id = location.pathname.match(/^\/games\/(\d+)/)[1];
127                     location.replace([
128                         'http://9leap.net/games/', id, '/result',
129                         '?score=', encodeURIComponent(score),
130                         '&result=', encodeURIComponent(result)
131                     ].join(''));
132                 };
133                 this.endScene.addEventListener('touchend', submit);
134                 window.setTimeout(submit, 3000);
135             }
136             enchant.Core.instance.end = function() {
137             };
138         }
139     });
140 
141     /**
142      * @scope enchant.nineleap.SplashScene.prototype
143      */
144     enchant.nineleap.SplashScene = enchant.Class.create(enchant.Scene, {
145         /**
146          * @extends enchant.Scene
147          * @constructs
148          * スプラッシュ画像を表示するシーン。
149          */
150         initialize: function() {
151             enchant.Scene.call(this);
152         },
153 
154         /**
155          * 中央に表示する画像
156          * @type {enchant.Surface}
157          */
158         image: {
159             get: function() {
160                 return this._image;
161             },
162             set: function(image) {
163                 this._image = image;
164 
165                 // discard all child nodes
166                 while (this.firstChild) {
167                     this.removeChild(this.firstChild);
168                 }
169 
170                 // generate an Sprite object and put it on center
171                 var sprite = new enchant.Sprite(image.width, image.height);
172                 sprite.image = image;
173                 sprite.x = (this.width - image.width) / 2;
174                 sprite.y = (this.height - image.height) / 2;
175                 this.addChild(sprite);
176             }
177         }
178     });
179 
180 })();
181