Guest User

Untitled

a guest
Nov 17th, 2012
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * create a new snowball
  3.  *
  4.  * @param x - int
  5.  * @param y - int
  6.  */
  7. xmas.Entity.Image.Snowball = function(x, y) {
  8.    
  9.     this.src          = 'snowball';
  10.    
  11.     // setting an offset to the image to have the ball spawn in the middle of the mouse click
  12.     this.offsetX      = 0;
  13.     this.offsetY      = 0;
  14.    
  15.     // use radius to check for collision later
  16.     this.r            = 0;
  17.    
  18.     // actual draw points - should include some kind of offset calculation
  19.     this.drawX        = 0;
  20.     this.drawY        = 0;
  21.    
  22.     this.scaleX       = 1;
  23.     this.scaleY       = 1;
  24.    
  25.     this.z            = 0;
  26.     this.vx           = 0;
  27.     this.vy           = 3;
  28.     this.vz           = 5;
  29.    
  30.     this.gravity      = - 0.097;
  31.    
  32.     this._constructor(this.src, x, y, 1);
  33.    
  34.     this.update = function() {
  35.      
  36.       this.x  += this.vx;
  37.       this.y  += this.vy;
  38.       this.z  += this.vz;
  39.      
  40.       this.vy -= this.gravity;
  41.    
  42.       // need to check if on the ground later
  43.       // todo
  44.      
  45.       // did I hit something somewhere "back" in the environment..?
  46.       // todo
  47.       // this.r   = ?
  48.    
  49. //      this.vx *= 0.99;
  50. //      this.vy *= 0.99;
  51. //      this.vz *= 0.99;
  52.  
  53.     };
  54.    
  55.    
  56.     this.onImgLoaded = function() {
  57.       this.img.inViewport   = true;
  58.       this.img.widthScaled  = this.img.width;
  59.       this.img.heightScaled = this.img.height;
  60.      
  61.       // set radius for later collision detection because the image is a ball :)
  62.       this.r                =   this.img.widthScaled/2;
  63.      
  64.       // update coords for the offset to have the ball spawn at the center of the mouse position
  65.       this.offsetX          = - this.img.widthScaled/2;
  66.       this.offsetY          = - this.img.heightScaled/2;
  67.       this.x               +=   this.offsetX;
  68.       this.y               +=   this.offsetY;
  69.     };
  70.    
  71.     this.debug = function() {
  72.       console.log("coord", this.x, this.y, this.z);
  73.       console.log("pos  ", this.drawX, this.drawY);
  74.       console.log("scale", this.scaleX, this.scaleY);
  75.       console.log("imgO ", this.img.width, this.img.height)
  76.       console.log("img  ", this.img.widthScaled, this.img.heightScaled);
  77.       console.log("---------------------------------");
  78.     };
  79.    
  80.     /**
  81.      * renders the image
  82.      */
  83.     this.render = function() {
  84.       if(this.isVisible() && this.img.isLoaded && this.img.inViewport) {
  85.  
  86.        
  87.         this.drawX  = this.x / this.z;
  88.         this.drawY  = this.y / this.z;
  89.         // using this instead will simply drop the ball
  90.         //this.drawX  = this.x;
  91.         //this.drawY  = this.y;
  92.        
  93.         // scaleX and scaleY becoming *very* low - about 10^(-100) - so no scaling is happening
  94.         this.scaleX = this.scaleX / this.z;
  95.         this.scaleY = this.scaleY / this.z;
  96.        
  97.         this.img.widthScaled  = this.img.width - this.img.width * this.scaleX;
  98.         this.img.heightScaled = this.img.width - this.img.width * this.scaleY;
  99.  
  100.         //this.debug();
  101.  
  102. //        xmas.Draw.image(this.img, this.drawX, this.drawY, this.img.widthScaled, this.img.heightScaled);
  103.  
  104.         xmas.ctxPreRender.drawImage(this.img, this.drawX, this.drawY, this.img.widthScaled, this.img.heightScaled);
  105.  
  106.       }
  107.     };    
  108.        
  109.    
  110. }
  111.  
  112.  
  113. /**
  114.  * create a new image
  115.  *
  116.  * @param src - string
  117.  * @param x - int
  118.  * @param y - int
  119.  * @param opacity - float
  120.  */
  121. xmas.Entity.Image = function(src, x, y, opacity) {
  122.  
  123.     this.type         = 'image';  
  124.     this.group        = null;
  125.     this.isLoaded     = false;
  126.     this.img          = null;
  127.    
  128.     var self          = this;
  129.    
  130.     this._constructor  = function(src, x, y, opacity) {
  131.      
  132.       this.x            = x;              // the x coordinate
  133.       this.y            = y;              // the y coordinate
  134.       this.opacity      = opacity || 1;   // initial opacity; the dot will fade out
  135.       // create image
  136.       this.img          = new Image();
  137.       this.img.opacity  = this.opacity;
  138.      
  139.       this.loadImage(src);
  140.      
  141.     };
  142.    
  143.     this.setOpacity  = function(opacity) {
  144.       this.opacity     = opacity;
  145.       this.img.opacity = opacity;
  146.     };
  147.    
  148.     /**
  149.      * loads an image
  150.      *
  151.      * @param src - string
  152.      */
  153.     this.loadImage = function(src) {
  154.      
  155.       var self            = this;
  156.       this.img.isLoaded   = false;
  157.       this.img.inViewport = false;
  158.      
  159.       this.img.onload   = function() {
  160.         this.isLoaded   = true;
  161.         this.calculateCenter();
  162.         self.isLoaded   = true;
  163.         self.onImgLoaded();
  164.       }
  165.      
  166.       var source        = xmas.Resources.get(this.type, src);
  167.      
  168.       if(source) {
  169.         this.img.src      = source;
  170.       }
  171.     };
  172.    
  173.     this.init   = function() {
  174.       // nothing to do here
  175.     };
  176.  
  177.     this.update = function() {
  178.       // nothing to do here
  179.     };
  180.    
  181.     this.onImgLoaded = function() {
  182.       // do when image is loaded, nothing to do here
  183.     }
  184.  
  185.     /**
  186.      * when changing condition
  187.      */
  188.     this.onChangeCondition = function() {
  189.       this.setImageSpriteByCondition(this.getCondition());
  190.     };
  191.  
  192.     /**
  193.      * set the image sprite by status, assuming all sprites have equal withs
  194.      * and are set in one row
  195.      *
  196.      * sx = 0      sx = w      sx = w*2    sx = w*3
  197.      * |-----------|-----------|-----------|-----------|
  198.      * |  Default  |   Hover   |  Active   | InActive  |
  199.      * |-----------|-----------|-----------|-----------|
  200.      *
  201.      * @param condition - int
  202.      */
  203.     this.setImageSpriteByCondition = function(condition) {
  204.       if(this.img && this.img.isLoaded) {
  205.        
  206.         this.sy = 0;
  207.         this.sw = this.w;
  208.         this.sh = this.h;
  209.  
  210.         switch(condition) {
  211.           case xmas.Entity.CONDITION._default:
  212.             this.sx = 0;
  213.           break;
  214.           case xmas.Entity.CONDITION._hover:
  215.             this.sx = this.w;
  216.           break;
  217.           case xmas.Entity.CONDITION._active:
  218.             this.sx = this.w * 2;
  219.           break;
  220.           case xmas.Entity.CONDITION._inactive:
  221.             this.sx = this.w * 3;
  222.           break;
  223.         }
  224.  
  225.       }
  226.     };  
  227.  
  228.     /**
  229.      * renders the image
  230.      * @param dw - int new width (optional)
  231.      * @param dh - int new height (optional)
  232.      */
  233.     this.render = function(dw, dh) {
  234.       if(this.isVisible() && this.img.isLoaded && this.img.inViewport) {
  235.         xmas.Draw.image(this.img, this.x, this.y, dw, dh);
  236.       }
  237.     };
  238.  
  239.     this._constructor(src, x, y, opacity);
  240.  
  241. }
  242.  
  243.  
  244. // inheritance
  245. xmas.Entity.Image.prototype   = new xmas.Entity();
  246. xmas.Entity.Image.constructor = xmas.Entity.Image;
  247. xmas.Entity.Image.Snowball.prototype   = new xmas.Entity.Image();
  248. xmas.Entity.Image.Snowball.constructor = xmas.Entity.Image.Snowball;
Advertisement
Add Comment
Please, Sign In to add comment