OSDN Git Service

add to face image for yukkuri
[yukkurioverwint/YukkuriOverwinter.git] / main.js
1 enchant();
2 //////////////////////////////////////
3 //global
4 var ctl = null;
5 var mangIcon = null;
6 var mangTouch = null;
7 var mangLabel = null;
8 var mangMsg = null;
9 var LabelGroup = enchant.Class.mixClasses(Label, Group,true);
10 var net = new Net();
11 //////////////////////////////////////
12 //define
13 //////////////////////////////////////
14 var PALYER_INIT_X = 100;
15 var PALYER_INIT_Y = 100;
16 var CHARA_WIDTH = 64;
17 var CHARA_HEIGHT = 64;
18 var GAME_WIDTH = 800;
19 var GAME_HEIGHT = 600;
20 var FPS = 20;
21 var HUNGRY_MAX = 100;
22
23 var EResPath = {
24         YUKKURI_BASE : './data/yukkuri_base.png',
25         YUKKURI_SHADOW : './data/shadow.png',
26         MARISA_FACE_NORMAL : './data/marisa/face_normal1.png',
27         MARISA_FACE_TIRED : './data/marisa/face_tired1.png',
28         MARISA_FACE_ANGRY : './data/marisa/face_angry1.png',
29         MARISA_FACE_CRY1 : './data/marisa/face_cry1.png',
30         MARISA_FACE_CRY2 : './data/marisa/face_cry2.png',
31         MARISA_FACE_EAT1 : './data/marisa/face_eat1.png',
32         MARISA_FACE_EAT2 : './data/marisa/face_eat2.png',
33         MARISA_FACE_HAPPY1 : './data/marisa/face_happy1.png',
34         MARISA_FACE_HAPPY2 : './data/marisa/face_happy2.png',
35         MARISA_FACE_SLEEP : './data/marisa/face_sleep1.png',
36         MARISA_HEAR : './data/marisa/hear1.png',
37         OBJECT : './data/object.png',
38         MAP0 : './data/map0.png',
39         COMMAND : './data/command.png',
40         OKAZARI : './data/okazari.png',
41 };
42 var EFace = {
43         NORMAL:0,
44         TIRED:1,
45         ANGRY:2,
46         CRY1:3,
47         CRY2:4,
48         EAT1:5,
49         EAT2:6,
50         HAPPY1:7,
51         HAPPY2:8,
52         SLEEP:9,
53 };
54 var ECommand = {
55         WORLD_CURSOR: 0,
56         WORLD_TARGET: 1,
57         WORLD_APPLE: 2,
58 };
59
60 var EMenuPos = {
61         X : 600,
62         Y : 0,
63         WIDTH : 200,
64         HEIGHT : GAME_HEIGHT,
65 };
66 var EFieldPos = {
67         X : 0,
68         Y : 0,
69         WIDTH: GAME_WIDTH - EMenuPos.WIDTH,
70         HEIGHT: GAME_HEIGHT,
71 };
72 var EAction = {
73         WAIT : 0,
74         SLEEP : 1,
75         HUNT : 3,
76         THINK : 4,
77         WALK : 5,
78         DEAD: 6,
79         EAT_START : 8,
80         EAT_WAIT : 9,
81         MOVE_TO_EAT_START: 10,
82         MOVE_TO_EAT_UNDERWAY: 11,
83         MOVE_TO_COMMAND: 12,
84         NONE: 9999
85 };
86 var EDirection = {
87         LEFT : 0,
88         RIGHT: 1,
89 };
90 var EMsg = {
91         WALK: 0,
92         SLEEP: 1,
93         EAT: 2,
94 };
95 window.onload = function(){
96         //init game
97         var def = new Object();
98         var foodGroup = new Group();
99         var SpriteGroup = enchant.Class.mixClasses(Sprite, Group,true);
100         var menuBg = new SpriteGroup(EMenuPos.WIDTH, EMenuPos.HEIGHT);
101         var fieldBg = new enchant.Sprite(EFieldPos.WIDTH, EFieldPos.HEIGHT);
102         var game = new Game(GAME_WIDTH, GAME_HEIGHT);
103         game.fps = FPS;
104         var _loadArr = [];
105         var i=0;
106         for(var _v in EResPath){
107                 _loadArr[i] = EResPath[_v];
108                 i++;
109         }
110         game.preload(_loadArr);
111         var MapGroup = enchant.Class.mixClasses(Map, Group,true);
112         var backgroundMap = new MapGroup(16, 16);
113         include("./class.js");
114         include("./ctrl.js");
115         ctl = new Ctrl();
116         ctl.init(game);
117         ctl.setBackgroundMap(backgroundMap);
118         var Action = enchant.Class.create({
119                 initialize: function (yukkuri){
120                         this.status = EAction.THINK;
121                         this.yukkuri = yukkuri;
122                         this.isMoving = false;
123                         this.targetNode = null;
124                 },
125                 setStatus: function(eaction){
126                         this.status = eaction;
127                 },
128                 observe: function(){
129                         if(this.yukkuri.isDead()){
130                                 this.status = EAction.DEAD;
131                         }
132                         if(this.yukkuri.isSleep()){
133                                 this.status = EAction.SLEEP;
134                         }
135
136                 },
137                 act : function(){
138                         this.observe();
139                         switch(this.status){
140                                 case EAction.THINK:
141                                         if(this.yukkuri.age%2 === 0){
142                                                 this.yukkuri.changeFace();
143                                                 this.search();
144                                         }
145                                 break;
146                                 case EAction.MOVE_TO_EAT_START:
147                                         this.move_to_eat_start();
148                                 break;
149                                 case EAction.MOVE_TO_EAT_UNDERWAY:
150                                 break;
151                                 //Random Walk
152                                 case EAction.WALK:
153                                         this.walk(0);
154                                 break;
155                                 case EAction.SLEEP:
156                                         this.sleep();
157                                 break;
158                                 case EAction.DEAD:
159                                         this.dead();
160                                 break;
161                                 case EAction.EAT_START:
162                                         this.eat_start();
163                                 break;
164                                 case EAction.EAT_WAIT:
165                                 break;
166                                 case EAction.MOVE_TO_COMMAND:
167                                         this.move_to_command();
168                                 break;
169                         }
170                 },
171
172                 move_to_eat_start : function(){
173                         new MoveToEatEvent({
174                                 "type": 'food',
175                                 "targetNode": this.targetNode,
176                                 "action": this,
177                                 "myYukkuri": this.yukkuri
178                         });
179                 },
180                 walk : function(retryCounter){
181                         if(retryCounter > 15){
182                                 retryCounter = 0;
183                                 this.status = EAction.NONE;
184                                 console.log("retryCount over");
185                                 return;
186                         }
187                         if(this.yukkuri.imgGroup.tl.queue.length === 0){
188                                 var frame = 200;
189                                 if(this.isMoving){
190                                         this.isMoving = false;
191                                         this.status = EAction.THINK;
192                                         this.yukkuri.vx = 0;
193                                         this.yukkuri.vy = 0;
194                                 }else{
195                                         // this.isMoving = true;
196                                         var xpos = 150 - (retryCounter * 10);
197                                         var ypos = 150 - (retryCounter * 10);
198                                         if(rand(8) === 0){
199                                                 this.yukkuri.vx = 0;
200                                                 this.yukkuri.vy = -ypos;
201                                         }
202                                         else if(rand(8) == 1){
203                                                 this.yukkuri.vx = xpos;
204                                                 this.yukkuri.vy = -ypos;
205                                                 this.yukkuri.direction = EDirection.RIGHT;
206                                         }
207                                         else if(rand(8) == 2){
208                                                 this.yukkuri.vx = xpos;
209                                                 this.yukkuri.vy = 0;
210                                                 this.yukkuri.direction = EDirection.RIGHT;
211                                         }
212                                         else if(rand(8) == 3){
213                                                 this.yukkuri.vx = xpos;
214                                                 this.yukkuri.vy = ypos;
215                                                 this.yukkuri.direction = EDirection.RIGHT;
216                                         }
217                                         else if(rand(8) == 4){
218                                                 this.yukkuri.vx = 0;
219                                                 this.yukkuri.vy = ypos;
220                                         }
221                                         else if(rand(8) == 5){
222                                                 this.yukkuri.vx = -xpos;
223                                                 this.yukkuri.vy = ypos;
224                                                 this.yukkuri.direction = EDirection.LEFT;
225                                         }
226                                         else if(rand(8) == 6){
227                                                 this.yukkuri.vx = -xpos;
228                                                 this.yukkuri.vy = 0;
229                                                 this.yukkuri.direction = EDirection.LEFT;
230                                         }
231                                         else if(rand(8) == 7){
232                                                 this.yukkuri.vx = -xpos;
233                                                 this.yukkuri.vy = -ypos;
234                                                 this.yukkuri.direction = EDirection.LEFT;
235                                         }
236                                         if (this.yukkuri.vx || this.yukkuri.vy) {
237                                                 var map = ctl.backgroundMap;
238                                                 // var x = this.yukkuri.x + (this.yukkuri.moveX ? this.yukkuri.moveX / Math.abs(this.yukkuri.moveX) * 16 : 0) + 16;
239                                                 // var y = this.yukkuri.y + (this.yukkuri.moveY ? this.yukkuri.moveY / Math.abs(this.yukkuri.moveY) * 16 : 0) + 16;
240                                                 var x = this.yukkuri.imgGroup.x + this.yukkuri.vx;
241                                                 var y = this.yukkuri.imgGroup.y + this.yukkuri.vy + this.yukkuri.height / 2;
242                                                 if (0 <= x && x < map.width && 0 <= y && y < map.height && !map.hitTest(x, y)) {
243                                                         // console.log("あたってないよ:"+ this.yukkuri.imgGroup.x + ":" + this.yukkuri.imgGroup.y);
244                                                         // console.log("X:" + this.yukkuri.moveX);
245                                                         // console.log("Y:" + this.yukkuri.moveY);
246                                                         this.isMoving = true;
247                                                         this.yukkuri.reverse();
248                                                         this.yukkuri.moveBy(this.yukkuri.vx, this.yukkuri.vy, frame);
249                                                         mangMsg.output(this.yukkuri,EMsg.WALK);
250                                                 }else{
251                                                         // console.log("HIT:"+ this.yukkuri.imgGroup.x + ":" + this.yukkuri.imgGroup.y);
252                                                         this.walk(retryCounter+1);
253                                                 }
254                                         }
255                                 }
256                         }
257                 },
258                 sleep: function(){
259                         var yukkuri = this.yukkuri;
260
261                         if(yukkuri.isSleeping){
262                                 if(yukkuri.age%4 === 0 && yukkuri.age !== 0)yukkuri.param.sleep--;
263                                 if(yukkuri.param.sleep <= 0){
264                                         yukkuri.param.sleep = 0;
265                                         yukkuri.isSleeping = false;
266                                         yukkuri.tweet("ゆっくりおきるよ!");
267                                         this.status = EAction.THINK;
268                                         yukkuri.imgBody.tl.resume();
269                                         yukkuri.imgGroup.tl.resume();
270                                         yukkuri.animation();
271                                         yukkuri.face.image = game.assets[EResPath.MARISA_FACE_NORMAL];
272                                 }
273                         }
274                 },
275                 dead : function(){
276                         this.yukkuri.tweet("もっとゆっくりしたかった…");
277                         this.yukkuri.imgBody.tl.clear();
278                         this.yukkuri.imgBody.tl.pause();
279                         this.yukkuri.imgGroup.tl.clear();
280                         this.yukkuri.imgGroup.tl.pause();
281                 },
282                 eat_start: function(){
283                         new EatEvent({
284                                 "type": 'eat',
285                                 "targetNode": this.targetNode,
286                                 "action": this,
287                                 "myYukkuri": this.yukkuri
288                         });
289
290                 },
291                 move_to_command: function(){
292
293                 },
294                 search : function(){
295                         var nodes = ctl.getObjs();
296                         // l = game.rootScene.childNodes.length;
297                         // l = game.rootScene.childNodes.length;
298                         l = nodes.length;
299                         // for (var i = 0;  i < l; i++) {
300                         for (var key in nodes) {
301                                 var node = nodes[key];
302                                 if (node instanceof Food) {
303                                         if(this.yukkuri.isKuhuku() && this.yukkuri.within(node, this.yukkuri.getRange())){
304
305                                                 //A yukkuri to go to the food area
306                                                 this.targetNode = node;
307                                                 this.setStatus(EAction.MOVE_TO_EAT_START);
308
309                                                 return;
310                                         }else{
311                                                 //not hunbry or not food.
312                                         }
313                                 }
314                         }
315                         this.status =  EAction.WALK;
316                 }
317         });
318         var Event = enchant.Class.create({
319                 initialize: function (_data){
320                 },
321                 onactionstart:function(self){
322                         return function(e){
323                                 if(typeof e === "undefined")return;
324                         };
325                 },
326                 onactiontick:function(self){
327                         return function(e){
328                                 if(typeof e === "undefined")return;
329                         };
330                 },
331                 onactionend:function(self){
332                         return function(e){
333                                 if(typeof e === "undefined")return;
334                         };
335                 }
336         });
337
338         var EatEvent = enchant.Class.create(Event,{
339                 initialize: function (_data){
340                         this.data = _data;
341                         this.yukkuri = _data.myYukkuri;
342                         this.food = _data.targetNode;
343                         this.action = _data.action;
344                         this.action.setStatus(EAction.EAT_WAIT);
345                         this.yukkuri.tweet("む~しゃむ~しゃ!それなり~");
346                         mangMsg.output(this.yukkuri, EMsg.EAT);
347                         this.yukkuri.setFaceImage(EFace.EAT2);
348                         // this.yukkuri.image = game.assets[EResPath.MARISA_];
349
350                         // var sec = FPS * (this.yukkuri.param.hungry/10 + 3);
351                         var sec = FPS * 3;
352
353                         this.yukkuri.wait(sec, {
354                                 "onactionstart": this.onactionstart(this),
355                                 "onactiontick": this.onactiontick(this),
356                                 "onactionend": this.onactionend(this)
357                         });
358                 },
359                 onactionstart:function(self){
360                         return function(e){
361                                 if(typeof e === "undefined")return;
362                                 self.yukkuri.eat(self.food);
363                         };
364                 },
365                 onactiontick:function(self){
366                         return function(e){
367                                 if(typeof e === "undefined")return;
368                         };
369                 },
370                 onactionend:function(self){
371                         return function(e){
372                                 if(typeof e === "undefined")return;
373                                 if(self.food.getAmount() <= 0 || self.yukkuri.isManpuku()){
374                                         self.action.setStatus(EAction.THINK);
375                                         self.yukkuri.setFaceImage(EFace.NORMAL);
376                                 }else{
377                                         self.action.setStatus(EAction.EAT_START);
378                                 }
379                         };
380                 }
381         });
382         /**
383          * Move Event
384          * -When yukkuri find Food.
385          * @param  _data
386          * type: "food"
387          * targetNode: enchant.Node
388          * action:Action
389          * myYukkuri:Yukkuri
390          */
391         var MoveToEatEvent = enchant.Class.create(Event,{
392                 initialize: function (_data){
393                         this.data = _data;
394                         this.yukkuri = this.data.myYukkuri;
395                         this.action = this.data.action;
396
397                         var yukkuri = this.yukkuri;
398                         var node = this.data.targetNode;
399                         //food distance
400                         if(this.data.type == "food"){
401                                 this.action.status = EAction.MOVE_TO_EAT_UNDERWAY;
402                                 yukkuri.vx = node.x - yukkuri.getX();
403                                 yukkuri.vy = node.y - yukkuri.getY();
404                                 var distance = (Math.abs(yukkuri.vx) + Math.abs(yukkuri.vy)) / 2;
405                                 var frame = distance + distance/5 + 1;
406                                 yukkuri.direction = yukkuri.vx > 0 ? EDirection.RIGHT : EDirection.LEFT;
407
408                                 yukkuri.moveBy(yukkuri.vx, yukkuri.vy, frame, {
409                                         "onactionstart": this.onactionstart(this),
410                                         "onactiontick": this.onactiontick(this),
411                                         "onactionend": this.onactionend(this)
412                                 });
413                         }
414                 },
415                 onactionstart:function(self){
416                         return function(e){
417                                 if(typeof e === "undefined")return;
418                                 console.log("moveEvent onactionstart");
419                                 self.yukkuri.tweet("ゆゆ??\nたべものさん\nはっけんなのぜ!");
420                         };
421                 },
422                 onactiontick:function(self){
423                         return function(e){
424                                 if(typeof e === "undefined")return;
425                         };
426                 },
427                 onactionend:function(self){
428                         return function(e){
429                                 if(typeof e === "undefined")return;
430                                 self.action.setStatus(EAction.EAT_START);
431                         };
432                 }
433         });
434         var MoveCommandEvent = enchant.Class.create(Event,{
435                 initialize: function (_data){
436                         this.data = _data;
437                         this.yukkuri = this.data.myYukkuri;
438                         this.action = this.data.action;
439                         this.vx = this.data.vx;
440                         this.vy = this.data.vy;
441
442                         var yukkuri = this.yukkuri;
443                         this.action.status = EAction.MOVE_TO_EAT_UNDERWAY;
444                         yukkuri.vx = node.x - yukkuri.getX();
445                         yukkuri.vy = node.y - yukkuri.getY();
446                         var distance = (Math.abs(yukkuri.vx) + Math.abs(yukkuri.vy)) / 2;
447                         var frame = distance + 1;
448                         yukkuri.moveBy(yukkuri.vx, yukkuri.vy, frame, {
449                                 "onactionstart": this.onactionstart(this),
450                                 "onactiontick": this.onactiontick(this),
451                                 "onactionend": this.onactionend(this)
452                         });
453                 },
454                 onactionstart:function(self){
455                         return function(e){
456                                 if(typeof e === "undefined")return;
457                         };
458                 },
459                 onactiontick:function(self){
460                         return function(e){
461                                 if(typeof e === "undefined")return;
462                         };
463                 },
464                 onactionend:function(self){
465                         return function(e){
466                                 if(typeof e === "undefined")return;
467                                 self.action.setStatus(EAction.THINK);
468                         };
469                 }
470         });
471
472         var ObjSprite = enchant.Class.create(enchant.Sprite,{
473                 initialize: function (w, h){
474                         enchant.Sprite.call(this, w, h);
475                         this.id = guid();
476                         ctl.addObj(this);
477                 },
478                 removeAll: function(){
479                         this.remove();
480                         ctl.removeObj(this);
481                 }
482         });
483
484         var Food = enchant.Class.create(ObjSprite,{
485                 initialize: function (type, x, y){
486                         this.classname = "Food";
487                         ObjSprite.call(this,16,16);
488                         this.image = game.assets[EResPath.OBJECT];
489                         this.frame = 15;
490                         this.x = x;
491                         this.y = y;
492                         //一つにつきgiveの値分、hunguryを減少させられる
493                         this.give = 40;
494                         this.amount = 3;
495                         // backgroundMap.addChild(this);
496                         foodGroup.addChild(this);
497                         this.addEventListener('enterframe', function(){
498                                 if(this.amount <= 0){
499                                         this.removeAll();
500                                 }
501                         });
502                 },
503                 getAmount: function(){
504                         return this.amount;
505                 },
506                 getGive: function(){
507                         return this.give;
508                 }
509         });
510         var Okazari = enchant.Class.create(enchant.Sprite,{
511                 initialize: function(){
512                         this.classname = "Okazari";
513                         enchant.Sprite.call(this, 64, 64);
514                         this.image = game.assets[EResPath.OKAZARI];
515                         this.x = -CHARA_WIDTH / 2;
516                         this.y = -12 - CHARA_HEIGHT / 2;
517                 }
518         });
519         var Yukkuri = enchant.Class.create(ObjSprite,{
520                 initialize: function(x, y){
521                         ObjSprite.call(this, 64, 64);
522                         this.classname = "Yukkuri";
523                         this.imgGroup = new SpriteGroup();
524                         this.imgBody = new SpriteGroup();
525                         this.imgGroup.id = guid();
526                         this.imgBody.id = guid();
527                         this.x = -CHARA_WIDTH / 2;
528                         this.y = -CHARA_HEIGHT / 2;
529                         this.vx = 0;
530                         this.vy = 0;
531                         this.image = game.assets[EResPath.YUKKURI_BASE];
532                         this.direction = EDirection.RIGHT;
533                         this.addEventListener('enterframe', this.runEnterframe);
534                         this.isSleeping = false;
535                 },
536                 runEnterframe:function(){
537                         this.act();
538                         this.runYukkuri();
539                         this.runHungry();
540                         this.runSleep();
541                 },
542                 reverse:function(){
543                         if(this.direction == EDirection.RIGHT){
544                                 this.imgBody.scaleX = 1;
545                         }
546                         else if(this.direction == EDirection.LEFT){
547                                 this.imgBody.scaleX = -1;
548                         }
549                 },
550                 tweet:function(text){
551                         this._tweet.text(text, this.x - this._tweet.width/4, this.y - this._tweet.height);
552                 },
553                 moveTo:function(x, y, time){
554                         this.imgGroup.tl.moveTo(x, y, time, enchant.Easing.SIN_EASEINOUT);
555                 },
556                 moveBy:function(x, y, time, eventFunctions){
557                         var self = this;
558                         var params = {
559                                 x: function() {
560                                         return self.imgGroup.x + x;
561                                 },
562                                 y: function() {
563                                         return self.imgGroup.y + y;
564                                 },
565                                 time: time,
566                                 easing: enchant.Easing.SIN_EASEINOUT
567                         };
568                         //-Event register [onactionstart,onactiontick,onactionend]
569                         if(eventFunctions !== undefined){
570                                 for(var key in eventFunctions){
571                                         params[key] = eventFunctions[key];
572                                 }
573                         }
574                         this.imgGroup.tl.tween(params);
575
576                 },
577                 wait:function(frame, eventFunctions){
578                         this.moveBy(0,1,frame,eventFunctions);
579                 },
580                 act: function(){
581                         this.action.act();
582                 },
583                 animation: function(){
584                         this.imgBody.tl.moveBy(0, -5, 10, enchant.Easing.SWING).moveBy(0, 5, 10, enchant.Easing.SWING).loop();
585                         // this.tl.moveBy(0, -5, 10, enchant.Easing.SWING).moveBy(0, 5, 10, enchant.Easing.SWING).loop();
586                         // this.okazari.tl.moveBy(0, -5, 10, enchant.Easing.SWING).moveBy(0, 5, 10, enchant.Easing.SWING).loop();
587                         // this.face.tl.moveBy(0, -5, 10, enchant.Easing.SWING).moveBy(0, 5, 10, enchant.Easing.SWING).loop();
588                         // this.hear.tl.moveBy(0, -5, 10, enchant.Easing.SWING).moveBy(0, 5, 10, enchant.Easing.SWING).loop();
589                 },
590                 getX: function(){
591                         return this.imgGroup.x;
592                 },
593                 getY: function(){
594                         return this.imgGroup.y;
595                 },
596                 moveX: function(x){
597                         this.imgGroup.x += x;
598                         // this.x += x;
599                         // this.shadow.x += x;
600                         // this._tweet.x += x;
601                 },
602                 moveY: function(y){
603                         this.imgGroup.y += y;
604                         // this.y += y;
605                         // this.shadow.y += y;
606                         // this._tweet.y += y;
607                 },
608                 // changeFace: function(erespath){
609                 //      this.face.image = game.assets[erespath];
610                 // },
611                 loadParamsXML: function(url){
612                         var http = new JKL.ParseXML( url );
613                         return http.parse();
614                 },
615                 runYukkuri: function(){
616                         if(this.param.hungry > 70){
617                                 if(this.age%(50 - this.param.hungry - 70) === 0 && this.age !== 0){
618                                         this.param.yukkuri -= 1;
619                                 }
620                         }
621                         else if(this.param.hungry < 10){
622                                 if(this.age%50 === 0 && this.age !== 0) this.param.yukkuri += 1;
623                         }
624
625                         if(this.param.yukkuri <= 0)this.param.yukkuri = 0;
626                         else if(this.param.yukkuri >= this.param.maxYukkuri)this.param.yukkuri = this.param.maxYukkuri;
627                 },
628                 runHungry: function(){
629                         if(this.age%50 === 0 && this.age !== 0)this.param.hungry++;
630                         if(this.param.hungry >= 100)this.param.hungry = 100;
631                 },
632                 runSleep: function(){
633                         if(!this.isSleeping){
634                                 if(this.age%100 === 0 && this.age !== 0)this.param.sleep++;
635                                 if(this.param.sleep >= 100){
636                                         this.param.sleep = 100;
637                                         this.isSleeping = true;
638                                         this.tweet("zzz...zzz...zzz...");
639                                         this.imgBody.tl.pause();
640                                         this.imgGroup.tl.pause();
641                                         this.imgBody.tl.clear();
642                                         this.imgGroup.tl.clear();
643                                         this.face.image = game.assets[EResPath.MARISA_FACE_SLEEP];
644
645                                 }
646
647                         }
648                 },
649                 changeFace: function(){
650                         if(this.param.yukkuri >= 80){
651                                 this.setFaceImage(EFace.HAPPY1);
652                         }
653                         else if(this.param.yukkuri >= 50){
654                                 this.setFaceImage(EFace.NORMAL);
655                         }
656                         else if(this.param.yukkuri >= 30){
657                                 this.setFaceImage(EFace.TIRED);
658                         }
659                         else if(this.param.yukkuri >= 10){
660                                 this.setFaceImage(EFace.CRY1);
661                         }
662                         else if(this.param.yukkuri >= 1){
663                                 this.setFaceImage(EFace.CRY2);
664                         }
665                 },
666                 getRange: function(){
667                         return this.param.range;
668                 },
669                 isSleep: function(){
670                         return this.isSleeping;
671                 },
672                 isDead: function(){
673                         return this.param.yukkuri <= 0;
674                 },
675                 isManpuku: function(){
676                         return this.param.hungry <= 20;
677                 },
678                 isKuhuku: function(){
679                         return this.param.hungry >= 40;
680                 },
681                 getHungry: function(){
682                         return this.param.hungry;
683                 },
684                 setHungry: function(hungry){
685                         this.param.hungry = hungry;
686                         if(this.param.hungry < 0)this.param.hungry = 0;
687                 },
688                 eat:function(food){
689                         food.amount--;
690                         this.setHungry(this.param.hungry - food.getGive());
691                 },
692                 destruct: function(){
693
694                 }
695         });
696         var Marisa = enchant.Class.create(Yukkuri,{
697                 initialize: function(x, y){
698                         Yukkuri.call(this,x,y);
699                         this.classname = "Marisa";
700
701
702                         // var xml = this.loadParamsXML("./data/marisa/params.xml");
703                         var json = net.load("./data/marisa/params.json");
704                         this.param = json.root.param;
705                         this.shadow = new enchant.Sprite(64, 64);
706                         this.shadow.image = game.assets[EResPath.YUKKURI_SHADOW];
707                         this.shadow.x = 0 + this.x;
708                         this.shadow.y = CHARA_HEIGHT / 16 + this.x;
709                         this.shadow.image._element.style.zIndex = 2;
710
711                         this._style.zIndex = 5;
712
713                         this.face = new enchant.Sprite(64, 64);
714                         this.face.image = game.assets[EResPath.MARISA_FACE_NORMAL];
715                         this.face.x = -CHARA_WIDTH / 2;
716                         this.face.y = -CHARA_HEIGHT / 2;
717                         this.face._style.zIndex = 0;
718
719                         this.faceStatus ={};
720                         this.faceStatus[EFace.NORMAL] = EResPath.MARISA_FACE_NORMAL;
721                         this.faceStatus[EFace.CRY1] = EResPath.MARISA_FACE_CRY1;
722                         this.faceStatus[EFace.CRY2] = EResPath.MARISA_FACE_CRY2;
723                         this.faceStatus[EFace.ANGRY] = EResPath.MARISA_FACE_ANGRY;
724                         this.faceStatus[EFace.HAPPY1] = EResPath.MARISA_FACE_HAPPY1;
725                         this.faceStatus[EFace.HAPPY2] = EResPath.MARISA_FACE_HAPPY2;
726                         this.faceStatus[EFace.EAT1] = EResPath.MARISA_FACE_EAT1;
727                         this.faceStatus[EFace.EAT2] = EResPath.MARISA_FACE_EAT2;
728                         this.faceStatus[EFace.TIRED] = EResPath.MARISA_FACE_TIRED;
729                         this.faceStatus[EFace.SLEEP] = EResPath.MARISA_FACE_SLEEP;
730
731
732                         this.hear = new enchant.Sprite(64, 64);
733                         this.hear.image = game.assets[EResPath.MARISA_HEAR];
734                         this.hear.x = -CHARA_WIDTH / 2;
735                         this.hear.y = -CHARA_HEIGHT / 2;
736                         this.hear._style.zIndex = 0;
737
738                         this.okazari = new Okazari();
739                         this.okazari.image._element.style.zIndex = 1;
740
741                         // this.imgGroup._style.zIndex = 1;
742
743                         this.imgGroup.addChild(this.shadow);
744                         this.imgBody.addChild(this);
745                         this.imgBody.addChild(this.face);
746                         this.imgBody.addChild(this.hear);
747                         this.imgBody.addChild(this.okazari);
748                         this.imgGroup.addChild(this.imgBody);
749                         backgroundMap.addChild(this.imgGroup);
750                         this._tweet = new TTweet(148, 64);
751                         this._tweet.image._element.style.zIndex = 20;
752
753                         this.imgGroup.addChild(this._tweet);
754                         this.imgGroup.x = x;
755                         this.imgGroup.y = y;
756                         this.animation();
757                         this.action = new Action(this);
758                         this.id = guid();
759                         ctl.addObj(this);
760                 },
761                 setFaceImage: function(eface){
762                         var path = this.faceStatus[eface];
763                         this.face.image = game.assets[path];
764                 }
765         });
766         var Player = enchant.Class.create(Marisa,{
767                 initialize: function(x, y){
768                         // this.runEnterframe = function(){
769                         // },
770                         Marisa.call(this, x, y);
771                         this.classname = "Player";
772                         ctl.setPlayer(this);
773                         // this.removeEventListener('enterframe',this.runEnterframe);
774                         this.addEventListener('enterframe', function(){
775                                 if(this.age%10 === 0 && this.age !== 0){
776                                         // console.log("palyer:" + this.age);
777                                 }
778                                 this.act();
779
780                                 ///////////////////////////////////////////////
781                                 //Action pattern for debug
782                                 if(game.input.left){
783                                         // this.changeFace(EResPath.MARISA_FACE_TIRED);
784                                         this.moveX(-10);
785                                         this.direction = EDirection.LEFT;
786                                 }
787                                 if(game.input.right){
788                                         this.moveX(10);
789                                         this.direction = EDirection.RIGHT;
790                                 }
791                                 if(game.input.up){
792                                         this.moveY(-10);
793                                         this.tweet("ゆっくりしていってね!");
794                                 }
795                                 if(game.input.down){
796                                         this.moveY(10);
797                                 }
798                                 ///////////////////////////////////////////////
799                                 this.reverse();
800                                 // if(ctl.backgroundMap.hitTest(this.imgGroup.x, this.imgGroup.y + this.height / 2)){
801                                 //      console.log("hit:" + this.imgGroup.x + ":" +  (this.imgGroup.y + this.height/2) );
802                                 // }else{
803                                 //      console.log("not:" + this.imgGroup.x + ":" +  (this.imgGroup.y + this.image.height/2));
804                                 // }
805                                 //- Display the circle of search range.
806                                 // fieldBg.image.clear();
807                                 // fieldBg.image.context.beginPath();
808                                 // fieldBg.image.context.fillStyle = '#ff0000';
809                                 // fieldBg.image.context.arc(this.imgGroup.x + this.width/2, this.imgGroup.y + this.height/2, this.range, Math.PI * 2, false);
810                                 // fieldBg.image.context.fill();
811                         });
812                 }
813         });
814         game.onload = function(){
815                 def.Food = Food;
816                 mangTouch = new MangTouch(ctl);
817                 mangMsg = new MangMsg(ctl);
818                 ctl.setDefined(def);
819                 backgroundMap.image = game.assets[EResPath.MAP0];
820                 backgroundMap.loadData(_mapData);
821                 backgroundMap.collisionData = _collisionData;
822
823                 var menuSurface = new enchant.Surface(EMenuPos.WIDTH, EMenuPos.HEIGHT);
824                 var fieldSurface = new enchant.Surface(EFieldPos.WIDTH, EFieldPos.HEIGHT);
825                 fieldBg.image = fieldSurface;
826                 fieldBg.x = 0;
827                 fieldBg.y = 0;
828
829                 menuSurface.context.fillStyle = '#000';
830                 menuSurface.context.fillRect(0, 0, EMenuPos.WIDTH, EMenuPos.HEIGHT);
831                 menuBg.image = menuSurface;
832                 menuBg.x = EMenuPos.X;
833                 menuBg.y = EMenuPos.Y;
834
835                 backgroundMap.addChild(fieldBg);
836                 mangIcon = new MangIcon(ctl, menuBg);
837
838                 game.rootScene.addChild(backgroundMap);
839                 game.rootScene.addChild(menuBg);
840
841                 // var labelGroup = new LabelGroup();
842                 mangLabel = new MangLabel(ctl, menuBg);
843                 // var info = new Label("ゆっくり");
844                 // info.color = "#ffffff";
845                 // info.font = "14px 'Times New Roman'";
846                 // info.x = 4;
847                 // info.y = GAME_HEIGHT / 2;
848                 // labelGroup.addChild(info);
849                 // menuBg.addChild(labelGroup);
850
851
852                 // var food = new def.Food("apple", 200, 250);
853                 backgroundMap.addChild(foodGroup);
854                 var player = new Player(PALYER_INIT_X, PALYER_INIT_Y);
855                 new Marisa(PALYER_INIT_X + 200, PALYER_INIT_Y);
856                 var touchX = 0;
857
858                 game.rootScene.addEventListener('touchstart', function (e) {
859                         game.touched = true;
860                         var mang = mangTouch.get(ctl.getCurrentCommand());
861                         mang.touchstart(e);
862                 });
863                 game.rootScene.addEventListener('touchmove', function (e) {
864                         var mang = mangTouch.get(ctl.getCurrentCommand());
865                         mang.touchmove(e);
866                 });
867                 game.rootScene.addEventListener('touchend', function (e) {
868                         var mang = mangTouch.get(ctl.getCurrentCommand());
869                         mang.touchend(e);
870                         game.touched = false;
871                 });
872
873
874                 game.rootScene.addEventListener('enterframe', function(){
875                 //main frame
876                         mangLabel.draw(player);
877                 });
878
879                 this.addEventListener('enterframe', function(){
880                         //The priority processing of display instead of z-index
881                         backgroundMap.childNodes.sort(
882                                 function(a,b){
883                                         if(typeof a.id === "undefined" && typeof b.id === "undefined"){
884                                                 return 0;
885                                         }
886                                         else if(typeof a.id === "undefined"){
887                                                 return -1;
888                                         }
889                                         else if(typeof b.id === "undefined"){
890                                                 return 1;
891                                         }
892                                         if(a.y < b.y)return -1;
893                                         if(a.y > b.y)return 1;
894                                         return 0;
895                                 }
896                         );
897                 });
898
899         };
900         game.start();
901 };