OSDN Git Service

chromeでローカルでも動かせるにajax通信廃止。バグ対策思案中
[yukkurioverwint/YukkuriOverwinter.git] / main.js
1 enchant();
2 //////////////////////////////////////
3 /*
4     CanvasLayer初期化後にcanvasのコンテキストを操作する
5 */
6 // var initCanvas = CanvasLayer.prototype.initialize;
7
8 // CanvasLayer.prototype.initialize = function() {
9 //     initCanvas.call(this);
10 //     console.log("canvas");
11
12 //     // 拡大時の処理をNearest Neighbor方式に切り替え
13 //     // ただしChromeとFireFoxでのみ動作
14 //     this.context.imageSmoothingEnabled = false;
15 //     this.context.webkitImageSmoothingEnabled = false;
16 //     this.context.mozImageSmoothingEnabled = false;
17 // };
18 //global
19 var ctl = null;
20 var mangIcon = null;
21 var mangTouch = null;
22 var mangLabel = null;
23 var mangMsg = null;
24 var LabelGroup = enchant.Class.mixClasses(Label, Group,true);
25 var net = new Net();
26 //////////////////////////////////////
27 //define
28 //////////////////////////////////////
29 var PALYER_INIT_X = 100;
30 var PALYER_INIT_Y = 100;
31 var CHARA_WIDTH = 64;
32 var CHARA_HEIGHT = 64;
33 var GAME_WIDTH = 800;
34 var GAME_HEIGHT = 600;
35 var FPS = 20;
36 var BASE_FPS = 20;
37 var HUNGRY_MAX = 100;
38 var MIN = BASE_FPS;
39 var HOUR = MIN * 60;
40 // var DAY = HOUR * 24;
41 var DAY = HOUR ;
42 var GAME_START_TIME = HOUR * 6;
43
44 var EResPath = {
45         YUKKURI_BASE : './data/yukkuri_base.png',
46         YUKKURI_BODY_DEAD : './data/yukkuri_dead.png',
47         YUKKURI_UNUN : './data/unun.png',
48         YUKKURI_SHADOW : './data/shadow.png',
49         MARISA_FACE_NORMAL : './data/marisa/face_normal1.png',
50         MARISA_FACE_TIRED : './data/marisa/face_tired1.png',
51         MARISA_FACE_ANGRY : './data/marisa/face_angry1.png',
52         MARISA_FACE_CRY1 : './data/marisa/face_cry1.png',
53         MARISA_FACE_CRY2 : './data/marisa/face_cry2.png',
54         MARISA_FACE_EAT1 : './data/marisa/face_eat1.png',
55         MARISA_FACE_EAT2 : './data/marisa/face_eat2.png',
56         MARISA_FACE_HAPPY1 : './data/marisa/face_happy1.png',
57         MARISA_FACE_HAPPY2 : './data/marisa/face_happy2.png',
58         MARISA_FACE_SLEEP : './data/marisa/face_sleep1.png',
59         MARISA_HEAR : './data/marisa/hear1.png',
60         OBJECT : './data/object.png',
61         MAP0 : './data/map0.png',
62         COMMAND : './data/command.png',
63         OKAZARI : './data/okazari.png',
64         PLACE_TREE : './data/system/place_tree.png',
65         GRASS : './data/system/grass.png',
66         PLAYER_BADGE : './data/system/player_badge.png',
67         // PLACE_TREE : './data/system/rapture.png',
68 };
69 var EResPathReimu = {
70         getName: function(){
71                 return "reimu";
72         },
73         FACE_NORMAL : './data/reimu/face_normal1.png',
74         FACE_TIRED : './data/reimu/face_tired1.png',
75         FACE_ANGRY : './data/reimu/face_angry1.png',
76         FACE_CRY1 : './data/reimu/face_cry1.png',
77         FACE_CRY2 : './data/reimu/face_cry2.png',
78         FACE_EAT1 : './data/reimu/face_eat1.png',
79         FACE_EAT2 : './data/reimu/face_eat2.png',
80         FACE_HAPPY1 : './data/reimu/face_happy1.png',
81         FACE_HAPPY2 : './data/reimu/face_happy2.png',
82         FACE_SLEEP : './data/reimu/face_sleep1.png',
83         HEAR : './data/reimu/hear1.png',
84         OKAZARI : './data/reimu/okazari.png',
85         SUB1 : './data/reimu/sub1.png',
86
87 };
88
89 var EFace = {
90         NORMAL:0,
91         TIRED:1,
92         ANGRY:2,
93         CRY1:3,
94         CRY2:4,
95         EAT1:5,
96         EAT2:6,
97         HAPPY1:7,
98         HAPPY2:8,
99         SLEEP:9,
100 };
101 var EBody = {
102         NORMAL: 0,
103         DEAD: 1,
104 };
105
106 var ECommand = {
107         WORLD_CURSOR: 0,
108         WORLD_TARGET: 1,
109         WORLD_APPLE: 2,
110         WORLD_PLACE: 3,
111 };
112
113 var EMenuPos = {
114         X : 600,
115         Y : 0,
116         WIDTH : 200,
117         HEIGHT : GAME_HEIGHT,
118 };
119 var EFieldPos = {
120         X : 0,
121         Y : 0,
122         WIDTH: GAME_WIDTH - EMenuPos.WIDTH,
123         HEIGHT: GAME_HEIGHT,
124 };
125 var EAction = {
126         WAIT : 0,
127         HUNT : 3,
128         THINK : 4,
129         WALK : 5,
130         DEAD: 6,
131         EAT_START : 8,
132         EAT_WAIT : 9,
133         MOVE_TO_EAT_START: 10,
134         MOVE_TO_EAT_UNDERWAY: 11,
135         MOVE_TO_COMMAND: 12,
136         UNUN_START : 13,
137         UNUN_WAIT : 14,
138         UNUN_SMELL_START : 15,
139         UNUN_SMELL_WAIT : 16,
140         SLEEP_START : 17,
141         SLEEP_WAIT : 18,
142         COMMUNICATE_START : 19,
143         COMMUNICATE_WAIT: 20,
144         COMMUNICATE_RECV: 21,
145         MOVE_TO_PLACE_START_HOMELESS: 22,
146         MOVE_TO_PLACE_HOMELESS_UNDERWAY: 23,
147         DECLARE_PLACE_HOMELESS: 24,
148         MOVE_TO_HUNT_START: 25,
149         THINK_SCHEDULE: 26,
150         HUNT_START: 27,
151         MOVE_GOTO_HOME: 28,
152         IN_HOME: 29,
153         THINK_IN_HOME: 30,
154         EAT_IN_HOME: 31,
155         NONE: 9999
156 };
157 var EDirection = {
158         LEFT : 0,
159         RIGHT: 1,
160 };
161 var EMsg = {
162         WALK: 0,
163         SLEEP: 1,
164         EAT: 2,
165         GET_UP: 3,
166         DEAD: 4,
167         UNUN: 5,
168         UNUN_END: 6,
169         UNUN_SMELL: 7,
170         SEARCH_FOOD: 8,
171         EAT_FAILED: 9,
172         SAY_HELLO: 10,
173         TARGETING_EAT : 11,
174         DECLARE_PLACE: 12,
175         HUNT: 13,
176         REHUNT: 14,
177         HUNT_ESCAPE: 15,
178         YYUYYU: 16,
179         MOVE_GOTO_HOME:17,
180         HUNTING_MESSAGE:18,
181 };
182 var EWalkAnimation = {
183         START: 0,
184         END: 1,
185 };
186 var EGrow = {
187         ADULT : 0,
188         SUBADULT : 1,
189         CHILD : 2,
190         BABY : 3,
191 };
192 var EDate = {
193         MORNING : 0,
194         NOON : 1,
195         AFTERNOON : 2,
196         EVENING : 3,
197         NIGHT : 4,
198         MIDNIGHT : 5
199 };
200 var ESearch = {
201         UNUN: 0,
202         FOOD: 1,
203         YUKKURI: 2,
204         PLACE: 3
205 };
206 var EFood = {
207         BAD : 0,
208         NORMAL : 1,
209         GREAT : 2
210 };
211 var ObjSprite = enchant.Class.create(enchant.Sprite,{
212         initialize: function (w, h){
213                 enchant.Sprite.call(this, w, h);
214                 this.id = guid();
215                 ctl.addObj(this);
216
217
218                                 // // new BoxLabel(this.ctl,100, e.x, e.y);
219                                 // var box = new Box(this.ctl, 200, 200);
220
221                                 // box.text("テスト\naaaaaa\nfffffffff\nテスト\naaaaaa\nfffffffff\nテスト\naaaaaa\n", e.x, e.y);
222
223         },
224         getStatusForDisplay: function (){
225                 return "";
226         },
227         getX: function(){
228                 return this.x;
229         },
230         getY: function(){
231                 return this.y;
232         },
233         getWidth: function(){
234                 return this.width;
235         },
236         getHeight: function(){
237                 return this.height;
238         },
239         removeAll: function(){
240                 this.remove();
241                 ctl.removeObj(this);
242         }
243 });
244
245
246 enchant.Timeline.prototype.moveByEx = function(x, y, time, easing, eventFunctions){
247         var params = {
248                 x: function() {
249                         return this.x + x;
250                 },
251                 y: function() {
252                         return this.y + y;
253                 },
254                 time: time,
255                 easing: easing
256         };
257         //-Event register [onactionstart,onactiontick,onactionend]
258         if(eventFunctions !== undefined){
259                 for(var key in eventFunctions){
260                         params[key] = eventFunctions[key];
261                 }
262         }
263         return this.tween(params);
264 };
265 function plusFrameScale(frame, scale){
266         return (frame/2) * (1 - scale)
267 }
268
269
270
271 include("./lang/ja.js");
272 window.onload = function(){
273         var nodeSort = function (a, b){
274                 if(typeof a.id === "undefined" && typeof b.id === "undefined"){
275                         return 0;
276                 }
277                 else if(typeof a.id === "undefined"){
278                         return -1;
279                 }
280                 else if(typeof b.id === "undefined"){
281                         return 1;
282                 }
283                 var ay = a.y;
284                 var by = b.y;
285                 if(a instanceof Box && b instanceof Box){
286                         return 0;
287                 }
288                 if(a instanceof Box){
289                         return 1;
290                 }
291                 if(b instanceof Box){
292                         return -1;
293                 }
294                 if(a instanceof SpriteGroup){
295                         ay = a.y;
296                         ay += a.height/2;
297                 }
298                 if(b instanceof SpriteGroup){
299                         by = b.y;
300                         by += b.height /2;
301                 }
302
303                 if(a instanceof Place){
304                         ay += Math.round(a.height * 0.7);
305                 }
306                 if(b instanceof Place){
307                         by += Math.round(b.height * 0.7);
308                 }
309
310                 if(ay < by)return -1;
311                 else if(ay > by)return 1;
312                 if(a.id < b.id)return -1;
313                 else if(a.id > b.id)return 1;
314                 return 0;
315
316         }
317
318         //init game
319         var def = new Object();
320         var foodGroup = new Group();
321         var SpriteGroup = enchant.Class.mixClasses(Sprite, Group,true);
322         var menuBg = new SpriteGroup(EMenuPos.WIDTH, EMenuPos.HEIGHT);
323         var fieldBg = new enchant.Sprite(EFieldPos.WIDTH, EFieldPos.HEIGHT);
324         var windowFront = new enchant.Sprite(EFieldPos.WIDTH, EFieldPos.HEIGHT);
325         var game = new Game(GAME_WIDTH, GAME_HEIGHT);
326         game.fps = FPS;
327         var _loadArr = [];
328         var i=0;
329         var tmpName = EResPathReimu.getName();
330         i=0;
331         for(var _v in EResPathReimu){
332                 if(typeof EResPathReimu[_v] === "function")continue;
333                 EResPath[_v + "_" + tmpName] = EResPathReimu[_v];
334                 i++;
335         }
336         tmpName = null;
337
338         i=0;
339         for(var _v in EResPath){
340                 _loadArr[i] = EResPath[_v];
341                 i++;
342         }
343         game.preload(_loadArr);
344         var MapGroup = enchant.Class.mixClasses(Map, Group,true);
345         var backgroundMap = new MapGroup(16, 16);
346         include("./class.js");
347         include("./ctrl.js");
348         ctl = new Ctrl();
349         ctl.init(game);
350         ctl.setBackgroundMap(backgroundMap);
351         var Action = enchant.Class.create({
352                 initialize: function (yukkuri){
353                         this.status = EAction.THINK;
354                         this.yukkuri = yukkuri;
355                         this.isMoving = false;
356                         this.targetNode = null;
357                 },
358                 getStatus: function(){
359                         return this.status;
360                 },
361                 setStatus: function(eaction){
362                         this.status = eaction;
363                 },
364                 observe: function(){
365                         if(this.yukkuri.isDead()){
366                                 this.status = EAction.DEAD;
367                         }
368                         // else if(this.yukkuri.isSleep()){
369                         //      this.status = EAction.SLEEP;
370                         // }
371
372                 },
373                 act : function(){
374
375                         this.observe();
376                         switch(this.status){
377                                 case EAction.WAIT:
378                                         this.waitCnt++;
379                                         if(this.waitCnt >= BASE_FPS * 60 * 24 * 3){
380                                                 this.waitCnt = 0;
381                                                 this.setStatus(EAction.THINK);
382                                                 console.log("wait cnt end");
383                                         }
384                                 break;
385                                 case EAction.THINK_IN_HOME:
386                                         this.waitCnt = 0;
387                                         this.search();
388                                         this.think_in_home();
389                                 break;
390                                 case EAction.EAT_IN_HOME:
391                                         this.eat_in_home();
392                                 break;
393                                 case EAction.THINK:
394                                         this.waitCnt = 0;
395                                         if(this.yukkuri.age%2 === 0){
396                                                 this.yukkuri.changeFace();
397                                                 this.search();
398                                                 this.yukkuri.searchObjectMang.callEvent(this.yukkuri);
399                                         }
400                                 break;
401                                 case EAction.MOVE_TO_EAT_START:
402                                         this.move_to_eat_start();
403                                 break;
404                                 case EAction.MOVE_TO_EAT_UNDERWAY:
405                                 break;
406                                 //Random Walk
407                                 case EAction.WALK:
408                                         this.walk(0);
409                                 break;
410                                 case EAction.SLEEP_START:
411                                         this.sleep_start();
412                                 break;
413                                 case EAction.SLEEP_WAIT:
414                                 break;
415                                 case EAction.DEAD:
416                                         this.dead();
417                                 break;
418                                 case EAction.EAT_START:
419                                         this.eat_start();
420                                 break;
421                                 case EAction.EAT_WAIT:
422                                 break;
423                                 case EAction.UNUN_START:
424                                         this.unun_start();
425                                 break;
426                                 case EAction.UNUN_WAIT:
427                                 break;
428                                 case EAction.MOVE_TO_COMMAND:
429                                         this.move_to_command();
430                                 break;
431                                 case EAction.UNUN_SMELL_START:
432                                         this.unun_smell_start();
433                                 break;
434                                 case EAction.COMMUNICATE_START:
435                                         this.communicate_start();
436                                 break;
437                                 case EAction.COMMUNICATE_WAIT:
438                                 break;
439                                 case EAction.COMMUNICATE_RECV:
440                                 break;
441                                 case EAction.MOVE_TO_PLACE_START_HOMELESS:
442                                         this.move_to_place_start_homeless();
443                                 break;
444                                 case EAction.DECLARE_PLACE_HOMELESS:
445                                         this.declare_place_homeless();
446                                 break;
447                                 case EAction.MOVE_TO_HUNT_START:
448                                         this.move_to_hunt_start();
449                                 break;
450                                 case EAction.HUNT_START:
451                                         this.hunt_start();
452                                 break;
453                                 case EAction.MOVE_GOTO_HOME:
454                                         this.move_goto_home();
455                                 break;
456                                 case EAction.IN_HOME:
457                                         this.in_home();
458                                 break;
459
460                         }
461                         this.yukkuri.reverse();
462                 },
463                 thinkHavePlace: function(){
464                         var date = ctl.getDate();
465                         if(this.yukkuri.isPlaceRange()){
466                         }else{
467                                 //ゆっくりプレイスの範囲外
468                                 if(date === EDate.EVENING
469                                         || date === EDate.NIGHT
470                                         || date === EDate.MIDNIGHT
471                                         ){
472                                         this.setStatus(EAction.MOVE_GOTO_HOME);
473                                         return;
474                                 }
475                                 else if(date === EDate.EVENING){
476                                         // if(this.yukkuri.isItemsMax()){
477                                         //      this.setStatus(EAction.IN_HOME);
478                                         // }
479                                         // else{
480                                         //      this.setStatus(EAction.MOVE_TO_HUNT_START);
481                                         // }
482                                 }
483                         }
484                         if(this.yukkuri.isVeryKuhuku()){
485                                 //食べ物探したい
486                                 //なかったら
487                                 this.setStatus(EAction.MOVE_GOTO_HOME);
488                         }
489                         else if(this.yukkuri.isSleep()){
490                                 this.setStatus(EAction.SLEEP_START);
491                         }
492                         else if(this.yukkuri.getUnun() >= 80){
493                                 this.setStatus(EAction.UNUN_START);
494                         }
495                         else{
496                                 this.yukkuri.changeFace();
497                                 this.search();
498                         }
499                 },
500                 think_in_home: function(){
501                         var date = ctl.getDate();
502                         if((date === EDate.MIDNIGHT)
503                                 && !this.yukkuri.isSleep()
504                                 && !this.yukkuri.isVeryKuhuku()
505                                 ){
506                                 new SleepEvent({
507                                         "action": this,
508                                         "nextStatus":EAction.THINK_IN_HOME,
509                                         "myYukkuri": this.yukkuri
510                                 });
511                                 return;
512                         }
513                         else if(date === EDate.EVENING){
514                                 //QK
515                         }
516                         else if(this.yukkuri.isVeryKuhuku() && this.yukkuri.getPlace().isFood()){
517                                 this.setStatus(EAction.EAT_IN_HOME);
518                         }
519                         else{
520                                 //marisa is hunting move
521                                 if(this.yukkuri.beforeHuntingFood !== null){
522                                         new MoveToHuntEvent({
523                                                 "type": 'food',
524                                                 "targetNode": this.yukkuri.beforeHuntingFood,
525                                                 "action": this,
526                                                 "myYukkuri": this.yukkuri
527                                         });
528                                         return;
529                                 }else{
530                                         this.setStatus(EAction.WALK);
531                                 }
532                         }
533                 },
534                 eat_in_home: function(){
535                         new EatInHomeEvent({
536                                 "targetNode": this.yukkuri.getPlace(),
537                                 "action": this,
538                                 "myYukkuri": this.yukkuri
539                         });
540                 },
541                 move_to_eat_start : function(){
542                         new MoveToEatEvent({
543                                 "type": 'food',
544                                 "targetNode": this.targetNode,
545                                 "action": this,
546                                 "myYukkuri": this.yukkuri
547                         });
548                 },
549                 walkNew: function(){
550
551                 },
552                 walk : function(retryCounter){
553                         if(retryCounter > 15){
554                                 retryCounter = 0;
555                                 this.status = EAction.NONE;
556                                 console.log("retryCount over");
557                                 return;
558                         }
559                         if(this.yukkuri.imgGroup.tl.queue.length === 0){
560                                 var frame = 200;
561                                 frame += plusFrameScale(frame, this.yukkuri.getMyScale());
562                                 if(this.isMoving){
563                                         this.isMoving = false;
564                                         this.status = EAction.THINK;
565                                         this.yukkuri.vx = 0;
566                                         this.yukkuri.vy = 0;
567                                 }else{
568                                         var xpos = 150 + rand(40) - (retryCounter * 10);
569                                         var ypos = 150 + rand(40) - (retryCounter * 10);
570                                         if(rand(8) === 0){
571                                                 this.yukkuri.vx = 0;
572                                                 this.yukkuri.vy = -ypos;
573                                         }
574                                         else if(rand(8) == 1){
575                                                 this.yukkuri.vx = xpos;
576                                                 this.yukkuri.vy = -ypos;
577                                                 this.yukkuri.direction = EDirection.RIGHT;
578                                         }
579                                         else if(rand(8) == 2){
580                                                 this.yukkuri.vx = xpos;
581                                                 this.yukkuri.vy = 0;
582                                                 this.yukkuri.direction = EDirection.RIGHT;
583                                         }
584                                         else if(rand(8) == 3){
585                                                 this.yukkuri.vx = xpos;
586                                                 this.yukkuri.vy = ypos;
587                                                 this.yukkuri.direction = EDirection.RIGHT;
588                                         }
589                                         else if(rand(8) == 4){
590                                                 this.yukkuri.vx = 0;
591                                                 this.yukkuri.vy = ypos;
592                                         }
593                                         else if(rand(8) == 5){
594                                                 this.yukkuri.vx = -xpos;
595                                                 this.yukkuri.vy = ypos;
596                                                 this.yukkuri.direction = EDirection.LEFT;
597                                         }
598                                         else if(rand(8) == 6){
599                                                 this.yukkuri.vx = -xpos;
600                                                 this.yukkuri.vy = 0;
601                                                 this.yukkuri.direction = EDirection.LEFT;
602                                         }
603                                         else if(rand(8) == 7){
604                                                 this.yukkuri.vx = -xpos;
605                                                 this.yukkuri.vy = -ypos;
606                                                 this.yukkuri.direction = EDirection.LEFT;
607                                         }
608                                         if (this.yukkuri.vx || this.yukkuri.vy) {
609                                                 var map = ctl.backgroundMap;
610                                                 // var x = this.yukkuri.x + (this.yukkuri.moveX ? this.yukkuri.moveX / Math.abs(this.yukkuri.moveX) * 16 : 0) + 16;
611                                                 // var y = this.yukkuri.y + (this.yukkuri.moveY ? this.yukkuri.moveY / Math.abs(this.yukkuri.moveY) * 16 : 0) + 16;
612                                                 var x = this.yukkuri.imgGroup.x + this.yukkuri.vx;
613                                                 var y = this.yukkuri.imgGroup.y + this.yukkuri.vy + this.yukkuri.height / 2;
614                                                 if (0 <= x && x < map.width && 0 <= y && y < map.height && !map.hitTest(x, y)) {
615                                                         // console.log("あたってないよ:"+ this.yukkuri.imgGroup.x + ":" + this.yukkuri.imgGroup.y);
616                                                         // console.log("X:" + this.yukkuri.moveX);
617                                                         // console.log("Y:" + this.yukkuri.moveY);
618                                                         this.isMoving = true;
619                                                         this.yukkuri.reverse();
620                                                         this.yukkuri.moveBy(this.yukkuri.vx, this.yukkuri.vy, frame);
621                                                         mangMsg.output(this.yukkuri,EMsg.WALK);
622                                                         //うんうんくさい対策
623                                                         this.targetNode = null;
624                                                 }else{
625                                                         // console.log("HIT:"+ this.yukkuri.imgGroup.x + ":" + this.yukkuri.imgGroup.y);
626                                                         this.walk(retryCounter+1);
627                                                 }
628                                         }
629                                 }
630                         }
631                 },
632                 sleep_start: function(){
633                         new SleepEvent({
634                                 "action": this,
635                                 "myYukkuri": this.yukkuri
636                         });
637                 },
638                 dead : function(){
639                         mangMsg.output(this.yukkuri, EMsg.DEAD);
640                         this.yukkuri.tlPause();
641                         this.yukkuri.setBodyImage(EBody.DEAD);
642                 },
643                 eat_start: function(){
644                         new EatEvent({
645                                 "type": 'eat',
646                                 "targetNode": this.targetNode,
647                                 "action": this,
648                                 "myYukkuri": this.yukkuri
649                         });
650
651                 },
652                 unun_start: function(){
653                         new UnunEvent({
654                                 "action": this,
655                                 "myYukkuri": this.yukkuri
656                         });
657                 },
658                 unun_smell_start: function(){
659                         new UnunSmellEvent({
660                                 "action": this,
661                                 "myYukkuri": this.yukkuri
662                         });
663                 },
664                 communicate_start:function(){
665                         new ComSayHelloSendEvent({
666                                 "type": 'greeting',
667                                 "targetNode": this.targetNode,
668                                 "action": this,
669                                 "myYukkuri": this.yukkuri
670                         });
671                 },
672                 move_to_place_start_homeless:function(){
673                         new MoveToPlaceStartHomelessEvent({
674                                 "targetNode": this.targetNode,
675                                 "action": this,
676                                 "myYukkuri": this.yukkuri
677                         });
678                 },
679                 declare_place_homeless:function(){
680                         new DeclarePlaceHomelessEvent({
681                                 "targetNode": this.targetNode,
682                                 "action": this,
683                                 "myYukkuri": this.yukkuri
684                         });
685                 },
686                 move_to_hunt_start:function(){
687                         new MoveToHuntEvent({
688                                 "type": 'food',
689                                 "targetNode": this.targetNode,
690                                 "action": this,
691                                 "myYukkuri": this.yukkuri
692                         });
693                 },
694                 hunt_start:function(){
695                         new HuntStartEvent({
696                                 "type": 'food',
697                                 "targetNode": this.targetNode,
698                                 "action": this,
699                                 "myYukkuri": this.yukkuri
700                         });
701                 },
702                 move_goto_home:function(){
703                         var place = this.yukkuri.getPlace();
704                         new MoveGotoHomeEvent({
705                                 "type": 'hunt_end',
706                                 "targetNode": place,
707                                 "action": this,
708                                 "myYukkuri": this.yukkuri
709                         });
710                 },
711                 in_home:function(){
712                         var place = this.yukkuri.getPlace();
713                         new InHomeEvent({
714                                 "type": 'hunt_end',
715                                 "targetNode": place,
716                                 "action": this,
717                                 "myYukkuri": this.yukkuri
718                         });
719                 },
720                 move_to_command: function(){
721
722                 },
723                 search: function(){
724                         this.yukkuri.clearSearchObject();
725                         var nodes = ctl.getObjs();
726                         l = nodes.length;
727                         var ununFlg = false;
728
729                         for (var key in nodes) {
730                                 var node = nodes[key];
731                                 if(this.yukkuri.id === node.id)continue;
732                                 //うんうん臭いを検索
733                                 if (
734                                         node instanceof Unun
735                                         && !(this.targetNode instanceof Unun)
736                                         && this.yukkuri.within(node, this.yukkuri.getRange() / 3)) {
737
738                                         this.yukkuri.addSearchObject(ESearch.UNUN, node);
739                                 }
740                                 //食べ物を検索
741                                 else if (node instanceof Food && node.amount > 0) {
742                                         if(this.yukkuri.within(node, this.yukkuri.getRange())){
743                                                 this.yukkuri.addSearchObject(ESearch.FOOD, node);
744                                         }
745                                 }
746                                 //ゆっくりを検索
747                                 else if(node instanceof Yukkuri){
748                                         if(this.yukkuri.within(node, this.yukkuri.getRange())){
749                                                 this.yukkuri.addSearchObject(ESearch.YUKKURI, node);
750                                         }
751                                 }
752                                 //ゆっくりプレイスを検索
753                                 else if(node instanceof Place){
754                                         if(this.yukkuri.within(node, this.yukkuri.getRange())){
755                                                 this.yukkuri.addSearchObject(ESearch.PLACE, node);
756                                         }
757                                 }
758                         }
759                         this.status =  EAction.WALK;
760                 }
761         });
762 /**
763  * Range範囲内のObjSprを管理する
764  * 注意!:ゆっくりプレイスを持って入るもののみ使用される
765  * @param  {[type]} )
766  * @return {[type]}
767  */
768         var SearchObjectMang = enchant.Class.create({
769                 initialize: function (){
770                         this.init();
771                 },
772                 init:function(){
773                         this.datas = [];
774                         for(var key in ESearch){
775                                 var esearch = ESearch[key];
776                                 this.datas[esearch] = [];
777                         }
778                 },
779                 add :function(esearch, node){
780                         this.datas[esearch].push(node);
781                 },
782                 clear :function(){
783                         for(var key in ESearch){
784                                 var esearch = ESearch[key];
785                                 this.datas[esearch].length = 0;
786                         }
787                         // this.datas.length = 0;
788                         // this.init();
789                 },
790                 get:function (esearch){
791                         return this.datas[esearch];
792                 },
793                 getNear:function (esearch, yukkuri){
794                         var objects = this.datas[esearch];
795                         var nearDistance = null;
796                         var ret = null;
797                         for(var i=0;i<objects.length;i++){
798                                 var object = objects[i];
799                                 var nowDistance = object.getX() + object.getY() - yukkuri.getX() + yukkuri.getY();
800                                 if(nearDistance === null || nearDistance > nowDistance){
801                                         nearDistance = nowDistance;
802                                         ret = object;
803                                 }
804                         }
805                         return ret;
806                 },
807                 callEvent: function(yukkuri){
808                         if(yukkuri.isItemsMax() && yukkuri.isPlace()){
809                                 yukkuri.action.targetNode = yukkuri.getPlace();
810                                 yukkuri.action.setStatus(EAction.MOVE_GOTO_HOME);
811                                 return;
812                         }
813                         if(yukkuri.isVeryKuhuku()){
814                                 //エサを探す
815                                 var foods = this.get(ESearch.FOOD);
816                                 if(foods.length === 0 && yukkuri.isPlace()){
817                                         //家があるなら家に帰宅
818                                         yukkuri.action.targetNode = yukkuri.getPlace();
819                                         yukkuri.action.setStatus(EAction.MOVE_GOTO_HOME);
820                                         return;
821                                 }else{
822                                         var foods = this.get(ESearch.FOOD);
823                                         food = this.getNear(ESearch.FOOD, yukkuri);
824                                         if(food !== null){
825                                                 yukkuri.action.targetNode = food;
826                                                 yukkuri.action.setStatus(EAction.MOVE_TO_EAT_START);
827                                                 return;
828                                         }
829                                 }
830                         }
831
832                         if(yukkuri.isSleep()){
833                                 yukkuri.action.setStatus(EAction.SLEEP_START);
834                                 return;
835                         }
836
837                         else if(yukkuri.getUnun() >= 80){
838                                 yukkuri.action.setStatus(EAction.UNUN_START);
839                                 return;
840                         }
841
842
843                         if(!yukkuri.isItemsMax() && yukkuri.isPlace()){
844                                 food = this.getNear(ESearch.FOOD, yukkuri);
845                                 if(food !== null){
846                                         yukkuri.action.targetNode = food;
847                                         yukkuri.action.setStatus(EAction.MOVE_TO_HUNT_START);
848                                         return;
849                                 }
850                         }
851                         ununs = this.get(ESearch.UNUN);
852                         if(ununs.length !== 0){
853                                 yukkuri.action.targetNode = yukkuri.getPlace();
854                                 yukkuri.action.setStatus(EAction.UNUN_SMELL_START);
855                                 return;
856                         }
857                         var searchYukkuri = this.getNear(ESearch.YUKKURI, yukkuri);
858                         if(searchYukkuri !== null && yukkuri.isCommunication(searchYukkuri)){
859                                 yukkuri.action.targetNode = searchYukkuri;
860                                 yukkuri.action.setStatus(EAction.COMMUNICATE_START);
861                                 return;
862                         }
863                         if(!yukkuri.isPlace()){
864                                 var searchYukkuri = this.getNear(ESearch.PLACE, yukkuri);
865                                 if(searchYukkuri != null && !searchYukkuri.isOwner()){
866                                         yukkuri.action.targetNode = searchYukkuri;
867                                         yukkuri.action.setStatus(EAction.MOVE_TO_PLACE_START_HOMELESS);
868                                         return;
869                                 }
870                         }
871                         yukkuri.action.setStatus(EAction.WALK);
872
873                 }
874
875         });
876         var Event = enchant.Class.create({
877                 initialize: function (_data){
878                         this.guid = guid();
879                 },
880                 onactionstart:function(self){
881                         return this._onactionstart;
882                 },
883                 _onactionstart:function(e){
884                         if(typeof e === "undefined")return;
885                 },
886                 onactiontick:function(self){
887                         return function(e){
888                                 if(typeof e === "undefined")return;
889                         };
890                 },
891                 onactionend:function(self){
892                         return function(e){
893                                 if(typeof e === "undefined")return;
894                         };
895                 }
896         });
897         var WalkEvent = enchant.Class.create(Event,{
898                 initialize: function (_data){
899                         this.data = _data;
900                         this.yukkuri = _data.myYukkuri;
901                         this.action = _data.action;
902
903                         mangMsg.output(this.yukkuri, EMsg.SLEEP);
904                         this.action.setStatus(EAction.SLEEP_WAIT);
905                         this.yukkuri.setFaceImage(EFace.SLEEP);
906                         this.yukkuri.tlPause();
907                         var self = this;
908                         this.yukkuri.addEventListener('enterframe', function() {
909                                 var yukkuri = self.yukkuri;
910                                 self.run();
911                         });
912                 },
913                 run : function(){
914
915                 }
916         });
917         var SleepEvent = enchant.Class.create(Event,{
918                 initialize: function (_data){
919                         this.data = _data;
920                         this.yukkuri = _data.myYukkuri;
921                         this.action = _data.action;
922
923                         mangMsg.output(this.yukkuri, EMsg.SLEEP);
924                         this.action.setStatus(EAction.SLEEP_WAIT);
925                         this.yukkuri.setFaceImage(EFace.SLEEP);
926                         this.yukkuri.tlPause();
927                         var sec = FPS * 3;
928                         var self = this;
929                         this.yukkuri.addEventListener('enterframe', function() {
930                                 var yukkuri = self.yukkuri;
931                                 if(yukkuri.age%2 === 0 && yukkuri.age !== 0)yukkuri.param.sleep--;
932                                 if(yukkuri.param.sleep <= 0){
933                                         yukkuri.param.sleep = 0;
934                                         this.removeEventListener("enterframe",arguments.callee);
935                                         mangMsg.output(self.yukkuri, EMsg.GET_UP);
936                                         yukkuri.setFaceImage(EFace.NORMAL);
937                                         yukkuri.tlResume();
938                                         // self.action.setStatus(EAction.WAIT);
939                                         if(self.data.nextStatus !== undefined){
940                                                 self.action.setStatus(self.data.nextStatus);
941                                         }else{
942                                                 self.action.setStatus(EAction.THINK);
943                                         }
944                                 }
945                         });
946                 }
947         });
948
949         var UnunEvent = enchant.Class.create(Event,{
950                 initialize: function (_data){
951                         this.data = _data;
952                         this.yukkuri = _data.myYukkuri;
953                         this.action = _data.action;
954                         this.action.setStatus(EAction.UNUN_WAIT);
955                         mangMsg.output(this.yukkuri, EMsg.UNUN);
956                         this.yukkuri.setFaceImage(EFace.HAPPY2);
957                         var sec = FPS * 3;
958
959                         this.yukkuri.wait(sec, {
960                                 "onactionend": this.onactionend(this)
961                         });
962                 },
963                 onactionend:function(self){
964                         return function(e){
965                                 if(typeof e === "undefined")return;
966                                 self.yukkuri.unun();
967                                 self.action.setStatus(EAction.WAIT);
968                                 mangMsg.output(self.yukkuri, EMsg.UNUN_END);
969                                 new WaitEvent({
970                                         "waitframe": FPS * 1.5,
971                                         "action": self.action,
972                                         "myYukkuri": self.yukkuri
973                                 });
974                         };
975                 }
976         });
977         var WaitEvent = enchant.Class.create(Event,{
978                 initialize: function (_data){
979                         this.data = _data;
980                         this.yukkuri = _data.myYukkuri;
981                         this.action = _data.action;
982                         this.waitframe = _data.waitframe;
983                         this.yukkuri.wait(this.waitframe, {
984                                 "onactionstart": this.onactionstart(this),
985                                 "onactiontick": this.onactiontick(this),
986                                 "onactionend": this.onactionend(this)
987                         });
988                 },
989                 onactionend:function(self){
990                         return function(e){
991                                 if(typeof e === "undefined")return;
992                                 self.action.setStatus(EAction.THINK);
993                         };
994                 }
995         });
996         var UnunSmellEvent = enchant.Class.create(Event,{
997                 initialize: function (_data){
998                         this.data = _data;
999                         this.yukkuri = _data.myYukkuri;
1000                         this.action = _data.action;
1001                         this.action.setStatus(EAction.UNUN_SMELL_WAIT);
1002                         mangMsg.output(this.yukkuri, EMsg.UNUN_SMELL);
1003                         this.yukkuri.setFaceImage(EFace.CRY1);
1004                         var sec = FPS * 3;
1005                         this.yukkuri.setYukkuri(this.yukkuri.getYukkuri() - 3);
1006                         this.yukkuri.wait(sec, {
1007                                 "onactionend": this.onactionend(this)
1008                         });
1009                 },
1010                 onactionend:function(self){
1011                         return function(e){
1012                                 if(typeof e === "undefined")return;
1013                                 self.action.setStatus(EAction.WALK);
1014                         };
1015                 }
1016         });
1017
1018         var EatEvent = enchant.Class.create(Event,{
1019                 initialize: function (_data){
1020                         this.data = _data;
1021                         this.yukkuri = _data.myYukkuri;
1022                         this.food = _data.targetNode;
1023                         this.action = _data.action;
1024                         this.action.setStatus(EAction.EAT_WAIT);
1025
1026
1027                         var sec = BASE_FPS * 3;
1028                         if(this.yukkuri.eat(this.food)){
1029                                 mangMsg.output(this.yukkuri, EMsg.EAT);
1030                                 this.yukkuri.setFaceImage(EFace.EAT2);
1031                         }else{
1032                                 sec = BASE_FPS * 6;
1033                                 mangMsg.output(this.yukkuri, EMsg.EAT_FAILED);
1034                                 this.yukkuri.setFaceImage(EFace.CRY2);
1035                         }
1036                         this.yukkuri.wait(sec, {
1037                                 "onactionend": this.onactionend(this)
1038                         });
1039                 },
1040                 onactionend:function(self){
1041                         return function(e){
1042                                 if(typeof e === "undefined")return;
1043                                 if(self.food == null || self.food.getAmount() <= 0 || self.yukkuri.isManpuku()){
1044                                         self.action.setStatus(EAction.THINK);
1045                                         self.yukkuri.setFaceImage(EFace.NORMAL);
1046                                 }else{
1047                                         self.action.setStatus(EAction.EAT_START);
1048                                 }
1049                         };
1050                 }
1051         });
1052         /**
1053          * Move Event
1054          * -When yukkuri find Food.
1055          * @param  _data
1056          * type: "food"
1057          * targetNode: enchant.Node
1058          * action:Action
1059          * myYukkuri:Yukkuri
1060          */
1061         var MoveToEatEvent = enchant.Class.create(Event,{
1062                 initialize: function (_data){
1063                         this.data = _data;
1064                         this.yukkuri = this.data.myYukkuri;
1065                         this.action = this.data.action;
1066
1067                         var yukkuri = this.yukkuri;
1068                         var node = this.data.targetNode;
1069                         //food distance
1070                         if(this.data.type == "food"){
1071                                 this.action.status = EAction.MOVE_TO_EAT_UNDERWAY;
1072                                 yukkuri.vx = node.x - yukkuri.getX() + node.width / 2;
1073                                 yukkuri.vy = node.y - yukkuri.getY() - yukkuri.getHeight() / 2 + node.height / 2;
1074                                 var distance = (Math.abs(yukkuri.vx) + Math.abs(yukkuri.vy)) / 2;
1075                                 var frame = distance + distance/5 + 1;
1076                                 frame += plusFrameScale(frame, this.yukkuri.getMyScale());
1077                                 yukkuri.direction = yukkuri.vx > 0 ? EDirection.RIGHT : EDirection.LEFT;
1078                                 yukkuri.reverse();
1079
1080                                 yukkuri.moveBy(yukkuri.vx, yukkuri.vy, frame, {
1081                                         "onactionstart": this.onactionstart(this),
1082                                         "onactiontick": this.onactiontick(this),
1083                                         "onactionend": this.onactionend(this)
1084                                 });
1085                         }
1086                 },
1087                 onactionstart:function(self){
1088                         return function(e){
1089                                 if(typeof e === "undefined")return;
1090                                 mangMsg.output(self.yukkuri, EMsg.TARGETING_EAT);
1091                         };
1092                 },
1093                 onactiontick:function(self){
1094                         return function(e){
1095                                 if(typeof e === "undefined")return;
1096                         };
1097                 },
1098                 onactionend:function(self){
1099                         return function(e){
1100                                 if(typeof e === "undefined")return;
1101                                 self.action.setStatus(EAction.EAT_START);
1102                         };
1103                 }
1104         });
1105         var MoveToHuntEvent = enchant.Class.create(Event,{
1106                 initialize: function (_data){
1107                         this.data = _data;
1108                         this.yukkuri = this.data.myYukkuri;
1109                         this.action = this.data.action;
1110
1111                         var yukkuri = this.yukkuri;
1112                         var node = this.data.targetNode;
1113                         var nodeX = node.x;
1114                         var nodeY = node.y;
1115                         var nodeWidth = node.width;
1116                         var nodeHeight = node.height;
1117                         node = null;
1118
1119                         //food distance
1120                         if(this.data.type == "food"){
1121                                 this.action.setStatus(EAction.WAIT);
1122                                 yukkuri.vx = nodeX - yukkuri.getX() + nodeWidth / 2;
1123                                 yukkuri.vy = nodeY - yukkuri.getY() - yukkuri.getHeight() / 2 + nodeHeight / 2;
1124                                 var distance = (Math.abs(yukkuri.vx) + Math.abs(yukkuri.vy)) / 2;
1125                                 var frame = distance + distance/5 + 1;
1126                                 frame += plusFrameScale(frame, this.yukkuri.getMyScale());
1127                                 yukkuri.direction = yukkuri.vx > 0 ? EDirection.RIGHT : EDirection.LEFT;
1128                                 yukkuri.reverse();
1129                                 mangMsg.output(yukkuri, EMsg.TARGETING_EAT);
1130
1131                                 yukkuri.moveBy(yukkuri.vx, yukkuri.vy, frame, {
1132                                         "onactionend": this.onactionend(this)
1133                                 });
1134                         }
1135                 },
1136                 onactionend:function(self){
1137                         return function(e){
1138                                 if(typeof e === "undefined")return;
1139                                 self.action.setStatus(EAction.HUNT_START);
1140                         };
1141                 }
1142         });
1143         var HuntStartEvent = enchant.Class.create(Event,{
1144                 initialize: function (_data){
1145                         Event.call(this, _data);
1146                         this.initCnt = 0;
1147                         this.data = _data;
1148                         this.yukkuri = _data.myYukkuri;
1149                         this.food = _data.targetNode;
1150                         this.action = _data.action;
1151                         this.action.setStatus(EAction.WAIT);
1152                         this.huntingStatus = "hunting";
1153                         this.tick = 0;
1154                         this.yukkuri.beforeHuntingFood = this.food;
1155                         var sec = BASE_FPS * 8;
1156                         if(this.yukkuri.hunt(this.food)){
1157                                 this.huntingStatus = "hunting";
1158                                 mangMsg.output(this.yukkuri, EMsg.HUNTING_MESSAGE);
1159                                 this.yukkuri.setFaceImage(EFace.HAPPY1);
1160                         }else{
1161                                 if(this.yukkuri.isItemsMax()){
1162                                         sec = 1;
1163                                         this.huntingStatus = "return";
1164                                         // mangMsg.output(this.yukkuri, EMsg.REHUNT);
1165                                         this.yukkuri.setFaceImage(EFace.NORMAL);
1166                                 }else{
1167                                         sec = BASE_FPS * 3;
1168                                         this.huntingStatus = "cry";
1169                                         mangMsg.output(this.yukkuri, EMsg.EAT_FAILED);
1170                                         this.yukkuri.setFaceImage(EFace.CRY2);
1171                                 }
1172                         }
1173
1174                         this.yukkuri.wait(sec, {
1175                                 "onactiontick": this.onactiontick(this),
1176                                 "onactionend": this.onactionend(this)
1177                         });
1178                 },
1179                 onactiontick:function(self){
1180                         return function(e){
1181                                 if(typeof e === "undefined")return;
1182                                 self.tick++;
1183                                 if(self.huntingStatus == "hunting" && self.tick > BASE_FPS * 1){
1184                                         if(this.age%(BASE_FPS * 3) == 0){
1185                                                 // mangMsg.output(self.yukkuri, EMsg.YYUYYU)
1186                                                 mangMsg.output(self.yukkuri, EMsg.HUNTING_MESSAGE);
1187                                         }
1188                                 }
1189                         };
1190                 },
1191                 onactionend:function(self){
1192                         return function(e){
1193                                 if(typeof e === "undefined")return;
1194                                 if(self.huntingStatus == "hunting"){
1195                                         if(self.yukkuri.isItemsMax()){
1196                                                 self.action.setStatus(EAction.MOVE_GOTO_HOME);
1197                                                 // mangMsg.output(self.yukkuri, EMsg.MOVE_GOTO_HOME);
1198                                         }else{
1199                                                 self.action.setStatus(EAction.HUNT_START);
1200                                         }
1201                                 }
1202                                 else if(self.huntingStatus == "return"){
1203                                         self.action.setStatus(EAction.MOVE_GOTO_HOME);
1204                                 }
1205                                 else if(self.huntingStatus === "cry"){
1206                                         // self.action.setStatus(EAction.THINK);
1207                                         self.action.setStatus(EAction.WALK);
1208                                 }
1209                                 self.release();
1210                         };
1211                 },
1212                 release:function(){
1213                         this.action = null;
1214                         this.yukkuri = null;
1215                         this.food = null;
1216                         this.data = null;
1217                 }
1218         });
1219         var MoveGotoHomeEvent = enchant.Class.create(Event,{
1220                 initialize: function (_data){
1221                         this.data = _data;
1222                         this.yukkuri = this.data.myYukkuri;
1223                         this.action = this.data.action;
1224
1225                         var yukkuri = this.yukkuri;
1226                         var place = this.data.targetNode;
1227                         //food distance
1228                         if(this.data.type == "hunt_end"){
1229                                 this.action.setStatus(EAction.WAIT);
1230                                 yukkuri.vx = place.x - yukkuri.getX() + place.width / 2;
1231                                 yukkuri.vy = place.y - yukkuri.getY() - yukkuri.getHeight() / 2 + place.height;
1232                                 // console.log("place.x:" + place.x);
1233                                 // console.log("place.y:" + place.y);
1234                                 // console.log("yukkuri.getX():" + yukkuri.getX());
1235                                 // console.log("yukkuri.getY():" + yukkuri.getY());
1236                                 // console.log("yukkuri.vx:" + yukkuri.vx);
1237                                 // console.log("yukkuri.vy:" + yukkuri.vy);
1238                                 var distance = (Math.abs(yukkuri.vx) + Math.abs(yukkuri.vy)) / 2;
1239                                 var frame = distance + distance/2 + 1 ;
1240                                 frame += plusFrameScale(frame, this.yukkuri.getMyScale());
1241                                 yukkuri.direction = yukkuri.vx > 0 ? EDirection.RIGHT : EDirection.LEFT;
1242                                 yukkuri.reverse();
1243                                 mangMsg.output(this.yukkuri, EMsg.MOVE_GOTO_HOME)
1244
1245                                 yukkuri.moveBy(yukkuri.vx, yukkuri.vy, frame, {
1246                                         "onactiontick": this.onactiontick(this),
1247                                         "onactionend": this.onactionend(this)
1248                                 });
1249                         }
1250                 },
1251                 onactiontick:function(self){
1252                         return function(e){
1253                                 if(typeof e === "undefined")return;
1254                                 if(this.age%(BASE_FPS * 5) == 0){
1255                                         mangMsg.output(self.yukkuri, EMsg.YYUYYU)
1256                                 }
1257                         };
1258                 },
1259                 onactionend:function(self){
1260                         return function(e){
1261                                 if(typeof e === "undefined")return;
1262                                 self.action.setStatus(EAction.IN_HOME);
1263                                 self.release();
1264                         };
1265                 },
1266                 release:function(){
1267                         this.action = null;
1268                         this.yukkuri = null;
1269                         this.data = null;
1270                 }
1271         });
1272         var InHomeEvent = enchant.Class.create(Event,{
1273                 initialize: function (_data){
1274                         this.data = _data;
1275                         this.yukkuri = this.data.myYukkuri;
1276                         this.action = this.data.action;
1277
1278                         var yukkuri = this.yukkuri;
1279                         var place = this.data.targetNode;
1280                         if(yukkuri.isVeryKuhuku()){
1281                                 if(place.isFood()){
1282                                         this.action.setStatus(EAction.EAT_IN_HOME);
1283                                         return;
1284                                 }else{
1285                                         mangMsg.output(yukkuri, EMsg.EAT_FAILED);
1286                                 }
1287                         }
1288                         this.action.setStatus(EAction.WAIT);
1289                         var items = this.yukkuri.getItems();
1290                         if(items.length > 0){
1291                                 place.setItems(items);
1292                         }else{
1293
1294                         }
1295                         var sec = BASE_FPS * 10;
1296                         this.yukkuri.wait(sec, {
1297                                 "onactiontick": this.onactiontick(this),
1298                                 "onactionend": this.onactionend(this)
1299                         });
1300                 },
1301                 onactiontick:function(self){
1302                         return function(e){
1303                                 if(typeof e === "undefined")return;
1304                                 if(this.age%(BASE_FPS * 5) == 0){
1305                                         mangMsg.output(self.yukkuri, EMsg.YYUYYU)
1306                                 }
1307                         };
1308                 },
1309                 onactionend:function(self){
1310                         return function(e){
1311                                 if(typeof e === "undefined")return;
1312                                 self.action.setStatus(EAction.THINK);
1313                         };
1314                 }
1315         });
1316         var EatInHomeEvent = enchant.Class.create(Event,{
1317                 initialize: function (_data){
1318                         this.data = _data;
1319                         this.yukkuri = _data.myYukkuri;
1320                         this.place = _data.targetNode;
1321                         this.action = _data.action;
1322                         this.action.setStatus(EAction.EAT_WAIT);
1323
1324                         var sec = BASE_FPS * 3;
1325                         var foodHash = this.place.getFoodHash();
1326                         if(foodHash !== false && this.yukkuri.eat(foodHash)){
1327                                 mangMsg.output(this.yukkuri, EMsg.EAT);
1328                                 this.yukkuri.setFaceImage(EFace.EAT2);
1329                         }else{
1330                                 sec = BASE_FPS * 6;
1331                                 mangMsg.output(this.yukkuri, EMsg.EAT_FAILED);
1332                                 this.yukkuri.setFaceImage(EFace.CRY2);
1333                         }
1334
1335                         this.yukkuri.wait(sec, {
1336                                 "onactionend": this.onactionend(this)
1337                         });
1338                 },
1339                 onactionend:function(self){
1340                         return function(e){
1341                                 if(typeof e === "undefined")return;
1342                                 if(self.place.isFood() && !self.yukkuri.isManpuku()){
1343                                         self.action.setStatus(EAction.EAT_IN_HOME);
1344
1345                                         // new EatInHomeEvent({
1346                                         //      "targetNode": self.place,
1347                                         //      "action": self.action,
1348                                         //      "myYukkuri": self.yukkuri
1349                                         // });
1350                                 }else{
1351                                         self.action.setStatus(EAction.THINK_IN_HOME);
1352                                         self.yukkuri.setFaceImage(EFace.NORMAL);
1353                                 }
1354                         };
1355                 }
1356         });
1357
1358
1359         var MoveCommandEvent = enchant.Class.create(Event,{
1360                 initialize: function (_data){
1361                         this.data = _data;
1362                         this.yukkuri = this.data.myYukkuri;
1363                         this.action = this.data.action;
1364                         this.vx = this.data.vx;
1365                         this.vy = this.data.vy;
1366
1367                         var yukkuri = this.yukkuri;
1368                         this.action.status = EAction.MOVE_TO_EAT_UNDERWAY;
1369                         yukkuri.vx = node.x - yukkuri.getX();
1370                         yukkuri.vy = node.y - yukkuri.getY();
1371                         var distance = (Math.abs(yukkuri.vx) + Math.abs(yukkuri.vy)) / 2;
1372                         var frame = distance + 1;
1373                         frame += plusFrameScale(frame, this.yukkuri.getMyScale());
1374
1375                         yukkuri.moveBy(yukkuri.vx, yukkuri.vy, frame, {
1376                                 "onactionstart": this.onactionstart(this),
1377                                 "onactiontick": this.onactiontick(this),
1378                                 "onactionend": this.onactionend(this)
1379                         });
1380                 },
1381                 onactionstart:function(self){
1382                         return function(e){
1383                                 if(typeof e === "undefined")return;
1384                         };
1385                 },
1386                 onactiontick:function(self){
1387                         return function(e){
1388                                 if(typeof e === "undefined")return;
1389                         };
1390                 },
1391                 onactionend:function(self){
1392                         return function(e){
1393                                 if(typeof e === "undefined")return;
1394                                 self.action.setStatus(EAction.THINK);
1395                         };
1396                 }
1397         });
1398         /**
1399          * Say hello contact to send communication .
1400          */
1401         var ComSayHelloSendEvent = enchant.Class.create(Event,{
1402                 initialize: function (_data){
1403                         this.data = _data;
1404                         this.yukkuri = _data.myYukkuri;
1405                         this.targetYukkuri = _data.targetNode;
1406                         this.action = _data.action;
1407                         this.action.setStatus(EAction.COMMUNICATE_WAIT);
1408                         mangMsg.output(this.yukkuri, EMsg.SAY_HELLO, this.targetYukkuri);
1409
1410                         this.targetYukkuri.action.setStatus(EAction.COMMUNICATE_RECV);
1411                         this.targetYukkuri.tlPause();
1412                         this.yukkuri.tlPause();
1413                         this.yukkuri.syncDirection(this.targetYukkuri);
1414
1415                         this.yukkuri.addAddress(this.targetYukkuri);
1416                         this.yukkuri.getAddress(this.targetYukkuri).isGreeting = true;
1417
1418                         var sec = BASE_FPS * 2;
1419                         this.yukkuri.wait(sec, {
1420                                 "onactiontick": this.onactiontick(this),
1421                                 "onactionend": this.onactionend(this)
1422                         });
1423                 },
1424                 onactiontick:function(self){
1425                         return function(e){
1426                                 if(typeof e === "undefined")return;
1427                         };
1428                 },
1429                 onactionend:function(self){
1430                         return function(e){
1431                                 if(typeof e === "undefined")return;
1432                                 new ComSayHelloRecvEvent({
1433                                         "type": 'greeting',
1434                                         "targetNode": self.yukkuri,
1435                                         "action": self.targetYukkuri.action,
1436                                         "myYukkuri": self.targetYukkuri
1437                                 });
1438                         };
1439                 }
1440         });
1441         var ComSayHelloRecvEvent = enchant.Class.create(Event,{
1442                 initialize: function (_data){
1443                         this.data = _data;
1444                         this.yukkuri = _data.myYukkuri;
1445                         this.targetYukkuri = _data.targetNode;
1446                         this.action = _data.action;
1447                         mangMsg.output(this.yukkuri, EMsg.SAY_HELLO, this.targetYukkuri);
1448                         this.yukkuri.addAddress(this.targetYukkuri);
1449                         this.yukkuri.getAddress(this.targetYukkuri).isGreeting = true;
1450
1451                         var sec = BASE_FPS * 2;
1452                         this.yukkuri.wait(sec, {
1453                                 "onactiontick": this.onactiontick(this),
1454                                 "onactionend": this.onactionend(this)
1455                         });
1456                 },
1457                 onactiontick:function(self){
1458                         return function(e){
1459                                 if(typeof e === "undefined")return;
1460                         };
1461                 },
1462                 onactionend:function(self){
1463                         return function(e){
1464                                 if(typeof e === "undefined")return;
1465                                 self.action.setStatus(EAction.THINK);
1466
1467                                 self.yukkuri.action.setStatus(EAction.THINK);
1468                                 self.yukkuri.animation();
1469                                 self.targetYukkuri.action.setStatus(EAction.THINK);
1470                                 self.targetYukkuri.animation();
1471                         };
1472                 }
1473         });
1474         var MoveToPlaceStartHomelessEvent = enchant.Class.create(Event,{
1475                 initialize: function (_data){
1476                         this.data = _data;
1477                         this.yukkuri = this.data.myYukkuri;
1478                         this.action = this.data.action;
1479
1480                         var yukkuri = this.yukkuri;
1481                         var node = this.data.targetNode;
1482
1483                         this.action.status = EAction.MOVE_TO_PLACE_HOMELESS_UNDERWAY;
1484                         yukkuri.vx = node.x + node.width /2  - yukkuri.getX();
1485                         yukkuri.vy = node.y + node.height - yukkuri.getY() - yukkuri.getHeight() / 2;
1486                         var distance = (Math.abs(yukkuri.vx) + Math.abs(yukkuri.vy)) / 2;
1487                         var frame = distance + distance/5 + 1;
1488                         frame += plusFrameScale(frame, this.yukkuri.getMyScale());
1489
1490                         yukkuri.direction = yukkuri.vx > 0 ? EDirection.RIGHT : EDirection.LEFT;
1491                         yukkuri.reverse();
1492
1493                         yukkuri.moveBy(yukkuri.vx, yukkuri.vy, frame, {
1494                                 "onactionend": this.onactionend(this)
1495                         });
1496                 },
1497                 onactionend:function(self){
1498                         return function(e){
1499                                 if(typeof e === "undefined")return;
1500                                 self.action.setStatus(EAction.DECLARE_PLACE_HOMELESS);
1501                                 self.data = null;
1502                                 self.yukkuri = null;
1503                                 self.action = null;
1504                         };
1505                 }
1506         });
1507         var DeclarePlaceHomelessEvent = enchant.Class.create(Event,{
1508                 initialize: function (_data){
1509                         this.data = _data;
1510                         this.yukkuri = _data.myYukkuri;
1511                         this.place = _data.targetNode;
1512                         this.action = _data.action;
1513                         this.action.setStatus(EAction.WAIT);
1514
1515
1516                         var sec = BASE_FPS * 3;
1517                         this.yukkuri.declarePlace(this.place);
1518                         this.place.setOwner(this.yukkuri);
1519                         mangMsg.output(this.yukkuri, EMsg.DECLARE_PLACE);
1520
1521                         this.yukkuri.setFaceImage(EFace.NORMAL);
1522                         this.yukkuri.wait(sec, {
1523                                 "onactionend": this.onactionend(this)
1524                         });
1525                 },
1526                 onactionend:function(self){
1527                         return function(e){
1528                                 if(typeof e === "undefined")return;
1529                                 self.action.setStatus(EAction.THINK);
1530                         };
1531                 }
1532         });
1533
1534         var Food = enchant.Class.create(ObjSprite,{
1535                 initialize: function (type, x, y){
1536                         this.classname = "Food";
1537                         //一つにつきgiveの値分、hunguryを減少させられる
1538                         if(type == "apple"){
1539                                 ObjSprite.call(this,16,16);
1540                                 this.image = game.assets[EResPath.OBJECT];
1541                                 this.frame = 15;
1542                                 this.give = 40;
1543                                 this.amount = 3;
1544                         }else if(type == "grass"){
1545                                 ObjSprite.call(this,64,64);
1546                                 this.image = game.assets[EResPath.GRASS];
1547                                 this.frame = 0;
1548                                 this.give = 10;
1549                                 this.amount = 20;
1550                         }
1551                         this.x = x;
1552                         this.y = y;
1553                         foodGroup.addChild(this);
1554                         this.addEventListener('enterframe', function(){
1555                                 if(this.amount <= 0){
1556                                         this.removeAll();
1557                                 }
1558                         });
1559                 },
1560                 getAmount: function(){
1561                         return this.amount;
1562                 },
1563                 getGive: function(){
1564                         return this.give;
1565                 }
1566         });
1567         var Place = enchant.Class.create(ObjSprite,{
1568                 initialize: function (type, x, y){
1569                         ObjSprite.call(this,128,128);
1570                         this.image = game.assets[EResPath.PLACE_TREE];
1571                         this.x = x - this.width / 2;
1572                         this.y = y - this.height / 2;
1573                         backgroundMap.addChild(this);
1574                         this.owner = null;
1575                         this.foods = [];
1576                         for(var key in EFood){
1577                                 var efood = EFood[key];
1578                                 this.foods[efood] = 0;
1579                         }
1580                         // this.addEventListener('enterframe', function(){
1581                         //      if(this.age%10 === 0 && this.age !== 0){
1582                         //              console.log("x/y " + this.x + "/" + this.y);
1583                         //      }
1584                         //      if(game.input.left){
1585                         //              this.x -= 10;;
1586                         //      }
1587                         //      if(game.input.right){
1588                         //              this.x += 10;;
1589                         //      }
1590                         //      if(game.input.up){
1591                         //              this.y -= 10;;
1592                         //      }
1593                         //      if(game.input.down){
1594                         //              this.y += 10;;
1595                         //      }
1596                         // });
1597                 },
1598                 getStatusForDisplay: function(){
1599                         var ret = "";
1600                         var ownerId = "なし";
1601                         if(this.owner == null)ownerId = "なし";
1602                         else ownerId = this.owner.id;
1603                         ret += "プレイスID " + this.id + "\n";
1604                         ret += "オーナーID " + ownerId + "\n";
1605                         ret += "食料うめぇ  " + this.foods[EFood.GREAT] + "\n";
1606                         ret += "食料それなりー " + this.foods[EFood.NORMAL] + "\n";
1607                         ret += "食料にがにが  " + this.foods[EFood.BAD] + "\n";
1608                         return ret;
1609                 },
1610                 setItems:function(items){
1611                         var len = items.length;
1612                         for(var i=0; i<len; i++){
1613                                 var item = items[i];
1614                                 if(item instanceof Food){
1615                                         this.foods[EFood.NORMAL]++;
1616                                 }
1617                         }
1618                         items.length = 0;
1619                 },
1620                 isFood:function(){
1621                         var ret = 0;
1622                         for(var key in EFood){
1623                                 var efood = EFood[key];
1624                                 ret += this.foods[efood];
1625                         }
1626                         return ret !== 0;
1627                 },
1628                 getFoodHash:function(){
1629                         if(this.foods[EFood.GREAT] !== 0){
1630                                 this.foods[EFood.GREAT]--;
1631                                 return {give:20,yukkuri:15,unun:20};
1632                         }
1633                         else if(this.foods[EFood.NORMAL] !== 0){
1634                                 this.foods[EFood.NORMAL]--;
1635                                 return {give:10,yukkuri:5,unun:10};
1636                         }
1637                         else if(this.foods[EFood.BAD] !== 0){
1638                                 this.foods[EFood.BAD]--;
1639                                 return {give:10,yukkuri:-2,unun:10};
1640                         }
1641                         return false;
1642                 },
1643                 setOwner: function(yukkuri){
1644                         this.owner = yukkuri;
1645                 },
1646                 isOwner: function(){
1647                         return this.owner !== null;
1648                 }
1649         });
1650         var Unun = enchant.Class.create(ObjSprite,{
1651                 initialize: function (x, y){
1652                         this.classname = "Unun";
1653                         ObjSprite.call(this,32,32);
1654                         this.image = game.assets[EResPath.YUKKURI_UNUN];
1655                         this.x = x;
1656                         this.y = y;
1657                         backgroundMap.addChild(this);
1658                         this.lifetime = 30 * 24 * 3;
1659                         this.addEventListener('enterframe', this.runEnterframe(this));
1660                         // game.rootScene.addEventListener('enterframe', this.runEnterframe(this));
1661
1662                 },
1663                 runEnterframe:function(self){
1664                         return function(e){
1665                                 if(typeof e === "undefined")return;
1666                                 if(this.age%BASE_FPS === 0 && this.age !== 0){
1667                                         self.lifetime--;
1668                                         if(self.lifetime <= 0){
1669                                                 this.parentNode.removeChild(this);
1670                                                 ctl.backgroundMap.removeChild(this);
1671                                                 ctl.removeObj(this);
1672                                                 self.image = null;
1673                                         }
1674                                 }
1675
1676                         };
1677                 }
1678                 // runEnterframe: function(){
1679                 //      console.log("enterframe:" + this.id);
1680                 // }
1681         });
1682         var Okazari = enchant.Class.create(enchant.Sprite,{
1683                 initialize: function(yukkuri){
1684                         this.classname = "Okazari";
1685                         if(yukkuri.myCode === "REIMU"){
1686                                 enchant.Sprite.call(this, 80, 32);
1687                                 this.image = game.assets[EResPathReimu.OKAZARI];
1688                                 this.x = -CHARA_WIDTH / 2;
1689                                 this.y = 0;
1690                         }else{
1691                                 enchant.Sprite.call(this, 64, 64);
1692                                 this.image = game.assets[EResPath.OKAZARI];
1693                                 this.x = -CHARA_WIDTH / 2;
1694                                 this.y = -CHARA_HEIGHT / 2 - yukkuri.getWidth()/6;
1695                         }
1696                         this.yukkuri = yukkuri;
1697                 },
1698                 getWidth: function(){
1699                         return this.width;
1700                         // return this.width * this.yukkuri.myScaleX;
1701                 },
1702                 getHeight: function(){
1703                         return this.height;
1704                         // return this.height * this.yukkuri.myScaleY;
1705                 }
1706         });
1707         var Address = enchant.Class.create({
1708                 initialize: function(yukkuri){
1709                         this.friendlyLv = 1;
1710                         this.yukkuri = yukkuri;
1711                         this.isGreeting = false;
1712                         this.greetingWaitFrame = 0;
1713                         this.comMatrix = {
1714                                 "beginSayHello" : false,
1715                                 "sayHello" : false,
1716                         };
1717                         this.evtHash = {};
1718                 },
1719                 set:function(key, value){
1720                         this.comMatrix[key] = value;
1721                 },
1722                 get:function(key){
1723                         return this.comMatrix[key];
1724                 }
1725         });
1726
1727         var Yukkuri = enchant.Class.create(ObjSprite,{
1728                 initialize: function(x, y, grow){
1729                         ObjSprite.call(this, 64, 64);
1730                         this.searchObjectMang = new SearchObjectMang();
1731                         this.grow = grow;
1732                         this.myCode = "YUKKURI";
1733                         this.myName = "ゆっくり";
1734                         this.beforeHuntingFood = null;
1735                         if(grow == EGrow.ADULT){
1736                                 this.myScaleX = 1;
1737                                 this.myScaleY = 1;
1738                                 this.mySize = "L";
1739                         }
1740                         else if(grow == EGrow.SUBADULT){
1741                                 this.myScaleX = 0.8;
1742                                 this.myScaleY = 0.8;
1743                                 this.mySize = "M";
1744                         }
1745                         else if(grow == EGrow.CHILD){
1746                                 this.myScaleX = 0.6;
1747                                 this.myScaleY = 0.6;
1748                                 this.mySize = "S";
1749                         }
1750                         else if(grow == EGrow.BABY){
1751                                 this.myScaleX = 0.3;
1752                                 this.myScaleY = 0.3;
1753                                 this.mySize = "SS";
1754                         }
1755
1756                         // ctl.game.rootScene.addEventListener('touchstart', this.touchstart);
1757                         this.classname = "Yukkuri";
1758                         this.addressBook = {};
1759                         this.imgGroup = new SpriteGroup();
1760                         this.imgBody = new SpriteGroup();
1761                         this.imgGroup.id = guid();
1762                         this.imgBody.id = guid();
1763                         this.x = -CHARA_WIDTH / 2;
1764                         this.y = -CHARA_HEIGHT / 2;
1765                         this.vx = 0;
1766                         this.vy = 0;
1767                         this.image = game.assets[EResPath.YUKKURI_BASE];
1768                         this.direction = EDirection.RIGHT;
1769                         this.addEventListener('enterframe', this.runEnterframe);
1770                         this.bodyStatus ={};
1771                         this.bodyStatus[EBody.NORMAL] = EResPath.YUKKURI_BASE;
1772                         this.bodyStatus[EBody.DEAD] = EResPath.YUKKURI_BODY_DEAD;
1773                         this.walkAnimationStatus = EWalkAnimation.END;
1774                         this.place = null;
1775                         this.items = [];
1776                         this.actionQueue = [];
1777                 },
1778                 runEnterframe:function(){
1779                         // try{
1780                                 this.act();
1781                                 if(this.isDead())return;
1782                                 this.runYukkuri();
1783                                 this.runHungry();
1784                                 this.runUnun();
1785                                 this.runSleep();
1786                                 this.runAddress();
1787                         // }catch(e){
1788                         //      console.log(e);
1789                         // }
1790                 },
1791                 reverse:function(){
1792                         if(this.direction == EDirection.RIGHT){
1793                                 this.imgBody.scaleX = 1;
1794                         }
1795                         else if(this.direction == EDirection.LEFT){
1796                                 this.imgBody.scaleX = -1;
1797                         }
1798                 },
1799                 tweet:function(text){
1800                         text = text.replace(/%name%/g, this.myName);
1801                         this._tweet.text(text, this.x - this._tweet.width/4, this.y - this._tweet.height);
1802                 },
1803                 moveTo:function(x, y, time){
1804                         this.imgGroup.tl.moveTo(x, y, time, enchant.Easing.SIN_EASEINOUT);
1805                 },
1806                 moveBy:function(x, y, time, eventFunctions){
1807                         var self = this;
1808                         var params = {
1809                                 x: function() {
1810                                         return self.imgGroup.x + x;
1811                                 },
1812                                 y: function() {
1813                                         return self.imgGroup.y + y;
1814                                 },
1815                                 time: time,
1816                                 easing: enchant.Easing.SIN_EASEINOUT
1817                         };
1818                         //-Event register [onactionstart,onactiontick,onactionend]
1819                         if(eventFunctions !== undefined){
1820                                 for(var key in eventFunctions){
1821                                         params[key] = eventFunctions[key];
1822                                 }
1823                         }
1824                         return this.imgGroup.tl.tween(params);
1825                 },
1826                 wait:function(frame, eventFunctions){
1827                         this.moveBy(0,1,frame,eventFunctions);
1828                 },
1829                 act: function(){
1830                         this.action.act();
1831                 },
1832                 animation: function(){
1833
1834                         var frame = 10;
1835                         frame += plusFrameScale(frame, this.getMyScale());
1836                         var move = 5;
1837                         move -= plusFrameScale(move, this.getMyScale());
1838                         if(this.walkAnimationStatus == EWalkAnimation.END){
1839                                 this.imgBody.y = 0;
1840                                 this.imgBody.tl.moveByEx(0, -move, frame, enchant.Easing.SWING, {
1841                                         "onactionstart": function(e){
1842                                                 this.walkAnimationStatus = EWalkAnimation.START;
1843                                         }
1844                                 })
1845                                 .moveByEx(0, move, frame, enchant.Easing.SWING,
1846                                 {
1847                                         "onactionend": function(e){
1848                                                 this.walkAnimationStatus = EWalkAnimation.END;
1849                                         }
1850                                 }).loop();
1851
1852                         }else{
1853                                 this.imgBody.tl.loop();
1854                         }
1855                 },
1856                 getMyScale: function(){
1857                         return (this.myScaleX + this.myScaleY) / 2;
1858                 },
1859                 getWidth: function(){
1860                         return this.width * this.myScaleX;
1861                 },
1862                 getHeight: function(){
1863                         return this.height * this.myScaleY;
1864                 },
1865                 getX: function(){
1866                         return this.imgGroup.x;
1867                 },
1868                 getY: function(){
1869                         return this.imgGroup.y;
1870                 },
1871                 moveX: function(x){
1872                         this.imgGroup.x += x;
1873                         // this.x += x;
1874                         // this.shadow.x += x;
1875                         // this._tweet.x += x;
1876                 },
1877                 moveY: function(y){
1878                         this.imgGroup.y += y;
1879                         // this.y += y;
1880                         // this.shadow.y += y;
1881                         // this._tweet.y += y;
1882                 },
1883                 // changeFace: function(erespath){
1884                 //      this.face.image = game.assets[erespath];
1885                 // },
1886                 loadParamsXML: function(url){
1887                         var http = new JKL.ParseXML( url );
1888                         return http.parse();
1889                 },
1890                 runYukkuri: function(){
1891                         if(this.param.hungry > 70){
1892                                 if(this.age%(50 - this.param.hungry - 70) === 0 && this.age !== 0){
1893                                         this.param.yukkuri -= 1;
1894                                 }
1895                         }
1896                         else if(this.param.hungry < 10){
1897                                 if(this.age%50 === 0 && this.age !== 0) this.param.yukkuri += 1;
1898                         }
1899
1900                         if(this.param.yukkuri <= 0)this.param.yukkuri = 0;
1901                         else if(this.param.yukkuri >= this.param.maxYukkuri)this.param.yukkuri = this.param.maxYukkuri;
1902                 },
1903                 runHungry: function(){
1904                         if(this.age%50 === 0 && this.age !== 0)this.param.hungry++;
1905                         if(this.param.hungry >= 100)this.param.hungry = 100;
1906                 },
1907                 runUnun: function(){
1908                         if(this.age%50 === 0 && this.age !== 0)this.param.unun++;
1909                         if(this.param.unun >= 100)this.param.unun = 100;
1910                 },
1911                 runSleep: function(){
1912                         if(this.action.getStatus() != EAction.SLEEP_START && this.action.getStatus() != EAction.SLEEP_WAIT){
1913                                 if(this.age%80 === 0 && this.age !== 0)this.param.sleep++;
1914                                 if(this.param.sleep >= 100){
1915                                         this.param.sleep = 100;
1916                                 }
1917                         }
1918                 },
1919                 runAddress: function(){
1920                         for(var key in this.addressBook){
1921                                 var address = this.addressBook[key];
1922                                 //挨拶時間。24時間に1度だけ。
1923                                 if(address.isGreeting){
1924                                         address.greetingWaitFrame++;
1925                                         if(address.greetingWaitFrame >= DAY){
1926                                                 address.greetingWaitFrame = 0;
1927                                                 address.isGreeting = false;
1928                                         }
1929                                 }
1930                         }
1931                 },
1932                 changeFace: function(){
1933                         if(this.param.yukkuri >= 80){
1934                                 this.setFaceImage(EFace.HAPPY1);
1935                         }
1936                         else if(this.param.yukkuri >= 50){
1937                                 this.setFaceImage(EFace.NORMAL);
1938                         }
1939                         else if(this.param.yukkuri >= 30){
1940                                 this.setFaceImage(EFace.TIRED);
1941                         }
1942                         else if(this.param.yukkuri >= 10){
1943                                 this.setFaceImage(EFace.CRY1);
1944                         }
1945                         else if(this.param.yukkuri >= 1){
1946                                 this.setFaceImage(EFace.CRY2);
1947                         }
1948                 },
1949                 getRange: function(){
1950                         return this.param.range;
1951                 },
1952                 isSleep: function(){
1953                         return this.param.sleep >= 100;
1954                 },
1955                 isDead: function(){
1956                         return this.param.yukkuri <= 0;
1957                 },
1958                 isManpuku: function(){
1959                         return this.param.hungry <= 20;
1960                 },
1961                 isKuhuku: function(){
1962                         return this.param.hungry >= 40;
1963                 },
1964                 isVeryKuhuku: function(){
1965                         return this.param.hungry >= 80;
1966                 },
1967                 getYukkuri: function(){
1968                         return this.param.yukkuri;
1969                 },
1970                 setYukkuri: function(yukkuri){
1971                         this.param.yukkuri = yukkuri;
1972                         if(this.param.yukkuri < 0)this.param.yukkuri = 0;
1973                 },
1974                 getHungry: function(){
1975                         return this.param.hungry;
1976                 },
1977                 setHungry: function(hungry){
1978                         this.param.hungry = hungry;
1979                         if(this.param.hungry < 0)this.param.hungry = 0;
1980                 },
1981                 getUnun: function(){
1982                         return this.param.unun;
1983                 },
1984                 setUnun: function(unun){
1985                         this.param.unun = unun;
1986                         if(this.param.unun < 0)this.param.unun = 0;
1987                 },
1988
1989                 eat:function(food){
1990                         if(food instanceof Food){
1991                                 //Food object case
1992                                 if(food.amount > 0){
1993                                         food.amount--;
1994                                         this.setHungry(this.param.hungry - food.getGive());
1995                                         this.setUnun(this.getUnun() + food.getGive() / 4);
1996                                         this.setYukkuri(this.getYukkuri() + 5);
1997                                         return true;
1998                                 }else{
1999                                         this.setYukkuri(this.getYukkuri() - 5);
2000                                         return false;
2001                                 }
2002                         }else{
2003                                 //hash case
2004                                 if(food != null){
2005                                         this.setHungry(this.param.hungry - food.give);
2006                                         this.setUnun(this.getUnun() + food.unun);
2007                                         this.setYukkuri(this.getYukkuri() + food.yukkuri);
2008                                         return true;
2009                                 }else{
2010                                         return false;
2011                                 }
2012                         }
2013                 },
2014                 hunt:function(food){
2015                         if(food.amount > 0){
2016                                 if(!this.isItemsMax()){
2017                                         food.amount--;
2018                                         this.items.push(food);
2019                                         this.setYukkuri(this.getYukkuri() + 1);
2020                                         return true;
2021                                 }
2022                         }
2023                         return false;
2024                 },
2025                 declarePlace: function(place){
2026                         this.place = place;
2027                 },
2028                 getPlace:function(){
2029                         return this.place;
2030                 },
2031                 isPlace: function(){
2032                         return this.place !== null;
2033                 },
2034
2035                 unun:function(){
2036                         if(this.direction == EDirection.RIGHT){
2037                                 new Unun(this.getX(), this.getY() + this.getHeight()/5 );
2038                         }else{
2039                                 new Unun(this.getX() -  this.getWidth()/5, this.getY() + this.getHeight()/5 );
2040                         }
2041                         this.setUnun(0);
2042                 },
2043                 setBodyImage: function(ebody){
2044                         var path = this.bodyStatus[ebody];
2045                         this.image = game.assets[path];
2046                 },
2047                 tlPause:function(){
2048                         // this.imgBody.tl.pause();
2049                         // this.imgGroup.tl.pause();
2050                         this.imgBody.tl.clear();
2051                         this.imgGroup.tl.clear();
2052                         this.imgBody.tl.unloop();
2053                         this.imgGroup.tl.unloop();
2054                         this.imgBody.y = 0;
2055
2056                 },
2057                 tlResume: function(){
2058                         // this.imgBody.tl.resume();
2059                         // this.imgGroup.tl.resume();
2060                         this.animation();
2061                 },
2062                 syncDirection:function(senderYukkuri){
2063                         if(this.getX() > senderYukkuri.getX()){
2064                                 this.direction = EDirection.LEFT;
2065                                 senderYukkuri.direction = EDirection.RIGHT;
2066                         }else{
2067                                 this.direction = EDirection.RIGHT;
2068                                 senderYukkuri.direction = EDirection.LEFT;
2069                         }
2070                 },
2071                 isCommunication: function(yukkuri){
2072                         if(this.addressBook[yukkuri.id] === undefined) return true;
2073                         if(this.addressBook[yukkuri.id].isGreeting) return false;
2074                         return true;
2075                 },
2076                 addAddress: function(yukkuri){
2077                         this.addressBook[yukkuri.id] = new Address(yukkuri);
2078                 },
2079                 getAddress: function(yukkuri){
2080                         if(this.addressBook[yukkuri.id] === undefined)return null;
2081                         return this.addressBook[yukkuri.id];
2082                 },
2083                 getStatusForDisplay: function (){
2084                         var ret = "";
2085                         var placeId = (this.place==null)?"無し" : this.place.id;
2086                         ret += "ID " + this.id + "\n";
2087                         ret += "プレイス  " + placeId + "\n";
2088                         ret += "ゆっくり  " + this.getYukkuri() + "\n";
2089                         ret += "空腹  " + this.getHungry() + "\n";
2090                         ret += "うんうん  " + this.getUnun() + "\n";
2091                         ret += "もちもの  " + this.items.length + "\n";
2092                         ret += "アクション  " + this.action.status + "\n";
2093                         return ret;
2094                 },
2095                 isItemsMax:function(){
2096                         return this.items.length >= this.param.items_max;
2097                 },
2098                 getItems:function(){
2099                         return this.items;
2100                 },
2101                 addSearchObject:function(esearch, node){
2102                         this.searchObjectMang.add(esearch, node)
2103                 },
2104                 clearSearchObject:function(){
2105                         this.searchObjectMang.clear()
2106                 },
2107                 setFaceImage: function(eface){
2108                         var path = this.faceStatus[eface];
2109                         this.face.image = game.assets[path];
2110                 },
2111                 myScale: function(_scaleX, _scaleY){
2112                         this.face.scale(_scaleX,_scaleY);
2113                         if(this.myCode === "REIMU"){
2114                                 this.okazari.x = -CHARA_WIDTH / 2;
2115                                 this.okazari.x = -CHARA_WIDTH / 2 - CHARA_WIDTH / 8;
2116                                 this.okazari.y = -CHARA_HEIGHT / 3 - CHARA_HEIGHT/7;
2117                         }else{
2118                                 this.okazari.x = -CHARA_WIDTH /2;
2119                                 this.okazari.y = -CHARA_HEIGHT / 2 - CHARA_HEIGHT/6;
2120                         }
2121
2122                         this.okazari.scale(_scaleX,_scaleY);
2123                         this.hear.scale(_scaleX,_scaleY);
2124                         this.scale(_scaleX,_scaleY);
2125                         this.shadow.scale(_scaleX, _scaleY);
2126                 },
2127                 send: function(targetYukkuri){
2128                         targetYukkuri.recv();
2129                 },
2130                 recv: function(object, callback){
2131                         this.actionQueue.push({
2132                                 object : object,
2133                                 actionStatus : EAction.COMMUNICATE_RECV,
2134                                 callback: callback
2135                         });
2136                 },
2137                 destruct: function(){
2138
2139                 }
2140         });
2141         var Marisa = enchant.Class.create(Yukkuri,{
2142                 initialize: function(x, y, grow){
2143                         Yukkuri.call(this,x,y, grow);
2144                         this.classname = "Marisa";
2145                         this.myCode = "MARISA";
2146                         this.myName = "まりさ";
2147
2148                         // var xml = this.loadParamsXML("./data/marisa/params.xml");
2149                         // var json = net.load("./data/marisa/params.json");
2150                         // this.param = json.root.param;
2151                         this.param = MarisaParam;
2152                         if(this.grow == EGrow.BABY){
2153                                 this.myName = "まりちゃ";
2154                                 this.param.range /= 4;
2155                         }
2156                         else if(this.grow == EGrow.CHILD){
2157                                 this.param.range /= 3;
2158                         }
2159                         else if(this.grow == EGrow.SUBADULT){
2160                                 this.param.range *= 0.8;
2161                         }
2162                         this.shadow = new enchant.Sprite(64, 64);
2163                         this.shadow.image = game.assets[EResPath.YUKKURI_SHADOW];
2164                         this.shadow.x = 0 + this.x;
2165                         this.shadow.y = this.getWidth() / 16 + this.x;
2166                         this.shadow.image._element.style.zIndex = 2;
2167
2168                         this._style.zIndex = 5;
2169
2170                         this.face = new enchant.Sprite(64, 64);
2171                         this.face.image = game.assets[EResPath.MARISA_FACE_NORMAL];
2172                         this.face.x = -CHARA_WIDTH / 2;
2173                         this.face.y = -CHARA_HEIGHT / 2;
2174                         this.face._style.zIndex = 0;
2175
2176                         this.faceStatus ={};
2177                         this.faceStatus[EFace.NORMAL] = EResPath.MARISA_FACE_NORMAL;
2178                         this.faceStatus[EFace.CRY1] = EResPath.MARISA_FACE_CRY1;
2179                         this.faceStatus[EFace.CRY2] = EResPath.MARISA_FACE_CRY2;
2180                         this.faceStatus[EFace.ANGRY] = EResPath.MARISA_FACE_ANGRY;
2181                         this.faceStatus[EFace.HAPPY1] = EResPath.MARISA_FACE_HAPPY1;
2182                         this.faceStatus[EFace.HAPPY2] = EResPath.MARISA_FACE_HAPPY2;
2183                         this.faceStatus[EFace.EAT1] = EResPath.MARISA_FACE_EAT1;
2184                         this.faceStatus[EFace.EAT2] = EResPath.MARISA_FACE_EAT2;
2185                         this.faceStatus[EFace.TIRED] = EResPath.MARISA_FACE_TIRED;
2186                         this.faceStatus[EFace.SLEEP] = EResPath.MARISA_FACE_SLEEP;
2187
2188
2189                         this.hear = new enchant.Sprite(64, 64);
2190                         this.hear.image = game.assets[EResPath.MARISA_HEAR];
2191                         this.hear.x = -CHARA_WIDTH / 2;
2192                         this.hear.y = -CHARA_HEIGHT / 2;
2193                         this.hear._style.zIndex = 0;
2194
2195                         this.okazari = new Okazari(this);
2196
2197                         // this.imgGroup._style.zIndex = 1;
2198
2199
2200                         this.imgGroup.addChild(this.shadow);
2201                         this.imgBody.addChild(this);
2202                         this.imgBody.addChild(this.face);
2203                         this.imgBody.addChild(this.hear);
2204                         this.imgBody.addChild(this.okazari);
2205                         this.myScale(this.myScaleX, this.myScaleY)
2206                         if(this instanceof Player){
2207                                 // this.myScaleX = 0.5;
2208                                 // this.myScaleY = 0.5;
2209
2210                                 this.budge = new enchant.Sprite(16, 16);
2211                                 this.budge.image = game.assets[EResPath.PLAYER_BADGE];
2212                                 this.imgBody.addChild(this.budge);
2213                                 this.budge.x -= this.getWidth()/2;
2214                                 this.budge.y -= this.getHeight()/2;
2215                                 this.budge.scale(this.myScaleX, this.myScaleX);
2216                         }
2217                         this.imgGroup.width = this.getWidth();
2218                         this.imgGroup.height = this.getHeight();
2219                         this.imgGroup.addChild(this.imgBody);
2220                         backgroundMap.addChild(this.imgGroup);
2221                         this._tweet = new TTweet(148, 64);
2222
2223                         this.imgGroup.addChild(this._tweet);
2224                         this.imgGroup.x = x;
2225                         this.imgGroup.y = y;
2226
2227                         this.animation();
2228                         this.action = new Action(this);
2229                         ctl.addObj(this);
2230                 }
2231         });
2232         var Player = enchant.Class.create(Marisa,{
2233                 initialize: function(x, y){
2234                         // this.runEnterframe = function(){
2235                         // },
2236                         Marisa.call(this, x, y, EGrow.ADULT);
2237                         this.classname = "Player";
2238                         this.cmdQue = [];
2239                         ctl.setPlayer(this);
2240                         // this.removeEventListener('enterframe',this.runEnterframe);
2241                         this.addEventListener('enterframe', function(){
2242
2243
2244                                 if(this.age%10 === 0 && this.age !== 0){
2245                                         // console.log("palyer:" + this.age);
2246                                 }
2247                                 // this.act();
2248                                 ///////////////////////////////////////////////
2249                                 //Action pattern  debug
2250                                 if(game.input.left){
2251                                         // this.changeFace(EResPath.MARISA_FACE_TIRED);
2252                                         this.moveX(-10);
2253                                         this.direction = EDirection.LEFT;
2254                                 }
2255                                 if(game.input.right){
2256                                         this.moveX(10);
2257                                         this.direction = EDirection.RIGHT;
2258                                 }
2259                                 if(game.input.up){
2260                                         this.moveY(-10);
2261                                 }
2262                                 if(game.input.down){
2263                                         this.moveY(10);
2264                                 }
2265
2266                                 // if(this.age%20 == 0)console.log("player:" + (this.getY() + this.getHeight()/2));
2267                                 ///////////////////////////////////////////////
2268                                 // this.reverse();
2269                                 // if(ctl.backgroundMap.hitTest(this.imgGroup.x, this.imgGroup.y + this.height / 2)){
2270                                 //      console.log("hit:" + this.imgGroup.x + ":" +  (this.imgGroup.y + this.height/2) );
2271                                 // }else{
2272                                 //      console.log("not:" + this.imgGroup.x + ":" +  (this.imgGroup.y + this.image.height/2));
2273                                 // }
2274                                 //- Display the circle of search range.
2275                                 // fieldBg.image.clear();
2276                                 // fieldBg.image.context.beginPath();
2277                                 // fieldBg.image.context.fillStyle = '#ff0000';
2278                                 // fieldBg.image.context.arc(this.imgGroup.x + this.width/2, this.imgGroup.y + this.height/2, this.range, Math.PI * 2, false);
2279                                 // fieldBg.image.context.fill();
2280                         });
2281                 }
2282         });
2283         var Reimu = enchant.Class.create(Yukkuri,{
2284                 initialize: function(x, y, grow){
2285                         Yukkuri.call(this,x,y, grow);
2286                         this.classname = "Reimu";
2287                         this.myCode = "REIMU";
2288                         this.myName = "れいむ";
2289
2290                         // var xml = this.loadParamsXML("./data/marisa/params.xml");
2291                         // var json = net.load("./data/reimu/params.json");
2292                         // this.param = json.root.param;
2293                         this.param = ReimuParam;
2294                         if(this.grow == EGrow.BABY){
2295                                 this.myName = "れいみゅ";
2296                                 this.param.range /= 4;
2297                         }
2298                         else if(this.grow == EGrow.CHILD){
2299                                 this.param.range /= 3;
2300                         }
2301                         else if(this.grow == EGrow.SUBADULT){
2302                                 this.param.range *= 0.8;
2303                         }
2304                         this.shadow = new enchant.Sprite(64, 64);
2305                         this.shadow.image = game.assets[EResPath.YUKKURI_SHADOW];
2306                         this.shadow.x = 0 + this.x;
2307                         this.shadow.y = this.getWidth() / 16 + this.x;
2308                         this.shadow.image._element.style.zIndex = 2;
2309
2310                         this._style.zIndex = 5;
2311
2312                         this.face = new enchant.Sprite(64, 64);
2313                         this.face.image = game.assets[EResPathReimu.FACE_NORMAL];
2314                         this.face.x = -CHARA_WIDTH / 2;
2315                         this.face.y = -CHARA_HEIGHT / 2;
2316                         this.face._style.zIndex = 0;
2317
2318                         this.faceStatus ={};
2319                         this.faceStatus[EFace.NORMAL] = EResPathReimu.FACE_NORMAL;
2320                         this.faceStatus[EFace.CRY1] = EResPathReimu.FACE_CRY1;
2321                         this.faceStatus[EFace.CRY2] = EResPathReimu.FACE_CRY2;
2322                         this.faceStatus[EFace.ANGRY] = EResPathReimu.FACE_ANGRY;
2323                         this.faceStatus[EFace.HAPPY1] = EResPathReimu.FACE_HAPPY1;
2324                         this.faceStatus[EFace.HAPPY2] = EResPathReimu.FACE_HAPPY2;
2325                         this.faceStatus[EFace.EAT1] = EResPathReimu.FACE_EAT1;
2326                         this.faceStatus[EFace.EAT2] = EResPathReimu.FACE_EAT2;
2327                         this.faceStatus[EFace.TIRED] = EResPathReimu.FACE_TIRED;
2328                         this.faceStatus[EFace.SLEEP] = EResPathReimu.FACE_SLEEP;
2329
2330
2331                         this.hear = new enchant.Sprite(64, 64);
2332                         this.hear.image = game.assets[EResPathReimu.HEAR];
2333                         this.hear.x = -CHARA_WIDTH / 2;
2334                         this.hear.y = -CHARA_HEIGHT / 2;
2335
2336                         this.sub1 = new enchant.Sprite(64, 64);
2337                         this.sub1.image = game.assets[EResPathReimu.SUB1];
2338                         this.sub1.x = -CHARA_WIDTH / 2;
2339                         this.sub1.y = -CHARA_HEIGHT / 2;
2340
2341                         this.okazari = new Okazari(this);
2342
2343                         // this.imgGroup._style.zIndex = 1;
2344
2345
2346                         this.imgGroup.addChild(this.shadow);
2347                         this.imgBody.addChild(this.okazari);
2348                         this.imgBody.addChild(this);
2349                         this.imgBody.addChild(this.face);
2350                         this.imgBody.addChild(this.hear);
2351                         this.imgBody.addChild(this.sub1);
2352                         this.myScale(this.myScaleX, this.myScaleY)
2353                         if(this instanceof Player){
2354                                 // this.myScaleX = 0.5;
2355                                 // this.myScaleY = 0.5;
2356
2357                                 this.budge = new enchant.Sprite(16, 16);
2358                                 this.budge.image = game.assets[EResPath.PLAYER_BADGE];
2359                                 this.imgBody.addChild(this.budge);
2360                                 this.budge.x -= this.getWidth()/2;
2361                                 this.budge.y -= this.getHeight()/2;
2362                                 this.budge.scale(this.myScaleX, this.myScaleX);
2363                         }
2364                         this.imgGroup.width = this.getWidth();
2365                         this.imgGroup.height = this.getHeight();
2366                         this.imgGroup.addChild(this.imgBody);
2367                         backgroundMap.addChild(this.imgGroup);
2368                         this._tweet = new TTweet(148, 64);
2369
2370                         this.imgGroup.addChild(this._tweet);
2371                         this.imgGroup.x = x;
2372                         this.imgGroup.y = y;
2373
2374                         this.animation();
2375                         this.action = new Action(this);
2376                         ctl.addObj(this);
2377                 }
2378         });
2379
2380         game.onload = function(){
2381                 def.Food = Food;
2382                 def.Unun = Unun;
2383                 def.Place = Place;
2384                 def.ObjSprite = ObjSprite;
2385                 def.Yukkuri = Yukkuri;
2386                 def.nodeSort = nodeSort;
2387                 mangTouch = new MangTouch(ctl);
2388                 mangMsg = new MangMsg(ctl);
2389                 ctl.setDefined(def);
2390                 backgroundMap.image = game.assets[EResPath.MAP0];
2391                 backgroundMap.loadData(_mapData);
2392                 backgroundMap.collisionData = _collisionData;
2393
2394                 var menuSurface = new enchant.Surface(EMenuPos.WIDTH, EMenuPos.HEIGHT);
2395                 var fieldSurface = new enchant.Surface(EFieldPos.WIDTH, EFieldPos.HEIGHT);
2396                 var windowSurface = new enchant.Surface(EFieldPos.WIDTH, EFieldPos.HEIGHT);
2397                 fieldBg.image = fieldSurface;
2398                 fieldBg.x = 0;
2399                 fieldBg.y = 0;
2400
2401                 windowFront.image = windowSurface;
2402                 windowFront.x = 0;
2403                 windowFront.y = 0;
2404                 windowFront.opacity = 0.5;
2405
2406                 windowSurface.context.fillStyle = '#000';
2407                 windowSurface.context.fillRect(0 + 20, EFieldPos.HEIGHT - 60, EFieldPos.WIDTH - 40, EFieldPos.HEIGHT);
2408
2409
2410                 menuSurface.context.fillStyle = '#000';
2411                 menuSurface.context.fillRect(0, 0, EMenuPos.WIDTH, EMenuPos.HEIGHT);
2412                 menuBg.image = menuSurface;
2413                 menuBg.x = EMenuPos.X;
2414                 menuBg.y = EMenuPos.Y;
2415                 backgroundMap.addChild(fieldBg);
2416                 mangIcon = new MangIcon(ctl, menuBg);
2417
2418                 game.rootScene.addChild(backgroundMap);
2419                 game.rootScene.addChild(windowFront);
2420                 game.rootScene.addChild(menuBg);
2421
2422                 // var labelGroup = new LabelGroup();
2423                 mangLabel = new MangLabel(ctl, menuBg);
2424                 // var info = new Label("ゆっくり");
2425                 // info.color = "#ffffff";
2426                 // info.font = "14px 'Times New Roman'";
2427                 // info.x = 4;
2428                 // info.y = GAME_HEIGHT / 2;
2429                 // labelGroup.addChild(info);
2430                 // menuBg.addChild(labelGroup);
2431
2432
2433                 // var food = new def.Food("apple", 200, 250);
2434                 backgroundMap.addChild(foodGroup);
2435                 // backgroundMap.addChild(placeGroup);
2436                 var player = new Player(PALYER_INIT_X, PALYER_INIT_Y);
2437                 new Marisa(PALYER_INIT_X + 50, PALYER_INIT_Y + 200, EGrow.ADULT);
2438                 new Marisa(PALYER_INIT_X + 100, PALYER_INIT_Y + 200, EGrow.SUBADULT);
2439                 new Marisa(PALYER_INIT_X + 150, PALYER_INIT_Y + 200, EGrow.CHILD);
2440                 new Reimu(PALYER_INIT_X + 400, PALYER_INIT_Y + 400, EGrow.ADULT);
2441                 // new Unun(100, 100);
2442                 // new Place("tree", PALYER_INIT_X, PALYER_INIT_Y - 50);
2443                 var touchX = 0;
2444
2445
2446                 game.rootScene.addEventListener('touchstart', function (e) {
2447                         game.touched = true;
2448                         var mang = mangTouch.get(ctl.getCurrentCommand());
2449                         mang.touchstart(e);
2450                 });
2451                 game.rootScene.addEventListener('touchmove', function (e) {
2452                         var mang = mangTouch.get(ctl.getCurrentCommand());
2453                         mang.touchmove(e);
2454                 });
2455                 game.rootScene.addEventListener('touchend', function (e) {
2456                         var mang = mangTouch.get(ctl.getCurrentCommand());
2457                         mang.touchend(e);
2458                         game.touched = false;
2459                 });
2460
2461
2462                 game.rootScene.addEventListener('enterframe', function(){
2463                         //main frame
2464                         mangLabel.draw(player);
2465                         if(this.age%BASE_FPS == 0 ){
2466                                 ctl.calcTime();
2467                         }
2468                         ctl.time++;
2469                 });
2470                 this.addEventListener('enterframe', function(){
2471                         //The priority processing of display instead of z-index
2472
2473                         backgroundMap.childNodes.sort(nodeSort);
2474                 });
2475
2476         };
2477         game.start();
2478 };