OSDN Git Service

b1e3d2868085e4a21c46e2546f57ca4a07b5a963
[yukkurioverwint/YukkuriOverwinter.git] / main.js
1 enchant();
2 //////////////////////////////////////
3 //global
4 var ctl = null;
5 var mangIcon = null;
6 var mangTouch = null;
7 var mangLabel = null;
8 var mangMsg = null;
9 var LabelGroup = enchant.Class.mixClasses(Label, Group,true);
10 var net = new Net();
11 //////////////////////////////////////
12 //define
13 //////////////////////////////////////
14 var PALYER_INIT_X = 100;
15 var PALYER_INIT_Y = 100;
16 var CHARA_WIDTH = 64;
17 var CHARA_HEIGHT = 64;
18 var GAME_WIDTH = 800;
19 var GAME_HEIGHT = 600;
20 var FPS = 20;
21 var BASE_FPS = 20;
22 var HUNGRY_MAX = 100;
23 var MIN = BASE_FPS;
24 var HOUR = MIN * 60;
25 // var DAY = HOUR * 24;
26 var DAY = HOUR ;
27
28 var EResPath = {
29         YUKKURI_BASE : './data/yukkuri_base.png',
30         YUKKURI_BODY_DEAD : './data/yukkuri_dead.png',
31         YUKKURI_UNUN : './data/unun.png',
32         YUKKURI_SHADOW : './data/shadow.png',
33         MARISA_FACE_NORMAL : './data/marisa/face_normal1.png',
34         MARISA_FACE_TIRED : './data/marisa/face_tired1.png',
35         MARISA_FACE_ANGRY : './data/marisa/face_angry1.png',
36         MARISA_FACE_CRY1 : './data/marisa/face_cry1.png',
37         MARISA_FACE_CRY2 : './data/marisa/face_cry2.png',
38         MARISA_FACE_EAT1 : './data/marisa/face_eat1.png',
39         MARISA_FACE_EAT2 : './data/marisa/face_eat2.png',
40         MARISA_FACE_HAPPY1 : './data/marisa/face_happy1.png',
41         MARISA_FACE_HAPPY2 : './data/marisa/face_happy2.png',
42         MARISA_FACE_SLEEP : './data/marisa/face_sleep1.png',
43         MARISA_HEAR : './data/marisa/hear1.png',
44         OBJECT : './data/object.png',
45         MAP0 : './data/map0.png',
46         COMMAND : './data/command.png',
47         OKAZARI : './data/okazari.png',
48         PLACE_TREE : './data/system/place_tree.png',
49         GRASS : './data/system/grass.png',
50         PLAYER_BADGE : './data/system/player_badge.png',
51         // PLACE_TREE : './data/system/rapture.png',
52 };
53 var EFace = {
54         NORMAL:0,
55         TIRED:1,
56         ANGRY:2,
57         CRY1:3,
58         CRY2:4,
59         EAT1:5,
60         EAT2:6,
61         HAPPY1:7,
62         HAPPY2:8,
63         SLEEP:9,
64 };
65 var EBody = {
66         NORMAL: 0,
67         DEAD: 1,
68 };
69
70 var ECommand = {
71         WORLD_CURSOR: 0,
72         WORLD_TARGET: 1,
73         WORLD_APPLE: 2,
74         WORLD_PLACE: 3,
75 };
76
77 var EMenuPos = {
78         X : 600,
79         Y : 0,
80         WIDTH : 200,
81         HEIGHT : GAME_HEIGHT,
82 };
83 var EFieldPos = {
84         X : 0,
85         Y : 0,
86         WIDTH: GAME_WIDTH - EMenuPos.WIDTH,
87         HEIGHT: GAME_HEIGHT,
88 };
89 var EAction = {
90         WAIT : 0,
91         HUNT : 3,
92         THINK : 4,
93         WALK : 5,
94         DEAD: 6,
95         EAT_START : 8,
96         EAT_WAIT : 9,
97         MOVE_TO_EAT_START: 10,
98         MOVE_TO_EAT_UNDERWAY: 11,
99         MOVE_TO_COMMAND: 12,
100         UNUN_START : 13,
101         UNUN_WAIT : 14,
102         UNUN_SMELL_START : 15,
103         UNUN_SMELL_WAIT : 16,
104         SLEEP_START : 17,
105         SLEEP_WAIT : 18,
106         COMMUNICATE_START : 19,
107         COMMUNICATE_WAIT: 20,
108         COMMUNICATE_RECV: 21,
109         MOVE_TO_PLACE_START_HOMELESS: 22,
110         MOVE_TO_PLACE_HOMELESS_UNDERWAY: 23,
111         DECLARE_PLACE_HOMELESS: 24,
112         MOVE_TO_HUNT_START: 25,
113         THINK_SCHEDULE: 26,
114         NONE: 9999
115 };
116 var EDirection = {
117         LEFT : 0,
118         RIGHT: 1,
119 };
120 var EMsg = {
121         WALK: 0,
122         SLEEP: 1,
123         EAT: 2,
124         GET_UP: 3,
125         DEAD: 4,
126         UNUN: 5,
127         UNUN_END: 6,
128         UNUN_SMELL: 7,
129         SEARCH_FOOD: 8,
130         EAT_FAILED: 9,
131         SAY_HELLO: 10,
132         TARGETING_EAT : 11,
133         DECLARE_PLACE: 12,
134 };
135 var EWalkAnimation = {
136         START: 0,
137         END: 1,
138 };
139
140
141 var ObjSprite = enchant.Class.create(enchant.Sprite,{
142         initialize: function (w, h){
143                 enchant.Sprite.call(this, w, h);
144                 this.id = guid();
145                 ctl.addObj(this);
146
147
148                                 // // new BoxLabel(this.ctl,100, e.x, e.y);
149                                 // var box = new Box(this.ctl, 200, 200);
150
151                                 // box.text("テスト\naaaaaa\nfffffffff\nテスト\naaaaaa\nfffffffff\nテスト\naaaaaa\n", e.x, e.y);
152
153         },
154         getStatusForDisplay: function (){
155                 return "";
156         },
157         getX: function(){
158                 return this.x;
159         },
160         getY: function(){
161                 return this.y;
162         },
163         removeAll: function(){
164                 this.remove();
165                 ctl.removeObj(this);
166         }
167 });
168
169
170 enchant.Timeline.prototype.moveByEx = function(x, y, time, easing, eventFunctions){
171         var params = {
172                 x: function() {
173                         return this.x + x;
174                 },
175                 y: function() {
176                         return this.y + y;
177                 },
178                 time: time,
179                 easing: easing
180         };
181         //-Event register [onactionstart,onactiontick,onactionend]
182         if(eventFunctions !== undefined){
183                 for(var key in eventFunctions){
184                         params[key] = eventFunctions[key];
185                 }
186         }
187         return this.tween(params);
188 };
189
190
191
192 include("./lang/ja.js");
193 window.onload = function(){
194         var nodeSort = function (a, b){
195                 if(typeof a.id === "undefined" && typeof b.id === "undefined"){
196                         return 0;
197                 }
198                 else if(typeof a.id === "undefined"){
199                         return -1;
200                 }
201                 else if(typeof b.id === "undefined"){
202                         return 1;
203                 }
204                 var ay = a.y;
205                 var by = b.y;
206                 if(a instanceof Box && b instanceof Box){
207                         return 0;
208                 }
209                 if(a instanceof Box){
210                         return 1;
211                 }
212                 if(b instanceof Box){
213                         return -1;
214                 }
215
216                 if(a instanceof Place){
217                         ay += Math.round(a.height * 0.7);
218                 }
219                 if(b instanceof Place){
220                         by += Math.round(b.height * 0.7);
221                 }
222
223                 if(ay < by)return -1;
224                 else if(ay > by)return 1;
225                 if(a.id < b.id)return -1;
226                 else if(a.id > b.id)return 1;
227                 return 0;
228
229         }
230
231         //init game
232         var def = new Object();
233         var foodGroup = new Group();
234         var SpriteGroup = enchant.Class.mixClasses(Sprite, Group,true);
235         var menuBg = new SpriteGroup(EMenuPos.WIDTH, EMenuPos.HEIGHT);
236         var fieldBg = new enchant.Sprite(EFieldPos.WIDTH, EFieldPos.HEIGHT);
237         var windowFront = new enchant.Sprite(EFieldPos.WIDTH, EFieldPos.HEIGHT);
238         var game = new Game(GAME_WIDTH, GAME_HEIGHT);
239         game.fps = FPS;
240         var _loadArr = [];
241         var i=0;
242         for(var _v in EResPath){
243                 _loadArr[i] = EResPath[_v];
244                 i++;
245         }
246         game.preload(_loadArr);
247         var MapGroup = enchant.Class.mixClasses(Map, Group,true);
248         var backgroundMap = new MapGroup(16, 16);
249         include("./class.js");
250         include("./ctrl.js");
251         ctl = new Ctrl();
252         ctl.init(game);
253         ctl.setBackgroundMap(backgroundMap);
254         var Action = enchant.Class.create({
255                 initialize: function (yukkuri){
256                         this.status = EAction.THINK;
257                         this.yukkuri = yukkuri;
258                         this.isMoving = false;
259                         this.targetNode = null;
260                 },
261                 getStatus: function(){
262                         return this.status;
263                 },
264                 setStatus: function(eaction){
265                         this.status = eaction;
266                 },
267                 observe: function(){
268                         if(this.yukkuri.isDead()){
269                                 this.status = EAction.DEAD;
270                         }
271                         // else if(this.yukkuri.isSleep()){
272                         //      this.status = EAction.SLEEP;
273                         // }
274
275                 },
276                 act : function(){
277
278                         this.observe();
279                         switch(this.status){
280                                 case EAction.WAIT:
281
282                                 break;
283                                 case EAction.THINK:
284                                         if(this.yukkuri.age%2 === 0){
285                                                 if(this.yukkuri.isPlace()){
286                                                         if(this.yukkuri.isSleep()){
287                                                                 this.setStatus(EAction.SLEEP_START);
288                                                         }
289                                                         else if(this.yukkuri.getUnun() >= 80){
290                                                                 this.setStatus(EAction.UNUN_START);
291                                                         }
292                                                         else{
293                                                                 this.yukkuri.changeFace();
294                                                                 this.searchHavePlace();
295                                                         }
296                                                 }
297                                                 else{
298                                                         if(this.yukkuri.isSleep()){
299                                                                 this.setStatus(EAction.SLEEP_START);
300                                                         }
301                                                         else if(this.yukkuri.getUnun() >= 80){
302                                                                 this.setStatus(EAction.UNUN_START);
303                                                         }
304                                                         else{
305                                                                 // this.yukkuri.param.yukkuri -= 1;
306                                                                 this.yukkuri.changeFace();
307                                                                 this.search();
308                                                         }
309                                                 }
310
311                                         }
312                                 break;
313                                 case EAction.MOVE_TO_EAT_START:
314                                         this.move_to_eat_start();
315                                 break;
316                                 case EAction.MOVE_TO_EAT_UNDERWAY:
317                                 break;
318                                 //Random Walk
319                                 case EAction.WALK:
320                                         this.walk(0);
321                                 break;
322                                 case EAction.SLEEP_START:
323                                         this.sleep_start();
324                                 break;
325                                 case EAction.SLEEP_WAIT:
326                                 break;
327                                 case EAction.DEAD:
328                                         this.dead();
329                                 break;
330                                 case EAction.EAT_START:
331                                         this.eat_start();
332                                 break;
333                                 case EAction.EAT_WAIT:
334                                 break;
335                                 case EAction.UNUN_START:
336                                         this.unun_start();
337                                 break;
338                                 case EAction.UNUN_WAIT:
339                                 break;
340                                 case EAction.MOVE_TO_COMMAND:
341                                         this.move_to_command();
342                                 break;
343                                 case EAction.UNUN_SMELL_START:
344                                         this.unun_smell_start();
345                                 break;
346                                 case EAction.COMMUNICATE_START:
347                                         this.communicate_start();
348                                 break;
349                                 case EAction.COMMUNICATE_WAIT:
350                                 break;
351                                 case EAction.COMMUNICATE_RECV:
352                                 break;
353                                 case EAction.MOVE_TO_PLACE_START_HOMELESS:
354                                         this.move_to_place_start_homeless();
355                                 break;
356                                 case EAction.DECLARE_PLACE_HOMELESS:
357                                         this.declare_place_homeless();
358                                 break;
359
360                         }
361                         this.yukkuri.reverse();
362                 },
363
364                 move_to_eat_start : function(){
365                         new MoveToEatEvent({
366                                 "type": 'food',
367                                 "targetNode": this.targetNode,
368                                 "action": this,
369                                 "myYukkuri": this.yukkuri
370                         });
371                 },
372                 walkNew: function(){
373
374                 },
375                 walk : function(retryCounter){
376                         if(retryCounter > 15){
377                                 retryCounter = 0;
378                                 this.status = EAction.NONE;
379                                 console.log("retryCount over");
380                                 return;
381                         }
382                         if(this.yukkuri.imgGroup.tl.queue.length === 0){
383                                 var frame = 200;
384                                 if(this.isMoving){
385                                         this.isMoving = false;
386                                         this.status = EAction.THINK;
387                                         this.yukkuri.vx = 0;
388                                         this.yukkuri.vy = 0;
389                                 }else{
390                                         var xpos = 150 + rand(40) - (retryCounter * 10);
391                                         var ypos = 150 + rand(40) - (retryCounter * 10);
392                                         if(rand(8) === 0){
393                                                 this.yukkuri.vx = 0;
394                                                 this.yukkuri.vy = -ypos;
395                                         }
396                                         else if(rand(8) == 1){
397                                                 this.yukkuri.vx = xpos;
398                                                 this.yukkuri.vy = -ypos;
399                                                 this.yukkuri.direction = EDirection.RIGHT;
400                                         }
401                                         else if(rand(8) == 2){
402                                                 this.yukkuri.vx = xpos;
403                                                 this.yukkuri.vy = 0;
404                                                 this.yukkuri.direction = EDirection.RIGHT;
405                                         }
406                                         else if(rand(8) == 3){
407                                                 this.yukkuri.vx = xpos;
408                                                 this.yukkuri.vy = ypos;
409                                                 this.yukkuri.direction = EDirection.RIGHT;
410                                         }
411                                         else if(rand(8) == 4){
412                                                 this.yukkuri.vx = 0;
413                                                 this.yukkuri.vy = ypos;
414                                         }
415                                         else if(rand(8) == 5){
416                                                 this.yukkuri.vx = -xpos;
417                                                 this.yukkuri.vy = ypos;
418                                                 this.yukkuri.direction = EDirection.LEFT;
419                                         }
420                                         else if(rand(8) == 6){
421                                                 this.yukkuri.vx = -xpos;
422                                                 this.yukkuri.vy = 0;
423                                                 this.yukkuri.direction = EDirection.LEFT;
424                                         }
425                                         else if(rand(8) == 7){
426                                                 this.yukkuri.vx = -xpos;
427                                                 this.yukkuri.vy = -ypos;
428                                                 this.yukkuri.direction = EDirection.LEFT;
429                                         }
430                                         if (this.yukkuri.vx || this.yukkuri.vy) {
431                                                 var map = ctl.backgroundMap;
432                                                 // var x = this.yukkuri.x + (this.yukkuri.moveX ? this.yukkuri.moveX / Math.abs(this.yukkuri.moveX) * 16 : 0) + 16;
433                                                 // var y = this.yukkuri.y + (this.yukkuri.moveY ? this.yukkuri.moveY / Math.abs(this.yukkuri.moveY) * 16 : 0) + 16;
434                                                 var x = this.yukkuri.imgGroup.x + this.yukkuri.vx;
435                                                 var y = this.yukkuri.imgGroup.y + this.yukkuri.vy + this.yukkuri.height / 2;
436                                                 if (0 <= x && x < map.width && 0 <= y && y < map.height && !map.hitTest(x, y)) {
437                                                         // console.log("あたってないよ:"+ this.yukkuri.imgGroup.x + ":" + this.yukkuri.imgGroup.y);
438                                                         // console.log("X:" + this.yukkuri.moveX);
439                                                         // console.log("Y:" + this.yukkuri.moveY);
440                                                         this.isMoving = true;
441                                                         this.yukkuri.reverse();
442                                                         this.yukkuri.moveBy(this.yukkuri.vx, this.yukkuri.vy, frame);
443                                                         mangMsg.output(this.yukkuri,EMsg.WALK);
444                                                         //うんうんくさい対策
445                                                         this.targetNode = null;
446                                                 }else{
447                                                         // console.log("HIT:"+ this.yukkuri.imgGroup.x + ":" + this.yukkuri.imgGroup.y);
448                                                         this.walk(retryCounter+1);
449                                                 }
450                                         }
451                                 }
452                         }
453                 },
454                 sleep_start: function(){
455                         new SleepEvent({
456                                 "action": this,
457                                 "myYukkuri": this.yukkuri
458                         });
459                 },
460                 dead : function(){
461                         mangMsg.output(this.yukkuri, EMsg.DEAD);
462                         this.yukkuri.tlPause();
463                         this.yukkuri.setBodyImage(EBody.DEAD);
464                 },
465                 eat_start: function(){
466                         new EatEvent({
467                                 "type": 'eat',
468                                 "targetNode": this.targetNode,
469                                 "action": this,
470                                 "myYukkuri": this.yukkuri
471                         });
472
473                 },
474                 unun_start: function(){
475                         new UnunEvent({
476                                 "action": this,
477                                 "myYukkuri": this.yukkuri
478                         });
479                 },
480                 unun_smell_start: function(){
481                         new UnunSmellEvent({
482                                 "action": this,
483                                 "myYukkuri": this.yukkuri
484                         });
485                 },
486                 communicate_start:function(){
487                         new ComSayHelloSendEvent({
488                                 "type": 'greeting',
489                                 "targetNode": this.targetNode,
490                                 "action": this,
491                                 "myYukkuri": this.yukkuri
492                         });
493                 },
494                 move_to_place_start_homeless:function(){
495                         new MoveToPlaceStartHomelessEvent({
496                                 "targetNode": this.targetNode,
497                                 "action": this,
498                                 "myYukkuri": this.yukkuri
499                         });
500                 },
501                 declare_place_homeless:function(){
502                         new DeclarePlaceHomelessEvent({
503                                 "targetNode": this.targetNode,
504                                 "action": this,
505                                 "myYukkuri": this.yukkuri
506                         });
507                 },
508                 move_to_command: function(){
509
510                 },
511                 searchHavePlace: function(){
512                         var nodes = ctl.getObjs();
513                         l = nodes.length;
514                         var ununFlg = false;
515
516                         for (var key in nodes) {
517                                 var node = nodes[key];
518                                 if(this.yukkuri.id === node.id)continue;
519                                 if (!ununFlg
520                                         && node instanceof Unun
521                                         && !(this.targetNode instanceof Unun)
522                                         && this.yukkuri.within(node, this.yukkuri.getRange() / 2)) {
523                                         ununFlg = true;
524                                         this.targetNode = node;
525                                         this.setStatus(EAction.UNUN_SMELL_START);
526                                         return;
527                                 }
528                                 else if (node instanceof Food) {
529                                         if(this.yukkuri.isKuhuku() && this.yukkuri.within(node, this.yukkuri.getRange())){
530
531                                                 //A yukkuri to go to the food area
532                                                 this.targetNode = node;
533                                                 this.setStatus(EAction.MOVE_TO_EAT_START);
534
535                                                 return;
536                                         }else{
537                                                 //not hunbry or not food.
538                                         }
539                                 }
540                                 else if(node instanceof Yukkuri){
541                                         if(this.yukkuri.isCommunication(node) && this.yukkuri.within(node, this.yukkuri.getRange())){
542                                                 this.targetNode = node;
543                                                 this.setStatus(EAction.COMMUNICATE_START);
544                                                 return;
545                                         }
546                                 }
547                                 else if(node instanceof Place){
548                                         if(!node.isOwner() && !this.yukkuri.isPlace() && this.yukkuri.within(node, this.yukkuri.getRange())){
549                                                 this.targetNode = node;
550                                                 this.setStatus(EAction.MOVE_TO_PLACE_START_HOMELESS);
551                                                 return;
552                                         }
553                                 }
554                         }
555                         this.status =  EAction.WALK;
556                 },
557                 search : function(){
558                         var nodes = ctl.getObjs();
559                         l = nodes.length;
560                         var ununFlg = false;
561
562                         for (var key in nodes) {
563                                 var node = nodes[key];
564                                 if(this.yukkuri.id === node.id)continue;
565                                 if (!ununFlg
566                                         && node instanceof Unun
567                                         && !(this.targetNode instanceof Unun)
568                                         && this.yukkuri.within(node, this.yukkuri.getRange() / 2)) {
569                                         ununFlg = true;
570                                         this.targetNode = node;
571                                         this.setStatus(EAction.UNUN_SMELL_START);
572                                         return;
573                                 }
574                                 else if (node instanceof Food) {
575                                         if(this.yukkuri.isKuhuku() && this.yukkuri.within(node, this.yukkuri.getRange())){
576
577                                                 //A yukkuri to go to the food area
578                                                 this.targetNode = node;
579                                                 this.setStatus(EAction.MOVE_TO_EAT_START);
580
581                                                 return;
582                                         }else{
583                                                 //not hunbry or not food.
584                                         }
585                                 }
586                                 else if(node instanceof Yukkuri){
587                                         if(this.yukkuri.isCommunication(node) && this.yukkuri.within(node, this.yukkuri.getRange())){
588                                                 this.targetNode = node;
589                                                 this.setStatus(EAction.COMMUNICATE_START);
590                                                 return;
591                                         }
592                                 }
593                                 else if(node instanceof Place){
594                                         if(!node.isOwner() && !this.yukkuri.isPlace() && this.yukkuri.within(node, this.yukkuri.getRange())){
595                                                 this.targetNode = node;
596                                                 this.setStatus(EAction.MOVE_TO_PLACE_START_HOMELESS);
597                                                 return;
598                                         }
599                                 }
600                         }
601                         this.status =  EAction.WALK;
602                 }
603         });
604         var Event = enchant.Class.create({
605                 initialize: function (_data){
606                 },
607                 onactionstart:function(self){
608                         return function(e){
609                                 if(typeof e === "undefined")return;
610                         };
611                 },
612                 onactiontick:function(self){
613                         return function(e){
614                                 if(typeof e === "undefined")return;
615                         };
616                 },
617                 onactionend:function(self){
618                         return function(e){
619                                 if(typeof e === "undefined")return;
620                         };
621                 }
622         });
623         var WalkEvent = enchant.Class.create(Event,{
624                 initialize: function (_data){
625                         this.data = _data;
626                         this.yukkuri = _data.myYukkuri;
627                         this.action = _data.action;
628
629                         mangMsg.output(this.yukkuri, EMsg.SLEEP);
630                         this.action.setStatus(EAction.SLEEP_WAIT);
631                         this.yukkuri.setFaceImage(EFace.SLEEP);
632                         this.yukkuri.tlPause();
633                         var self = this;
634                         this.yukkuri.addEventListener('enterframe', function() {
635                                 var yukkuri = self.yukkuri;
636                                 self.run();
637                         });
638                 },
639                 run : function(){
640
641                 }
642         });
643         var SleepEvent = enchant.Class.create(Event,{
644                 initialize: function (_data){
645                         this.data = _data;
646                         this.yukkuri = _data.myYukkuri;
647                         this.action = _data.action;
648
649                         mangMsg.output(this.yukkuri, EMsg.SLEEP);
650                         this.action.setStatus(EAction.SLEEP_WAIT);
651                         this.yukkuri.setFaceImage(EFace.SLEEP);
652                         this.yukkuri.tlPause();
653                         var sec = FPS * 3;
654                         var self = this;
655                         this.yukkuri.addEventListener('enterframe', function() {
656                                 var yukkuri = self.yukkuri;
657                                 if(yukkuri.age%2 === 0 && yukkuri.age !== 0)yukkuri.param.sleep--;
658                                 if(yukkuri.param.sleep <= 0){
659                                         yukkuri.param.sleep = 0;
660                                         this.removeEventListener("enterframe",arguments.callee);
661                                         mangMsg.output(self.yukkuri, EMsg.GET_UP);
662                                         yukkuri.setFaceImage(EFace.NORMAL);
663                                         yukkuri.tlResume();
664                                         // self.action.setStatus(EAction.WAIT);
665                                         self.action.setStatus(EAction.THINK);
666                                 }
667                         });
668                 }
669         });
670
671         var UnunEvent = enchant.Class.create(Event,{
672                 initialize: function (_data){
673                         this.data = _data;
674                         this.yukkuri = _data.myYukkuri;
675                         this.action = _data.action;
676                         this.action.setStatus(EAction.UNUN_WAIT);
677                         mangMsg.output(this.yukkuri, EMsg.UNUN);
678                         this.yukkuri.setFaceImage(EFace.HAPPY2);
679                         var sec = FPS * 3;
680
681                         this.yukkuri.wait(sec, {
682                                 "onactionend": this.onactionend(this)
683                         });
684                 },
685                 onactionend:function(self){
686                         return function(e){
687                                 if(typeof e === "undefined")return;
688                                 self.yukkuri.unun();
689                                 self.action.setStatus(EAction.WAIT);
690                                 mangMsg.output(self.yukkuri, EMsg.UNUN_END);
691                                 new WaitEvent({
692                                         "waitframe": FPS * 1.5,
693                                         "action": self.action,
694                                         "myYukkuri": self.yukkuri
695                                 });
696                         };
697                 }
698         });
699         var WaitEvent = enchant.Class.create(Event,{
700                 initialize: function (_data){
701                         this.data = _data;
702                         this.yukkuri = _data.myYukkuri;
703                         this.action = _data.action;
704                         this.waitframe = _data.waitframe;
705                         this.yukkuri.wait(this.waitframe, {
706                                 "onactionstart": this.onactionstart(this),
707                                 "onactiontick": this.onactiontick(this),
708                                 "onactionend": this.onactionend(this)
709                         });
710                 },
711                 onactionend:function(self){
712                         return function(e){
713                                 if(typeof e === "undefined")return;
714                                 self.action.setStatus(EAction.THINK);
715                         };
716                 }
717         });
718         var UnunSmellEvent = enchant.Class.create(Event,{
719                 initialize: function (_data){
720                         this.data = _data;
721                         this.yukkuri = _data.myYukkuri;
722                         this.action = _data.action;
723                         this.action.setStatus(EAction.UNUN_SMELL_WAIT);
724                         mangMsg.output(this.yukkuri, EMsg.UNUN_SMELL);
725                         this.yukkuri.setFaceImage(EFace.CRY1);
726                         var sec = FPS * 3;
727                         this.yukkuri.setYukkuri(this.yukkuri.getYukkuri() - 3);
728                         this.yukkuri.wait(sec, {
729                                 "onactionend": this.onactionend(this)
730                         });
731                 },
732                 onactionend:function(self){
733                         return function(e){
734                                 if(typeof e === "undefined")return;
735                                 self.action.setStatus(EAction.THINK);
736                         };
737                 }
738         });
739
740         var EatEvent = enchant.Class.create(Event,{
741                 initialize: function (_data){
742                         this.data = _data;
743                         this.yukkuri = _data.myYukkuri;
744                         this.food = _data.targetNode;
745                         this.action = _data.action;
746                         this.action.setStatus(EAction.EAT_WAIT);
747
748
749                         var sec = BASE_FPS * 3;
750                         if(this.yukkuri.eat(this.food)){
751                                 mangMsg.output(this.yukkuri, EMsg.EAT);
752                                 this.yukkuri.setFaceImage(EFace.EAT2);
753                         }else{
754                                 sec = BASE_FPS * 6;
755                                 mangMsg.output(this.yukkuri, EMsg.EAT_FAILED);
756                                 this.yukkuri.setFaceImage(EFace.CRY2);
757                         }
758                         this.yukkuri.wait(sec, {
759                                 "onactionend": this.onactionend(this)
760                         });
761                 },
762                 onactionend:function(self){
763                         return function(e){
764                                 if(typeof e === "undefined")return;
765                                 if(self.food.getAmount() <= 0 || self.yukkuri.isManpuku()){
766                                         self.action.setStatus(EAction.THINK);
767                                         self.yukkuri.setFaceImage(EFace.NORMAL);
768                                 }else{
769                                         self.action.setStatus(EAction.EAT_START);
770                                 }
771                         };
772                 }
773         });
774         /**
775          * Move Event
776          * -When yukkuri find Food.
777          * @param  _data
778          * type: "food"
779          * targetNode: enchant.Node
780          * action:Action
781          * myYukkuri:Yukkuri
782          */
783         var MoveToEatEvent = enchant.Class.create(Event,{
784                 initialize: function (_data){
785                         this.data = _data;
786                         this.yukkuri = this.data.myYukkuri;
787                         this.action = this.data.action;
788
789                         var yukkuri = this.yukkuri;
790                         var node = this.data.targetNode;
791                         //food distance
792                         if(this.data.type == "food"){
793                                 this.action.status = EAction.MOVE_TO_EAT_UNDERWAY;
794                                 yukkuri.vx = node.x - yukkuri.getX() + node.width / 2;
795                                 yukkuri.vy = node.y - yukkuri.getY() - yukkuri.getHeight() / 2 + node.height / 2;
796                                 var distance = (Math.abs(yukkuri.vx) + Math.abs(yukkuri.vy)) / 2;
797                                 var frame = distance + distance/5 + 1;
798                                 yukkuri.direction = yukkuri.vx > 0 ? EDirection.RIGHT : EDirection.LEFT;
799                                 yukkuri.reverse();
800
801                                 yukkuri.moveBy(yukkuri.vx, yukkuri.vy, frame, {
802                                         "onactionstart": this.onactionstart(this),
803                                         "onactiontick": this.onactiontick(this),
804                                         "onactionend": this.onactionend(this)
805                                 });
806                         }
807                 },
808                 onactionstart:function(self){
809                         return function(e){
810                                 if(typeof e === "undefined")return;
811                                 mangMsg.output(EMsg.TARGETING_EAT);
812                         };
813                 },
814                 onactiontick:function(self){
815                         return function(e){
816                                 if(typeof e === "undefined")return;
817                         };
818                 },
819                 onactionend:function(self){
820                         return function(e){
821                                 if(typeof e === "undefined")return;
822                                 self.action.setStatus(EAction.EAT_START);
823                         };
824                 }
825         });
826         var MoveCommandEvent = enchant.Class.create(Event,{
827                 initialize: function (_data){
828                         this.data = _data;
829                         this.yukkuri = this.data.myYukkuri;
830                         this.action = this.data.action;
831                         this.vx = this.data.vx;
832                         this.vy = this.data.vy;
833
834                         var yukkuri = this.yukkuri;
835                         this.action.status = EAction.MOVE_TO_EAT_UNDERWAY;
836                         yukkuri.vx = node.x - yukkuri.getX();
837                         yukkuri.vy = node.y - yukkuri.getY();
838                         var distance = (Math.abs(yukkuri.vx) + Math.abs(yukkuri.vy)) / 2;
839                         var frame = distance + 1;
840                         yukkuri.moveBy(yukkuri.vx, yukkuri.vy, frame, {
841                                 "onactionstart": this.onactionstart(this),
842                                 "onactiontick": this.onactiontick(this),
843                                 "onactionend": this.onactionend(this)
844                         });
845                 },
846                 onactionstart:function(self){
847                         return function(e){
848                                 if(typeof e === "undefined")return;
849                         };
850                 },
851                 onactiontick:function(self){
852                         return function(e){
853                                 if(typeof e === "undefined")return;
854                         };
855                 },
856                 onactionend:function(self){
857                         return function(e){
858                                 if(typeof e === "undefined")return;
859                                 self.action.setStatus(EAction.THINK);
860                         };
861                 }
862         });
863         /**
864          * Say hello contact to send communication .
865          */
866         var ComSayHelloSendEvent = enchant.Class.create(Event,{
867                 initialize: function (_data){
868                         this.data = _data;
869                         this.yukkuri = _data.myYukkuri;
870                         this.targetYukkuri = _data.targetNode;
871                         this.action = _data.action;
872                         this.action.setStatus(EAction.COMMUNICATE_WAIT);
873
874                         mangMsg.output(this.yukkuri, EMsg.SAY_HELLO, this.targetYukkuri);
875
876                         this.targetYukkuri.action.setStatus(EAction.COMMUNICATE_RECV);
877                         this.targetYukkuri.tlPause();
878                         this.yukkuri.tlPause();
879                         this.yukkuri.syncDirection(this.targetYukkuri);
880
881                         this.yukkuri.addAddress(this.targetYukkuri);
882                         this.yukkuri.getAddress(this.targetYukkuri).isGreeting = true;
883
884                         var sec = BASE_FPS * 2;
885                         this.yukkuri.wait(sec, {
886                                 "onactiontick": this.onactiontick(this),
887                                 "onactionend": this.onactionend(this)
888                         });
889                 },
890                 onactiontick:function(self){
891                         return function(e){
892                                 if(typeof e === "undefined")return;
893                         };
894                 },
895                 onactionend:function(self){
896                         return function(e){
897                                 if(typeof e === "undefined")return;
898                                 new ComSayHelloRecvEvent({
899                                         "type": 'greeting',
900                                         "targetNode": self.yukkuri,
901                                         "action": self.targetYukkuri.action,
902                                         "myYukkuri": self.targetYukkuri
903                                 });
904                         };
905                 }
906         });
907         var ComSayHelloRecvEvent = enchant.Class.create(Event,{
908                 initialize: function (_data){
909                         this.data = _data;
910                         this.yukkuri = _data.myYukkuri;
911                         this.targetYukkuri = _data.targetNode;
912                         this.action = _data.action;
913                         mangMsg.output(this.yukkuri, EMsg.SAY_HELLO, this.targetYukkuri);
914                         this.yukkuri.addAddress(this.targetYukkuri);
915                         this.yukkuri.getAddress(this.targetYukkuri).isGreeting = true;
916
917                         var sec = BASE_FPS * 2;
918                         this.yukkuri.wait(sec, {
919                                 "onactiontick": this.onactiontick(this),
920                                 "onactionend": this.onactionend(this)
921                         });
922                 },
923                 onactiontick:function(self){
924                         return function(e){
925                                 if(typeof e === "undefined")return;
926                         };
927                 },
928                 onactionend:function(self){
929                         return function(e){
930                                 if(typeof e === "undefined")return;
931                                 self.action.setStatus(EAction.THINK);
932
933                                 self.yukkuri.action.setStatus(EAction.THINK);
934                                 self.yukkuri.animation();
935                                 self.targetYukkuri.action.setStatus(EAction.THINK);
936                                 self.targetYukkuri.animation();
937                         };
938                 }
939         });
940         var MoveToPlaceStartHomelessEvent = enchant.Class.create(Event,{
941                 initialize: function (_data){
942                         this.data = _data;
943                         this.yukkuri = this.data.myYukkuri;
944                         this.action = this.data.action;
945
946                         var yukkuri = this.yukkuri;
947                         var node = this.data.targetNode;
948
949                         this.action.status = EAction.MOVE_TO_PLACE_HOMELESS_UNDERWAY;
950                         yukkuri.vx = node.x + node.width /2  - yukkuri.getX();
951                         yukkuri.vy = node.y + node.height - yukkuri.getY() - yukkuri.getHeight() / 2;
952                         var distance = (Math.abs(yukkuri.vx) + Math.abs(yukkuri.vy)) / 2;
953                         var frame = distance + distance/5 + 1;
954                         yukkuri.direction = yukkuri.vx > 0 ? EDirection.RIGHT : EDirection.LEFT;
955                         yukkuri.reverse();
956
957                         yukkuri.moveBy(yukkuri.vx, yukkuri.vy, frame, {
958                                 "onactionend": this.onactionend(this)
959                         });
960                 },
961                 onactionend:function(self){
962                         return function(e){
963                                 if(typeof e === "undefined")return;
964                                 self.action.setStatus(EAction.DECLARE_PLACE_HOMELESS);
965                                 self.data = null;
966                                 self.yukkuri = null;
967                                 self.action = null;
968                         };
969                 }
970         });
971         var DeclarePlaceHomelessEvent = enchant.Class.create(Event,{
972                 initialize: function (_data){
973                         this.data = _data;
974                         this.yukkuri = _data.myYukkuri;
975                         this.place = _data.targetNode;
976                         this.action = _data.action;
977                         this.action.setStatus(EAction.WAIT);
978
979
980                         var sec = BASE_FPS * 3;
981                         this.yukkuri.declarePlace(this.place);
982                         this.place.setOwner(this.yukkuri);
983                         mangMsg.output(this.yukkuri, EMsg.DECLARE_PLACE);
984
985                         this.yukkuri.setFaceImage(EFace.NORMAL);
986                         this.yukkuri.wait(sec, {
987                                 "onactionend": this.onactionend(this)
988                         });
989                 },
990                 onactionend:function(self){
991                         return function(e){
992                                 if(typeof e === "undefined")return;
993                                 self.action.setStatus(EAction.THINK);
994                         };
995                 }
996         });
997
998         var Food = enchant.Class.create(ObjSprite,{
999                 initialize: function (type, x, y){
1000                         this.classname = "Food";
1001                         //一つにつきgiveの値分、hunguryを減少させられる
1002                         if(type == "apple"){
1003                                 ObjSprite.call(this,16,16);
1004                                 this.image = game.assets[EResPath.OBJECT];
1005                                 this.frame = 15;
1006                                 this.give = 40;
1007                                 this.amount = 3;
1008                         }else if(type == "grass"){
1009                                 ObjSprite.call(this,64,64);
1010                                 this.image = game.assets[EResPath.GRASS];
1011                                 this.frame = 0;
1012                                 this.give = 10;
1013                                 this.amount = 50;
1014                         }
1015                         this.x = x;
1016                         this.y = y;
1017                         foodGroup.addChild(this);
1018                         this.addEventListener('enterframe', function(){
1019                                 if(this.amount <= 0){
1020                                         this.removeAll();
1021                                 }
1022                         });
1023                 },
1024                 getAmount: function(){
1025                         return this.amount;
1026                 },
1027                 getGive: function(){
1028                         return this.give;
1029                 }
1030         });
1031         var Place = enchant.Class.create(ObjSprite,{
1032                 initialize: function (type, x, y){
1033                         ObjSprite.call(this,128,128);
1034                         this.image = game.assets[EResPath.PLACE_TREE];
1035                         this.x = x - this.width / 2;
1036                         this.y = y - this.height / 2;
1037                         backgroundMap.addChild(this);
1038                         this.owner = null;
1039                         this.foodsGraet = 0;
1040                         this.foodsNormal = 0;
1041                         this.foodsBad = 0;
1042                         // this.addEventListener('enterframe', function(){
1043                         //      if(this.age%10 === 0 && this.age !== 0){
1044                         //              console.log("x/y " + this.x + "/" + this.y);
1045                         //      }
1046                         //      if(game.input.left){
1047                         //              this.x -= 10;;
1048                         //      }
1049                         //      if(game.input.right){
1050                         //              this.x += 10;;
1051                         //      }
1052                         //      if(game.input.up){
1053                         //              this.y -= 10;;
1054                         //      }
1055                         //      if(game.input.down){
1056                         //              this.y += 10;;
1057                         //      }
1058                         // });
1059                 },
1060                 getStatusForDisplay: function(){
1061                         var ret = "";
1062                         var ownerId = "なし";
1063                         if(this.owner == null)ownerId = "なし";
1064                         else ownerId = this.owner.id;
1065                         ret += "プレイスID " + this.id + "\n";
1066                         ret += "オーナーID " + ownerId + "\n";
1067                         ret += "食料うめぇ  " + this.foodsGraet + "\n";
1068                         ret += "食料それなりー " + this.foodsNormal + "\n";
1069                         ret += "食料にがにが  " + this.foodsBad + "\n";
1070                         return ret;
1071                 },
1072                 setOwner: function(yukkuri){
1073                         this.owner = yukkuri;
1074                 },
1075                 isOwner: function(){
1076                         return this.owner !== null;
1077                 }
1078         });
1079         var Unun = enchant.Class.create(ObjSprite,{
1080                 initialize: function (x, y){
1081                         this.classname = "Unun";
1082                         ObjSprite.call(this,32,32);
1083                         this.image = game.assets[EResPath.YUKKURI_UNUN];
1084                         this.x = x;
1085                         this.y = y;
1086                         backgroundMap.addChild(this);
1087                         this.lifetime = 60 * 24 * 3;
1088                         this.addEventListener('enterframe', this.runEnterframe(this));
1089                         // game.rootScene.addEventListener('enterframe', this.runEnterframe(this));
1090
1091                 },
1092                 runEnterframe:function(self){
1093                         return function(e){
1094                                 if(typeof e === "undefined")return;
1095                                 if(this.age%BASE_FPS === 0 && this.age !== 0){
1096                                         self.lifetime--;
1097                                         if(self.lifetime <= 0){
1098                                                 this.parentNode.removeChild(this);
1099                                                 ctl.backgroundMap.removeChild(this);
1100                                                 ctl.removeObj(this);
1101                                                 self.image = null;
1102                                         }
1103                                 }
1104
1105                         };
1106                 }
1107                 // runEnterframe: function(){
1108                 //      console.log("enterframe:" + this.id);
1109                 // }
1110         });
1111         var Okazari = enchant.Class.create(enchant.Sprite,{
1112                 initialize: function(){
1113                         this.classname = "Okazari";
1114                         enchant.Sprite.call(this, 64, 64);
1115                         this.image = game.assets[EResPath.OKAZARI];
1116                         this.x = -CHARA_WIDTH / 2;
1117                         this.y = -12 - CHARA_HEIGHT / 2;
1118                 }
1119         });
1120         var Address = enchant.Class.create({
1121                 initialize: function(yukkuri){
1122                         this.friendlyLv = 1;
1123                         this.yukkuri = yukkuri;
1124                         this.isGreeting = false;
1125                         this.greetingWaitFrame = 0;
1126                         this.comMatrix = {
1127                                 "beginSayHello" : false,
1128                                 "sayHello" : false,
1129                         };
1130                         this.evtHash = {};
1131                 },
1132                 set:function(key, value){
1133                         this.comMatrix[key] = value;
1134                 },
1135                 get:function(key){
1136                         return this.comMatrix[key];
1137                 }
1138         });
1139
1140         var Yukkuri = enchant.Class.create(ObjSprite,{
1141                 initialize: function(x, y){
1142                         ObjSprite.call(this, 64, 64);
1143                         // ctl.game.rootScene.addEventListener('touchstart', this.touchstart);
1144                         this.classname = "Yukkuri";
1145                         this.addressBook = {};
1146                         this.imgGroup = new SpriteGroup();
1147                         this.imgBody = new SpriteGroup();
1148                         this.imgGroup.id = guid();
1149                         this.imgBody.id = guid();
1150                         this.x = -CHARA_WIDTH / 2;
1151                         this.y = -CHARA_HEIGHT / 2;
1152                         this.vx = 0;
1153                         this.vy = 0;
1154                         this.image = game.assets[EResPath.YUKKURI_BASE];
1155                         this.direction = EDirection.RIGHT;
1156                         this.addEventListener('enterframe', this.runEnterframe);
1157                         this.bodyStatus ={};
1158                         this.bodyStatus[EBody.NORMAL] = EResPath.YUKKURI_BASE;
1159                         this.bodyStatus[EBody.DEAD] = EResPath.YUKKURI_BODY_DEAD;
1160                         this.walkAnimationStatus = EWalkAnimation.END;
1161                         this.place = null;
1162                 },
1163                 runEnterframe:function(){
1164                         // this.imgGroup.x = 0;
1165                         // return ;
1166                         try{
1167                                 this.act();
1168                                 if(this.isDead())return;
1169                                 this.runYukkuri();
1170                                 this.runHungry();
1171                                 this.runUnun();
1172                                 this.runSleep();
1173                                 this.runAddress();
1174                         }catch(e){
1175                                 console.log(e);
1176                         }
1177                 },
1178                 reverse:function(){
1179                         if(this.direction == EDirection.RIGHT){
1180                                 this.imgBody.scaleX = 1;
1181                         }
1182                         else if(this.direction == EDirection.LEFT){
1183                                 this.imgBody.scaleX = -1;
1184                         }
1185                 },
1186                 tweet:function(text){
1187                         this._tweet.text(text, this.x - this._tweet.width/4, this.y - this._tweet.height);
1188                 },
1189                 moveTo:function(x, y, time){
1190                         this.imgGroup.tl.moveTo(x, y, time, enchant.Easing.SIN_EASEINOUT);
1191                 },
1192                 moveBy:function(x, y, time, eventFunctions){
1193                         var self = this;
1194                         var params = {
1195                                 x: function() {
1196                                         return self.imgGroup.x + x;
1197                                 },
1198                                 y: function() {
1199                                         return self.imgGroup.y + y;
1200                                 },
1201                                 time: time,
1202                                 easing: enchant.Easing.SIN_EASEINOUT
1203                         };
1204                         //-Event register [onactionstart,onactiontick,onactionend]
1205                         if(eventFunctions !== undefined){
1206                                 for(var key in eventFunctions){
1207                                         params[key] = eventFunctions[key];
1208                                 }
1209                         }
1210                         return this.imgGroup.tl.tween(params);
1211                 },
1212                 wait:function(frame, eventFunctions){
1213                         this.moveBy(0,1,frame,eventFunctions);
1214                 },
1215                 act: function(){
1216                         this.action.act();
1217                 },
1218                 animation: function(){
1219                         if(this.walkAnimationStatus == EWalkAnimation.END){
1220                                 this.imgBody.y = 0;
1221                                 this.imgBody.tl.moveByEx(0, -5, 10, enchant.Easing.SWING, {
1222                                         "onactionstart": function(e){
1223                                                 this.walkAnimationStatus = EWalkAnimation.START;
1224                                         }
1225                                 })
1226                                 .moveByEx(0, 5, 10, enchant.Easing.SWING,
1227                                 {
1228                                         "onactionend": function(e){
1229                                                 this.walkAnimationStatus = EWalkAnimation.END;
1230                                         }
1231                                 }).loop();
1232
1233                         }else{
1234                                 this.imgBody.tl.loop();
1235                         }
1236                 },
1237                 getWidth: function(){
1238                         return this.width;
1239                 },
1240                 getHeight: function(){
1241                         return this.height;
1242                 },
1243                 getX: function(){
1244                         return this.imgGroup.x;
1245                 },
1246                 getY: function(){
1247                         return this.imgGroup.y;
1248                 },
1249                 moveX: function(x){
1250                         this.imgGroup.x += x;
1251                         // this.x += x;
1252                         // this.shadow.x += x;
1253                         // this._tweet.x += x;
1254                 },
1255                 moveY: function(y){
1256                         this.imgGroup.y += y;
1257                         // this.y += y;
1258                         // this.shadow.y += y;
1259                         // this._tweet.y += y;
1260                 },
1261                 // changeFace: function(erespath){
1262                 //      this.face.image = game.assets[erespath];
1263                 // },
1264                 loadParamsXML: function(url){
1265                         var http = new JKL.ParseXML( url );
1266                         return http.parse();
1267                 },
1268                 runYukkuri: function(){
1269                         if(this.param.hungry > 70){
1270                                 if(this.age%(50 - this.param.hungry - 70) === 0 && this.age !== 0){
1271                                         this.param.yukkuri -= 1;
1272                                 }
1273                         }
1274                         else if(this.param.hungry < 10){
1275                                 if(this.age%50 === 0 && this.age !== 0) this.param.yukkuri += 1;
1276                         }
1277
1278                         if(this.param.yukkuri <= 0)this.param.yukkuri = 0;
1279                         else if(this.param.yukkuri >= this.param.maxYukkuri)this.param.yukkuri = this.param.maxYukkuri;
1280                 },
1281                 runHungry: function(){
1282                         if(this.age%50 === 0 && this.age !== 0)this.param.hungry++;
1283                         if(this.param.hungry >= 100)this.param.hungry = 100;
1284                 },
1285                 runUnun: function(){
1286                         if(this.age%50 === 0 && this.age !== 0)this.param.unun++;
1287                         if(this.param.unun >= 100)this.param.unun = 100;
1288                 },
1289                 runSleep: function(){
1290                         if(this.action.getStatus() != EAction.SLEEP_START && this.action.getStatus() != EAction.SLEEP_WAIT){
1291                                 if(this.age%80 === 0 && this.age !== 0)this.param.sleep++;
1292                                 if(this.param.sleep >= 100){
1293                                         this.param.sleep = 100;
1294                                 }
1295                         }
1296                 },
1297                 runAddress: function(){
1298                         for(var key in this.addressBook){
1299                                 var address = this.addressBook[key];
1300                                 //挨拶時間。24時間に1度だけ。
1301                                 if(address.isGreeting){
1302                                         address.greetingWaitFrame++;
1303                                         if(address.greetingWaitFrame >= DAY){
1304                                                 address.greetingWaitFrame = 0;
1305                                                 address.isGreeting = false;
1306                                         }
1307                                 }
1308                         }
1309                 },
1310                 changeFace: function(){
1311                         if(this.param.yukkuri >= 80){
1312                                 this.setFaceImage(EFace.HAPPY1);
1313                         }
1314                         else if(this.param.yukkuri >= 50){
1315                                 this.setFaceImage(EFace.NORMAL);
1316                         }
1317                         else if(this.param.yukkuri >= 30){
1318                                 this.setFaceImage(EFace.TIRED);
1319                         }
1320                         else if(this.param.yukkuri >= 10){
1321                                 this.setFaceImage(EFace.CRY1);
1322                         }
1323                         else if(this.param.yukkuri >= 1){
1324                                 this.setFaceImage(EFace.CRY2);
1325                         }
1326                 },
1327                 getRange: function(){
1328                         return this.param.range;
1329                 },
1330                 isSleep: function(){
1331                         return this.param.sleep >= 100;
1332                 },
1333                 isDead: function(){
1334                         return this.param.yukkuri <= 0;
1335                 },
1336                 isManpuku: function(){
1337                         return this.param.hungry <= 20;
1338                 },
1339                 isKuhuku: function(){
1340                         return this.param.hungry >= 40;
1341                 },
1342                 getYukkuri: function(){
1343                         return this.param.yukkuri;
1344                 },
1345                 setYukkuri: function(yukkuri){
1346                         this.param.yukkuri = yukkuri;
1347                         if(this.param.yukkuri < 0)this.param.yukkuri = 0;
1348                 },
1349                 getHungry: function(){
1350                         return this.param.hungry;
1351                 },
1352                 setHungry: function(hungry){
1353                         this.param.hungry = hungry;
1354                         if(this.param.hungry < 0)this.param.hungry = 0;
1355                 },
1356                 getUnun: function(){
1357                         return this.param.unun;
1358                 },
1359                 setUnun: function(unun){
1360                         this.param.unun = unun;
1361                         if(this.param.unun < 0)this.param.unun = 0;
1362                 },
1363
1364                 eat:function(food){
1365                         if(food.amount > 0){
1366                                 food.amount--;
1367                                 this.setHungry(this.param.hungry - food.getGive());
1368                                 this.setUnun(this.getUnun() + food.getGive() / 4);
1369                                 this.setYukkuri(this.getYukkuri() + 5);
1370                                 return true;
1371                         }else{
1372                                 this.setYukkuri(this.getYukkuri() - 5);
1373                                 return false;
1374                         }
1375                 },
1376                 declarePlace: function(place){
1377                         this.place = place;
1378                 },
1379                 isPlace: function(){
1380                         return this.place !== null;
1381                 },
1382
1383                 unun:function(){
1384                         if(this.direction == EDirection.RIGHT){
1385                                 new Unun(this.getX(), this.getY() + this.getHeight()/5 );
1386                         }else{
1387                                 new Unun(this.getX() -  this.getWidth()/5, this.getY() + this.getHeight()/5 );
1388                         }
1389                         this.setUnun(0);
1390                 },
1391                 setBodyImage: function(ebody){
1392                         var path = this.bodyStatus[ebody];
1393                         this.image = game.assets[path];
1394                 },
1395                 tlPause:function(){
1396                         // this.imgBody.tl.pause();
1397                         // this.imgGroup.tl.pause();
1398                         this.imgBody.tl.unloop();
1399                         this.imgGroup.tl.unloop();
1400                         this.imgBody.y = 0;
1401
1402                 },
1403                 tlResume: function(){
1404                         // this.imgBody.tl.resume();
1405                         // this.imgGroup.tl.resume();
1406                         this.animation();
1407                 },
1408                 syncDirection:function(senderYukkuri){
1409                         if(this.getX() > senderYukkuri.getX()){
1410                                 this.direction = EDirection.LEFT;
1411                                 senderYukkuri.direction = EDirection.RIGHT;
1412                         }else{
1413                                 this.direction = EDirection.RIGHT;
1414                                 senderYukkuri.direction = EDirection.LEFT;
1415                         }
1416                 },
1417                 isCommunication: function(yukkuri){
1418                         if(this.addressBook[yukkuri.id] === undefined) return true;
1419                         if(this.addressBook[yukkuri.id].isGreeting) return false;
1420                         return true;
1421                 },
1422                 addAddress: function(yukkuri){
1423                         this.addressBook[yukkuri.id] = new Address(yukkuri);
1424                 },
1425                 getAddress: function(yukkuri){
1426                         if(this.addressBook[yukkuri.id] === undefined)return null;
1427                         return this.addressBook[yukkuri.id];
1428                 },
1429                 getStatusForDisplay: function (){
1430                         var ret = "";
1431                         var placeId = (this.place==null)?"無し" : this.place.id;
1432                         ret += "ID " + this.id + "\n";
1433                         ret += "プレイス  " + placeId + "\n";
1434                         ret += "うんうん  " + this.getUnun() + "\n";
1435                         return ret;
1436                 },
1437                 destruct: function(){
1438
1439                 }
1440         });
1441         var Marisa = enchant.Class.create(Yukkuri,{
1442                 initialize: function(x, y){
1443                         Yukkuri.call(this,x,y);
1444                         this.classname = "Marisa";
1445
1446                         // var xml = this.loadParamsXML("./data/marisa/params.xml");
1447                         var json = net.load("./data/marisa/params.json");
1448                         this.param = json.root.param;
1449                         this.shadow = new enchant.Sprite(64, 64);
1450                         this.shadow.image = game.assets[EResPath.YUKKURI_SHADOW];
1451                         this.shadow.x = 0 + this.x;
1452                         this.shadow.y = CHARA_HEIGHT / 16 + this.x;
1453                         this.shadow.image._element.style.zIndex = 2;
1454
1455                         this._style.zIndex = 5;
1456
1457                         this.face = new enchant.Sprite(64, 64);
1458                         this.face.image = game.assets[EResPath.MARISA_FACE_NORMAL];
1459                         this.face.x = -CHARA_WIDTH / 2;
1460                         this.face.y = -CHARA_HEIGHT / 2;
1461                         this.face._style.zIndex = 0;
1462
1463                         this.faceStatus ={};
1464                         this.faceStatus[EFace.NORMAL] = EResPath.MARISA_FACE_NORMAL;
1465                         this.faceStatus[EFace.CRY1] = EResPath.MARISA_FACE_CRY1;
1466                         this.faceStatus[EFace.CRY2] = EResPath.MARISA_FACE_CRY2;
1467                         this.faceStatus[EFace.ANGRY] = EResPath.MARISA_FACE_ANGRY;
1468                         this.faceStatus[EFace.HAPPY1] = EResPath.MARISA_FACE_HAPPY1;
1469                         this.faceStatus[EFace.HAPPY2] = EResPath.MARISA_FACE_HAPPY2;
1470                         this.faceStatus[EFace.EAT1] = EResPath.MARISA_FACE_EAT1;
1471                         this.faceStatus[EFace.EAT2] = EResPath.MARISA_FACE_EAT2;
1472                         this.faceStatus[EFace.TIRED] = EResPath.MARISA_FACE_TIRED;
1473                         this.faceStatus[EFace.SLEEP] = EResPath.MARISA_FACE_SLEEP;
1474
1475
1476                         this.hear = new enchant.Sprite(64, 64);
1477                         this.hear.image = game.assets[EResPath.MARISA_HEAR];
1478                         this.hear.x = -CHARA_WIDTH / 2;
1479                         this.hear.y = -CHARA_HEIGHT / 2;
1480                         this.hear._style.zIndex = 0;
1481
1482                         this.okazari = new Okazari();
1483                         this.okazari.image._element.style.zIndex = 1;
1484
1485                         // this.imgGroup._style.zIndex = 1;
1486
1487                         this.imgGroup.addChild(this.shadow);
1488                         this.imgBody.addChild(this);
1489                         this.imgBody.addChild(this.face);
1490                         this.imgBody.addChild(this.hear);
1491                         this.imgBody.addChild(this.okazari);
1492                         if(this instanceof Player){
1493                                 this.budge = new enchant.Sprite(16, 16);
1494                                 this.budge.image = game.assets[EResPath.PLAYER_BADGE];
1495                                 this.imgBody.addChild(this.budge);
1496                                 this.budge.x -= this.width/2;
1497                                 this.budge.y -= this.height/2;
1498                         }
1499                         this.imgGroup.addChild(this.imgBody);
1500                         backgroundMap.addChild(this.imgGroup);
1501                         this._tweet = new TTweet(148, 64);
1502
1503                         this.imgGroup.addChild(this._tweet);
1504                         this.imgGroup.x = x;
1505                         this.imgGroup.y = y;
1506                         this.animation();
1507                         this.action = new Action(this);
1508                         ctl.addObj(this);
1509                 },
1510                 setFaceImage: function(eface){
1511                         var path = this.faceStatus[eface];
1512                         this.face.image = game.assets[path];
1513                 }
1514         });
1515         var Player = enchant.Class.create(Marisa,{
1516                 initialize: function(x, y){
1517                         // this.runEnterframe = function(){
1518                         // },
1519                         Marisa.call(this, x, y);
1520                         this.classname = "Player";
1521                         ctl.setPlayer(this);
1522                         // this.removeEventListener('enterframe',this.runEnterframe);
1523                         this.addEventListener('enterframe', function(){
1524
1525
1526                                 if(this.age%10 === 0 && this.age !== 0){
1527                                         // console.log("palyer:" + this.age);
1528                                 }
1529                                 this.act();
1530                                 ///////////////////////////////////////////////
1531                                 //Action pattern  debug
1532                                 if(game.input.left){
1533                                         // this.changeFace(EResPath.MARISA_FACE_TIRED);
1534                                         this.moveX(-10);
1535                                         this.direction = EDirection.LEFT;
1536                                 }
1537                                 if(game.input.right){
1538                                         this.moveX(10);
1539                                         this.direction = EDirection.RIGHT;
1540                                 }
1541                                 if(game.input.up){
1542                                         this.moveY(-10);
1543                                 }
1544                                 if(game.input.down){
1545                                         this.moveY(10);
1546                                 }
1547                                 ///////////////////////////////////////////////
1548                                 // this.reverse();
1549                                 // if(ctl.backgroundMap.hitTest(this.imgGroup.x, this.imgGroup.y + this.height / 2)){
1550                                 //      console.log("hit:" + this.imgGroup.x + ":" +  (this.imgGroup.y + this.height/2) );
1551                                 // }else{
1552                                 //      console.log("not:" + this.imgGroup.x + ":" +  (this.imgGroup.y + this.image.height/2));
1553                                 // }
1554                                 //- Display the circle of search range.
1555                                 // fieldBg.image.clear();
1556                                 // fieldBg.image.context.beginPath();
1557                                 // fieldBg.image.context.fillStyle = '#ff0000';
1558                                 // fieldBg.image.context.arc(this.imgGroup.x + this.width/2, this.imgGroup.y + this.height/2, this.range, Math.PI * 2, false);
1559                                 // fieldBg.image.context.fill();
1560                         });
1561                 }
1562         });
1563         game.onload = function(){
1564                 def.Food = Food;
1565                 def.Place = Place;
1566                 def.ObjSprite = ObjSprite;
1567                 def.Yukkuri = Yukkuri;
1568                 def.nodeSort = nodeSort;
1569                 mangTouch = new MangTouch(ctl);
1570                 mangMsg = new MangMsg(ctl);
1571                 ctl.setDefined(def);
1572                 backgroundMap.image = game.assets[EResPath.MAP0];
1573                 backgroundMap.loadData(_mapData);
1574                 backgroundMap.collisionData = _collisionData;
1575
1576                 var menuSurface = new enchant.Surface(EMenuPos.WIDTH, EMenuPos.HEIGHT);
1577                 var fieldSurface = new enchant.Surface(EFieldPos.WIDTH, EFieldPos.HEIGHT);
1578                 var windowSurface = new enchant.Surface(EFieldPos.WIDTH, EFieldPos.HEIGHT);
1579                 fieldBg.image = fieldSurface;
1580                 fieldBg.x = 0;
1581                 fieldBg.y = 0;
1582
1583                 windowFront.image = windowSurface;
1584                 windowFront.x = 0;
1585                 windowFront.y = 0;
1586                 windowFront.opacity = 0.5;
1587
1588                 windowSurface.context.fillStyle = '#000';
1589                 windowSurface.context.fillRect(0 + 20, EFieldPos.HEIGHT - 60, EFieldPos.WIDTH - 40, EFieldPos.HEIGHT);
1590
1591
1592                 menuSurface.context.fillStyle = '#000';
1593                 menuSurface.context.fillRect(0, 0, EMenuPos.WIDTH, EMenuPos.HEIGHT);
1594                 menuBg.image = menuSurface;
1595                 menuBg.x = EMenuPos.X;
1596                 menuBg.y = EMenuPos.Y;
1597                 backgroundMap.addChild(fieldBg);
1598                 mangIcon = new MangIcon(ctl, menuBg);
1599
1600                 game.rootScene.addChild(backgroundMap);
1601                 game.rootScene.addChild(windowFront);
1602                 game.rootScene.addChild(menuBg);
1603
1604                 // var labelGroup = new LabelGroup();
1605                 mangLabel = new MangLabel(ctl, menuBg);
1606                 // var info = new Label("ゆっくり");
1607                 // info.color = "#ffffff";
1608                 // info.font = "14px 'Times New Roman'";
1609                 // info.x = 4;
1610                 // info.y = GAME_HEIGHT / 2;
1611                 // labelGroup.addChild(info);
1612                 // menuBg.addChild(labelGroup);
1613
1614
1615                 // var food = new def.Food("apple", 200, 250);
1616                 backgroundMap.addChild(foodGroup);
1617                 // backgroundMap.addChild(placeGroup);
1618                 var player = new Player(PALYER_INIT_X, PALYER_INIT_Y);
1619                 new Marisa(PALYER_INIT_X + 200, PALYER_INIT_Y + 200);
1620                 new Marisa(PALYER_INIT_X + 800, PALYER_INIT_Y + 800);
1621                 new Unun(100, 100);
1622                 // new Place("tree", PALYER_INIT_X, PALYER_INIT_Y - 50);
1623                 var touchX = 0;
1624
1625                 game.rootScene.addEventListener('touchstart', function (e) {
1626                         game.touched = true;
1627                         var mang = mangTouch.get(ctl.getCurrentCommand());
1628                         mang.touchstart(e);
1629                 });
1630                 game.rootScene.addEventListener('touchmove', function (e) {
1631                         var mang = mangTouch.get(ctl.getCurrentCommand());
1632                         mang.touchmove(e);
1633                 });
1634                 game.rootScene.addEventListener('touchend', function (e) {
1635                         var mang = mangTouch.get(ctl.getCurrentCommand());
1636                         mang.touchend(e);
1637                         game.touched = false;
1638                 });
1639
1640
1641                 game.rootScene.addEventListener('enterframe', function(){
1642                 //main frame
1643                         mangLabel.draw(player);
1644                 });
1645                 this.addEventListener('enterframe', function(){
1646                         //The priority processing of display instead of z-index
1647                         backgroundMap.childNodes.sort(nodeSort);
1648                 });
1649
1650         };
1651         game.start();
1652 };