OSDN Git Service

add is sleeping to action
[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 LabelGroup = enchant.Class.mixClasses(Label, Group,true);
9 var net = new Net();
10 //////////////////////////////////////
11 //define
12 //////////////////////////////////////
13 var PALYER_INIT_X = 100;
14 var PALYER_INIT_Y = 100;
15 var CHARA_WIDTH = 64;
16 var CHARA_HEIGHT = 64;
17 var GAME_WIDTH = 800;
18 var GAME_HEIGHT = 600;
19 var EResPath = {
20         YUKKURI_BASE : './data/yukkuri_base.png',
21         YUKKURI_SHADOW : './data/shadow.png',
22         MARISA_FACE_NORMAL : './data/marisa/face_normal.png',
23         MARISA_FACE_TIRED : './data/marisa/face_tired.png',
24         MARISA_HEAR : './data/marisa/hear.png',
25         OBJECT : './data/object.png',
26         MAP0 : './data/map0.png',
27         COMMAND : './data/command.png',
28         OKAZARI : './data/okazari.png',
29 };
30 var ECommand = {
31         WORLD_CURSOR: 0,
32         WORLD_TARGET: 1,
33 };
34
35 var EMenuPos = {
36         X : 600,
37         Y : 0,
38         WIDTH : 200,
39         HEIGHT : GAME_HEIGHT,
40 };
41 var EFieldPos = {
42         X : 0,
43         Y : 0,
44         WIDTH: GAME_WIDTH - EMenuPos.WIDTH,
45         HEIGHT: GAME_HEIGHT,
46 };
47 var EAction = {
48         WAIT : 0,
49         SLEEP : 1,
50         EAT : 2,
51         HUNT : 3,
52         THINK : 4,
53         WALK : 5,
54         DEAD: 6,
55         MOVE: 7
56 };
57 var EDirection = {
58         LEFT : 0,
59         RIGHT: 1,
60 };
61 window.onload = function(){
62
63
64         //init game
65         var SpriteGroup = enchant.Class.mixClasses(Sprite, Group,true);
66         var menuBg = new SpriteGroup(EMenuPos.WIDTH, EMenuPos.HEIGHT);
67         var fieldBg = new enchant.Sprite(EFieldPos.WIDTH, EFieldPos.HEIGHT);
68         var game = new Game(GAME_WIDTH, GAME_HEIGHT);
69         var _loadArr = [];
70         var i=0;
71         for(var _v in EResPath){
72                 _loadArr[i] = EResPath[_v];
73                 i++;
74         }
75         game.preload(_loadArr);
76         var MapGroup = enchant.Class.mixClasses(Map, Group,true);
77         var backgroundMap = new MapGroup(16, 16);
78         include("./class.js");
79         include("./ctrl.js");
80         ctl = new Ctrl();
81         ctl.init(game);
82         ctl.setBackgroundMap(backgroundMap);
83         var Action = enchant.Class.create({
84                 initialize: function (yukkuri){
85                         this.status = EAction.THINK;
86                         // this.status = EAction.EAT;
87                         this.yukkuri = yukkuri;
88                         this.isMoving = false;
89                 },
90                 observe: function(){
91                         if(this.yukkuri.isDead()){
92                                 this.status = EAction.DEAD;
93                         }
94                         if(this.yukkuri.isSleep()){
95                                 this.status = EAction.SLEEP;
96                         }
97
98                 },
99                 act : function(){
100                         this.observe();
101                         switch(this.status){
102                                 case EAction.THINK:
103                                         // this.status = EAction.WALK;
104                                         // yukkuri.moveTo(120, 120, 30);
105                                         if(this.yukkuri.age%10 === 0){
106                                                 this.search();
107                                         }
108                                 break;
109                                 case EAction.MOVE:
110                                         this.move();
111                                 break;
112                                 //Random Walk
113                                 case EAction.WALK:
114                                         this.walk(0);
115                                 break;
116                                 case EAction.SLEEP:
117                                         this.sleep();
118                                 break;
119                                 case EAction.DEAD:
120                                         this.dead();
121                                 break;
122                         }
123                 },
124                 move : function(){
125
126                 },
127                 walk : function(retryCounter){
128                         if(retryCounter > 15){
129                                 retryCounter = 0;
130                                 this.status = EAction.EAT;
131                                 console.log("retryCount over");
132                                 return;
133                         }
134                         if(this.yukkuri.imgGroup.tl.queue.length === 0){
135                                 var frame = 200;
136                                 if(this.isMoving){
137                                         this.isMoving = false;
138                                         this.status = EAction.THINK;
139                                         this.yukkuri.vx = 0;
140                                         this.yukkuri.vy = 0;
141                                 }else{
142                                         // this.isMoving = true;
143                                         var xpos = 150 - (retryCounter * 10);
144                                         var ypos = 150 - (retryCounter * 10);
145                                         if(rand(8) === 0){
146                                                 this.yukkuri.vx = 0;
147                                                 this.yukkuri.vy = -ypos;
148                                         }
149                                         else if(rand(8) == 1){
150                                                 this.yukkuri.vx = xpos;
151                                                 this.yukkuri.vy = -ypos;
152                                                 this.yukkuri.direction = EDirection.RIGHT;
153                                         }
154                                         else if(rand(8) == 2){
155                                                 this.yukkuri.vx = xpos;
156                                                 this.yukkuri.vy = 0;
157                                                 this.yukkuri.direction = EDirection.RIGHT;
158                                         }
159                                         else if(rand(8) == 3){
160                                                 this.yukkuri.vx = xpos;
161                                                 this.yukkuri.vy = ypos;
162                                                 this.yukkuri.direction = EDirection.RIGHT;
163                                         }
164                                         else if(rand(8) == 4){
165                                                 this.yukkuri.vx = 0;
166                                                 this.yukkuri.vy = ypos;
167                                         }
168                                         else if(rand(8) == 5){
169                                                 this.yukkuri.vx = -xpos;
170                                                 this.yukkuri.vy = ypos;
171                                                 this.yukkuri.direction = EDirection.LEFT;
172                                         }
173                                         else if(rand(8) == 6){
174                                                 this.yukkuri.vx = -xpos;
175                                                 this.yukkuri.vy = 0;
176                                                 this.yukkuri.direction = EDirection.LEFT;
177                                         }
178                                         else if(rand(8) == 7){
179                                                 this.yukkuri.vx = -xpos;
180                                                 this.yukkuri.vy = -ypos;
181                                                 this.yukkuri.direction = EDirection.LEFT;
182                                         }
183                                         if (this.yukkuri.vx || this.yukkuri.vy) {
184                                                 var map = ctl.backgroundMap;
185                                                 // var x = this.yukkuri.x + (this.yukkuri.moveX ? this.yukkuri.moveX / Math.abs(this.yukkuri.moveX) * 16 : 0) + 16;
186                                                 // var y = this.yukkuri.y + (this.yukkuri.moveY ? this.yukkuri.moveY / Math.abs(this.yukkuri.moveY) * 16 : 0) + 16;
187                                                 var x = this.yukkuri.imgGroup.x + this.yukkuri.vx;
188                                                 var y = this.yukkuri.imgGroup.y + this.yukkuri.vy + this.yukkuri.height / 2;
189                                                 if (0 <= x && x < map.width && 0 <= y && y < map.height && !map.hitTest(x, y)) {
190                                                         // console.log("あたってないよ:"+ this.yukkuri.imgGroup.x + ":" + this.yukkuri.imgGroup.y);
191                                                         // console.log("X:" + this.yukkuri.moveX);
192                                                         // console.log("Y:" + this.yukkuri.moveY);
193                                                         this.isMoving = true;
194                                                         this.yukkuri.reverse();
195                                                         this.yukkuri.moveBy(this.yukkuri.vx, this.yukkuri.vy, frame);
196                                                 }else{
197                                                         // console.log("HIT:"+ this.yukkuri.imgGroup.x + ":" + this.yukkuri.imgGroup.y);
198                                                         this.walk(retryCounter+1);
199                                                 }
200                                         }
201                                 }
202                         }
203                 },
204                 sleep: function(){
205                         var yukkuri = this.yukkuri;
206
207                         if(yukkuri.isSleeping){
208                                 if(yukkuri.age%10 === 0 && yukkuri.age !== 0)yukkuri.param.sleep--;
209                                 if(yukkuri.param.sleep <= 0){
210                                         yukkuri.param.sleep = 0;
211                                         yukkuri.isSleeping = false;
212                                         yukkuri.tweet("ゆっくりおきるよ!");
213                                         this.status = EAction.THINK;
214                                 }
215                         }
216                 },
217                 dead : function(){
218                         this.yukkuri.tweet("もっとゆっくりしたかった…");
219                         this.yukkuri.imgBody.tl.clear();
220                         this.yukkuri.imgBody.tl.pause();
221                         this.yukkuri.imgGroup.tl.clear();
222                         this.yukkuri.imgGroup.tl.pause();
223                         // this.yukkuri.imgBody.tl.unloop();
224                         // this.yukkuri.imgBody.tl.removeFromScene();
225                         // this.yukkuri.imgGroup.tl.removeFromScene();
226                 },
227                 search : function(){
228                         if(true)return;
229                         for (var i = 0, l = game.rootScene.childNodes.length; i < l; i++) {
230                                 var node = game.rootScene.childNodes[i];
231                                 if (node instanceof Food) {
232
233                                         if(this.yukkuri.within(node, this.yukkuri.range)){
234                                                 //A yukkuri to go to the food area
235                                                 this.status = EAction.MOVE;
236                                                 new MoveEvent({
237                                                         "type": 'food',
238                                                         "targetNode": node,
239                                                         "myYukkuri": this.yukkuri
240                                                 });
241                                         }else{
242                                                 // console.log("no:");
243                                         }
244                                 }
245                         }
246                         this.status =  EAction.WALK;
247                 }
248         });
249         var MoveEvent = enchant.Class.create({
250                 initialize: function (dataObj){
251                         this.move
252                 }
253
254         });
255         var Food = enchant.Class.create(enchant.Sprite,{
256                 initialize: function (type, x, y){
257                         enchant.Sprite.call(this,16,16);
258                         this.image = game.assets[EResPath.OBJECT];
259                         this.frame = 15;
260                         this.x = x;
261                         this.y = y;
262                         // this.moveTo(180, 252);
263                         backgroundMap.addChild(this);
264                 }
265         });
266         var Okazari = enchant.Class.create(enchant.Sprite,{
267                 initialize: function(){
268                         enchant.Sprite.call(this, 64, 64);
269                         this.image = game.assets[EResPath.OKAZARI];
270                         this.x = -CHARA_WIDTH / 2;
271                         this.y = -12 - CHARA_HEIGHT / 2;
272                 }
273         });
274         var Yukkuri = enchant.Class.create(enchant.Sprite,{
275                 initialize: function(x, y){
276                         this.imgGroup = new Group();
277                         this.imgBody = new Group();
278                         enchant.Sprite.call(this, 64, 64);
279                         this.x = -CHARA_WIDTH / 2;
280                         this.y = -CHARA_HEIGHT / 2;
281                         this.vx = 0;
282                         this.vy = 0;
283                         this.image = game.assets[EResPath.YUKKURI_BASE];
284                         this.direction = EDirection.RIGHT;
285                         this.addEventListener('enterframe', this.runEnterframe);
286                         this.isSleeping = false;
287                 },
288                 runEnterframe:function(){
289                         this.runYukkuri();
290                         this.runHungry();
291                 },
292                 reverse:function(){
293                         if(this.direction == EDirection.RIGHT){
294                                 this.imgBody.scaleX = 1;
295                         }
296                         else if(this.direction == EDirection.LEFT){
297                                 this.imgBody.scaleX = -1;
298                         }
299                 },
300                 tweet:function(text){
301                         this._tweet.text(text, this.x - this._tweet.width/4, this.y - this._tweet.height);
302                 },
303                 moveTo:function(x, y, time){
304                         this.imgGroup.tl.moveTo(x, y, time, enchant.Easing.SIN_EASEINOUT);
305                 },
306                 moveBy:function(x, y, time){
307                         this.imgGroup.tl.moveBy(x, y, time, enchant.Easing.SIN_EASEINOUT);
308
309                 },
310                 act: function(){
311                         this.action.act();
312                 },
313                 animation: function(){
314                         this.imgBody.tl.moveBy(0, -5, 10, enchant.Easing.SWING).moveBy(0, 5, 10, enchant.Easing.SWING).loop();
315                         // this.tl.moveBy(0, -5, 10, enchant.Easing.SWING).moveBy(0, 5, 10, enchant.Easing.SWING).loop();
316                         // this.okazari.tl.moveBy(0, -5, 10, enchant.Easing.SWING).moveBy(0, 5, 10, enchant.Easing.SWING).loop();
317                         // this.face.tl.moveBy(0, -5, 10, enchant.Easing.SWING).moveBy(0, 5, 10, enchant.Easing.SWING).loop();
318                         // this.hear.tl.moveBy(0, -5, 10, enchant.Easing.SWING).moveBy(0, 5, 10, enchant.Easing.SWING).loop();
319                 },
320                 moveX: function(x){
321                         this.imgGroup.x += x;
322                         // this.x += x;
323                         // this.shadow.x += x;
324                         // this._tweet.x += x;
325                 },
326                 moveY: function(y){
327                         this.imgGroup.y += y;
328                         // this.y += y;
329                         // this.shadow.y += y;
330                         // this._tweet.y += y;
331                 },
332                 changeFace: function(erespath){
333                         this.face.image = game.assets[erespath];
334                 },
335                 loadParamsXML: function(url){
336                         var http = new JKL.ParseXML( url );
337                         return http.parse();
338                 },
339                 runYukkuri: function(){
340                         if(this.param.hungry > 70){
341                                 if(this.age%(50 - this.param.hungry - 70) === 0 && this.age !== 0){
342                                         this.param.yukkuri -= 1;
343                                 }
344                         }
345                         else if(this.param.hungry < 10){
346                                 if(this.age%50 === 0 && this.age !== 0) this.param.yukkuri += 1;
347                         }
348
349                         if(this.param.yukkuri <= 0)this.param.yukkuri = 0;
350                         else if(this.param.yukkuri >= this.param.maxYukkuri)this.param.yukkuri = this.param.maxYukkuri;
351                 },
352                 runHungry: function(){
353                         if(this.age%100 === 0 && this.age !== 0)this.param.hungry++;
354                         if(this.param.hungry >= 100)this.param.hungry = 100;
355                 },
356                 runSleep: function(){
357                         if(!this.isSleeping){
358                                 if(this.age%100 === 0 && this.age !== 0)this.param.sleep++;
359                                 if(this.param.sleep >= 100){
360                                         this.param.sleep = 100;
361                                         this.isSleeping = true;
362                                 }
363
364                         }
365                 },
366                 isSleep: function(){
367                         return this.isSleeping;
368                 },
369                 isDead: function(){
370                         return this.param.yukkuri <= 0;
371                 },
372                 destruct: function(){
373
374                 }
375         });
376         var Marisa = enchant.Class.create(Yukkuri,{
377                 initialize: function(x, y){
378                         Yukkuri.call(this,x,y);
379
380
381                         // var xml = this.loadParamsXML("./data/marisa/params.xml");
382                         var json = net.load("./data/marisa/params.json");
383                         this.param = json.root.param;
384                         this.range = 200;
385                         this.shadow = new enchant.Sprite(64, 64);
386                         this.shadow.image = game.assets[EResPath.YUKKURI_SHADOW];
387                         this.shadow.x = 0 + this.x;
388                         this.shadow.y = CHARA_HEIGHT / 16 + this.x;
389
390                         this.face = new enchant.Sprite(64, 64);
391                         this.face.image = game.assets[EResPath.MARISA_FACE_NORMAL];
392                         this.face.x = -CHARA_WIDTH / 2;
393                         this.face.y = -CHARA_HEIGHT / 2;
394
395                         this.hear = new enchant.Sprite(64, 64);
396                         this.hear.image = game.assets[EResPath.MARISA_HEAR];
397                         this.hear.x = -CHARA_WIDTH / 2;
398                         this.hear.y = -CHARA_HEIGHT / 2;
399
400                         this.okazari = new Okazari();
401                         this.imgGroup.addChild(this.shadow);
402                         this.imgBody.addChild(this);
403                         this.imgBody.addChild(this.face);
404                         this.imgBody.addChild(this.hear);
405                         this.imgBody.addChild(this.okazari);
406                         this.imgGroup.addChild(this.imgBody);
407                         backgroundMap.addChild(this.imgGroup);
408                         this._tweet = new TTweet(148, 64);
409
410                         this.imgGroup.addChild(this._tweet);
411                         this.imgGroup.x = x;
412                         this.imgGroup.y = y;
413                         this.animation();
414                         this.action = new Action(this);
415
416                 }
417         });
418         var Player = enchant.Class.create(Marisa,{
419                 initialize: function(x, y){
420                         // this.runEnterframe = function(){
421                         // },
422                         Marisa.call(this, x, y);
423                         ctl.setPlayer(this);
424                         // this.removeEventListener('enterframe',this.runEnterframe);
425                         this.addEventListener('enterframe', function(){
426                                 if(this.age%10 === 0 && this.age !== 0){
427                                         // console.log("palyer:" + this.age);
428                                 }
429                                 this.act();
430
431                                 //Action pattern for debug
432                                 if(game.input.left){
433                                         this.changeFace(EResPath.MARISA_FACE_TIRED);
434                                         this.moveX(-10);
435                                         this.direction = EDirection.LEFT;
436                                 }
437                                 if(game.input.right){
438                                         this.moveX(10);
439                                         this.direction = EDirection.RIGHT;
440                                 }
441                                 if(game.input.up){
442                                         this.moveY(-10);
443                                         // this.tweet("ゆっくりしていってね!");
444                                 }
445                                 if(game.input.down){
446                                         this.moveY(10);
447                                 }
448                                 this.reverse();
449                                 // if(ctl.backgroundMap.hitTest(this.imgGroup.x, this.imgGroup.y + this.height / 2)){
450                                 //      console.log("hit:" + this.imgGroup.x + ":" +  (this.imgGroup.y + this.height/2) );
451                                 // }else{
452                                 //      console.log("not:" + this.imgGroup.x + ":" +  (this.imgGroup.y + this.image.height/2));
453                                 // }
454                                 //- Display the circle of search range.
455                                 // fieldBg.image.clear();
456                                 // fieldBg.image.context.beginPath();
457                                 // fieldBg.image.context.fillStyle = '#ff0000';
458                                 // fieldBg.image.context.arc(this.imgGroup.x + this.width/2, this.imgGroup.y + this.height/2, this.range, Math.PI * 2, false);
459                                 // fieldBg.image.context.fill();
460                         });
461                 }
462         });
463         game.onload = function(){
464                 mangTouch = new MangTouch(ctl);
465                 backgroundMap.image = game.assets[EResPath.MAP0];
466                 backgroundMap.loadData(_mapData);
467                 backgroundMap.collisionData = _collisionData;
468
469                 var menuSurface = new enchant.Surface(EMenuPos.WIDTH, EMenuPos.HEIGHT);
470                 var fieldSurface = new enchant.Surface(EFieldPos.WIDTH, EFieldPos.HEIGHT);
471                 fieldBg.image = fieldSurface;
472                 fieldBg.x = 0;
473                 fieldBg.y = 0;
474
475                 menuSurface.context.fillStyle = '#000';
476                 menuSurface.context.fillRect(0, 0, EMenuPos.WIDTH, EMenuPos.HEIGHT);
477                 menuBg.image = menuSurface;
478                 menuBg.x = EMenuPos.X;
479                 menuBg.y = EMenuPos.Y;
480
481                 backgroundMap.addChild(fieldBg);
482                 mangIcon = new MangIcon(ctl, menuBg);
483
484                 game.rootScene.addChild(backgroundMap);
485                 game.rootScene.addChild(menuBg);
486
487                 // var labelGroup = new LabelGroup();
488                 mangLabel = new MangLabel(ctl, menuBg);
489                 // var info = new Label("ゆっくり");
490                 // info.color = "#ffffff";
491                 // info.font = "14px 'Times New Roman'";
492                 // info.x = 4;
493                 // info.y = GAME_HEIGHT / 2;
494                 // labelGroup.addChild(info);
495                 // menuBg.addChild(labelGroup);
496
497
498                 var food = new Food("apple", 200, 250);
499                 var player = new Player(PALYER_INIT_X, PALYER_INIT_Y);
500                 var touchX = 0;
501
502                 game.rootScene.addEventListener('touchstart', function (e) {
503                         game.touched = true;
504                         var mang = mangTouch.get(ctl.getCurrentCommand());
505                         mang.touchstart(e);
506                 });
507                 game.rootScene.addEventListener('touchmove', function (e) {
508                         var mang = mangTouch.get(ctl.getCurrentCommand());
509                         mang.touchmove(e);
510                 });
511                 game.rootScene.addEventListener('touchend', function (e) {
512                         var mang = mangTouch.get(ctl.getCurrentCommand());
513                         mang.touchend(e);
514                         game.touched = false;
515                 });
516
517
518                 game.rootScene.addEventListener('enterframe', function(){
519                 //main frame
520                         mangLabel.draw(player);
521                 });
522         };
523         game.start();
524 };