[render] added animates
This commit is contained in:
62
src/client/haxe/ru/m/animate/Animate.hx
Normal file
62
src/client/haxe/ru/m/animate/Animate.hx
Normal file
@@ -0,0 +1,62 @@
|
||||
package ru.m.animate;
|
||||
|
||||
import flash.display.PixelSnapping;
|
||||
import haxe.Timer;
|
||||
import flash.display.Bitmap;
|
||||
import flash.display.BitmapData;
|
||||
|
||||
|
||||
class Animate extends Bitmap {
|
||||
|
||||
private static var timer:Timer;
|
||||
private static var instances:Array<Animate> = [];
|
||||
|
||||
private static function init():Void {
|
||||
if (timer == null) {
|
||||
timer = new Timer(30);
|
||||
timer.run = updateAll;
|
||||
}
|
||||
}
|
||||
|
||||
private static function updateAll():Void {
|
||||
for (instance in instances) {
|
||||
if (instance.playing) {
|
||||
instance.update();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public var playing(default, set):Bool;
|
||||
private var frames:Array<BitmapData>;
|
||||
private var index:Int;
|
||||
|
||||
public function new(frames:Array<BitmapData>) {
|
||||
super(frames[0], PixelSnapping.AUTO, true);
|
||||
this.frames = frames;
|
||||
init();
|
||||
instances.push(this);
|
||||
}
|
||||
|
||||
public function set_playing(value:Bool):Bool {
|
||||
if (playing != value) {
|
||||
playing = value;
|
||||
}
|
||||
return playing;
|
||||
}
|
||||
|
||||
private function update():Void {
|
||||
if (++index >= frames.length) {
|
||||
index = 0;
|
||||
}
|
||||
var nextBitmapData = frames[index];
|
||||
x -= (nextBitmapData.width - bitmapData.width) / 2;
|
||||
y -= (nextBitmapData.height - bitmapData.height) / 2;
|
||||
bitmapData = nextBitmapData;
|
||||
}
|
||||
|
||||
public function dispose():Void {
|
||||
if (instances.indexOf(this) > -1) {
|
||||
instances.remove(this);
|
||||
}
|
||||
}
|
||||
}
|
||||
31
src/client/haxe/ru/m/animate/OnceAnimate.hx
Normal file
31
src/client/haxe/ru/m/animate/OnceAnimate.hx
Normal file
@@ -0,0 +1,31 @@
|
||||
package ru.m.animate;
|
||||
|
||||
import promhx.Deferred;
|
||||
import flash.display.BitmapData;
|
||||
import promhx.Promise;
|
||||
|
||||
|
||||
class OnceAnimate extends Animate {
|
||||
|
||||
private var deferred:Deferred<Animate>;
|
||||
|
||||
public function new(frames:Array<BitmapData>) {
|
||||
super(frames);
|
||||
}
|
||||
|
||||
public function play():Promise<Animate> {
|
||||
deferred = new Deferred();
|
||||
playing = true;
|
||||
return deferred.promise();
|
||||
}
|
||||
|
||||
override private function update():Void {
|
||||
super.update();
|
||||
if (index == 0) {
|
||||
playing = false;
|
||||
if (deferred != null) {
|
||||
deferred.resolve(this);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user