2 //////////////////////////////////////
8 var LabelGroup = enchant.Class.mixClasses(Label, Group,true);
10 //////////////////////////////////////
12 //////////////////////////////////////
13 var PALYER_INIT_X = 100;
14 var PALYER_INIT_Y = 100;
16 var CHARA_HEIGHT = 64;
18 var GAME_HEIGHT = 600;
23 YUKKURI_BASE : './data/yukkuri_base.png',
24 YUKKURI_SHADOW : './data/shadow.png',
25 MARISA_FACE_NORMAL : './data/marisa/face_normal1.png',
26 MARISA_FACE_TIRED : './data/marisa/face_tired1.png',
27 MARISA_FACE_ANGRY : './data/marisa/face_angry1.png',
28 MARISA_FACE_CRY1 : './data/marisa/face_cry1.png',
29 MARISA_FACE_CRY2 : './data/marisa/face_cry2.png',
30 MARISA_FACE_EAT1 : './data/marisa/face_eat1.png',
31 MARISA_FACE_EAT2 : './data/marisa/face_eat2.png',
32 MARISA_FACE_HAPPY1 : './data/marisa/face_happy1.png',
33 MARISA_FACE_HAPPY2 : './data/marisa/face_happy2.png',
34 MARISA_FACE_SLEEP : './data/marisa/face_sleep1.png',
35 MARISA_HEAR : './data/marisa/hear1.png',
36 OBJECT : './data/object.png',
37 MAP0 : './data/map0.png',
38 COMMAND : './data/command.png',
39 OKAZARI : './data/okazari.png',
57 WIDTH: GAME_WIDTH - EMenuPos.WIDTH,
69 MOVE_TO_EAT_START: 10,
70 MOVE_TO_EAT_UNDERWAY: 11,
79 window.onload = function(){
81 var def = new Object();
82 var foodGroup = new Group();
83 var SpriteGroup = enchant.Class.mixClasses(Sprite, Group,true);
84 var menuBg = new SpriteGroup(EMenuPos.WIDTH, EMenuPos.HEIGHT);
85 var fieldBg = new enchant.Sprite(EFieldPos.WIDTH, EFieldPos.HEIGHT);
86 var game = new Game(GAME_WIDTH, GAME_HEIGHT);
90 for(var _v in EResPath){
91 _loadArr[i] = EResPath[_v];
94 game.preload(_loadArr);
95 var MapGroup = enchant.Class.mixClasses(Map, Group,true);
96 var backgroundMap = new MapGroup(16, 16);
97 include("./class.js");
101 ctl.setBackgroundMap(backgroundMap);
102 var Action = enchant.Class.create({
103 initialize: function (yukkuri){
104 this.status = EAction.THINK;
105 this.yukkuri = yukkuri;
106 this.isMoving = false;
107 this.targetNode = null;
109 setStatus: function(eaction){
110 this.status = eaction;
113 if(this.yukkuri.isDead()){
114 this.status = EAction.DEAD;
116 if(this.yukkuri.isSleep()){
117 this.status = EAction.SLEEP;
125 if(this.yukkuri.age%6 === 0){
129 case EAction.MOVE_TO_EAT_START:
130 this.move_to_eat_start();
132 case EAction.MOVE_TO_EAT_UNDERWAY:
144 case EAction.EAT_START:
147 case EAction.EAT_WAIT:
149 case EAction.MOVE_TO_COMMAND:
150 this.move_to_command();
155 move_to_eat_start : function(){
158 "targetNode": this.targetNode,
160 "myYukkuri": this.yukkuri
163 walk : function(retryCounter){
164 if(retryCounter > 15){
166 this.status = EAction.NONE;
167 console.log("retryCount over");
170 if(this.yukkuri.imgGroup.tl.queue.length === 0){
173 this.isMoving = false;
174 this.status = EAction.THINK;
178 // this.isMoving = true;
179 var xpos = 150 - (retryCounter * 10);
180 var ypos = 150 - (retryCounter * 10);
183 this.yukkuri.vy = -ypos;
185 else if(rand(8) == 1){
186 this.yukkuri.vx = xpos;
187 this.yukkuri.vy = -ypos;
188 this.yukkuri.direction = EDirection.RIGHT;
190 else if(rand(8) == 2){
191 this.yukkuri.vx = xpos;
193 this.yukkuri.direction = EDirection.RIGHT;
195 else if(rand(8) == 3){
196 this.yukkuri.vx = xpos;
197 this.yukkuri.vy = ypos;
198 this.yukkuri.direction = EDirection.RIGHT;
200 else if(rand(8) == 4){
202 this.yukkuri.vy = ypos;
204 else if(rand(8) == 5){
205 this.yukkuri.vx = -xpos;
206 this.yukkuri.vy = ypos;
207 this.yukkuri.direction = EDirection.LEFT;
209 else if(rand(8) == 6){
210 this.yukkuri.vx = -xpos;
212 this.yukkuri.direction = EDirection.LEFT;
214 else if(rand(8) == 7){
215 this.yukkuri.vx = -xpos;
216 this.yukkuri.vy = -ypos;
217 this.yukkuri.direction = EDirection.LEFT;
219 if (this.yukkuri.vx || this.yukkuri.vy) {
220 var map = ctl.backgroundMap;
221 // var x = this.yukkuri.x + (this.yukkuri.moveX ? this.yukkuri.moveX / Math.abs(this.yukkuri.moveX) * 16 : 0) + 16;
222 // var y = this.yukkuri.y + (this.yukkuri.moveY ? this.yukkuri.moveY / Math.abs(this.yukkuri.moveY) * 16 : 0) + 16;
223 var x = this.yukkuri.imgGroup.x + this.yukkuri.vx;
224 var y = this.yukkuri.imgGroup.y + this.yukkuri.vy + this.yukkuri.height / 2;
225 if (0 <= x && x < map.width && 0 <= y && y < map.height && !map.hitTest(x, y)) {
226 // console.log("あたってないよ:"+ this.yukkuri.imgGroup.x + ":" + this.yukkuri.imgGroup.y);
227 // console.log("X:" + this.yukkuri.moveX);
228 // console.log("Y:" + this.yukkuri.moveY);
229 this.isMoving = true;
230 this.yukkuri.reverse();
231 this.yukkuri.moveBy(this.yukkuri.vx, this.yukkuri.vy, frame);
233 // console.log("HIT:"+ this.yukkuri.imgGroup.x + ":" + this.yukkuri.imgGroup.y);
234 this.walk(retryCounter+1);
241 var yukkuri = this.yukkuri;
243 if(yukkuri.isSleeping){
244 if(yukkuri.age%4 === 0 && yukkuri.age !== 0)yukkuri.param.sleep--;
245 if(yukkuri.param.sleep <= 0){
246 yukkuri.param.sleep = 0;
247 yukkuri.isSleeping = false;
248 yukkuri.tweet("ゆっくりおきるよ!");
249 this.status = EAction.THINK;
250 yukkuri.imgBody.tl.resume();
251 yukkuri.imgGroup.tl.resume();
253 yukkuri.face.image = game.assets[EResPath.MARISA_FACE_NORMAL];
258 this.yukkuri.tweet("もっとゆっくりしたかった…");
259 this.yukkuri.imgBody.tl.clear();
260 this.yukkuri.imgBody.tl.pause();
261 this.yukkuri.imgGroup.tl.clear();
262 this.yukkuri.imgGroup.tl.pause();
264 eat_start: function(){
267 "targetNode": this.targetNode,
269 "myYukkuri": this.yukkuri
273 move_to_command: function(){
277 var nodes = ctl.getObjs();
278 // l = game.rootScene.childNodes.length;
279 // l = game.rootScene.childNodes.length;
281 // for (var i = 0; i < l; i++) {
282 for (var key in nodes) {
283 var node = nodes[key];
284 if (node instanceof Food) {
285 if(this.yukkuri.isKuhuku() && this.yukkuri.within(node, this.yukkuri.getRange())){
287 //A yukkuri to go to the food area
288 this.targetNode = node;
289 this.setStatus(EAction.MOVE_TO_EAT_START);
293 //not hunbry or not food.
297 this.status = EAction.WALK;
300 var Event = enchant.Class.create({
301 initialize: function (_data){
303 onactionstart:function(self){
305 if(typeof e === "undefined")return;
308 onactiontick:function(self){
310 if(typeof e === "undefined")return;
313 onactionend:function(self){
315 if(typeof e === "undefined")return;
320 var EatEvent = enchant.Class.create(Event,{
321 initialize: function (_data){
323 this.yukkuri = _data.myYukkuri;
324 this.food = _data.targetNode;
325 this.action = _data.action;
326 this.action.setStatus(EAction.EAT_WAIT);
327 this.yukkuri.tweet("む~しゃむ~しゃ!");
329 // var sec = FPS * (this.yukkuri.param.hungry/10 + 3);
332 this.yukkuri.wait(sec, {
333 "onactionstart": this.onactionstart(this),
334 "onactiontick": this.onactiontick(this),
335 "onactionend": this.onactionend(this)
338 onactionstart:function(self){
340 if(typeof e === "undefined")return;
341 self.yukkuri.eat(self.food);
344 onactiontick:function(self){
346 if(typeof e === "undefined")return;
349 onactionend:function(self){
351 if(typeof e === "undefined")return;
352 if(self.food.getAmount() <= 0 || self.yukkuri.isManpuku()){
353 self.action.setStatus(EAction.THINK);
355 self.action.setStatus(EAction.EAT_START);
362 * -When yukkuri find Food.
365 * targetNode: enchant.Node
369 var MoveToEatEvent = enchant.Class.create(Event,{
370 initialize: function (_data){
372 this.yukkuri = this.data.myYukkuri;
373 this.action = this.data.action;
375 var yukkuri = this.yukkuri;
376 var node = this.data.targetNode;
378 if(this.data.type == "food"){
379 this.action.status = EAction.MOVE_TO_EAT_UNDERWAY;
380 yukkuri.vx = node.x - yukkuri.getX();
381 yukkuri.vy = node.y - yukkuri.getY();
382 var distance = (Math.abs(yukkuri.vx) + Math.abs(yukkuri.vy)) / 2;
383 var frame = distance + distance/5 + 1;
384 yukkuri.direction = yukkuri.vx > 0 ? EDirection.RIGHT : EDirection.LEFT;
386 yukkuri.moveBy(yukkuri.vx, yukkuri.vy, frame, {
387 "onactionstart": this.onactionstart(this),
388 "onactiontick": this.onactiontick(this),
389 "onactionend": this.onactionend(this)
393 onactionstart:function(self){
395 if(typeof e === "undefined")return;
396 console.log("moveEvent onactionstart");
397 self.yukkuri.tweet("ゆゆ??\nたべものさん\nはっけんなのぜ!");
400 onactiontick:function(self){
402 if(typeof e === "undefined")return;
405 onactionend:function(self){
407 if(typeof e === "undefined")return;
408 self.action.setStatus(EAction.EAT_START);
412 var MoveCommandEvent = enchant.Class.create(Event,{
413 initialize: function (_data){
415 this.yukkuri = this.data.myYukkuri;
416 this.action = this.data.action;
417 this.vx = this.data.vx;
418 this.vy = this.data.vy;
420 var yukkuri = this.yukkuri;
421 this.action.status = EAction.MOVE_TO_EAT_UNDERWAY;
422 yukkuri.vx = node.x - yukkuri.getX();
423 yukkuri.vy = node.y - yukkuri.getY();
424 var distance = (Math.abs(yukkuri.vx) + Math.abs(yukkuri.vy)) / 2;
425 var frame = distance + 1;
426 yukkuri.moveBy(yukkuri.vx, yukkuri.vy, frame, {
427 "onactionstart": this.onactionstart(this),
428 "onactiontick": this.onactiontick(this),
429 "onactionend": this.onactionend(this)
432 onactionstart:function(self){
434 if(typeof e === "undefined")return;
437 onactiontick:function(self){
439 if(typeof e === "undefined")return;
442 onactionend:function(self){
444 if(typeof e === "undefined")return;
445 self.action.setStatus(EAction.THINK);
450 var ObjSprite = enchant.Class.create(enchant.Sprite,{
451 initialize: function (w, h){
452 enchant.Sprite.call(this, w, h);
456 removeAll: function(){
462 var Food = enchant.Class.create(ObjSprite,{
463 initialize: function (type, x, y){
464 this.classname = "Food";
465 ObjSprite.call(this,16,16);
466 this.image = game.assets[EResPath.OBJECT];
470 //一つにつきgiveの値分、hunguryを減少させられる
473 // backgroundMap.addChild(this);
474 foodGroup.addChild(this);
475 this.addEventListener('enterframe', function(){
476 if(this.amount <= 0){
481 getAmount: function(){
488 var Okazari = enchant.Class.create(enchant.Sprite,{
489 initialize: function(){
490 this.classname = "Okazari";
491 enchant.Sprite.call(this, 64, 64);
492 this.image = game.assets[EResPath.OKAZARI];
493 this.x = -CHARA_WIDTH / 2;
494 this.y = -12 - CHARA_HEIGHT / 2;
497 var Yukkuri = enchant.Class.create(ObjSprite,{
498 initialize: function(x, y){
499 ObjSprite.call(this, 64, 64);
500 this.classname = "Yukkuri";
501 this.imgGroup = new Group();
502 this.imgBody = new Group();
503 this.x = -CHARA_WIDTH / 2;
504 this.y = -CHARA_HEIGHT / 2;
507 this.image = game.assets[EResPath.YUKKURI_BASE];
508 this.direction = EDirection.RIGHT;
509 this.addEventListener('enterframe', this.runEnterframe);
510 this.isSleeping = false;
512 runEnterframe:function(){
519 if(this.direction == EDirection.RIGHT){
520 this.imgBody.scaleX = 1;
522 else if(this.direction == EDirection.LEFT){
523 this.imgBody.scaleX = -1;
526 tweet:function(text){
527 this._tweet.text(text, this.x - this._tweet.width/4, this.y - this._tweet.height);
529 moveTo:function(x, y, time){
530 this.imgGroup.tl.moveTo(x, y, time, enchant.Easing.SIN_EASEINOUT);
532 moveBy:function(x, y, time, eventFunctions){
536 return self.imgGroup.x + x;
539 return self.imgGroup.y + y;
542 easing: enchant.Easing.SIN_EASEINOUT
544 //-Event register [onactionstart,onactiontick,onactionend]
545 if(eventFunctions !== undefined){
546 for(var key in eventFunctions){
547 params[key] = eventFunctions[key];
550 this.imgGroup.tl.tween(params);
553 wait:function(frame, eventFunctions){
554 this.moveBy(0,1,frame,eventFunctions);
559 animation: function(){
560 this.imgBody.tl.moveBy(0, -5, 10, enchant.Easing.SWING).moveBy(0, 5, 10, enchant.Easing.SWING).loop();
561 // this.tl.moveBy(0, -5, 10, enchant.Easing.SWING).moveBy(0, 5, 10, enchant.Easing.SWING).loop();
562 // this.okazari.tl.moveBy(0, -5, 10, enchant.Easing.SWING).moveBy(0, 5, 10, enchant.Easing.SWING).loop();
563 // this.face.tl.moveBy(0, -5, 10, enchant.Easing.SWING).moveBy(0, 5, 10, enchant.Easing.SWING).loop();
564 // this.hear.tl.moveBy(0, -5, 10, enchant.Easing.SWING).moveBy(0, 5, 10, enchant.Easing.SWING).loop();
567 return this.imgGroup.x;
570 return this.imgGroup.y;
573 this.imgGroup.x += x;
575 // this.shadow.x += x;
576 // this._tweet.x += x;
579 this.imgGroup.y += y;
581 // this.shadow.y += y;
582 // this._tweet.y += y;
584 changeFace: function(erespath){
585 this.face.image = game.assets[erespath];
587 loadParamsXML: function(url){
588 var http = new JKL.ParseXML( url );
591 runYukkuri: function(){
592 if(this.param.hungry > 70){
593 if(this.age%(50 - this.param.hungry - 70) === 0 && this.age !== 0){
594 this.param.yukkuri -= 1;
597 else if(this.param.hungry < 10){
598 if(this.age%50 === 0 && this.age !== 0) this.param.yukkuri += 1;
601 if(this.param.yukkuri <= 0)this.param.yukkuri = 0;
602 else if(this.param.yukkuri >= this.param.maxYukkuri)this.param.yukkuri = this.param.maxYukkuri;
604 runHungry: function(){
605 if(this.age%10 === 0 && this.age !== 0)this.param.hungry++;
606 if(this.param.hungry >= 100)this.param.hungry = 100;
608 runSleep: function(){
609 if(!this.isSleeping){
610 if(this.age%100 === 0 && this.age !== 0)this.param.sleep++;
611 if(this.param.sleep >= 100){
612 this.param.sleep = 100;
613 this.isSleeping = true;
614 this.tweet("zzz...zzz...zzz...");
615 this.imgBody.tl.pause();
616 this.imgGroup.tl.pause();
617 this.imgBody.tl.clear();
618 this.imgGroup.tl.clear();
619 this.face.image = game.assets[EResPath.MARISA_FACE_SLEEP];
625 getRange: function(){
626 return this.param.range;
629 return this.isSleeping;
632 return this.param.yukkuri <= 0;
634 isManpuku: function(){
635 return this.param.hungry <= 20;
637 isKuhuku: function(){
638 return this.param.hungry >= 40;
640 getHungry: function(){
641 return this.param.hungry;
643 setHungry: function(hungry){
644 this.param.hungry = hungry;
645 if(this.param.hungry < 0)this.param.hungry = 0;
649 this.setHungry(this.param.hungry - food.getGive());
651 destruct: function(){
655 var Marisa = enchant.Class.create(Yukkuri,{
656 initialize: function(x, y){
657 Yukkuri.call(this,x,y);
658 this.classname = "Marisa";
661 // var xml = this.loadParamsXML("./data/marisa/params.xml");
662 var json = net.load("./data/marisa/params.json");
663 this.param = json.root.param;
664 this.shadow = new enchant.Sprite(64, 64);
665 this.shadow.image = game.assets[EResPath.YUKKURI_SHADOW];
666 this.shadow.x = 0 + this.x;
667 this.shadow.y = CHARA_HEIGHT / 16 + this.x;
669 this.face = new enchant.Sprite(64, 64);
670 this.face.image = game.assets[EResPath.MARISA_FACE_NORMAL];
671 this.face.x = -CHARA_WIDTH / 2;
672 this.face.y = -CHARA_HEIGHT / 2;
674 this.hear = new enchant.Sprite(64, 64);
675 this.hear.image = game.assets[EResPath.MARISA_HEAR];
676 this.hear.x = -CHARA_WIDTH / 2;
677 this.hear.y = -CHARA_HEIGHT / 2;
679 this.okazari = new Okazari();
680 this.imgGroup.addChild(this.shadow);
681 this.imgBody.addChild(this);
682 this.imgBody.addChild(this.face);
683 this.imgBody.addChild(this.hear);
684 this.imgBody.addChild(this.okazari);
685 this.imgGroup.addChild(this.imgBody);
686 backgroundMap.addChild(this.imgGroup);
687 this._tweet = new TTweet(148, 64);
689 this.imgGroup.addChild(this._tweet);
693 this.action = new Action(this);
698 var Player = enchant.Class.create(Marisa,{
699 initialize: function(x, y){
700 // this.runEnterframe = function(){
702 Marisa.call(this, x, y);
703 this.classname = "Player";
705 // this.removeEventListener('enterframe',this.runEnterframe);
706 this.addEventListener('enterframe', function(){
707 if(this.age%10 === 0 && this.age !== 0){
708 // console.log("palyer:" + this.age);
712 ///////////////////////////////////////////////
713 //Action pattern for debug
714 // if(game.input.left){
715 // this.changeFace(EResPath.MARISA_FACE_TIRED);
717 // this.direction = EDirection.LEFT;
719 // if(game.input.right){
721 // this.direction = EDirection.RIGHT;
723 // if(game.input.up){
725 // // this.tweet("ゆっくりしていってね!");
727 // if(game.input.down){
730 ///////////////////////////////////////////////
732 // if(ctl.backgroundMap.hitTest(this.imgGroup.x, this.imgGroup.y + this.height / 2)){
733 // console.log("hit:" + this.imgGroup.x + ":" + (this.imgGroup.y + this.height/2) );
735 // console.log("not:" + this.imgGroup.x + ":" + (this.imgGroup.y + this.image.height/2));
737 //- Display the circle of search range.
738 // fieldBg.image.clear();
739 // fieldBg.image.context.beginPath();
740 // fieldBg.image.context.fillStyle = '#ff0000';
741 // fieldBg.image.context.arc(this.imgGroup.x + this.width/2, this.imgGroup.y + this.height/2, this.range, Math.PI * 2, false);
742 // fieldBg.image.context.fill();
746 game.onload = function(){
748 mangTouch = new MangTouch(ctl);
750 backgroundMap.image = game.assets[EResPath.MAP0];
751 backgroundMap.loadData(_mapData);
752 backgroundMap.collisionData = _collisionData;
754 var menuSurface = new enchant.Surface(EMenuPos.WIDTH, EMenuPos.HEIGHT);
755 var fieldSurface = new enchant.Surface(EFieldPos.WIDTH, EFieldPos.HEIGHT);
756 fieldBg.image = fieldSurface;
760 menuSurface.context.fillStyle = '#000';
761 menuSurface.context.fillRect(0, 0, EMenuPos.WIDTH, EMenuPos.HEIGHT);
762 menuBg.image = menuSurface;
763 menuBg.x = EMenuPos.X;
764 menuBg.y = EMenuPos.Y;
766 backgroundMap.addChild(fieldBg);
767 mangIcon = new MangIcon(ctl, menuBg);
769 game.rootScene.addChild(backgroundMap);
770 game.rootScene.addChild(menuBg);
772 // var labelGroup = new LabelGroup();
773 mangLabel = new MangLabel(ctl, menuBg);
774 // var info = new Label("ゆっくり");
775 // info.color = "#ffffff";
776 // info.font = "14px 'Times New Roman'";
778 // info.y = GAME_HEIGHT / 2;
779 // labelGroup.addChild(info);
780 // menuBg.addChild(labelGroup);
783 // var food = new def.Food("apple", 200, 250);
784 backgroundMap.addChild(foodGroup);
785 var player = new Player(PALYER_INIT_X, PALYER_INIT_Y);
786 new Marisa(PALYER_INIT_X + 200, PALYER_INIT_Y);
789 game.rootScene.addEventListener('touchstart', function (e) {
791 var mang = mangTouch.get(ctl.getCurrentCommand());
794 game.rootScene.addEventListener('touchmove', function (e) {
795 var mang = mangTouch.get(ctl.getCurrentCommand());
798 game.rootScene.addEventListener('touchend', function (e) {
799 var mang = mangTouch.get(ctl.getCurrentCommand());
801 game.touched = false;
805 game.rootScene.addEventListener('enterframe', function(){
807 mangLabel.draw(player);