OSDN Git Service

sleep refactoring
[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 BASE_FPS = 20;
22 var HUNGRY_MAX = 100;
23
24 var EResPath = {
25         YUKKURI_BASE : './data/yukkuri_base.png',
26         YUKKURI_BODY_DEAD : './data/yukkuri_dead.png',
27         YUKKURI_UNUN : './data/unun.png',
28         YUKKURI_SHADOW : './data/shadow.png',
29         MARISA_FACE_NORMAL : './data/marisa/face_normal1.png',
30         MARISA_FACE_TIRED : './data/marisa/face_tired1.png',
31         MARISA_FACE_ANGRY : './data/marisa/face_angry1.png',
32         MARISA_FACE_CRY1 : './data/marisa/face_cry1.png',
33         MARISA_FACE_CRY2 : './data/marisa/face_cry2.png',
34         MARISA_FACE_EAT1 : './data/marisa/face_eat1.png',
35         MARISA_FACE_EAT2 : './data/marisa/face_eat2.png',
36         MARISA_FACE_HAPPY1 : './data/marisa/face_happy1.png',
37         MARISA_FACE_HAPPY2 : './data/marisa/face_happy2.png',
38         MARISA_FACE_SLEEP : './data/marisa/face_sleep1.png',
39         MARISA_HEAR : './data/marisa/hear1.png',
40         OBJECT : './data/object.png',
41         MAP0 : './data/map0.png',
42         COMMAND : './data/command.png',
43         OKAZARI : './data/okazari.png',
44 };
45 var EFace = {
46         NORMAL:0,
47         TIRED:1,
48         ANGRY:2,
49         CRY1:3,
50         CRY2:4,
51         EAT1:5,
52         EAT2:6,
53         HAPPY1:7,
54         HAPPY2:8,
55         SLEEP:9,
56 };
57 var EBody = {
58         NORMAL: 0,
59         DEAD: 1,
60 };
61
62 var ECommand = {
63         WORLD_CURSOR: 0,
64         WORLD_TARGET: 1,
65         WORLD_APPLE: 2,
66 };
67
68 var EMenuPos = {
69         X : 600,
70         Y : 0,
71         WIDTH : 200,
72         HEIGHT : GAME_HEIGHT,
73 };
74 var EFieldPos = {
75         X : 0,
76         Y : 0,
77         WIDTH: GAME_WIDTH - EMenuPos.WIDTH,
78         HEIGHT: GAME_HEIGHT,
79 };
80 var EAction = {
81         WAIT : 0,
82         HUNT : 3,
83         THINK : 4,
84         WALK : 5,
85         DEAD: 6,
86         EAT_START : 8,
87         EAT_WAIT : 9,
88         MOVE_TO_EAT_START: 10,
89         MOVE_TO_EAT_UNDERWAY: 11,
90         MOVE_TO_COMMAND: 12,
91         UNUN_START : 13,
92         UNUN_WAIT : 14,
93         UNUN_SMELL_START : 15,
94         UNUN_SMELL_WAIT : 16,
95         SLEEP_START : 17,
96         SLEEP_WAIT : 18,
97         NONE: 9999
98 };
99 var EDirection = {
100         LEFT : 0,
101         RIGHT: 1,
102 };
103 var EMsg = {
104         WALK: 0,
105         SLEEP: 1,
106         EAT: 2,
107         GET_UP: 3,
108         DEAD: 4,
109         UNUN: 5,
110         UNUN_END: 6,
111         UNUN_SMELL_START: 7,
112         SEARCH_FOOD: 8,
113 };
114 include("./lang/ja.js");
115 window.onload = function(){
116
117         //init game
118         var def = new Object();
119         var foodGroup = new Group();
120         var SpriteGroup = enchant.Class.mixClasses(Sprite, Group,true);
121         var menuBg = new SpriteGroup(EMenuPos.WIDTH, EMenuPos.HEIGHT);
122         var fieldBg = new enchant.Sprite(EFieldPos.WIDTH, EFieldPos.HEIGHT);
123         var game = new Game(GAME_WIDTH, GAME_HEIGHT);
124         game.fps = FPS;
125         var _loadArr = [];
126         var i=0;
127         for(var _v in EResPath){
128                 _loadArr[i] = EResPath[_v];
129                 i++;
130         }
131         game.preload(_loadArr);
132         var MapGroup = enchant.Class.mixClasses(Map, Group,true);
133         var backgroundMap = new MapGroup(16, 16);
134         include("./class.js");
135         include("./ctrl.js");
136         ctl = new Ctrl();
137         ctl.init(game);
138         ctl.setBackgroundMap(backgroundMap);
139         var Action = enchant.Class.create({
140                 initialize: function (yukkuri){
141                         this.status = EAction.THINK;
142                         this.yukkuri = yukkuri;
143                         this.isMoving = false;
144                         this.targetNode = null;
145                 },
146                 getStatus: function(){
147                         return this.status;
148                 },
149                 setStatus: function(eaction){
150                         this.status = eaction;
151                 },
152                 observe: function(){
153                         if(this.yukkuri.isDead()){
154                                 this.status = EAction.DEAD;
155                         }
156                         // else if(this.yukkuri.isSleep()){
157                         //      this.status = EAction.SLEEP;
158                         // }
159
160                 },
161                 act : function(){
162                         this.observe();
163                         switch(this.status){
164                                 case EAction.WAIT:
165
166                                 break;
167                                 case EAction.THINK:
168                                         if(this.yukkuri.age%2 === 0){
169                                                 if(this.yukkuri.isSleep()){
170                                                         this.setStatus(EAction.SLEEP_START);
171                                                 }
172                                                 else if(this.yukkuri.getUnun() >= 80){
173                                                         this.setStatus(EAction.UNUN_START);
174                                                 }else{
175                                                         // this.yukkuri.param.yukkuri -= 1;
176                                                         this.yukkuri.changeFace();
177                                                         this.search();
178                                                 }
179
180                                         }
181                                 break;
182                                 case EAction.MOVE_TO_EAT_START:
183                                         this.move_to_eat_start();
184                                 break;
185                                 case EAction.MOVE_TO_EAT_UNDERWAY:
186                                 break;
187                                 //Random Walk
188                                 case EAction.WALK:
189                                         this.walk(0);
190                                 break;
191                                 case EAction.SLEEP_START:
192                                         this.sleep_start();
193                                 break;
194                                 case EAction.SLEEP_WAIT:
195                                 break;
196                                 case EAction.DEAD:
197                                         this.dead();
198                                 break;
199                                 case EAction.EAT_START:
200                                         this.eat_start();
201                                 break;
202                                 case EAction.EAT_WAIT:
203                                 break;
204                                 case EAction.UNUN_START:
205                                         this.unun_start();
206                                 break;
207                                 case EAction.UNUN_WAIT:
208                                 break;
209                                 case EAction.MOVE_TO_COMMAND:
210                                         this.move_to_command();
211                                 break;
212                                 case EAction.UNUN_SMELL_START:
213                                         this.unun_smell_start();
214                                 break;
215                         }
216                 },
217
218                 move_to_eat_start : function(){
219                         new MoveToEatEvent({
220                                 "type": 'food',
221                                 "targetNode": this.targetNode,
222                                 "action": this,
223                                 "myYukkuri": this.yukkuri
224                         });
225                 },
226                 walk : function(retryCounter){
227                         if(retryCounter > 15){
228                                 retryCounter = 0;
229                                 this.status = EAction.NONE;
230                                 console.log("retryCount over");
231                                 return;
232                         }
233                         if(this.yukkuri.imgGroup.tl.queue.length === 0){
234                                 var frame = 200;
235                                 if(this.isMoving){
236                                         this.isMoving = false;
237                                         this.status = EAction.THINK;
238                                         this.yukkuri.vx = 0;
239                                         this.yukkuri.vy = 0;
240                                 }else{
241                                         // this.isMoving = true;
242                                         var xpos = 150 + rand(40) - (retryCounter * 10);
243                                         var ypos = 150 + rand(40) - (retryCounter * 10);
244                                         if(rand(8) === 0){
245                                                 this.yukkuri.vx = 0;
246                                                 this.yukkuri.vy = -ypos;
247                                         }
248                                         else if(rand(8) == 1){
249                                                 this.yukkuri.vx = xpos;
250                                                 this.yukkuri.vy = -ypos;
251                                                 this.yukkuri.direction = EDirection.RIGHT;
252                                         }
253                                         else if(rand(8) == 2){
254                                                 this.yukkuri.vx = xpos;
255                                                 this.yukkuri.vy = 0;
256                                                 this.yukkuri.direction = EDirection.RIGHT;
257                                         }
258                                         else if(rand(8) == 3){
259                                                 this.yukkuri.vx = xpos;
260                                                 this.yukkuri.vy = ypos;
261                                                 this.yukkuri.direction = EDirection.RIGHT;
262                                         }
263                                         else if(rand(8) == 4){
264                                                 this.yukkuri.vx = 0;
265                                                 this.yukkuri.vy = ypos;
266                                         }
267                                         else if(rand(8) == 5){
268                                                 this.yukkuri.vx = -xpos;
269                                                 this.yukkuri.vy = ypos;
270                                                 this.yukkuri.direction = EDirection.LEFT;
271                                         }
272                                         else if(rand(8) == 6){
273                                                 this.yukkuri.vx = -xpos;
274                                                 this.yukkuri.vy = 0;
275                                                 this.yukkuri.direction = EDirection.LEFT;
276                                         }
277                                         else if(rand(8) == 7){
278                                                 this.yukkuri.vx = -xpos;
279                                                 this.yukkuri.vy = -ypos;
280                                                 this.yukkuri.direction = EDirection.LEFT;
281                                         }
282                                         if (this.yukkuri.vx || this.yukkuri.vy) {
283                                                 var map = ctl.backgroundMap;
284                                                 // var x = this.yukkuri.x + (this.yukkuri.moveX ? this.yukkuri.moveX / Math.abs(this.yukkuri.moveX) * 16 : 0) + 16;
285                                                 // var y = this.yukkuri.y + (this.yukkuri.moveY ? this.yukkuri.moveY / Math.abs(this.yukkuri.moveY) * 16 : 0) + 16;
286                                                 var x = this.yukkuri.imgGroup.x + this.yukkuri.vx;
287                                                 var y = this.yukkuri.imgGroup.y + this.yukkuri.vy + this.yukkuri.height / 2;
288                                                 if (0 <= x && x < map.width && 0 <= y && y < map.height && !map.hitTest(x, y)) {
289                                                         // console.log("あたってないよ:"+ this.yukkuri.imgGroup.x + ":" + this.yukkuri.imgGroup.y);
290                                                         // console.log("X:" + this.yukkuri.moveX);
291                                                         // console.log("Y:" + this.yukkuri.moveY);
292                                                         this.isMoving = true;
293                                                         this.yukkuri.reverse();
294                                                         this.yukkuri.moveBy(this.yukkuri.vx, this.yukkuri.vy, frame);
295                                                         mangMsg.output(this.yukkuri,EMsg.WALK);
296                                                         //うんうんくさい対策
297                                                         this.targetNode = null;
298                                                 }else{
299                                                         // console.log("HIT:"+ this.yukkuri.imgGroup.x + ":" + this.yukkuri.imgGroup.y);
300                                                         this.walk(retryCounter+1);
301                                                 }
302                                         }
303                                 }
304                         }
305                 },
306                 sleep_start: function(){
307                         new SleepEvent({
308                                 "action": this,
309                                 "myYukkuri": this.yukkuri
310                         });
311                 },
312                 dead : function(){
313                         console.log("dead");
314                         mangMsg.output(this.yukkuri, EMsg.DEAD);
315                         this.yukkuri.imgBody.tl.clear();
316                         this.yukkuri.imgBody.tl.pause();
317                         this.yukkuri.imgGroup.tl.clear();
318                         this.yukkuri.imgGroup.tl.pause();
319                         this.yukkuri.setBodyImage(EBody.DEAD);
320                 },
321                 eat_start: function(){
322                         new EatEvent({
323                                 "type": 'eat',
324                                 "targetNode": this.targetNode,
325                                 "action": this,
326                                 "myYukkuri": this.yukkuri
327                         });
328
329                 },
330                 unun_start: function(){
331                         new UnunEvent({
332                                 "action": this,
333                                 "myYukkuri": this.yukkuri
334                         });
335                 },
336                 unun_smell_start: function(){
337                         new UnunSmellEvent({
338                                 "action": this,
339                                 "myYukkuri": this.yukkuri
340                         });
341                 },
342                 move_to_command: function(){
343
344                 },
345                 search : function(){
346                         var nodes = ctl.getObjs();
347                         l = nodes.length;
348                         var ununFlg = false;
349
350                         for (var key in nodes) {
351                                 var node = nodes[key];
352                                 if (!ununFlg
353                                         && node instanceof Unun
354                                         && !(this.targetNode instanceof Unun)
355                                         && this.yukkuri.within(node, this.yukkuri.getRange() / 2)) {
356                                         ununFlg = true;
357                                         this.targetNode = node;
358                                         this.setStatus(EAction.UNUN_SMELL_START);
359                                         return;
360                                 }
361                                 else if (node instanceof Food) {
362                                         if(this.yukkuri.isKuhuku() && this.yukkuri.within(node, this.yukkuri.getRange())){
363
364                                                 //A yukkuri to go to the food area
365                                                 this.targetNode = node;
366                                                 this.setStatus(EAction.MOVE_TO_EAT_START);
367
368                                                 return;
369                                         }else{
370                                                 //not hunbry or not food.
371                                         }
372                                 }
373                         }
374                         this.status =  EAction.WALK;
375                 }
376         });
377         var Event = enchant.Class.create({
378                 initialize: function (_data){
379                 },
380                 onactionstart:function(self){
381                         return function(e){
382                                 if(typeof e === "undefined")return;
383                         };
384                 },
385                 onactiontick:function(self){
386                         return function(e){
387                                 if(typeof e === "undefined")return;
388                         };
389                 },
390                 onactionend:function(self){
391                         return function(e){
392                                 if(typeof e === "undefined")return;
393                         };
394                 }
395         });
396         var SleepEvent = enchant.Class.create(Event,{
397                 initialize: function (_data){
398                         this.data = _data;
399                         this.yukkuri = _data.myYukkuri;
400                         this.action = _data.action;
401
402                         mangMsg.output(this.yukkuri, EMsg.SLEEP);
403                         this.action.setStatus(EAction.SLEEP_WAIT);
404                         this.yukkuri.setFaceImage(EFace.SLEEP);
405                         this.yukkuri.tlPause();
406                         var sec = FPS * 3;
407                         // this.yukkuri.enterframe(function(){
408                         //      console.log("enterfmrae");
409                         // });
410                         var self = this;
411                         this.yukkuri.addEventListener('enterframe', function() {
412                                 var yukkuri = self.yukkuri;
413                                 if(yukkuri.age%2 === 0 && yukkuri.age !== 0)yukkuri.param.sleep--;
414                                 if(yukkuri.param.sleep <= 0){
415                                         yukkuri.param.sleep = 0;
416                                         self.action.setStatus(EAction.WAIT);
417                                         mangMsg.output(self.yukkuri, EMsg.GET_UP);
418                                         self.yukkuri.setFaceImage(EFace.NORMAL);
419                                         yukkuri.tlResume();
420                                         this.removeEventListener("enterframe",arguments.callee);
421                                         self.action.setStatus(EAction.THINK);
422                                 }
423                         });
424                 },
425                 onactionstart:function(self){
426                         return function(e){
427                                 if(typeof e === "undefined")return;
428                                 console.log("astart");
429                         };
430                 },
431                 onactiontick:function(self){
432                         return function(e){
433                                 if(typeof e === "undefined")return;
434                         };
435                 },
436                 onactionend:function(self){
437                         return function(e){
438                                 if(typeof e === "undefined")return;
439                                 console.log("enddd");
440                         };
441                 }
442
443                 // initialize: function (_data){
444                 //      console.log("init");
445                 //      this.data = _data;
446                 //      this.yukkuri = _data.myYukkuri;
447                 //      this.action = _data.action;
448                 //      this.action.setStatus(EAction.SLEEP_WAIT);
449                 //      mangMsg.output(this.yukkuri, EMsg.SLEEP);
450                 //      this.yukkuri.setFaceImage(EFace.SLEEP);
451                 //      this.yukkuri.tlPause();
452
453                 //      var sec = BASE_FPS * 1;
454                 //      console.log("sec:"+ sec);
455                 //      this.yukkuri.wait(sec, {
456                 //              "onactionstart": this.onactionstart(this),
457                 //              "onactiontick": this.onactiontick(this),
458                 //              "onactionend": this.onactionend(this)
459                 //      });
460                 // },
461                 // onactionstart:function(self){
462                 //      return function(e){
463                 //              console.log("start");
464                 //              if(typeof e === "undefined")return;
465                 //      };
466                 // },
467                 // onactiontick:function(self){
468                 //      return function(e){
469                 //              if(typeof e === "undefined")return;
470                 //              console.log("tick");
471                 //              var yukkuri = self.yukkuri;
472                 //              if(yukkuri.age%4 === 0 && yukkuri.age !== 0)yukkuri.param.sleep--;
473                 //              if(yukkuri.param.sleep <= 0){
474                 //                      yukkuri.param.sleep = 0;
475                 //              }
476                 //      };
477                 // },
478                 // onactionend:function(self){
479                 //      return function(e){
480                 //              if(typeof e === "undefined")return;
481                 //              self.action.setStatus(EAction.WAIT);
482                 //              self.yukkuri.setFaceImage(EFace.NORMAL);
483                 //              mangMsg.output(self.yukkuri, EMsg.GET_UP);
484                 //              yukkuri.tlResume();
485                 //              new WaitEvent({
486                 //                      "waitframe": FPS * 1.5,
487                 //                      "action": self.action,
488                 //                      "myYukkuri": self.yukkuri
489                 //              });
490                 //      };
491                 // }
492         });
493
494         var UnunEvent = enchant.Class.create(Event,{
495                 initialize: function (_data){
496                         this.data = _data;
497                         this.yukkuri = _data.myYukkuri;
498                         this.action = _data.action;
499                         this.action.setStatus(EAction.UNUN_WAIT);
500                         mangMsg.output(this.yukkuri, EMsg.UNUN);
501                         this.yukkuri.setFaceImage(EFace.HAPPY2);
502                         var sec = FPS * 3;
503
504                         this.yukkuri.wait(sec, {
505                                 "onactionstart": this.onactionstart(this),
506                                 "onactiontick": this.onactiontick(this),
507                                 "onactionend": this.onactionend(this)
508                         });
509                 },
510                 onactionstart:function(self){
511                         return function(e){
512                                 if(typeof e === "undefined")return;
513                         };
514                 },
515                 onactiontick:function(self){
516                         return function(e){
517                                 if(typeof e === "undefined")return;
518                         };
519                 },
520                 onactionend:function(self){
521                         return function(e){
522                                 if(typeof e === "undefined")return;
523                                 self.yukkuri.unun();
524                                 self.action.setStatus(EAction.WAIT);
525                                 mangMsg.output(self.yukkuri, EMsg.UNUN_END);
526                                 new WaitEvent({
527                                         "waitframe": FPS * 1.5,
528                                         "action": self.action,
529                                         "myYukkuri": self.yukkuri
530                                 });
531                         };
532                 }
533         });
534         var WaitEvent = enchant.Class.create(Event,{
535                 initialize: function (_data){
536                         this.data = _data;
537                         this.yukkuri = _data.myYukkuri;
538                         this.action = _data.action;
539                         this.waitframe = _data.waitframe;
540                         this.yukkuri.wait(this.waitframe, {
541                                 "onactionstart": this.onactionstart(this),
542                                 "onactiontick": this.onactiontick(this),
543                                 "onactionend": this.onactionend(this)
544                         });
545                 },
546                 onactionend:function(self){
547                         return function(e){
548                                 if(typeof e === "undefined")return;
549                                 self.action.setStatus(EAction.THINK);
550                         };
551                 }
552         });
553         var UnunSmellEvent = enchant.Class.create(Event,{
554                 initialize: function (_data){
555                         this.data = _data;
556                         this.yukkuri = _data.myYukkuri;
557                         this.action = _data.action;
558                         this.action.setStatus(EAction.UNUN_SMELL_WAIT);
559                         mangMsg.output(this.yukkuri, EMsg.UNUN_SMELL);
560                         this.yukkuri.setFaceImage(EFace.CRY1);
561                         var sec = FPS * 3;
562                         this.yukkuri.setYukkuri(this.yukkuri.getYukkuri() - 3);
563                         this.yukkuri.wait(sec, {
564                                 "onactionstart": this.onactionstart(this),
565                                 "onactiontick": this.onactiontick(this),
566                                 "onactionend": this.onactionend(this)
567                         });
568                 },
569                 onactionend:function(self){
570                         return function(e){
571                                 if(typeof e === "undefined")return;
572                                 self.action.setStatus(EAction.THINK);
573                         };
574                 }
575         });
576
577         var EatEvent = enchant.Class.create(Event,{
578                 initialize: function (_data){
579                         this.data = _data;
580                         this.yukkuri = _data.myYukkuri;
581                         this.food = _data.targetNode;
582                         this.action = _data.action;
583                         this.action.setStatus(EAction.EAT_WAIT);
584                         mangMsg.output(this.yukkuri, EMsg.EAT);
585                         this.yukkuri.setFaceImage(EFace.EAT2);
586
587                         // var sec = FPS * (this.yukkuri.param.hungry/10 + 3);
588                         var sec = FPS * 3;
589
590                         this.yukkuri.wait(sec, {
591                                 "onactionstart": this.onactionstart(this),
592                                 "onactiontick": this.onactiontick(this),
593                                 "onactionend": this.onactionend(this)
594                         });
595                 },
596                 onactionstart:function(self){
597                         return function(e){
598                                 if(typeof e === "undefined")return;
599                                 self.yukkuri.eat(self.food);
600                         };
601                 },
602                 onactiontick:function(self){
603                         return function(e){
604                                 if(typeof e === "undefined")return;
605                         };
606                 },
607                 onactionend:function(self){
608                         return function(e){
609                                 if(typeof e === "undefined")return;
610                                 if(self.food.getAmount() <= 0 || self.yukkuri.isManpuku()){
611                                         self.action.setStatus(EAction.THINK);
612                                         self.yukkuri.setFaceImage(EFace.NORMAL);
613                                 }else{
614                                         self.action.setStatus(EAction.EAT_START);
615                                 }
616                         };
617                 }
618         });
619         /**
620          * Move Event
621          * -When yukkuri find Food.
622          * @param  _data
623          * type: "food"
624          * targetNode: enchant.Node
625          * action:Action
626          * myYukkuri:Yukkuri
627          */
628         var MoveToEatEvent = enchant.Class.create(Event,{
629                 initialize: function (_data){
630                         this.data = _data;
631                         this.yukkuri = this.data.myYukkuri;
632                         this.action = this.data.action;
633
634                         var yukkuri = this.yukkuri;
635                         var node = this.data.targetNode;
636                         //food distance
637                         if(this.data.type == "food"){
638                                 this.action.status = EAction.MOVE_TO_EAT_UNDERWAY;
639                                 yukkuri.vx = node.x - yukkuri.getX();
640                                 yukkuri.vy = node.y - yukkuri.getY();
641                                 var distance = (Math.abs(yukkuri.vx) + Math.abs(yukkuri.vy)) / 2;
642                                 var frame = distance + distance/5 + 1;
643                                 yukkuri.direction = yukkuri.vx > 0 ? EDirection.RIGHT : EDirection.LEFT;
644
645                                 yukkuri.moveBy(yukkuri.vx, yukkuri.vy, frame, {
646                                         "onactionstart": this.onactionstart(this),
647                                         "onactiontick": this.onactiontick(this),
648                                         "onactionend": this.onactionend(this)
649                                 });
650                         }
651                 },
652                 onactionstart:function(self){
653                         return function(e){
654                                 if(typeof e === "undefined")return;
655                                 self.yukkuri.tweet("ゆゆ??\nたべものさん\nはっけんなのぜ!");
656                         };
657                 },
658                 onactiontick:function(self){
659                         return function(e){
660                                 if(typeof e === "undefined")return;
661                         };
662                 },
663                 onactionend:function(self){
664                         return function(e){
665                                 if(typeof e === "undefined")return;
666                                 self.action.setStatus(EAction.EAT_START);
667                         };
668                 }
669         });
670         var MoveCommandEvent = enchant.Class.create(Event,{
671                 initialize: function (_data){
672                         this.data = _data;
673                         this.yukkuri = this.data.myYukkuri;
674                         this.action = this.data.action;
675                         this.vx = this.data.vx;
676                         this.vy = this.data.vy;
677
678                         var yukkuri = this.yukkuri;
679                         this.action.status = EAction.MOVE_TO_EAT_UNDERWAY;
680                         yukkuri.vx = node.x - yukkuri.getX();
681                         yukkuri.vy = node.y - yukkuri.getY();
682                         var distance = (Math.abs(yukkuri.vx) + Math.abs(yukkuri.vy)) / 2;
683                         var frame = distance + 1;
684                         yukkuri.moveBy(yukkuri.vx, yukkuri.vy, frame, {
685                                 "onactionstart": this.onactionstart(this),
686                                 "onactiontick": this.onactiontick(this),
687                                 "onactionend": this.onactionend(this)
688                         });
689                 },
690                 onactionstart:function(self){
691                         return function(e){
692                                 if(typeof e === "undefined")return;
693                         };
694                 },
695                 onactiontick:function(self){
696                         return function(e){
697                                 if(typeof e === "undefined")return;
698                         };
699                 },
700                 onactionend:function(self){
701                         return function(e){
702                                 if(typeof e === "undefined")return;
703                                 self.action.setStatus(EAction.THINK);
704                         };
705                 }
706         });
707
708         var ObjSprite = enchant.Class.create(enchant.Sprite,{
709                 initialize: function (w, h){
710                         enchant.Sprite.call(this, w, h);
711                         this.id = guid();
712                         ctl.addObj(this);
713                 },
714                 removeAll: function(){
715                         this.remove();
716                         ctl.removeObj(this);
717                 }
718         });
719
720         var Food = enchant.Class.create(ObjSprite,{
721                 initialize: function (type, x, y){
722                         this.classname = "Food";
723                         ObjSprite.call(this,16,16);
724                         this.image = game.assets[EResPath.OBJECT];
725                         this.frame = 15;
726                         this.x = x;
727                         this.y = y;
728                         //一つにつきgiveの値分、hunguryを減少させられる
729                         this.give = 40;
730                         this.amount = 3;
731                         foodGroup.addChild(this);
732                         this.addEventListener('enterframe', function(){
733                                 if(this.amount <= 0){
734                                         this.removeAll();
735                                 }
736                         });
737                 },
738                 getAmount: function(){
739                         return this.amount;
740                 },
741                 getGive: function(){
742                         return this.give;
743                 }
744         });
745         var Unun = enchant.Class.create(ObjSprite,{
746                 initialize: function (x, y){
747                         this.classname = "Unun";
748                         ObjSprite.call(this,32,32);
749                         this.image = game.assets[EResPath.YUKKURI_UNUN];
750                         this.x = x;
751                         this.y = y;
752                         backgroundMap.addChild(this);
753                 }
754         });
755         var Okazari = enchant.Class.create(enchant.Sprite,{
756                 initialize: function(){
757                         this.classname = "Okazari";
758                         enchant.Sprite.call(this, 64, 64);
759                         this.image = game.assets[EResPath.OKAZARI];
760                         this.x = -CHARA_WIDTH / 2;
761                         this.y = -12 - CHARA_HEIGHT / 2;
762                 }
763         });
764         var Yukkuri = enchant.Class.create(ObjSprite,{
765                 initialize: function(x, y){
766                         ObjSprite.call(this, 64, 64);
767                         this.classname = "Yukkuri";
768                         this.imgGroup = new SpriteGroup();
769                         this.imgBody = new SpriteGroup();
770                         this.imgGroup.id = guid();
771                         this.imgBody.id = guid();
772                         this.x = -CHARA_WIDTH / 2;
773                         this.y = -CHARA_HEIGHT / 2;
774                         this.vx = 0;
775                         this.vy = 0;
776                         this.image = game.assets[EResPath.YUKKURI_BASE];
777                         this.direction = EDirection.RIGHT;
778                         this.addEventListener('enterframe', this.runEnterframe);
779                         this.isSleeping = false;
780                         this.bodyStatus ={};
781                         this.bodyStatus[EBody.NORMAL] = EResPath.YUKKURI_BASE;
782                         this.bodyStatus[EBody.DEAD] = EResPath.YUKKURI_BODY_DEAD;
783                 },
784                 runEnterframe:function(){
785                         this.act();
786                         if(this.isDead())return;
787                         this.runYukkuri();
788                         this.runHungry();
789                         this.runUnun();
790                         this.runSleep();
791                 },
792                 reverse:function(){
793                         if(this.direction == EDirection.RIGHT){
794                                 this.imgBody.scaleX = 1;
795                         }
796                         else if(this.direction == EDirection.LEFT){
797                                 this.imgBody.scaleX = -1;
798                         }
799                 },
800                 tweet:function(text){
801                         this._tweet.text(text, this.x - this._tweet.width/4, this.y - this._tweet.height);
802                 },
803                 moveTo:function(x, y, time){
804                         this.imgGroup.tl.moveTo(x, y, time, enchant.Easing.SIN_EASEINOUT);
805                 },
806                 moveBy:function(x, y, time, eventFunctions){
807                         var self = this;
808                         var params = {
809                                 x: function() {
810                                         return self.imgGroup.x + x;
811                                 },
812                                 y: function() {
813                                         return self.imgGroup.y + y;
814                                 },
815                                 time: time,
816                                 easing: enchant.Easing.SIN_EASEINOUT
817                         };
818                         //-Event register [onactionstart,onactiontick,onactionend]
819                         if(eventFunctions !== undefined){
820                                 for(var key in eventFunctions){
821                                         params[key] = eventFunctions[key];
822                                 }
823                         }
824                         this.imgGroup.tl.tween(params);
825
826                 },
827                 wait:function(frame, eventFunctions){
828                         this.moveBy(0,1,frame,eventFunctions);
829                 },
830                 act: function(){
831                         this.action.act();
832                 },
833                 animation: function(){
834                         this.imgBody.tl.moveBy(0, -5, 10, enchant.Easing.SWING).moveBy(0, 5, 10, enchant.Easing.SWING).loop();
835                         // this.tl.moveBy(0, -5, 10, enchant.Easing.SWING).moveBy(0, 5, 10, enchant.Easing.SWING).loop();
836                         // this.okazari.tl.moveBy(0, -5, 10, enchant.Easing.SWING).moveBy(0, 5, 10, enchant.Easing.SWING).loop();
837                         // this.face.tl.moveBy(0, -5, 10, enchant.Easing.SWING).moveBy(0, 5, 10, enchant.Easing.SWING).loop();
838                         // this.hear.tl.moveBy(0, -5, 10, enchant.Easing.SWING).moveBy(0, 5, 10, enchant.Easing.SWING).loop();
839                 },
840                 getWidth: function(){
841                         return this.width;
842                 },
843                 getHeight: function(){
844                         return this.height;
845                 },
846                 getX: function(){
847                         return this.imgGroup.x;
848                 },
849                 getY: function(){
850                         return this.imgGroup.y;
851                 },
852                 moveX: function(x){
853                         this.imgGroup.x += x;
854                         // this.x += x;
855                         // this.shadow.x += x;
856                         // this._tweet.x += x;
857                 },
858                 moveY: function(y){
859                         this.imgGroup.y += y;
860                         // this.y += y;
861                         // this.shadow.y += y;
862                         // this._tweet.y += y;
863                 },
864                 // changeFace: function(erespath){
865                 //      this.face.image = game.assets[erespath];
866                 // },
867                 loadParamsXML: function(url){
868                         var http = new JKL.ParseXML( url );
869                         return http.parse();
870                 },
871                 runYukkuri: function(){
872                         if(this.param.hungry > 70){
873                                 if(this.age%(50 - this.param.hungry - 70) === 0 && this.age !== 0){
874                                         this.param.yukkuri -= 1;
875                                 }
876                         }
877                         else if(this.param.hungry < 10){
878                                 if(this.age%50 === 0 && this.age !== 0) this.param.yukkuri += 1;
879                         }
880
881                         if(this.param.yukkuri <= 0)this.param.yukkuri = 0;
882                         else if(this.param.yukkuri >= this.param.maxYukkuri)this.param.yukkuri = this.param.maxYukkuri;
883                 },
884                 runHungry: function(){
885                         if(this.age%50 === 0 && this.age !== 0)this.param.hungry++;
886                         if(this.param.hungry >= 100)this.param.hungry = 100;
887                 },
888                 runUnun: function(){
889                         if(this.age%50 === 0 && this.age !== 0)this.param.unun++;
890                         if(this.param.unun >= 100)this.param.unun = 100;
891                 },
892                 runSleep: function(){
893                         if(this.action.getStatus() != EAction.SLEEP_START && this.action.getStatus() != EAction.SLEEP_WAIT){
894                                 if(this.age%80 === 0 && this.age !== 0)this.param.sleep++;
895                                 if(this.param.sleep >= 100){
896                                         this.param.sleep = 100;
897                                 }
898                         }
899                 },
900                 changeFace: function(){
901                         if(this.param.yukkuri >= 80){
902                                 this.setFaceImage(EFace.HAPPY1);
903                         }
904                         else if(this.param.yukkuri >= 50){
905                                 this.setFaceImage(EFace.NORMAL);
906                         }
907                         else if(this.param.yukkuri >= 30){
908                                 this.setFaceImage(EFace.TIRED);
909                         }
910                         else if(this.param.yukkuri >= 10){
911                                 this.setFaceImage(EFace.CRY1);
912                         }
913                         else if(this.param.yukkuri >= 1){
914                                 this.setFaceImage(EFace.CRY2);
915                         }
916                 },
917                 getRange: function(){
918                         return this.param.range;
919                 },
920                 isSleep: function(){
921                         return this.param.sleep >= 100;
922                 },
923                 isDead: function(){
924                         return this.param.yukkuri <= 0;
925                 },
926                 isManpuku: function(){
927                         return this.param.hungry <= 20;
928                 },
929                 isKuhuku: function(){
930                         return this.param.hungry >= 40;
931                 },
932                 getYukkuri: function(){
933                         return this.param.yukkuri;
934                 },
935                 setYukkuri: function(yukkuri){
936                         this.param.yukkuri = yukkuri;
937                         if(this.param.yukkuri < 0)this.param.yukkuri = 0;
938                 },
939                 getHungry: function(){
940                         return this.param.hungry;
941                 },
942                 setHungry: function(hungry){
943                         this.param.hungry = hungry;
944                         if(this.param.hungry < 0)this.param.hungry = 0;
945                 },
946                 getUnun: function(){
947                         return this.param.unun;
948                 },
949                 setUnun: function(unun){
950                         this.param.unun = unun;
951                         if(this.param.unun < 0)this.param.unun = 0;
952                 },
953
954                 eat:function(food){
955                         food.amount--;
956                         this.setHungry(this.param.hungry - food.getGive());
957                         this.setUnun(this.getUnun() + food.getGive() / 4);
958                         this.setYukkuri(this.getYukkuri() + 5);
959                 },
960                 unun:function(){
961                         if(this.direction == EDirection.RIGHT){
962                                 new Unun(this.getX(), this.getY() + this.getHeight()/5 );
963                         }else{
964                                 new Unun(this.getX() -  this.getWidth()/5, this.getY() + this.getHeight()/5 );
965                         }
966                         this.setUnun(0);
967                 },
968                 setBodyImage: function(ebody){
969                         var path = this.bodyStatus[ebody];
970                         this.image = game.assets[path];
971                 },
972                 tlPause:function(){
973                         this.imgBody.tl.pause();
974                         this.imgGroup.tl.pause();
975                         this.imgBody.tl.clear();
976                         this.imgGroup.tl.clear();
977                 },
978                 tlResume: function(){
979                         this.imgBody.tl.resume();
980                         this.imgGroup.tl.resume();
981                         this.animation();
982                 },
983                 destruct: function(){
984
985                 }
986         });
987         var Marisa = enchant.Class.create(Yukkuri,{
988                 initialize: function(x, y){
989                         Yukkuri.call(this,x,y);
990                         this.classname = "Marisa";
991
992
993                         // var xml = this.loadParamsXML("./data/marisa/params.xml");
994                         var json = net.load("./data/marisa/params.json");
995                         this.param = json.root.param;
996                         this.shadow = new enchant.Sprite(64, 64);
997                         this.shadow.image = game.assets[EResPath.YUKKURI_SHADOW];
998                         this.shadow.x = 0 + this.x;
999                         this.shadow.y = CHARA_HEIGHT / 16 + this.x;
1000                         this.shadow.image._element.style.zIndex = 2;
1001
1002                         this._style.zIndex = 5;
1003
1004                         this.face = new enchant.Sprite(64, 64);
1005                         this.face.image = game.assets[EResPath.MARISA_FACE_NORMAL];
1006                         this.face.x = -CHARA_WIDTH / 2;
1007                         this.face.y = -CHARA_HEIGHT / 2;
1008                         this.face._style.zIndex = 0;
1009
1010                         this.faceStatus ={};
1011                         this.faceStatus[EFace.NORMAL] = EResPath.MARISA_FACE_NORMAL;
1012                         this.faceStatus[EFace.CRY1] = EResPath.MARISA_FACE_CRY1;
1013                         this.faceStatus[EFace.CRY2] = EResPath.MARISA_FACE_CRY2;
1014                         this.faceStatus[EFace.ANGRY] = EResPath.MARISA_FACE_ANGRY;
1015                         this.faceStatus[EFace.HAPPY1] = EResPath.MARISA_FACE_HAPPY1;
1016                         this.faceStatus[EFace.HAPPY2] = EResPath.MARISA_FACE_HAPPY2;
1017                         this.faceStatus[EFace.EAT1] = EResPath.MARISA_FACE_EAT1;
1018                         this.faceStatus[EFace.EAT2] = EResPath.MARISA_FACE_EAT2;
1019                         this.faceStatus[EFace.TIRED] = EResPath.MARISA_FACE_TIRED;
1020                         this.faceStatus[EFace.SLEEP] = EResPath.MARISA_FACE_SLEEP;
1021
1022
1023                         this.hear = new enchant.Sprite(64, 64);
1024                         this.hear.image = game.assets[EResPath.MARISA_HEAR];
1025                         this.hear.x = -CHARA_WIDTH / 2;
1026                         this.hear.y = -CHARA_HEIGHT / 2;
1027                         this.hear._style.zIndex = 0;
1028
1029                         this.okazari = new Okazari();
1030                         this.okazari.image._element.style.zIndex = 1;
1031
1032                         // this.imgGroup._style.zIndex = 1;
1033
1034                         this.imgGroup.addChild(this.shadow);
1035                         this.imgBody.addChild(this);
1036                         this.imgBody.addChild(this.face);
1037                         this.imgBody.addChild(this.hear);
1038                         this.imgBody.addChild(this.okazari);
1039                         this.imgGroup.addChild(this.imgBody);
1040                         backgroundMap.addChild(this.imgGroup);
1041                         this._tweet = new TTweet(148, 64);
1042                         this._tweet.image._element.style.zIndex = 20;
1043
1044                         this.imgGroup.addChild(this._tweet);
1045                         this.imgGroup.x = x;
1046                         this.imgGroup.y = y;
1047                         this.animation();
1048                         this.action = new Action(this);
1049                         this.id = guid();
1050                         ctl.addObj(this);
1051                 },
1052                 setFaceImage: function(eface){
1053                         var path = this.faceStatus[eface];
1054                         this.face.image = game.assets[path];
1055                 }
1056         });
1057         var Player = enchant.Class.create(Marisa,{
1058                 initialize: function(x, y){
1059                         // this.runEnterframe = function(){
1060                         // },
1061                         Marisa.call(this, x, y);
1062                         this.classname = "Player";
1063                         ctl.setPlayer(this);
1064                         // this.removeEventListener('enterframe',this.runEnterframe);
1065                         this.addEventListener('enterframe', function(){
1066                                 if(this.age%10 === 0 && this.age !== 0){
1067                                         // console.log("palyer:" + this.age);
1068                                 }
1069                                 this.act();
1070
1071                                 ///////////////////////////////////////////////
1072                                 //Action pattern for debug
1073                                 if(game.input.left){
1074                                         // this.changeFace(EResPath.MARISA_FACE_TIRED);
1075                                         this.moveX(-10);
1076                                         this.direction = EDirection.LEFT;
1077                                 }
1078                                 if(game.input.right){
1079                                         this.moveX(10);
1080                                         this.direction = EDirection.RIGHT;
1081                                 }
1082                                 if(game.input.up){
1083                                         this.moveY(-10);
1084                                         this.tweet("ゆっくりしていってね!");
1085                                 }
1086                                 if(game.input.down){
1087                                         this.moveY(10);
1088                                 }
1089                                 ///////////////////////////////////////////////
1090                                 this.reverse();
1091                                 // if(ctl.backgroundMap.hitTest(this.imgGroup.x, this.imgGroup.y + this.height / 2)){
1092                                 //      console.log("hit:" + this.imgGroup.x + ":" +  (this.imgGroup.y + this.height/2) );
1093                                 // }else{
1094                                 //      console.log("not:" + this.imgGroup.x + ":" +  (this.imgGroup.y + this.image.height/2));
1095                                 // }
1096                                 //- Display the circle of search range.
1097                                 // fieldBg.image.clear();
1098                                 // fieldBg.image.context.beginPath();
1099                                 // fieldBg.image.context.fillStyle = '#ff0000';
1100                                 // fieldBg.image.context.arc(this.imgGroup.x + this.width/2, this.imgGroup.y + this.height/2, this.range, Math.PI * 2, false);
1101                                 // fieldBg.image.context.fill();
1102                         });
1103                 }
1104         });
1105         game.onload = function(){
1106                 def.Food = Food;
1107                 mangTouch = new MangTouch(ctl);
1108                 mangMsg = new MangMsg(ctl);
1109                 ctl.setDefined(def);
1110                 backgroundMap.image = game.assets[EResPath.MAP0];
1111                 backgroundMap.loadData(_mapData);
1112                 backgroundMap.collisionData = _collisionData;
1113
1114                 var menuSurface = new enchant.Surface(EMenuPos.WIDTH, EMenuPos.HEIGHT);
1115                 var fieldSurface = new enchant.Surface(EFieldPos.WIDTH, EFieldPos.HEIGHT);
1116                 fieldBg.image = fieldSurface;
1117                 fieldBg.x = 0;
1118                 fieldBg.y = 0;
1119
1120                 menuSurface.context.fillStyle = '#000';
1121                 menuSurface.context.fillRect(0, 0, EMenuPos.WIDTH, EMenuPos.HEIGHT);
1122                 menuBg.image = menuSurface;
1123                 menuBg.x = EMenuPos.X;
1124                 menuBg.y = EMenuPos.Y;
1125
1126                 backgroundMap.addChild(fieldBg);
1127                 mangIcon = new MangIcon(ctl, menuBg);
1128
1129                 game.rootScene.addChild(backgroundMap);
1130                 game.rootScene.addChild(menuBg);
1131
1132                 // var labelGroup = new LabelGroup();
1133                 mangLabel = new MangLabel(ctl, menuBg);
1134                 // var info = new Label("ゆっくり");
1135                 // info.color = "#ffffff";
1136                 // info.font = "14px 'Times New Roman'";
1137                 // info.x = 4;
1138                 // info.y = GAME_HEIGHT / 2;
1139                 // labelGroup.addChild(info);
1140                 // menuBg.addChild(labelGroup);
1141
1142
1143                 // var food = new def.Food("apple", 200, 250);
1144                 backgroundMap.addChild(foodGroup);
1145                 var player = new Player(PALYER_INIT_X, PALYER_INIT_Y);
1146                 // new Marisa(PALYER_INIT_X + 200, PALYER_INIT_Y);
1147                 var touchX = 0;
1148
1149                 game.rootScene.addEventListener('touchstart', function (e) {
1150                         game.touched = true;
1151                         var mang = mangTouch.get(ctl.getCurrentCommand());
1152                         mang.touchstart(e);
1153                 });
1154                 game.rootScene.addEventListener('touchmove', function (e) {
1155                         var mang = mangTouch.get(ctl.getCurrentCommand());
1156                         mang.touchmove(e);
1157                 });
1158                 game.rootScene.addEventListener('touchend', function (e) {
1159                         var mang = mangTouch.get(ctl.getCurrentCommand());
1160                         mang.touchend(e);
1161                         game.touched = false;
1162                 });
1163
1164
1165                 game.rootScene.addEventListener('enterframe', function(){
1166                 //main frame
1167                         mangLabel.draw(player);
1168                 });
1169
1170                 this.addEventListener('enterframe', function(){
1171                         //The priority processing of display instead of z-index
1172                         backgroundMap.childNodes.sort(
1173                                 function(a,b){
1174                                         if(typeof a.id === "undefined" && typeof b.id === "undefined"){
1175                                                 return 0;
1176                                         }
1177                                         else if(typeof a.id === "undefined"){
1178                                                 return -1;
1179                                         }
1180                                         else if(typeof b.id === "undefined"){
1181                                                 return 1;
1182                                         }
1183                                         if(a.y < b.y)return -1;
1184                                         if(a.y > b.y)return 1;
1185                                         return 0;
1186                                 }
1187                         );
1188                 });
1189
1190         };
1191         game.start();
1192 };