212 lines
5.5 KiB
Haxe
212 lines
5.5 KiB
Haxe
package ru.m.tankz.render;
|
|
|
|
import ru.m.tankz.control.Control;
|
|
import flash.display.Sprite;
|
|
import ru.m.animate.Animate;
|
|
import ru.m.tankz.core.Eagle;
|
|
import flash.display.DisplayObject;
|
|
import flash.display.Shape;
|
|
import ru.m.geom.Direction;
|
|
import ru.m.geom.Rectangle;
|
|
import ru.m.tankz.core.Bullet;
|
|
import ru.m.tankz.map.Brick;
|
|
import openfl.Assets;
|
|
import ru.m.tankz.core.Tank;
|
|
import flash.display.Bitmap;
|
|
|
|
|
|
typedef TRectangle = {
|
|
var rect(default, null):Rectangle;
|
|
}
|
|
|
|
|
|
class RenderItem<T:TRectangle, D:DisplayObject> {
|
|
|
|
public var value(default, null):T;
|
|
public var view(default, null):D;
|
|
|
|
public function new(value:T) {
|
|
this.value = value;
|
|
this.view = null;
|
|
}
|
|
|
|
public function redraw():Void {}
|
|
|
|
public function update():Void {
|
|
var rect = value.rect;
|
|
view.rotation = calcRotate(rect.direction);
|
|
view.x = rect.x - rect.width * (rect.direction.x + 1) / 2 + rect.width * (rect.direction.y + 1) / 2 + 0.5 * rect.width;
|
|
view.y = rect.y - rect.height * (rect.direction.x + 1) / 2 - rect.height * (rect.direction.y + 1) / 2 + 1.5 * rect.height;
|
|
}
|
|
|
|
private static function calcRotate(direction:Direction):Float {
|
|
return (if (direction == Direction.RIGHT) {
|
|
0;
|
|
} else if (direction == Direction.LEFT) {
|
|
180;
|
|
} else if (direction == Direction.TOP) {
|
|
270;
|
|
} else if (direction == Direction.BOTTOM) {
|
|
90;
|
|
} else {
|
|
0;
|
|
});
|
|
}
|
|
|
|
public function dispose():Void {}
|
|
}
|
|
|
|
|
|
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 broken:Int;
|
|
|
|
public function new(value:Brick) {
|
|
super(value);
|
|
this.view = new Shape();
|
|
redraw();
|
|
}
|
|
|
|
override public function redraw():Void {
|
|
var image = Assets.getBitmapData(getImage());
|
|
var g = view.graphics;
|
|
g.clear();
|
|
if (value.destroyed) return;
|
|
g.beginBitmapFill(image);
|
|
g.drawRect(0, 0, value.rect.width, value.rect.height);
|
|
for (c in value.cells) {
|
|
if (c.destroyed) {
|
|
g.beginFill(0x000000);
|
|
g.drawRect(c.rect.x - value.rect.x, c.rect.y - value.rect.y, c.rect.width, c.rect.height);
|
|
}
|
|
}
|
|
g.endFill();
|
|
}
|
|
|
|
override public function update():Void {
|
|
super.update();
|
|
var b = value.broken;
|
|
if (b != this.broken) {
|
|
this.broken = b;
|
|
redraw();
|
|
}
|
|
}
|
|
|
|
private function getImage():String {
|
|
return 'resources/images/map/map_${value.config.type}.png';
|
|
}
|
|
}
|
|
|
|
|
|
class TankItem extends RenderItem<Tank, Sprite> {
|
|
|
|
private var type:String;
|
|
private var hits:Int;
|
|
|
|
private var tankView:Animate;
|
|
|
|
public function new(value:Tank) {
|
|
super(value);
|
|
view = new Sprite();
|
|
if (value.playerId.type == Control.HUMAN) {
|
|
view.addChild(buildHumanMarkView(value));
|
|
}
|
|
tankView = new Animate();
|
|
view.addChild(tankView);
|
|
redraw();
|
|
}
|
|
|
|
private static function buildHumanMarkView(tank:Tank):DisplayObject {
|
|
var view = new Shape();
|
|
view.graphics.beginFill(0x00aa00);
|
|
view.graphics.lineStyle(2, 0x00ff00);
|
|
view.graphics.drawCircle(0, 0, 23);
|
|
view.graphics.endFill();
|
|
view.x = tank.rect.width / 2;
|
|
view.y = tank.rect.height / 2;
|
|
view.alpha = 0.2;
|
|
return view;
|
|
}
|
|
|
|
override public function redraw():Void {
|
|
tankView.frames = getFrames().map(function(s) return Assets.getBitmapData(s));
|
|
}
|
|
|
|
private function getFrames():Array<String> {
|
|
var team = value.playerId.team;
|
|
var group = value.config.group;
|
|
var index = value.playerId.index;
|
|
if (team == 'radiant') {
|
|
index = 0;
|
|
}
|
|
if (team == 'dire') {
|
|
index = 1;
|
|
}
|
|
if (team == 'human' || team == 'radiant' || team == 'dire') {
|
|
group = 'player';
|
|
}
|
|
if (team == 'bot') {
|
|
index = value.hits;
|
|
}
|
|
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 {
|
|
super.update();
|
|
var t = value.config.type;
|
|
var h = value.hits;
|
|
if (t != this.type || h != this.hits) {
|
|
this.type = t;
|
|
this.hits = h;
|
|
redraw();
|
|
}
|
|
tankView.playing = (value.mx !=0 || value.my != 0);
|
|
}
|
|
|
|
override public function dispose():Void {
|
|
if (tankView != null) {
|
|
tankView.dispose();
|
|
tankView = null;
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
class BulletItem extends BitmapItem<Bullet> {
|
|
|
|
override private function getImage():String {
|
|
return 'resources/images/bullet/bullet_${value.config.piercing > 1 ? 1 : 0}.png';
|
|
}
|
|
}
|
|
|
|
|
|
class EagleItem extends BitmapItem<Eagle> {
|
|
|
|
public var destoyed(default, default):Bool;
|
|
|
|
override private function getImage():String {
|
|
return 'resources/images/eagle/eagle-${destoyed ? 1 : 0}.png';
|
|
}
|
|
}
|