rodrigolopezpeker

Genome2D > GSpriteTile

Dec 26th, 2013
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * Code by rodrigolopezpeker (aka 7interactive™) on 12/26/13 1:25 PM.
  3.  */
  4. package {
  5. import com.genome2d.components.GCamera;
  6. import com.genome2d.components.renderables.GSprite;
  7. import com.genome2d.context.GContext;
  8. import com.genome2d.core.GNode;
  9. import com.genome2d.g2d;
  10. import com.genome2d.textures.GTexture;
  11.  
  12. import flash.geom.Rectangle;
  13.  
  14. use namespace g2d;
  15.  
  16. /**
  17.  * Usage:
  18.  * use GSpriteTile::scaleX instead of node.transform.scaleX, is a quick workaround to match the transformations.
  19.  *
  20.  * grass = GNodeFactory.createNodeWithComponent(GSpriteTile) as GSpriteTile ;
  21.  * grass.textureId='grass';
  22.  * Genome2D.getInstance().root.addChild(grass.node);
  23.  * grass.scaleX = 2 ;
  24.  * grass.width = 200 ;
  25.  * grass.height = 200 ;
  26.  *
  27.  * You can play with the offsetX/offsetY values to create a scroll effect, or to offset
  28.  * the position of the uv to your needs, it uses PIXELS values, so should be easier to
  29.  * get an accurate offset :)
  30.  *
  31.  */
  32. public class GSpriteTile extends GSprite {
  33.     private var _tw:int;
  34.     private var _th:int;
  35.     private var _width:Number;
  36.     private var _height:Number;
  37.     private var _invalidateSize:Boolean;
  38.     private var _scaleX:Number=1;
  39.     private var _scaleY:Number=1;
  40.     private var _offsetX:Number = 0 ;
  41.     private var _offsetY:Number = 0 ;
  42.  
  43.     /**
  44.      * Constructor.
  45.      * @param pNode
  46.      */
  47.     public function GSpriteTile(pNode:GNode) {
  48.         super(pNode);
  49.     }
  50.  
  51.     override public function render(p_context:GContext, p_camera:GCamera, p_maskRect:Rectangle):void {
  52.         if(_invalidateSize){
  53.             _invalidateSize = false ;
  54.             resize() ;
  55.         }
  56.         super.render(p_context, p_camera, p_maskRect);
  57.     }
  58.  
  59.     private function resize():void {
  60.         var ratioX:Number = _width/_tw;
  61.         var ratioY:Number = _height/_th ;
  62.         cTexture.nUvScaleX = ratioX ;
  63.         cTexture.nUvScaleY = ratioY ;
  64.         cNode.cTransform.setScale( ratioX*_scaleX, ratioY*_scaleY) ;
  65.     }
  66.  
  67.     override public function set textureId(p_value:String):void {
  68.         super.textureId = p_value;
  69.         _width = _tw = cTexture.width ;
  70.         _height = _th = cTexture.height ;
  71.     }
  72.  
  73.     override public function setTexture(p_texture:GTexture):void {
  74.         super.setTexture(p_texture);
  75.         _width = _tw = cTexture.width ;
  76.         _height = _th = cTexture.height ;
  77.     }
  78.  
  79.     public function get width():Number {
  80.         return _width;
  81.     }
  82.  
  83.     public function set width(value:Number):void {
  84.         if(value==_width || value < 0) return ;
  85.         _width = value;
  86.         _invalidateSize = true
  87.     }
  88.  
  89.     public function get height():Number {
  90.         return _height;
  91.     }
  92.  
  93.     public function set height(value:Number):void {
  94.         if(value==_height || value < 0) return ;
  95.         _height = value;
  96.         _invalidateSize = true
  97.     }
  98.  
  99.     public function get scaleX():Number {
  100.         return _scaleX;
  101.     }
  102.  
  103.     public function set scaleX(value:Number):void {
  104.         if(value==_scaleX) return ;
  105.         _scaleX = value;
  106.         _invalidateSize = true
  107.     }
  108.  
  109.     public function get scaleY():Number {
  110.         return _scaleY;
  111.     }
  112.  
  113.     public function set scaleY(value:Number):void {
  114.         if(value==_scaleY) return ;
  115.         _scaleY = value;
  116.         _invalidateSize = true
  117.     }
  118.  
  119.     public function get offsetX():Number {
  120.         return _offsetX;
  121.     }
  122.  
  123.     public function set offsetX(value:Number):void {
  124.         if(value==_offsetX) return ;
  125.         _offsetX = value;
  126.         cTexture.nUvX = _offsetX/_tw ;
  127.     }
  128.  
  129.     public function get offsetY():Number {
  130.         return _offsetY;
  131.     }
  132.  
  133.     public function set offsetY(value:Number):void {
  134.         if(value==_offsetY) return ;
  135.         _offsetY = value;
  136.         cTexture.nUvY = _offsetY/_th ;
  137.     }
  138. }
  139. }
Advertisement
Add Comment
Please, Sign In to add comment