Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import nme.Assets;
- import nme.display.BitmapData;
- import nme.geom.Matrix;
- import nme.geom.ColorTransform;
- import ld.Rect;
- import ld.Game;
- import ld.Timer;
- using ld.Tween;
- class Entity extends Rect {
- public var drawDest:BitmapData;
- public var timer:Timer;
- public var ct:ColorTransform;
- public var dead = false;
- public var screenX(getScreenX, setScreenX):Float;
- public var screenY(getScreenY, setScreenY):Float;
- public var last:Rect;
- public var velocity:Rect;
- public var maxvelocity:Rect;
- public var accel:Rect;
- public var drag:Rect;
- public var scrollFactor:Rect;
- public var separatePriority:Float = 0;
- public var frame:Int;
- public var frames:Array<BitmapData>;
- public var anim:String;
- public var anims:Hash<Dynamic>;
- public var animFrame:Int = 0;
- private var _matrix:Matrix;
- public function new(w=0, h=0) {
- super(0,0,w,h);
- drawDest = Game.pixels;
- timer = new Timer();
- _matrix = new Matrix();
- ct = new ColorTransform();
- anims = new Hash<Dynamic>();
- last = new Rect();
- velocity = new Rect();
- maxvelocity = new Rect(10e9, 10e9);
- accel = new Rect();
- drag = new Rect();
- scrollFactor = new Rect(1, 1);
- /* Init animation timer */
- timer.delay(function() {
- if (anim != null) {
- var a = anims.get(anim);
- if (++animFrame >= a.frames.length) {
- animFrame = 0;
- }
- frame = a.frames[animFrame];
- timer.again(a.rate);
- } else {
- timer.again(.5);
- }
- }, 0);
- }
- public function getScreenX() { return x - Main.camera.x * scrollFactor.x; }
- public function setScreenX(param:Float) {
- return x = param + Main.camera.x * scrollFactor.x ;
- }
- public function getScreenY() { return y - Main.camera.y * scrollFactor.y; }
- public function setScreenY(param:Float) {
- return y = param + Main.camera.y * scrollFactor.y ;
- }
- public function addAnimation(name, frames, fps) {
- var a = { frames: frames, rate: 1/fps };
- anims.set(name, a);
- anim = name;
- }
- public function overlaps(e:Entity) {
- return (!dead && !e.dead && overlapsRect(e));
- }
- public function onOverlap(e:Entity) {
- if (overlapsRect(e)) {
- separate(e);
- }
- }
- public function separateX(e:Entity) {
- if (separatePriority >= e.separatePriority) {
- if (e.last.middleX < last.middleX) {
- e.right = left;
- } else {
- e.left = right;
- }
- e.velocity.x = 0;
- } else {
- e.separateX(this);
- }
- }
- public function separateY(e:Entity) {
- if (separatePriority >= e.separatePriority) {
- if (e.last.middleY < last.middleY) {
- e.bottom = top;
- } else {
- e.top = bottom;
- }
- e.velocity.y = 0;
- } else {
- e.separateY(this);
- }
- }
- public function separate(e:Entity) {
- var diffx = Math.abs(last.middleX - e.last.middleX);
- var diffy = Math.abs(last.middleY - e.last.middleY);
- if (diffx > diffy) {
- separateX(e);
- } else {
- separateY(e);
- }
- }
- public function loadImage(asset, w=0) {
- var img = Assets.getBitmapData(asset);
- var pnt = new Rect();
- frames = new Array<BitmapData>();
- if (w == 0) {
- frames.push(img);
- } else {
- for (i in 0...Std.int(img.width / w)) {
- var fr = new BitmapData(w, img.height, true);
- fr.fillRect(fr.rect, 0x0);
- pnt.x = -i * fr.width;
- fr.copyPixels(img, img.rect, pnt.flashPoint);
- frames.push(fr);
- }
- }
- if (width == 0) {
- width = frames[0].width;
- height = frames[0].height;
- }
- frame = 0;
- }
- public function updateMovement() {
- /* X axis */
- velocity.x += accel.x * Game.elapsed;
- x += velocity.x * Game.elapsed;
- if (Math.abs(velocity.x += accel.x * Game.elapsed) > maxvelocity.x) {
- velocity.x = maxvelocity.x * (velocity.x < 0 ? -1 : 1);
- }
- if (velocity.x != 0 && accel.x == 0 && drag.x != 0) {
- var drag = drag.x * Game.elapsed;
- if (drag > Math.abs(velocity.x)) {
- velocity.x = 0;
- } else {
- velocity.x -= drag * (velocity.x < 0 ? -1 : 1);
- }
- }
- /* Y axis */
- velocity.y += accel.y * Game.elapsed;
- y += velocity.y * Game.elapsed;
- if (Math.abs(velocity.y += accel.y * Game.elapsed) > maxvelocity.y) {
- velocity.y = maxvelocity.y * (velocity.y < 0 ? -1 : 1);
- }
- if (velocity.y != 0 && accel.y == 0 && drag.y != 0) {
- var drag = drag.y * Game.elapsed;
- if (drag > Math.abs(velocity.y)) {
- velocity.y = 0;
- } else {
- velocity.y -= drag * (velocity.y < 0 ? -1 : 1);
- }
- }
- }
- public function update() {
- this.cloneRect(last);
- updateMovement();
- timer.update(Game.elapsed);
- }
- public function draw() {
- if (frames == null) return;
- _matrix.identity();
- _matrix.translate(screenX, screenY);
- _matrix.tx = Math.round(_matrix.tx);
- _matrix.ty = Math.round(_matrix.ty);
- drawDest.draw(frames[frame], _matrix, ct);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment