Advertisement
Guest User

Untitled

a guest
Jan 4th, 2012
288
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (function(jy){
  2.     jy.SingleFrame = my.Class({
  3.         constructor : function(ox, oy, w, h) {
  4.             this.ox = ox;
  5.             this.oy = oy;
  6.             this.w = w;
  7.             this.h = h;
  8.         },
  9.         setFrame : function(elm) {
  10.             elm.style.backgroundPosition = (-1 * this.ox * this.w) + "px " + (-1 * this.oy * this.h) + "px";
  11.         }
  12.     });
  13.  
  14.     jy.AnimFrames = my.Class(jy.SingleFrame, {
  15.         constructor : function(ox, oy, w, h, l, nframes, loop) {
  16.             jy.AnimFrames.Super.call(this, ox, oy, w, h);
  17.             this.length = l;
  18.             this.nframes = nframes;
  19.             //number of ingame frames to stay on one anim frame
  20.             this.totalFrames = l * nframes;
  21.             this.currentFrame = 0;
  22.             this.loop = loop;
  23.             this.play = true;
  24.             this.animEndCallback = null;
  25.         },
  26.  
  27.         nextFrame : function(elm) {
  28.             var next = this.currentFrame + jy.frameSkip;
  29.             if(next >= this.totalFrames) {
  30.                 if(this.loop) {
  31.                     this.setFrame(elm,next % this.totalFrames);
  32.                 } else {
  33.                     if(typeof this.animEndCallback == "function") this.animEndCallback();
  34.                     this.play=false;
  35.                     this.setFrame(elm,this.totalFrames - 1);
  36.                 }
  37.             } else
  38.                 this.setFrame(elm,next);
  39.         },
  40.         setFrame : function(elm, f) {
  41.             var fx = (this.ox - Math.floor(f / this.nframes)) * this.w, fy = -1 * this.oy * this.h;
  42.             elm.style.backgroundPosition = fx + "px " + fy + "px";
  43.             this.currentFrame = f;
  44.         }
  45.     });
  46.  
  47.     jy.Div = my.Class({
  48.         constructor: function(x,y,w,h,bottom, right){
  49.             this.elm = document.createElement("div");
  50.             this.pos = new jy.Point(x,y);
  51.             this.setPos(x, y, bottom, right);
  52.             this.setSize(w, h);
  53.         },
  54.         show : function() {
  55.             this.elm.style.display = "block";
  56.         },
  57.         hide : function() {
  58.             this.elm.style.display = "none";
  59.         },
  60.         setPos : function(x,y,bottom, right) {
  61.             this.pos.set(x,y);
  62.             this.elm.style[bottom ? "bottom": "top"] = y + "px";
  63.             this.elm.style[right ? "right": "left"] = x + "px";
  64.         },
  65.         setSize : function(w, h) {
  66.             this.w = w;
  67.             this.h = h;
  68.             this.elm.style.width = w + "px";
  69.             this.elm.style.height = h + "px";
  70.         }
  71.     });
  72.  
  73.     jy.Image = my.Class(jy.Div,{
  74.         constructor: function(imgid, x ,y, bottom, right){
  75.             if (!(imgid in jy.images)) {
  76.                 this.img = null;
  77.                 console.log("Warning, image not declared");
  78.                 return;
  79.             }
  80.  
  81.             this.img = jy.images[imgid];
  82.             jy.Image.Super.call(this,x,y,this.img.w,this.img.h, bottom, right);
  83.             this.elm.className = "image";
  84.             this.elm.style.backgroundImage = 'url("' + this.img.src + '")';
  85.         },
  86.  
  87.         transforms : [],
  88.         applyTransforms : function() {
  89.             console.log(this.transforms);
  90.         }
  91.     });
  92.  
  93.  
  94.     jy.Sprite = my.Class(jy.Image, {
  95.         constructor : function(imgid, x, y, w, h, anims) {
  96.             jy.Sprite.Super.call(this, imgid, x, y);
  97.             if(!this.img) return;
  98.             this.setSize(w, h);
  99.             this.elm.className = "sprite";
  100.             this.anims = anims;
  101.         },
  102.  
  103.         move:function(dx,dy){
  104.             this.pos.add(dx,dy);
  105.             this.elm.style.top = this.pos.y + "px";
  106.             this.elm.style.left = this.pos.x + "px";
  107.         },
  108.  
  109.         setAnim : function(a, cb) {
  110.             if(this.anims && a in this.anims) {
  111.                 this.currentAnim = this.anims[a];
  112.                 this.currentFrame = 0;
  113.                 this.currentAnim.setFrame(this.elm, 0);
  114.                 if(this.currentAnim instanceof jy.AnimFrames) {
  115.                     this.currentAnim.play=true;
  116.                     this.currentAnim.animEndCallback = cb;
  117.                 } else if( typeof cb == 'function') cb();
  118.             } else {
  119.                 console.log("Anim " + a + " not found");
  120.             }
  121.         },
  122.         update : function() {
  123.             if(this.currentAnim instanceof jy.AnimFrames && this.currentAnim.play) {
  124.                 this.currentAnim.nextFrame(this.elm);
  125.             }
  126.         }
  127.     });
  128.  
  129. })(jy);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement