1 /**
  2  * @fileOverview
  3  * avatar.enchant.js
  4  * @version 0.2 (2012/08/09)
  5  * @requires enchant.js v0.4.0 or later
  6  * @author shi3z/UEI Corporation
  7  *
  8  * @description
  9  * enchant.js extension for 9leap.net
 10  * Avatar character plugin for enchant.js with 9leap.net
 11  *
 12  * @example
 13  * var core = new Core(320, 320);
 14  * core.preload('avatarBg1.png', 'avatarBg2.png', 'avatarBg3.png', 'bigmonster1.gif');
 15  * core.onload = function(){
 16  *     core.rootScene.backgroundColor = "#000000";
 17  *     // show infinite scrolling background
 18  *     bg = new AvatarBG(0);
 19  *     bg.y = 50;
 20  *     core.rootScene.addChild(bg);
 21  *     
 22  *     // show monster
 23  *     monster = new AvatarMonster(core.assets['bigmonster1.gif']);
 24  *     monster.x = 200;
 25  *     monster.y = 100;
 26  *     core.rootScene.addChild(monster);
 27  *     
 28  *     // show avatar
 29  *     chara = new Avatar("2:2:1:2004:21230:22480");
 30  *     core.rootScene.addChild(chara);
 31  *     chara.scaleX = -1;
 32  *     chara.scaleY = 1;
 33  *     chara.x = 50;
 34  *     chara.y = 100;
 35  * };
 36  * core.start();
 37  */
 38 
 39 /**
 40  * avatar namespace object
 41  * @type {Object}
 42  */
 43 enchant.avatar = {};
 44 
 45 /**
 46  * AvatarCharacter
 47  * Base class of enchant.Avatar and enchant.AvatarMonster
 48  * @scope enchant.avatar.AvatarCharacter.prototype
 49  */
 50 enchant.avatar.AvatarCharacter = enchant.Class.create(enchant.Sprite, {
 51     /**
 52      * Constructor of AvatarCharacter
 53      * @constructs
 54      * @param width
 55      * @param height
 56      */
 57     initialize: function(width, height) {
 58         enchant.Sprite.call(this, width, height);
 59         this.right();
 60 
 61         /**
 62          * Name of animation pattern;
 63          * @type {String}
 64          */
 65         this.action = "stop";
 66 
 67         /**
 68          * Array of animation patterns
 69          * @type {Object}
 70          */
 71         this.animPattern = { "stop": [ 0]};
 72 
 73         /**
 74          * Frame number of animation
 75          * @type {Object}
 76          */
 77         this.animFrame = 0;
 78         this.addEventListener('enterframe', function() {
 79             if ((~~(this.age) & 0x03) !== 0)return;
 80             if (this.action) {
 81                 var animPattern = this.animPattern[this.action];
 82                 this.frame = animPattern[this.animFrame];
 83                 this.animFrame++;
 84                 if (animPattern[this.animFrame] === -1) {
 85                     this.animFrame = 0;
 86                     this.action = "stop";
 87                 }
 88                 if (animPattern[this.animFrame] === -2) {
 89                     this.parentNode.removeChild(this);
 90                 }
 91                 if (this.animFrame >= animPattern.length) {
 92                     this.animFrame = 0;
 93                 }
 94             }
 95         });
 96     },
 97 
 98     /**
 99      * Flip to left
100      */
101     left: function() {
102         this.scaleX = 1;
103     },
104     /**
105      * Flip to right
106      */
107     right: function() {
108         this.scaleX = -1;
109     }
110 });
111 
112 
113 /**
114  * AvatarMonster
115  * subclass of enchant.avatar.AvatarCharacter
116  * @scope enchant.avatar.AvatarMonster.prototype
117  */
118 enchant.avatar.AvatarMonster = enchant.Class.create(enchant.avatar.AvatarCharacter, {
119     /**
120      * AvatarMonster
121      * Manage a monter animations
122      * @constructs
123      * @param {image} Image of monster
124      * @extends enchant.avatar.AvatarCharacter
125      */
126     initialize: function(image) {
127         var w = ~~(image.width / 4);
128         var h = w;
129         enchant.avatar.AvatarCharacter.call(this, w, h);
130         this.image = image;
131         this.animPattern = { "stop": [ 4, 4, 4, 3, 3, 3],
132             "walk": [ 2, 3, 4, 3],
133             "appear": [ 0, 1, 7, 6, 5, 4, 2, 3, -1],
134             "disappear": [ 3, 2, 4, 5, 6, 7, 1, 0, -2],
135             "attack": [ 5, 4, 6, 6, 6, -1]
136         };
137         this.action = "attack";
138         this.left();
139     }
140 });
141 
142 /**
143  * AvatarBG
144  * @scope enchant.AvatarBG.prototype
145  */
146 enchant.avatar.AvatarBG = enchant.Class.create(enchant.Group, {
147     /**
148      * A class of infinite scrolling background.
149      * @param mode {Number} 0 to 3
150      * @constructs
151      * @extends enchant.Group
152      */
153     initialize: function(mode) {
154         enchant.Group.call(this);
155         var core = enchant.Core.instance;
156 
157         this.veryfarbg = new enchant.Sprite(320, 51);
158         this.veryfarbg.y = 0;
159         this.veryfarbg.image = core.assets["avatarBg2.png"];
160         this.veryfarbg.frame = mode;
161         this.addChild(this.veryfarbg);
162         this.farbgs = [];
163         for (i = 0; i < 3; i++) {
164             var bg = new enchant.Sprite(320, 32);
165             bg.image = core.assets["avatarBg3.png"];
166             bg.frame = mode;
167             bg.x = i * 320 - 320;
168             bg.y = 20;
169             this.farbgs[this.farbgs.length] = bg;
170             this.addChild(bg);
171         }
172 
173         this.tiles = [];
174         for (i = 0; i < 14; i++) {
175             var tile = new enchant.Sprite(32, 128);
176             tile.image = core.assets["avatarBg1.png"];
177             tile.frame = mode;
178             tile.x = i * 31 - 48;
179             tile.y = 48;
180             this.tiles[this.tiles.length] = tile;
181             this.addChild(tile);
182         }
183     },
184     /**
185      * horizontal scrolling.
186      * @param {int} x Offset of x coordinate.
187      */
188     scroll: function(x) {
189         var dx = ~~(x / 2) % 320, ddx = ~~(x) % 32 * 2;
190         for (i = 0; i < 14; i++) {
191             this.tiles[i].x = i * 31 - ddx - 48;
192         }
193         for (i = 0; i < 3; i++) {
194             this.farbgs[i].x = i * 320 - dx - 320;
195         }
196     }
197 });
198 
199 /**
200  * Avatar
201  * @scope enchant.Avatar.prototype
202  */
203 enchant.avatar.Avatar = enchant.Class.create(enchant.avatar.AvatarCharacter, {
204     /**
205      * @param {int}code  Avatar code
206      * @extends enchant.avatar.AvatarCharacter
207      * @constructs
208      */
209     initialize: function(code) {
210         enchant.avatar.AvatarCharacter.call(this, 64, 64);
211         if (code) {
212             this.setCode(code);
213         } else {
214             this.gender = 1;
215             this.hairstyle = 1;
216             this.haircolor = 0;
217             this.weapon = 2545;
218             this.armor = 2135;
219             this.headpiece = 0;
220             this.loadImage();
221         }
222         this.animPattern = { "stop": [ 0],
223             "run": [ 1, 2, 3, 2],
224             "attack": [ 0, 2, 9, 10, 11, 5, 0, 0, 0, -1],
225             "special": [ 0, 6, 5, 13, 14, 15, 0, 0, -1],
226             "damage": [ 7, 7, 7, 7, 0, 0, 0, -1],
227             "dead": [8],
228             "demo": [ 1, 2, 3, 2, 1, 2, 3, 2, 1, 2, 3, 2, 1, 2, 3, 2, 1, 2, 3, 2, 1, 2, 3, 2, 0, 0, 0, 0, 2, 9, 10, 11, 5, 0, 0, 0,
229                 1, 2, 3, 2, 1, 2, 3, 2, 1, 2, 3, 2, 1, 2, 3, 2, 1, 2, 3, 2, 0, 6, 5, 13, 14, 15, 0, 0]
230         };
231 
232 
233     },
234     /**
235      * Reflesh avatar animation image
236      */
237     loadImage: function() {
238         var ___EnchantAvatarServerURL = "http://9leap.net/meruru/draw/draw.php";
239         this.opt = "gender=" + this.gender + "&job=0&hairstyle=" + this.hairstyle + "&haircolor=" + this.haircolor + "&weapon=" + this.weapon + "&armor=" + this.armor + "&headpiece=" + this.headpiece + "&invisible=0&x=0&y=0&w=4&h=4&dummy=.gif";
240         this.src = ___EnchantAvatarServerURL + "?" + this.opt;
241         (function(that) {
242             var core = enchant.Core.instance;
243             core.load(that.src, function() {
244                 that.image = core.assets[that.src];
245             });
246         })(this);
247     },
248     /**
249      * Get avatar code from actual object
250      * @return {String} code;
251      */
252     getCode: function() {
253         return this.gender + ":" + this.hairstyle + ":" + this.haircolor + ":" +
254             this.weapon + ":" + this.armor + ":" + this.headpiece;
255     },
256     /**
257      * Set avatar code and reflesh avatar image.
258      * @param {String} code
259      */
260     setCode: function(code) {
261         data = code.split(":");
262         this.gender = data[0];
263         this.hairstyle = data[1];
264         this.haircolor = data[2];
265         this.weapon = data[3];
266         this.armor = data[4];
267         this.headpiece = data[5];
268         this.loadImage();
269     }
270 });
271