Guest User

Untitled

a guest
Aug 24th, 2013
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Haxe 5.10 KB | None | 0 0
  1.  
  2. import nme.Assets;
  3. import nme.display.BitmapData;
  4. import nme.geom.Matrix;
  5. import nme.geom.ColorTransform;
  6. import ld.Rect;
  7. import ld.Game;
  8. import ld.Timer;
  9.  
  10. using ld.Tween;
  11.  
  12.  
  13. class Entity extends Rect {
  14.  
  15.  
  16.   public var drawDest:BitmapData;
  17.   public var timer:Timer;
  18.   public var ct:ColorTransform;
  19.   public var dead = false;
  20.  
  21.   public var screenX(getScreenX, setScreenX):Float;
  22.   public var screenY(getScreenY, setScreenY):Float;
  23.  
  24.   public var last:Rect;
  25.   public var velocity:Rect;
  26.   public var maxvelocity:Rect;
  27.   public var accel:Rect;
  28.   public var drag:Rect;
  29.   public var scrollFactor:Rect;
  30.  
  31.   public var separatePriority:Float = 0;
  32.  
  33.   public var frame:Int;
  34.   public var frames:Array<BitmapData>;
  35.  
  36.   public var anim:String;
  37.   public var anims:Hash<Dynamic>;
  38.   public var animFrame:Int = 0;
  39.  
  40.   private var _matrix:Matrix;
  41.  
  42.  
  43.   public function new(w=0, h=0) {
  44.     super(0,0,w,h);
  45.     drawDest = Game.pixels;
  46.  
  47.     timer = new Timer();
  48.     _matrix = new Matrix();
  49.     ct = new ColorTransform();
  50.     anims = new Hash<Dynamic>();
  51.  
  52.     last = new Rect();
  53.     velocity = new Rect();
  54.     maxvelocity = new Rect(10e9, 10e9);
  55.     accel = new Rect();
  56.     drag = new Rect();
  57.     scrollFactor = new Rect(1, 1);
  58.  
  59.     /* Init animation timer */
  60.     timer.delay(function() {
  61.       if (anim != null) {
  62.         var a = anims.get(anim);
  63.         if (++animFrame >= a.frames.length) {
  64.           animFrame = 0;
  65.         }
  66.         frame = a.frames[animFrame];
  67.         timer.again(a.rate);
  68.       } else {
  69.         timer.again(.5);
  70.       }
  71.     }, 0);
  72.   }
  73.  
  74.  
  75.   public function getScreenX() { return x - Main.camera.x * scrollFactor.x; }
  76.   public function setScreenX(param:Float) {
  77.     return x = param + Main.camera.x * scrollFactor.x ;
  78.   }
  79.  
  80.  
  81.   public function getScreenY() { return y - Main.camera.y * scrollFactor.y; }
  82.   public function setScreenY(param:Float) {
  83.     return y = param + Main.camera.y * scrollFactor.y ;
  84.   }
  85.  
  86.  
  87.   public function addAnimation(name, frames, fps) {
  88.     var a = { frames: frames, rate: 1/fps };
  89.     anims.set(name, a);
  90.     anim = name;
  91.   }
  92.  
  93.  
  94.   public function overlaps(e:Entity) {
  95.     return (!dead && !e.dead && overlapsRect(e));
  96.   }
  97.  
  98.  
  99.   public function onOverlap(e:Entity) {
  100.     if (overlapsRect(e)) {
  101.       separate(e);
  102.     }
  103.   }
  104.  
  105.  
  106.   public function separateX(e:Entity) {
  107.     if (separatePriority >= e.separatePriority) {
  108.       if (e.last.middleX < last.middleX) {
  109.         e.right = left;
  110.       } else {
  111.         e.left = right;
  112.       }
  113.       e.velocity.x = 0;
  114.     } else {
  115.       e.separateX(this);
  116.     }
  117.   }
  118.  
  119.  
  120.   public function separateY(e:Entity) {
  121.     if (separatePriority >= e.separatePriority) {
  122.       if (e.last.middleY < last.middleY) {
  123.         e.bottom = top;
  124.       } else {
  125.         e.top = bottom;
  126.       }
  127.       e.velocity.y = 0;
  128.     } else {
  129.       e.separateY(this);
  130.     }
  131.   }
  132.  
  133.  
  134.   public function separate(e:Entity) {
  135.     var diffx = Math.abs(last.middleX - e.last.middleX);
  136.     var diffy = Math.abs(last.middleY - e.last.middleY);
  137.     if (diffx > diffy) {
  138.       separateX(e);
  139.     } else {
  140.       separateY(e);
  141.     }
  142.   }
  143.  
  144.  
  145.   public function loadImage(asset, w=0) {
  146.     var img = Assets.getBitmapData(asset);
  147.     var pnt = new Rect();
  148.     frames = new Array<BitmapData>();
  149.     if (w == 0) {
  150.       frames.push(img);
  151.     } else {
  152.       for (i in 0...Std.int(img.width / w)) {
  153.         var fr = new BitmapData(w, img.height, true);
  154.         fr.fillRect(fr.rect, 0x0);
  155.         pnt.x = -i * fr.width;
  156.         fr.copyPixels(img, img.rect, pnt.flashPoint);
  157.         frames.push(fr);
  158.       }
  159.     }
  160.     if (width == 0) {
  161.       width = frames[0].width;
  162.       height = frames[0].height;
  163.     }
  164.     frame = 0;
  165.   }
  166.  
  167.  
  168.   public function updateMovement() {
  169.     /* X axis */
  170.     velocity.x += accel.x * Game.elapsed;
  171.     x += velocity.x * Game.elapsed;
  172.     if (Math.abs(velocity.x += accel.x * Game.elapsed) > maxvelocity.x) {
  173.       velocity.x = maxvelocity.x * (velocity.x < 0 ? -1 : 1);
  174.     }
  175.     if (velocity.x != 0 && accel.x == 0 && drag.x != 0) {
  176.       var drag = drag.x * Game.elapsed;
  177.       if (drag > Math.abs(velocity.x)) {
  178.         velocity.x = 0;
  179.       } else {
  180.         velocity.x -= drag * (velocity.x < 0 ? -1 : 1);
  181.       }
  182.     }
  183.     /* Y axis */
  184.     velocity.y += accel.y * Game.elapsed;
  185.     y += velocity.y * Game.elapsed;
  186.     if (Math.abs(velocity.y += accel.y * Game.elapsed) > maxvelocity.y) {
  187.       velocity.y = maxvelocity.y * (velocity.y < 0 ? -1 : 1);
  188.     }
  189.     if (velocity.y != 0 && accel.y == 0 && drag.y != 0) {
  190.       var drag = drag.y * Game.elapsed;
  191.       if (drag > Math.abs(velocity.y)) {
  192.         velocity.y = 0;
  193.       } else {
  194.         velocity.y -= drag * (velocity.y < 0 ? -1 : 1);
  195.       }
  196.     }
  197.   }
  198.  
  199.  
  200.   public function update() {
  201.     this.cloneRect(last);
  202.     updateMovement();
  203.     timer.update(Game.elapsed);
  204.   }
  205.  
  206.  
  207.   public function draw() {
  208.     if (frames == null) return;
  209.     _matrix.identity();
  210.     _matrix.translate(screenX, screenY);
  211.     _matrix.tx = Math.round(_matrix.tx);
  212.     _matrix.ty = Math.round(_matrix.ty);
  213.     drawDest.draw(frames[frame], _matrix, ct);
  214.   }
  215.  
  216.  
  217. }
Advertisement
Add Comment
Please, Sign In to add comment