Wolfos

Untitled

Jun 6th, 2014
299
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Haxe 2.03 KB | None | 0 0
  1. package tools;
  2. import com.haxepunk.graphics.Image;
  3. import com.haxepunk.HXP;
  4. import flash.geom.Point;
  5. import flash.geom.Rectangle;
  6.  
  7. /**
  8.  * ...
  9.  * @author Robin van Ee
  10.  * http://rvanee.nl
  11.  *
  12.  *
  13.  * Port of WolfEngine's SpriteRenderer component to HaxePunk
  14.  */
  15.  
  16. class WolfSprite
  17. {
  18.     public var scale (default, set) : Point;
  19.    
  20.     function set_scale(val : Point) : Point
  21.     {
  22.         scale = val;
  23.         Render();
  24.         return scale;
  25.     }
  26.    
  27.     public var frame (default, set) : Int = 0;
  28.    
  29.     function set_frame(f : Int) : Int
  30.     {
  31.         frame = f;
  32.         Render();
  33.         return frame;
  34.     }
  35.    
  36.     public var image : Image;
  37.    
  38.     public var frameWidth : Int;
  39.     public var frameHeight : Int;
  40.    
  41.     public var numFrames : Int;
  42.    
  43.     public var framesPerSecond : Int;
  44.    
  45.     private var clip : Array<Image>;
  46.    
  47.     private var sheetOffset : Int;
  48.    
  49.     private var _filename : String;
  50.    
  51.     private var realFrame : Float = 0;
  52.    
  53.    
  54.     public function new(filename: String, fWidth : Int, fHeight : Int, ?offset : Int = 0, ?nFrames = HXP.INT_MAX_VALUE, ?FPS = 60)
  55.     {
  56.         _filename = filename;
  57.         var sheet = new Image(filename);
  58.        
  59.         frameWidth = fWidth;
  60.         frameHeight = fHeight;
  61.         numFrames = nFrames;
  62.         framesPerSecond = FPS;
  63.        
  64.         if (offset != null) sheetOffset = offset;
  65.        
  66.         clip = new Array<Image>();
  67.        
  68.         for(y in 0...cast(sheet.height / frameHeight))
  69.         {
  70.             for(x in 0...cast(sheet.width / frameWidth))
  71.             {
  72.                 var tempRect : Rectangle = new Rectangle(x * frameWidth + (sheetOffset * x), y * frameHeight + (sheetOffset * y), frameWidth, frameHeight);
  73.                 var tempImage = new Image(filename, tempRect);
  74.                 clip.push(tempImage);
  75.             }
  76.         }
  77.        
  78.         scale = new Point(1, 1);
  79.        
  80.         sheet = null;
  81.        
  82.         Render();
  83.     }
  84.    
  85.     public function play()
  86.     {
  87.         realFrame += framesPerSecond * HXP.elapsed;
  88.         if (realFrame >= numFrames || realFrame >= clip.length) realFrame = 0;
  89.        
  90.         frame = cast(realFrame);
  91.         Render();
  92.     }
  93.    
  94.     private function Render()
  95.     {
  96.         image = clip[frame];
  97.         image.scaleX = scale.x;
  98.         image.scaleY = scale.y;
  99.         image.centerOrigin();
  100.     }
  101.    
  102. }
Advertisement
Add Comment
Please, Sign In to add comment