[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", "yaml": "1.3.0",
"orm": "2.1.0", "orm": "2.1.0",
"haxe-crypto": "0.0.7", "haxe-crypto": "0.0.7",
"svg": "1.1.2" "svg": "1.1.3"
} }
} }

View File

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

View File

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

View File

@@ -1,5 +1,6 @@
package ru.m.tankz; package ru.m.tankz;
import ru.m.animate.Animate.AnimateManager;
import flash.Lib; import flash.Lib;
import haxework.animate.FadeAnimate; import haxework.animate.FadeAnimate;
import haxework.animate.UnFadeAnimate; import haxework.animate.UnFadeAnimate;
@@ -49,6 +50,7 @@ class Init {
@:provide static var bus:IControlBus; @:provide static var bus:IControlBus;
@:provide static var loaderManager:ILoaderManager; @:provide static var loaderManager:ILoaderManager;
@:provide static var updater:Updater; @:provide static var updater:Updater;
@:provide static var animateManager:AnimateManager;
private static function buildConnection():IConnection<Request, Response> { private static function buildConnection():IConnection<Request, Response> {
var host:String = CompilationOption.get("host"); var host:String = CompilationOption.get("host");
@@ -75,6 +77,7 @@ class Init {
recordStorage = new RecordStorage(); recordStorage = new RecordStorage();
soundManager = new SoundManager(); soundManager = new SoundManager();
popupManager = new PopupManager(); popupManager = new PopupManager();
animateManager = new AnimateManager();
popupManager.showAnimateFactory = function(v) return new UnFadeAnimate(v, 100); popupManager.showAnimateFactory = function(v) return new UnFadeAnimate(v, 100);
popupManager.closeAnimateFactory = function(v) return new FadeAnimate(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 items:Map<Int, IRenderItem>;
private var paused:Bool; private var paused:Bool;
@:provide private static var animateManager:AnimateManager;
public function new() { public function new() {
super(); super();
items = new Map(); items = new Map();
@@ -119,12 +121,14 @@ class Render extends SpriteView implements IRender {
public function onGameEvent(event:GameEvent):Void { public function onGameEvent(event:GameEvent):Void {
switch event { switch event {
case START(start): case START(start):
animateManager.playing = true;
gridSize = start.level.size; gridSize = start.level.size;
content.addEventListener(Event.ENTER_FRAME, onEnterFrame); content.addEventListener(Event.ENTER_FRAME, onEnterFrame);
case COMPLETE(_): case COMPLETE(_):
content.removeEventListener(Event.ENTER_FRAME, onEnterFrame); content.removeEventListener(Event.ENTER_FRAME, onEnterFrame);
case PAUSE(paused): case PAUSE(paused):
this.paused = paused; this.paused = paused;
animateManager.playing = !paused;
case SPAWN(BRICK(bricks)): case SPAWN(BRICK(bricks)):
drawBackground(); drawBackground();
for (brick in bricks) { for (brick in bricks) {