From 52a4e44e474767fa54054c9c13afea79dc4635e3 Mon Sep 17 00:00:00 2001 From: shmyga Date: Fri, 16 Feb 2018 17:57:43 +0300 Subject: [PATCH] [client] added AniamteBundle --- src/client/haxe/ru/m/animate/Animate.hx | 27 ++++++-- src/client/haxe/ru/m/animate/OnceAnimate.hx | 5 -- .../haxe/ru/m/tankz/render/AnimateBundle.hx | 61 +++++++++++++++++ src/client/haxe/ru/m/tankz/render/Render.hx | 64 +++++------------- .../haxe/ru/m/tankz/render/RenderItem.hx | 43 +++++------- .../resources/image/eagle/eagle-protected.png | Bin 0 -> 1800 bytes src/common/haxe/ru/m/tankz/core/Eagle.hx | 4 +- src/common/haxe/ru/m/tankz/engine/Engine.hx | 4 +- src/common/haxe/ru/m/tankz/game/Game.hx | 5 ++ src/common/haxe/ru/m/tankz/game/Team.hx | 1 + 10 files changed, 126 insertions(+), 88 deletions(-) create mode 100644 src/client/haxe/ru/m/tankz/render/AnimateBundle.hx create mode 100644 src/client/resources/image/eagle/eagle-protected.png diff --git a/src/client/haxe/ru/m/animate/Animate.hx b/src/client/haxe/ru/m/animate/Animate.hx index 6cb4c0e..2c27205 100644 --- a/src/client/haxe/ru/m/animate/Animate.hx +++ b/src/client/haxe/ru/m/animate/Animate.hx @@ -6,6 +6,11 @@ import flash.display.Bitmap; import flash.display.BitmapData; +typedef Frame = { + var image:BitmapData; + var length:Int; +} + class Animate extends Bitmap { private static var timer:Timer; @@ -27,21 +32,29 @@ class Animate extends Bitmap { } public var playing(default, set):Bool; - public var frames(default, set):Array; + public var frames(default, set):Array; + + private var sequence:Array; private var index:Int; - public function new(?frames:Array) { + public function new(?frames:Array) { super(null, PixelSnapping.AUTO, true); this.frames = frames == null ? [] : frames; init(); instances.push(this); } - public function set_frames(value:Array):Array { + public function set_frames(value:Array):Array { + sequence = []; + index = 0; if (value != null) { frames = value; - bitmapData = frames[0]; - index = 0; + for (frame in frames) { + sequence = sequence.concat([for (i in 0...frame.length) frame.image]); + } + bitmapData = sequence[0]; + } else { + bitmapData = null; } return frames; } @@ -54,10 +67,10 @@ class Animate extends Bitmap { } private function update():Void { - if (++index >= frames.length) { + if (++index >= sequence.length) { index = 0; } - var nextBitmapData = frames[index]; + var nextBitmapData = sequence[index]; x -= (nextBitmapData.width - bitmapData.width) / 2; y -= (nextBitmapData.height - bitmapData.height) / 2; bitmapData = nextBitmapData; diff --git a/src/client/haxe/ru/m/animate/OnceAnimate.hx b/src/client/haxe/ru/m/animate/OnceAnimate.hx index 0d7b8c3..088ebff 100644 --- a/src/client/haxe/ru/m/animate/OnceAnimate.hx +++ b/src/client/haxe/ru/m/animate/OnceAnimate.hx @@ -1,7 +1,6 @@ package ru.m.animate; import promhx.Deferred; -import flash.display.BitmapData; import promhx.Promise; @@ -9,10 +8,6 @@ class OnceAnimate extends Animate { private var deferred:Deferred; - public function new(frames:Array) { - super(frames); - } - public function play():Promise { deferred = new Deferred(); playing = true; diff --git a/src/client/haxe/ru/m/tankz/render/AnimateBundle.hx b/src/client/haxe/ru/m/tankz/render/AnimateBundle.hx new file mode 100644 index 0000000..07bf73e --- /dev/null +++ b/src/client/haxe/ru/m/tankz/render/AnimateBundle.hx @@ -0,0 +1,61 @@ +package ru.m.tankz.render; + +import ru.m.draw.BitmapUtil; +import ru.m.draw.Color; +import flash.display.BitmapData; +import openfl.Assets; +import ru.m.animate.Animate; +import ru.m.animate.OnceAnimate; +import ru.m.tankz.core.Tank; +import ru.m.tankz.Type; + + +class AnimateBundle { + + private static function buildAnimate(template:String, sequence:Array):OnceAnimate { + var frames = [for (i in sequence) ({ + image: Assets.getBitmapData(StringTools.replace(template, '%index%', Std.string(i))), + length: 1 + })]; + return new OnceAnimate(frames); + } + + public static function tankSpawn():OnceAnimate { + return buildAnimate('resources/images/tank/appear/appear-%index%.png', [ + 0, 1, 2, 3, 3, 4, 5, 5, 6 + ]); + } + + public static function tankBoom():OnceAnimate { + return buildAnimate('resources/images/tank/kaboom/kaboom-%index%.png', [ + 0, 1, 2, 3, 4, 4, 4, 1, 4, 4, 7, 7, 8, 9, 9 + ]); + } + + public static function bulletBoom():OnceAnimate { + return buildAnimate('resources/images/bullet/boom/boom-%index%.png', [ + 0, 1, 1, 0, 0, 1 + ]); + } + + public static function tankProtect():Animate { + return new Animate([for (i in 0...2) ({ + image: Assets.getBitmapData('resources/images/tank/protect/protect-${i}.png'), + length: 5 + })]); + } + + public static function tankFrames(tank:Tank):Array { + var colors:Array = [tank.color, tank.bonus ? 0xff00aa : tank.color]; + return [for (i in 0...2) ({ + image: BitmapUtil.colorize(Assets.getBitmapData('resources/image/tank/${tank.config.skin}-${i}.png'), colors[i]), + length: 3 + })]; + } + + public static function bonusFrames(type:BonusType):Array { + var image = Assets.getBitmapData('resources/image/bonus/${type}.png'); + var empty = new BitmapData(image.width, image.height, true, 0x00000000); + return [{image: image, length: 15}, {image: empty, length: 15}]; + } +} diff --git a/src/client/haxe/ru/m/tankz/render/Render.hx b/src/client/haxe/ru/m/tankz/render/Render.hx index ac719ca..a5ffb53 100755 --- a/src/client/haxe/ru/m/tankz/render/Render.hx +++ b/src/client/haxe/ru/m/tankz/render/Render.hx @@ -1,15 +1,15 @@ package ru.m.tankz.render; -import ru.m.geom.Point; -import openfl.Assets; -import ru.m.animate.OnceAnimate; import flash.display.DisplayObjectContainer; -import ru.m.tankz.core.EntityType; -import ru.m.tankz.render.RenderItem; -import ru.m.tankz.engine.Engine; -import flash.display.Sprite; import flash.display.Graphics; +import flash.display.Sprite; import haxework.gui.SpriteView; +import ru.m.animate.Animate; +import ru.m.animate.OnceAnimate; +import ru.m.geom.Point; +import ru.m.tankz.core.EntityType; +import ru.m.tankz.engine.Engine; +import ru.m.tankz.render.RenderItem; class Render extends SpriteView implements EngineListener { @@ -105,7 +105,7 @@ class Render extends SpriteView implements EngineListener { items.set(tank.key, item); entryLayer.addChild(item.view); item.update(); - playTankSpawn(tank.rect.center); + playAnimate(tank.rect.center, AnimateBundle.tankSpawn()); case EntityType.BULLET(bullet): var item = new BulletItem(bullet); items.set(bullet.key, item); @@ -128,7 +128,9 @@ class Render extends SpriteView implements EngineListener { public function onCollision(entity:EntityType, with:EntityType):Void { switch [entity, with] { case [EntityType.BULLET(_), EntityType.EAGLE(eagle)]: - playTankBoom(eagle.rect.center); + if (eagle.death) { + playAnimate(eagle.rect.center, AnimateBundle.tankBoom()); + } case _: } } @@ -139,13 +141,14 @@ class Render extends SpriteView implements EngineListener { if (items.exists(tank.key)) { entryLayer.removeChild(items.get(tank.key).view); items.remove(tank.key); - playTankBoom(tank.rect.center); + playAnimate(tank.rect.center, AnimateBundle.tankBoom()); } case EntityType.BULLET(bullet): if (items.exists(bullet.key)) { entryLayer.removeChild(items.get(bullet.key).view); items.remove(bullet.key); - playBulletBoom(bullet.rect.center.add(new Point(bullet.rect.width * bullet.rect.direction.x, bullet.rect.height * bullet.rect.direction.y))); + var point = bullet.rect.center.add(new Point(bullet.rect.width * bullet.rect.direction.x, bullet.rect.height * bullet.rect.direction.y)); + playAnimate(point, AnimateBundle.bulletBoom()); } case EntityType.BONUS(bonus): if (items.exists(bonus.key)) { @@ -156,46 +159,11 @@ class Render extends SpriteView implements EngineListener { } } - private function playTankSpawn(point:Point):Void { - var arr = [ - 0, 1, 2, 3, 3, 4, 5, 5, 6 - ]; - var frames = [for (i in arr) Assets.getBitmapData('resources/images/tank/appear/appear-${i}.png')]; - var animate = new OnceAnimate(frames); + private function playAnimate(point:Point, animate:OnceAnimate):Void { animate.x = point.x - animate.width / 2; animate.y = point.y - animate.height / 2; upperLayer.addChild(animate); - animate.play().then(function(animate) { - upperLayer.removeChild(animate); - animate.dispose(); - }); - } - - private function playBulletBoom(point:Point):Void { - var arr = [ - 0, 1, 1, 0, 0, 1 - ]; - var frames = [for (i in arr) Assets.getBitmapData('resources/images/bullet/boom/boom-${i}.png')]; - var animate = new OnceAnimate(frames); - animate.x = point.x - animate.width / 2; - animate.y = point.y - animate.height / 2; - upperLayer.addChild(animate); - animate.play().then(function(animate) { - upperLayer.removeChild(animate); - animate.dispose(); - }); - } - - private function playTankBoom(point:Point):Void { - var arr = [ - 0, 1, 2, 3, 4, 4, 4, 1, 4, 4, 7, 7, 8, 9, 9 - ]; - var frames = [for (i in arr) Assets.getBitmapData('resources/images/tank/kaboom/kaboom-${i}.png')]; - var animate = new OnceAnimate(frames); - animate.x = point.x - animate.width / 2; - animate.y = point.y - animate.height / 2; - upperLayer.addChild(animate); - animate.play().then(function(animate) { + animate.play().then(function(animate:Animate):Void { upperLayer.removeChild(animate); animate.dispose(); }); diff --git a/src/client/haxe/ru/m/tankz/render/RenderItem.hx b/src/client/haxe/ru/m/tankz/render/RenderItem.hx index 6cb305a..1107be7 100644 --- a/src/client/haxe/ru/m/tankz/render/RenderItem.hx +++ b/src/client/haxe/ru/m/tankz/render/RenderItem.hx @@ -1,22 +1,19 @@ package ru.m.tankz.render; -import ru.m.tankz.Type.BrickType; -import openfl.display.BitmapData; -import ru.m.draw.Color; -import ru.m.tankz.core.Bonus; import flash.display.Bitmap; import flash.display.DisplayObject; import flash.display.Shape; import flash.display.Sprite; import openfl.Assets; import ru.m.animate.Animate; -import ru.m.draw.BitmapUtil; import ru.m.geom.Direction; import ru.m.geom.Rectangle; +import ru.m.tankz.core.Bonus; import ru.m.tankz.core.Bullet; import ru.m.tankz.core.Eagle; import ru.m.tankz.core.Tank; import ru.m.tankz.map.Brick; +import ru.m.tankz.Type.BrickType; typedef TRectangle = { @@ -96,8 +93,6 @@ class BrickItem extends BitmapItem { } } - - override private function getImage():String { return 'resources/image/map/${value.config.type}.png'; } @@ -112,9 +107,10 @@ class BrickAnimateItem extends AnimateItem { } override public function redraw():Void { - var frame0 = Assets.getBitmapData('resources/image/map/${value.config.type}-0.png'); - var frame1 = Assets.getBitmapData('resources/image/map/${value.config.type}-1.png'); - view.frames = [for (i in 0...15) frame0].concat([for (i in 0...15) frame1]); + view.frames = [for (i in 0...2) ({ + image: Assets.getBitmapData('resources/image/map/${value.config.type}-${i}.png'), + length: 15 + })]; view.playing = true; } } @@ -193,25 +189,17 @@ class TankItem extends RenderItem { view = new Sprite(); tankView = new Animate(); view.addChild(tankView); - protectView = new Animate( - [for (i in 0...5) Assets.getBitmapData('resources/images/tank/protect/protect-0.png')].concat( - [for (i in 0...5) Assets.getBitmapData('resources/images/tank/protect/protect-1.png')]) - ); + protectView = AnimateBundle.tankProtect(); protectView.visible = false; view.addChild(protectView); redraw(); } override public function redraw():Void { - var colors:Array = [value.color, value.bonus ? 0xff00aa : value.color]; - var frames = [for (frame in getFrames()) BitmapUtil.colorize(Assets.getBitmapData(frame), colors.shift())]; - tankView.frames = [ - frames[0], frames[0], frames[0], - frames[1], frames[1], frames[1], - ]; + tankView.frames = AnimateBundle.tankFrames(value); if (value.protect.active) { - protectView.x = (tankView.frames[0].width - protectView.frames[0].width) / 2; - protectView.y = (tankView.frames[0].height - protectView.frames[0].height) / 2; + protectView.x = (tankView.frames[0].image.width - protectView.frames[0].image.width) / 2; + protectView.y = (tankView.frames[0].image.height - protectView.frames[0].image.height) / 2; protectView.playing = true; protectView.visible = true; } else { @@ -262,18 +250,22 @@ class BulletItem extends BitmapItem { class EagleItem extends BitmapItem { private var death:Bool; + private var protected:Bool; override public function update():Void { super.update(); var d = value.death; - if (d != death) { + var p = value.protect.active; + if (d != death || p != protected) { death = d; + protected = p; redraw(); } } override private function getImage():String { - return 'resources/image/eagle/eagle${value.death ? '-death' : ''}.png'; + var suffix = value.death ? '-death' : value.protect.active ? '-protected' : ''; + return 'resources/image/eagle/eagle${suffix}.png'; } } @@ -287,8 +279,7 @@ class BonusItem extends AnimateItem { override public function redraw():Void { var image = Assets.getBitmapData('resources/image/bonus/${value.bonusType}.png'); - var empty = new BitmapData(image.width, image.height, true, 0x00000000); - view.frames = [for (i in 0...15) image].concat([for (i in 0...15) empty]); + view.frames = AnimateBundle.bonusFrames(value.bonusType); view.playing = true; } } diff --git a/src/client/resources/image/eagle/eagle-protected.png b/src/client/resources/image/eagle/eagle-protected.png new file mode 100644 index 0000000000000000000000000000000000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+J%a^KxCmM~?+S)2)Dh;Jf!tU{SC@(Lkva(V&yrL*FGBT1>4JNb6 z$w|(fIU{6UbeV+MYPItD^JnDy4K*DxK0dDcJlO`-#J={QR=iH9BNz;#*Xy}+=Z-4d z6c!e$K2LVc%FoZwm?hFO#?Lgn-HuMDNY3|>@Pb3ma z+YOW5ME)CqB>F)_iVOP4YlEQ|84t}fu)KU%WgfUumQS8KQNNA~!!kw}D^ zni}C}=CF_q(8-f0RZko{cI;4Y(KD5-%WYDUPts?3d3jPpLxZF?zM`TcYr0#K9*z&| q_e&2|H5Q3PRL4gm5w%NhRr?F3@e?mjuIG&a0000 0) { + cast(engine.entities[team.eagleId], Eagle).protect.on(10); + } case _: engine.destroy(tank); // :-D } diff --git a/src/common/haxe/ru/m/tankz/game/Team.hx b/src/common/haxe/ru/m/tankz/game/Team.hx index b9e00dc..3074cd3 100644 --- a/src/common/haxe/ru/m/tankz/game/Team.hx +++ b/src/common/haxe/ru/m/tankz/game/Team.hx @@ -12,6 +12,7 @@ class Team { public var players(default, null):Map; public var life(default, default):Int; public var isAlive(get, null):Bool; + public var eagleId(default, default):Int; public function new(config:TeamConfig, points:Array) { this.id = config.id;