[client] add AnimationManager

This commit is contained in:
2019-11-14 16:51:05 +03:00
parent 1aaa69f2d7
commit 8c16b5ba90
5 changed files with 46 additions and 33 deletions

View File

@@ -20,6 +20,6 @@
"yaml": "1.3.0",
"orm": "2.1.0",
"haxe-crypto": "0.0.7",
"svg": "1.1.2"
"svg": "1.1.3"
}
}

View File

@@ -1,47 +1,62 @@
package ru.m.animate;
import flash.display.PixelSnapping;
import haxe.Timer;
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.PixelSnapping;
import haxe.Timer;
typedef Frame = {
var image:BitmapData;
var length:Int;
}
class Animate extends Bitmap {
class AnimateManager {
public var playing(default, default):Bool;
private static var timer:Timer;
private static var instances:Array<Animate> = [];
private var timer:Timer;
private var animations:Array<Animate>;
private static function init():Void {
if (timer == null) {
timer = new Timer(30);
timer.run = updateAll;
}
public function new() {
animations = [];
timer = new Timer(30);
timer.run = update;
}
private static function updateAll():Void {
for (instance in instances) {
if (instance.playing) {
instance.update();
public function update():Void {
if (playing) {
for (animation in animations) {
if (animation.playing) {
animation.update();
}
}
}
}
public var playing(default, set):Bool;
public function add(animate:Animate):Void {
animations.push(animate);
}
public function remove(animate:Animate):Void {
if (animations.indexOf(animate) > -1) {
animations.remove(animate);
}
}
}
class Animate extends Bitmap {
public var playing(default, default):Bool;
public var frames(default, set):Array<Frame>;
private var sequence:Array<BitmapData>;
private var index:Int;
@:provide private static var manager:AnimateManager;
public function new(?frames:Array<Frame>) {
super(null, PixelSnapping.AUTO, true);
this.frames = frames == null ? [] : frames;
init();
instances.push(this);
manager.add(this);
}
public function set_frames(value:Array<Frame>):Array<Frame> {
@@ -59,14 +74,7 @@ class Animate extends Bitmap {
return frames;
}
public function set_playing(value:Bool):Bool {
if (playing != value) {
playing = value;
}
return playing;
}
private function update():Void {
public function update():Void {
if (++index >= sequence.length) {
index = 0;
}
@@ -77,8 +85,6 @@ class Animate extends Bitmap {
}
public function dispose():Void {
if (instances.indexOf(this) > -1) {
instances.remove(this);
}
manager.remove(this);
}
}

View File

@@ -3,7 +3,6 @@ package ru.m.animate;
import promhx.Deferred;
import promhx.Promise;
class OnceAnimate extends Animate {
private var deferred:Deferred<Animate>;
@@ -14,13 +13,14 @@ class OnceAnimate extends Animate {
return deferred.promise();
}
override private function update():Void {
override public function update():Void {
super.update();
if (index == 0) {
playing = false;
if (deferred != null) {
deferred.resolve(this);
}
dispose();
}
}
}

View File

@@ -1,5 +1,6 @@
package ru.m.tankz;
import ru.m.animate.Animate.AnimateManager;
import flash.Lib;
import haxework.animate.FadeAnimate;
import haxework.animate.UnFadeAnimate;
@@ -49,6 +50,7 @@ class Init {
@:provide static var bus:IControlBus;
@:provide static var loaderManager:ILoaderManager;
@:provide static var updater:Updater;
@:provide static var animateManager:AnimateManager;
private static function buildConnection():IConnection<Request, Response> {
var host:String = CompilationOption.get("host");
@@ -75,6 +77,7 @@ class Init {
recordStorage = new RecordStorage();
soundManager = new SoundManager();
popupManager = new PopupManager();
animateManager = new AnimateManager();
popupManager.showAnimateFactory = function(v) return new UnFadeAnimate(v, 100);
popupManager.closeAnimateFactory = function(v) return new FadeAnimate(v, 100);

View File

@@ -40,6 +40,8 @@ class Render extends SpriteView implements IRender {
private var items:Map<Int, IRenderItem>;
private var paused:Bool;
@:provide private static var animateManager:AnimateManager;
public function new() {
super();
items = new Map();
@@ -119,12 +121,14 @@ class Render extends SpriteView implements IRender {
public function onGameEvent(event:GameEvent):Void {
switch event {
case START(start):
animateManager.playing = true;
gridSize = start.level.size;
content.addEventListener(Event.ENTER_FRAME, onEnterFrame);
case COMPLETE(_):
content.removeEventListener(Event.ENTER_FRAME, onEnterFrame);
case PAUSE(paused):
this.paused = paused;
this.paused = paused;
animateManager.playing = !paused;
case SPAWN(BRICK(bricks)):
drawBackground();
for (brick in bricks) {