OSDN Git Service

- add OBJECT_TYPE
[flapp/flapp.git] / movieclip.js
index c0de00c..0845d32 100644 (file)
@@ -1,8 +1,13 @@
 (function(global) {
-    var FlappMovieClip = function(parentMovieClip) {
-       this.parentMovieClip = parentMovieClip?parentMovieCli:this;
+    var FlappMovieClip = function(parentMovieClip, name, matrix, colorTransform) {
+       this.OBJECT_TYPE = 2; // 1:Shape, 2: MovieClip
+       this.parentMovieClip = parentMovieClip?parentMovieCli:null;
        this.rootMovieClip = parentMovieClip?parentMovieClip.rootMovieClip:this;
+       this.name = name?name:'anonymous';
+       this.matrix = matrix;
+       this.colorTransform = colorTransform;
        this.childMovieClips = {}; // name => movieClip
+       this.childMovieClips_seqnum = 1; // for name movieClip
        this.clearControlTags();
        //
        this.prevShowFramePos = 0;
        this.canvas = canvas;
        this.canvasDirty = false; // dirtyFlag
        //
-       this.displayList = {}; // depth => [tag, matrix, colorTransform]
+       this.displayList = new FlappDisplay();
        //
        this.totalframes = 0;
        this.currentFrame = 0;
+       this.playing = true;
+       // this.loop = true;
+       this.loop = false;
+       var actionVarriableTable = {};
+       var actionVarriablOrigKeys = {};
     };
     FlappMovieClip.prototype = {
        clearControlTags: function(controlTag) {
            this.controlTagsList = [[]]; //
+           this.actionTagsList = [[]];
            this.labelMap = {}; // label => frameNum
            this.framesLoaded = 0;
        },
        appendControlTag: function(controlTag) {
            // console.debug("FlappMovieClip::appendControlTag");
+           if (controlTag.code === 12) { // DoAction
+               this.actionTagsList[this.framesLoaded].push(controlTag);
+               return ;
+           }
            this.controlTagsList[this.framesLoaded].push(controlTag);
            if (controlTag.code === 1) { // ShowFrame
                this.controlTagsList.push([]);
+               this.actionTagsList.push([]);
                this.framesLoaded++;
            } else if (controlTag.code === 43) { // FrameLabel
                this.labelMap[controlTag.name] = this.framesLoaded;
                this.appendControlTag(controlTags[i]);
            }
        },
+       addChildMovieClip: function(name, movieClip) {
+           this.childMovieClips[name] = movieClip;
+       },
+       deleteChildMovieClip: function(name) {
+           delete this.childMovieClips[name];
+       },
        control: function(dict) {
+           if (this.totalframes === 0) { // imcomplete
+               return false;
+           }
            console.debug("FlappMovieClip::control");
-           var tag, i, l;
            if (this.framesLoaderd < this.totalframes) { // imcomplete
                if (this.currentFrame < this.framesLoaderd) {
                    return false; // idle
                }
            }
+           for (var mc in this.childMovieClips) {
+               this.childMovieClips[mc].control(dict);
+           }
+           if (this.playing) {
+               this.controlThis(dict);
+           }
+           return true;
+       },
+       controlThis: function(dict) {
+           console.debug("FlappMovieClip::controlThis");
+           var tag, i, l;
+           var defineTag;
+           if ((this.currentFrame < 0 ) || (this.totalframes <= this.currentFrame)) {
+               this.currentFrame = 0;
+           }
            var controlTags = this.controlTagsList[this.currentFrame];
-           this.actionTagList = [];
+
            for (i = 0, l = controlTags.length ; i < l ; i++) {
-               tag = this.controlTags[i];
+               tag = controlTags[i];
                switch (tag.code) {
                case 1: // ShowFrame
                    break;
-               case 12: // DoAction
-                   this.actionTagList.push(tag);
-                   break;
                case 26: // PlaceObject2
                    // set display List;
+                   console.log(tag);
+                   if (tag.id === null) {
+                       defineTag = null;
+                   } else {
+                       defineTag = dict.get(tag.id);
+                   }
+                   if (defineTag === undefined) {
+                       console.log(dict);
+                   }
+                   var obj = null;
+                   if (defineTag.code === 39) { // DefineSprite
+                       var name = tag.name;
+                       if (name === null) {
+                           name = "instance"+this.childMovieClips_seqnum;
+                           defineTag.name = name;
+                           this.childMovieClips_seqnum++;
+                       }
+                       var obj = FlappMovieClip(this, name, tag.matrix, tag.colorTransform);
+                       obj.setControlTags(defineTag.controlTags);
+                       this.addChildMovieClip(name, obj);
+                   } else if (defineTag.code === 2) { // DefineShape
+                       var obj = FlappShape(name, tag.matrix, tag.colorTransform);
+                       obj.loadShapeTag(defineTag);
+                   }
+                   this.displayList.set(tag.depth, obj, tag);
                    break;
                }
            }
-           this.currentFrame = 0;
-           return true;
        },
        action: function() {
-           console.debug("FlappMovieClip::action");
-           var tag, i, l;
-           for (i = 0, l = this.actionTagList.length ; i < l ; i++) {
-               tag = this.actionTagList[i];
-               ;
+           for (var mc in this.childMovieClips) {
+               this.childMovieClips[mc].action();
+           }
+           if (this.playing) {
+               this.actionThis();
+           }
+
+       },
+       actionThis: function() {
+           var actionTags = this.actionTagsList[this.currentFrame];
+           var l = actionTags.length;
+           console.debug("FlappMovieClip::actionThis: actionTags.length:"+l);
+           for (var i = 0 ; i < l ; i++) {
+               var tag = actionTags[i];
+               var movieClip = this;
+               FlappAction.exec(tag, movieClip, this.rootMovieClip);
            }
        },
-       render: function(canvas, matrix, colorTransform) {
-           console.debug("FlappMovieClip::render");
-           
+       increment: function() {
+           for (var mc in this.childMovieClips) {
+               this.childMovieClips[mc].increment();
+           }
+           if (this.playing) {
+               this.incrementThis();
+           }
+       },
+       incrementThis: function() {
+           console.debug("FlappMovieClip::incrementThis: "+this.currentFrame);
+           this.currentFrame++;
+           if (this.totalframes <= this.currentFrame) {
+               if (this.loop) {
+                   this.currentFrame = 0; // play
+               } else {
+                   this.playing = false;
+               }
+           }
+           if (this.totalframes === 1) {
+               this.playing = false;
+           }
+       },
+       render: function(canvas) {
+           var depthList = this.displayList.sortedDepth().reverse();
+           for (var i = 0, l = depthList.length  ; i < l ; i++) {
+               var depth = depthList[i];
+               var obj = this.display.get(depth);
+               if (obj.OBJECT_TYPE === 2) { // MovieClip
+                   for (var movieClip in this.childMovieClips) {
+                       childMovieClips.render(this.canvas);
+                   }
+                   obj.render(this.canvas);
+               } else if ((obj.OBJECT_TYPE === 2) && (this.playing == true)) {
+                   obj.render(this.canvas);
+               }
+           }
+       },
+       setVariable: function(key, value) {
+           var lcKey = key.toLowerCase();
+           actionVarriableTable[lcKey] = value;
+           actionVarriablOrigKeys[lcKey] = key;
+       },
+       getVariable: function(key) {
+           var lcKey = key.toLowerCase();
+           if (lcKey in actionVarriableTable) {
+               return actionVarriableTable[lcKey];
+           }
+           return null;
+       },
+       gotoFrame: function(frameNum) {
+           console.debug("FlappMovieClip::gotoFrame"+frameNum);
+           this.currentFrame = frameNum;
+       },
+       gotoLabel: function(frameLabel) {
+           frameNum = labelMap[frameLabel];
+           console.debug("FlappMovieClip::gotoFrame"+frameLabel+"=>"+frameNum);
+           this.currentFrame = frameNum;
+       },
+       play: function() {
+           this.playing = true;
+       },
+       stop: function() {
+           this.playing = false;
+       },
+       destroy: function() { // destructor
+           for (name in this.childMovieClips) {
+               this.childMovieClips[name].destroy();
+           }
+           this.parentMovieClip = null;
+           this.rootMovieClip = null;
+           this.controlTagsList = null;
+           this.actionTagsList = null;
+           this.labelMap = null;
+           this.canvas = null;
+           this.displayList = null;
        }
     };
     global.FlappMovieClip = FlappMovieClip;