OSDN Git Service

sleep refactoring
authornagoling <>
Sun, 29 Sep 2013 10:02:47 +0000 (19:02 +0900)
committernagomi <nagomi@192.168.1.23>
Sun, 29 Sep 2013 10:02:47 +0000 (19:02 +0900)
main.js

diff --git a/main.js b/main.js
index 2fecbba..fc7ce79 100644 (file)
--- a/main.js
+++ b/main.js
@@ -18,6 +18,7 @@ var CHARA_HEIGHT = 64;
 var GAME_WIDTH = 800;
 var GAME_HEIGHT = 600;
 var FPS = 20;
+var BASE_FPS = 20;
 var HUNGRY_MAX = 100;
 
 var EResPath = {
@@ -78,7 +79,6 @@ var EFieldPos = {
 };
 var EAction = {
        WAIT : 0,
-       SLEEP : 1,
        HUNT : 3,
        THINK : 4,
        WALK : 5,
@@ -92,6 +92,8 @@ var EAction = {
        UNUN_WAIT : 14,
        UNUN_SMELL_START : 15,
        UNUN_SMELL_WAIT : 16,
+       SLEEP_START : 17,
+       SLEEP_WAIT : 18,
        NONE: 9999
 };
 var EDirection = {
@@ -141,6 +143,9 @@ window.onload = function(){
                        this.isMoving = false;
                        this.targetNode = null;
                },
+               getStatus: function(){
+                       return this.status;
+               },
                setStatus: function(eaction){
                        this.status = eaction;
                },
@@ -148,9 +153,9 @@ window.onload = function(){
                        if(this.yukkuri.isDead()){
                                this.status = EAction.DEAD;
                        }
-                       else if(this.yukkuri.isSleep()){
-                               this.status = EAction.SLEEP;
-                       }
+                       // else if(this.yukkuri.isSleep()){
+                       //      this.status = EAction.SLEEP;
+                       // }
 
                },
                act : function(){
@@ -161,7 +166,10 @@ window.onload = function(){
                                break;
                                case EAction.THINK:
                                        if(this.yukkuri.age%2 === 0){
-                                               if(this.yukkuri.getUnun() >= 80){
+                                               if(this.yukkuri.isSleep()){
+                                                       this.setStatus(EAction.SLEEP_START);
+                                               }
+                                               else if(this.yukkuri.getUnun() >= 80){
                                                        this.setStatus(EAction.UNUN_START);
                                                }else{
                                                        // this.yukkuri.param.yukkuri -= 1;
@@ -180,8 +188,10 @@ window.onload = function(){
                                case EAction.WALK:
                                        this.walk(0);
                                break;
-                               case EAction.SLEEP:
-                                       this.sleep();
+                               case EAction.SLEEP_START:
+                                       this.sleep_start();
+                               break;
+                               case EAction.SLEEP_WAIT:
                                break;
                                case EAction.DEAD:
                                        this.dead();
@@ -293,22 +303,11 @@ window.onload = function(){
                                }
                        }
                },
-               sleep: function(){
-                       var yukkuri = this.yukkuri;
-
-                       if(yukkuri.isSleeping){
-                               if(yukkuri.age%4 === 0 && yukkuri.age !== 0)yukkuri.param.sleep--;
-                               if(yukkuri.param.sleep <= 0){
-                                       yukkuri.param.sleep = 0;
-                                       yukkuri.isSleeping = false;
-                                       mangMsg.output(yukkuri, EMsg.GET_UP);
-                                       this.status = EAction.THINK;
-                                       yukkuri.imgBody.tl.resume();
-                                       yukkuri.imgGroup.tl.resume();
-                                       yukkuri.animation();
-                                       yukkuri.face.image = game.assets[EResPath.MARISA_FACE_NORMAL];
-                               }
-                       }
+               sleep_start: function(){
+                       new SleepEvent({
+                               "action": this,
+                               "myYukkuri": this.yukkuri
+                       });
                },
                dead : function(){
                        console.log("dead");
@@ -394,6 +393,104 @@ window.onload = function(){
                        };
                }
        });
+       var SleepEvent = enchant.Class.create(Event,{
+               initialize: function (_data){
+                       this.data = _data;
+                       this.yukkuri = _data.myYukkuri;
+                       this.action = _data.action;
+
+                       mangMsg.output(this.yukkuri, EMsg.SLEEP);
+                       this.action.setStatus(EAction.SLEEP_WAIT);
+                       this.yukkuri.setFaceImage(EFace.SLEEP);
+                       this.yukkuri.tlPause();
+                       var sec = FPS * 3;
+                       // this.yukkuri.enterframe(function(){
+                       //      console.log("enterfmrae");
+                       // });
+                       var self = this;
+                       this.yukkuri.addEventListener('enterframe', function() {
+                               var yukkuri = self.yukkuri;
+                               if(yukkuri.age%2 === 0 && yukkuri.age !== 0)yukkuri.param.sleep--;
+                               if(yukkuri.param.sleep <= 0){
+                                       yukkuri.param.sleep = 0;
+                                       self.action.setStatus(EAction.WAIT);
+                                       mangMsg.output(self.yukkuri, EMsg.GET_UP);
+                                       self.yukkuri.setFaceImage(EFace.NORMAL);
+                                       yukkuri.tlResume();
+                                       this.removeEventListener("enterframe",arguments.callee);
+                                       self.action.setStatus(EAction.THINK);
+                               }
+                       });
+               },
+               onactionstart:function(self){
+                       return function(e){
+                               if(typeof e === "undefined")return;
+                               console.log("astart");
+                       };
+               },
+               onactiontick:function(self){
+                       return function(e){
+                               if(typeof e === "undefined")return;
+                       };
+               },
+               onactionend:function(self){
+                       return function(e){
+                               if(typeof e === "undefined")return;
+                               console.log("enddd");
+                       };
+               }
+
+               // initialize: function (_data){
+               //      console.log("init");
+               //      this.data = _data;
+               //      this.yukkuri = _data.myYukkuri;
+               //      this.action = _data.action;
+               //      this.action.setStatus(EAction.SLEEP_WAIT);
+               //      mangMsg.output(this.yukkuri, EMsg.SLEEP);
+               //      this.yukkuri.setFaceImage(EFace.SLEEP);
+               //      this.yukkuri.tlPause();
+
+               //      var sec = BASE_FPS * 1;
+               //      console.log("sec:"+ sec);
+               //      this.yukkuri.wait(sec, {
+               //              "onactionstart": this.onactionstart(this),
+               //              "onactiontick": this.onactiontick(this),
+               //              "onactionend": this.onactionend(this)
+               //      });
+               // },
+               // onactionstart:function(self){
+               //      return function(e){
+               //              console.log("start");
+               //              if(typeof e === "undefined")return;
+               //      };
+               // },
+               // onactiontick:function(self){
+               //      return function(e){
+               //              if(typeof e === "undefined")return;
+               //              console.log("tick");
+               //              var yukkuri = self.yukkuri;
+               //              if(yukkuri.age%4 === 0 && yukkuri.age !== 0)yukkuri.param.sleep--;
+               //              if(yukkuri.param.sleep <= 0){
+               //                      yukkuri.param.sleep = 0;
+               //              }
+               //      };
+               // },
+               // onactionend:function(self){
+               //      return function(e){
+               //              if(typeof e === "undefined")return;
+               //              self.action.setStatus(EAction.WAIT);
+               //              self.yukkuri.setFaceImage(EFace.NORMAL);
+               //              mangMsg.output(self.yukkuri, EMsg.GET_UP);
+               //              yukkuri.tlResume();
+               //              new WaitEvent({
+               //                      "waitframe": FPS * 1.5,
+               //                      "action": self.action,
+               //                      "myYukkuri": self.yukkuri
+               //              });
+               //      };
+               // }
+       });
+
        var UnunEvent = enchant.Class.create(Event,{
                initialize: function (_data){
                        this.data = _data;
@@ -486,7 +583,6 @@ window.onload = function(){
                        this.action.setStatus(EAction.EAT_WAIT);
                        mangMsg.output(this.yukkuri, EMsg.EAT);
                        this.yukkuri.setFaceImage(EFace.EAT2);
-                       // this.yukkuri.image = game.assets[EResPath.MARISA_];
 
                        // var sec = FPS * (this.yukkuri.param.hungry/10 + 3);
                        var sec = FPS * 3;
@@ -794,21 +890,11 @@ window.onload = function(){
                        if(this.param.unun >= 100)this.param.unun = 100;
                },
                runSleep: function(){
-                       if(!this.isSleeping){
-                               if(this.age%100 === 0 && this.age !== 0)this.param.sleep++;
+                       if(this.action.getStatus() != EAction.SLEEP_START && this.action.getStatus() != EAction.SLEEP_WAIT){
+                               if(this.age%80 === 0 && this.age !== 0)this.param.sleep++;
                                if(this.param.sleep >= 100){
                                        this.param.sleep = 100;
-                                       this.isSleeping = true;
-                                       mangMsg.output(this, EMsg.SLEEP);
-
-                                       this.imgBody.tl.pause();
-                                       this.imgGroup.tl.pause();
-                                       this.imgBody.tl.clear();
-                                       this.imgGroup.tl.clear();
-                                       this.face.image = game.assets[EResPath.MARISA_FACE_SLEEP];
-
                                }
-
                        }
                },
                changeFace: function(){
@@ -832,7 +918,7 @@ window.onload = function(){
                        return this.param.range;
                },
                isSleep: function(){
-                       return this.isSleeping;
+                       return this.param.sleep >= 100;
                },
                isDead: function(){
                        return this.param.yukkuri <= 0;
@@ -883,6 +969,17 @@ window.onload = function(){
                        var path = this.bodyStatus[ebody];
                        this.image = game.assets[path];
                },
+               tlPause:function(){
+                       this.imgBody.tl.pause();
+                       this.imgGroup.tl.pause();
+                       this.imgBody.tl.clear();
+                       this.imgGroup.tl.clear();
+               },
+               tlResume: function(){
+                       this.imgBody.tl.resume();
+                       this.imgGroup.tl.resume();
+                       this.animation();
+               },
                destruct: function(){
 
                }
@@ -1046,7 +1143,7 @@ window.onload = function(){
                // var food = new def.Food("apple", 200, 250);
                backgroundMap.addChild(foodGroup);
                var player = new Player(PALYER_INIT_X, PALYER_INIT_Y);
-               new Marisa(PALYER_INIT_X + 200, PALYER_INIT_Y);
+               // new Marisa(PALYER_INIT_X + 200, PALYER_INIT_Y);
                var touchX = 0;
 
                game.rootScene.addEventListener('touchstart', function (e) {