OSDN Git Service

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