OSDN Git Service

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