Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (function(jy){
- jy.SingleFrame = my.Class({
- constructor : function(ox, oy, w, h) {
- this.ox = ox;
- this.oy = oy;
- this.w = w;
- this.h = h;
- },
- setFrame : function(elm) {
- elm.style.backgroundPosition = (-1 * this.ox * this.w) + "px " + (-1 * this.oy * this.h) + "px";
- }
- });
- jy.AnimFrames = my.Class(jy.SingleFrame, {
- constructor : function(ox, oy, w, h, l, nframes, loop) {
- jy.AnimFrames.Super.call(this, ox, oy, w, h);
- this.length = l;
- this.nframes = nframes;
- //number of ingame frames to stay on one anim frame
- this.totalFrames = l * nframes;
- this.currentFrame = 0;
- this.loop = loop;
- this.play = true;
- this.animEndCallback = null;
- },
- nextFrame : function(elm) {
- var next = this.currentFrame + jy.frameSkip;
- if(next >= this.totalFrames) {
- if(this.loop) {
- this.setFrame(elm,next % this.totalFrames);
- } else {
- if(typeof this.animEndCallback == "function") this.animEndCallback();
- this.play=false;
- this.setFrame(elm,this.totalFrames - 1);
- }
- } else
- this.setFrame(elm,next);
- },
- setFrame : function(elm, f) {
- var fx = (this.ox - Math.floor(f / this.nframes)) * this.w, fy = -1 * this.oy * this.h;
- elm.style.backgroundPosition = fx + "px " + fy + "px";
- this.currentFrame = f;
- }
- });
- jy.Div = my.Class({
- constructor: function(x,y,w,h,bottom, right){
- this.elm = document.createElement("div");
- this.pos = new jy.Point(x,y);
- this.setPos(x, y, bottom, right);
- this.setSize(w, h);
- },
- show : function() {
- this.elm.style.display = "block";
- },
- hide : function() {
- this.elm.style.display = "none";
- },
- setPos : function(x,y,bottom, right) {
- this.pos.set(x,y);
- this.elm.style[bottom ? "bottom": "top"] = y + "px";
- this.elm.style[right ? "right": "left"] = x + "px";
- },
- setSize : function(w, h) {
- this.w = w;
- this.h = h;
- this.elm.style.width = w + "px";
- this.elm.style.height = h + "px";
- }
- });
- jy.Image = my.Class(jy.Div,{
- constructor: function(imgid, x ,y, bottom, right){
- if (!(imgid in jy.images)) {
- this.img = null;
- console.log("Warning, image not declared");
- return;
- }
- this.img = jy.images[imgid];
- jy.Image.Super.call(this,x,y,this.img.w,this.img.h, bottom, right);
- this.elm.className = "image";
- this.elm.style.backgroundImage = 'url("' + this.img.src + '")';
- },
- transforms : [],
- applyTransforms : function() {
- console.log(this.transforms);
- }
- });
- jy.Sprite = my.Class(jy.Image, {
- constructor : function(imgid, x, y, w, h, anims) {
- jy.Sprite.Super.call(this, imgid, x, y);
- if(!this.img) return;
- this.setSize(w, h);
- this.elm.className = "sprite";
- this.anims = anims;
- },
- move:function(dx,dy){
- this.pos.add(dx,dy);
- this.elm.style.top = this.pos.y + "px";
- this.elm.style.left = this.pos.x + "px";
- },
- setAnim : function(a, cb) {
- if(this.anims && a in this.anims) {
- this.currentAnim = this.anims[a];
- this.currentFrame = 0;
- this.currentAnim.setFrame(this.elm, 0);
- if(this.currentAnim instanceof jy.AnimFrames) {
- this.currentAnim.play=true;
- this.currentAnim.animEndCallback = cb;
- } else if( typeof cb == 'function') cb();
- } else {
- console.log("Anim " + a + " not found");
- }
- },
- update : function() {
- if(this.currentAnim instanceof jy.AnimFrames && this.currentAnim.play) {
- this.currentAnim.nextFrame(this.elm);
- }
- }
- });
- })(jy);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement