[render] added animates
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "tankz",
|
||||
"version": "0.2.1",
|
||||
"version": "0.2.2",
|
||||
"private": true,
|
||||
"devDependencies": {
|
||||
"ansi-colors": "^1.0.1",
|
||||
|
||||
@@ -12,6 +12,7 @@
|
||||
<haxelib name="protohx" version="0.4.6"/>
|
||||
<haxelib name="haxework" version="git"/>
|
||||
<haxelib name="yaml" version="1.3.0"/>
|
||||
<window fps="30"/>
|
||||
<window width="1024" height="768" unless="html5"/>
|
||||
<haxeflag name="-D" value="swf-gpu"/>
|
||||
<haxeflag name="-D" value="native-trace"/>
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,8 @@
|
||||
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 flash.display.DisplayObject;
|
||||
@@ -19,20 +22,24 @@ class Render extends SpriteView implements EngineListener {
|
||||
private var groundLayer:Sprite;
|
||||
private var entryLayer:Sprite;
|
||||
private var upLayer:Sprite;
|
||||
private var upperLayer:Sprite;
|
||||
|
||||
private var background:Sprite;
|
||||
private var items:Map<String, RenderItem<Dynamic>>;
|
||||
private var items:Map<String, RenderItem<Dynamic, Dynamic>>;
|
||||
|
||||
public function new() {
|
||||
super();
|
||||
items = new Map();
|
||||
backgroundLayer = new Sprite();
|
||||
groundLayer = new Sprite();
|
||||
entryLayer = new Sprite();
|
||||
upLayer = new Sprite();
|
||||
upperLayer = new Sprite();
|
||||
contentAsSprite.addChild(backgroundLayer);
|
||||
contentAsSprite.addChild(groundLayer);
|
||||
contentAsSprite.addChild(entryLayer);
|
||||
contentAsSprite.addChild(upLayer);
|
||||
contentAsSprite.addChild(upperLayer);
|
||||
reset();
|
||||
}
|
||||
|
||||
@@ -74,7 +81,10 @@ class Render extends SpriteView implements EngineListener {
|
||||
}
|
||||
|
||||
public function reset():Void {
|
||||
items = new Map<String, RenderItem<Dynamic>>();
|
||||
for (item in items.iterator()) {
|
||||
item.dispose();
|
||||
}
|
||||
items = new Map();
|
||||
if (background != null) {
|
||||
backgroundLayer.removeChild(background);
|
||||
background = null;
|
||||
@@ -115,18 +125,51 @@ 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);
|
||||
}
|
||||
case EntityType.BULLET(bullet):
|
||||
if (items.exists(bullet.key)) {
|
||||
entryLayer.removeChild(items.get(bullet.key).view);
|
||||
items.remove(bullet.key);
|
||||
playBulletBoom(bullet.rect.center);
|
||||
}
|
||||
case EntityType.EAGLE(eagle):
|
||||
if (items.exists(eagle.key)) {
|
||||
cast(items.get(eagle.key), EagleItem).destoyed = true;
|
||||
items.get(eagle.key).redraw();
|
||||
playTankBoom(eagle.rect.center);
|
||||
}
|
||||
case _:
|
||||
}
|
||||
}
|
||||
|
||||
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) {
|
||||
upperLayer.removeChild(animate);
|
||||
animate.dispose();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package ru.m.tankz.render;
|
||||
|
||||
import ru.m.animate.Animate;
|
||||
import ru.m.tankz.core.Eagle;
|
||||
import flash.display.DisplayObject;
|
||||
import flash.display.Shape;
|
||||
@@ -17,23 +18,17 @@ typedef TRectangle = {
|
||||
}
|
||||
|
||||
|
||||
class RenderItem<T:TRectangle> {
|
||||
class RenderItem<T:TRectangle, D:DisplayObject> {
|
||||
|
||||
public var value(default, null):T;
|
||||
public var view(default, null):DisplayObject;
|
||||
|
||||
private var bitmap:Bitmap;
|
||||
public var view(default, null):D;
|
||||
|
||||
public function new(value:T) {
|
||||
this.value = value;
|
||||
this.bitmap = new Bitmap();
|
||||
this.view = bitmap;
|
||||
redraw();
|
||||
this.view = null;
|
||||
}
|
||||
|
||||
public function redraw():Void {
|
||||
bitmap.bitmapData = Assets.getBitmapData(getImage());
|
||||
}
|
||||
public function redraw():Void {}
|
||||
|
||||
public function update():Void {
|
||||
var rect = value.rect;
|
||||
@@ -42,10 +37,6 @@ class RenderItem<T:TRectangle> {
|
||||
view.y = rect.y - rect.height * (rect.direction.x + 1) / 2 - rect.height * (rect.direction.y + 1) / 2 + 1.5 * rect.height;
|
||||
}
|
||||
|
||||
private function getImage():String {
|
||||
return 'ERROR';
|
||||
}
|
||||
|
||||
private static function calcRotate(direction:Direction):Float {
|
||||
return (if (direction == Direction.RIGHT) {
|
||||
0;
|
||||
@@ -59,23 +50,42 @@ class RenderItem<T:TRectangle> {
|
||||
0;
|
||||
});
|
||||
}
|
||||
|
||||
public function dispose():Void {}
|
||||
}
|
||||
|
||||
|
||||
class BrickItem extends RenderItem<Brick> {
|
||||
class BitmapItem<T:TRectangle> extends RenderItem<T, Bitmap> {
|
||||
|
||||
public function new(value:T) {
|
||||
super(value);
|
||||
this.view = new Bitmap();
|
||||
redraw();
|
||||
}
|
||||
|
||||
override public function redraw():Void {
|
||||
view.bitmapData = Assets.getBitmapData(getImage());
|
||||
}
|
||||
|
||||
private function getImage():String {
|
||||
return 'ERROR';
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
class BrickItem extends RenderItem<Brick, Shape> {
|
||||
|
||||
private var shape:Shape;
|
||||
private var broken:Int;
|
||||
|
||||
public function new(value:Brick) {
|
||||
this.shape = new Shape();
|
||||
super(value);
|
||||
this.view = shape;
|
||||
this.view = new Shape();
|
||||
redraw();
|
||||
}
|
||||
|
||||
override public function redraw():Void {
|
||||
var image = Assets.getBitmapData(getImage());
|
||||
var g = shape.graphics;
|
||||
var g = view.graphics;
|
||||
g.clear();
|
||||
if (value.destroyed) return;
|
||||
g.beginBitmapFill(image);
|
||||
@@ -98,18 +108,22 @@ class BrickItem extends RenderItem<Brick> {
|
||||
}
|
||||
}
|
||||
|
||||
override private function getImage():String {
|
||||
private function getImage():String {
|
||||
return 'resources/images/map/map_${value.config.type}.png';
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
class TankItem extends RenderItem<Tank> {
|
||||
class TankItem extends RenderItem<Tank, Animate> {
|
||||
|
||||
private var type:String;
|
||||
|
||||
override private function getImage():String {
|
||||
public function new(value:Tank) {
|
||||
super(value);
|
||||
this.view = new Animate(getFrames().map(function(s) return Assets.getBitmapData(s)));
|
||||
}
|
||||
|
||||
private function getFrames():Array<String> {
|
||||
var group = value.config.group;
|
||||
var index = value.playerId.index;
|
||||
if (group == 'human') group = 'player';
|
||||
@@ -122,7 +136,10 @@ class TankItem extends RenderItem<Tank> {
|
||||
index = 1;
|
||||
}
|
||||
if (group == 'bot') index = 0;
|
||||
return 'resources/images/tank/${group}/tank_${group.charAt(0)}${value.config.type}_${index}-0.png';
|
||||
return [
|
||||
'resources/images/tank/${group}/tank_${group.charAt(0)}${value.config.type}_${index}-0.png',
|
||||
'resources/images/tank/${group}/tank_${group.charAt(0)}${value.config.type}_${index}-1.png',
|
||||
];
|
||||
}
|
||||
|
||||
override public function update():Void {
|
||||
@@ -132,11 +149,19 @@ class TankItem extends RenderItem<Tank> {
|
||||
this.type = t;
|
||||
redraw();
|
||||
}
|
||||
view.playing = (value.mx !=0 || value.my != 0);
|
||||
}
|
||||
|
||||
override public function dispose():Void {
|
||||
if (view != null) {
|
||||
view.dispose();
|
||||
view = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
class BulletItem extends RenderItem<Bullet> {
|
||||
class BulletItem extends BitmapItem<Bullet> {
|
||||
|
||||
override private function getImage():String {
|
||||
return 'resources/images/bullet/bullet_${value.config.piercing > 1 ? 1 : 0}.png';
|
||||
@@ -144,7 +169,7 @@ class BulletItem extends RenderItem<Bullet> {
|
||||
}
|
||||
|
||||
|
||||
class EagleItem extends RenderItem<Eagle> {
|
||||
class EagleItem extends BitmapItem<Eagle> {
|
||||
|
||||
public var destoyed(default, default):Bool;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user