private static const SHADOW_ALPHA:Number = 0.7;
private static const SHADOW_BLUR:Number = 13;
private static const SHADOW_RESOLUTION:Number = 2.5;
private static const SHADOW_DIRECTION:Vector3D = new Vector3D(0, 0, -1);
protected var shadow:Mesh;
private var shadowMaterial:TextureMaterial;
private var window:TankWindow2;
private var windowHeader:TankWindowWithHeader;
private const windowMargin:int = 11;
private var inner:TankWindowInner;
private var rootContainer:Object3DContainer;
private var cameraContainer:Object3DContainer;
private var camera:GameCamera;
private var timer:Timer;
private var tank:Tank3D;
private var rotationSpeed:Number;
private var lastTime:int;
private var loadedCounter:int = 0;
private var holdMouseX:int;
private var lastMouseX:int;
private var prelastMouseX:int;
private var rate:Number;
private var startAngle:Number = -150;
private var holdAngle:Number;
private var slowdownTimer:Timer;
private var resetRateInt:uint;
private var autoRotationDelay:int = 10000;
private var autoRotationTimer:Timer;
public var overlay:Shape;
private var firstAutoRotation:Boolean = true;
private var first_resize:Boolean = true;
private var dot : Number = Math.PI / 180;
private var dot1:Number = dot * 0.001;
protected var backgroundEraserTimer:Timer;
protected var backgroundEraser:Shape;
///
public function TankPreview(garageBoxId:Long, rotationSpeed:Number = 5)
{
this.windowHeader = new TankWindowWithHeader("МОЙ ТАНК");
addChild(this.windowHeader);
var box:Mesh = null;
var material:TextureMaterial = null;
this.overlay = new Shape();
super();
this.rotationSpeed = rotationSpeed;
this.window = new TankWindow2(400,300);
var localeService:ILocaleService = ILocaleService(Main.osgi.getService(ILocaleService));
addChild(this.window);
this.rootContainer = new Object3DContainer();
this.tank = new Tank3D(null, null, null);
this.rootContainer.addChild(this.tank);
this.tank.matrix.appendTranslation(0, 0, 0);//(-17, 0, 0)
initGarage();
this.camera = new GameCamera();
this.camera.view = new View(100, 100);
this.camera.view.hideLogo();
addChild(this.camera.view);
addChild(this.overlay);
this.camera.view.visible = false;
this.overlay.x = 0;
this.overlay.y = 9;
this.overlay.width = 1500;
this.overlay.height = 1300;
this.overlay.graphics.clear();
this.cameraContainer = new Object3DContainer();
this.rootContainer.addChild(this.cameraContainer);
this.cameraContainer.addChild(this.camera);
this.cameraContainer.z = 225;
//this.camera.z = -850;
//this.cameraContainer.rotationX = -115 * dot;
//this.cameraContainer.rotationZ = this.startAngle * dot;
this.cameraContainer.rotationX = -110 * Math.PI / 180;
//this.camera.y = garageService.getCameraAltitude();
this.camera.z = -910;
//this.camera.fov = garageService.getCameraFieldOfView();
this.camera.x = -20;
this.camera.y = 80;
this.cameraContainer.rotationZ = INITIAL_CAMERA_DIRECTION * Math.PI / 180;
this.inner = new TankWindowInner(0, 0, TankWindowInner.TRANSPARENT);
addChild(this.inner);
this.inner.mouseEnabled = true;
this.autoRotationTimer = new Timer(this.autoRotationDelay,1);
this.autoRotationTimer.addEventListener(TimerEvent.TIMER_COMPLETE,this.start);
this.timer = new Timer(50);
this.slowdownTimer = new Timer(20,1000000);
this.slowdownTimer.addEventListener(TimerEvent.TIMER,this.slowDown);
this.inner.addEventListener(MouseEvent.MOUSE_DOWN,this.onMouseDown);
Main.stage.addEventListener(Event.ENTER_FRAME, this.onRender);
//
this.backgroundEraser = new Shape();
this.backgroundEraser.blendMode = BlendMode.ERASE;
///
this.backgroundEraserTimer = new Timer(1000,1);
this.backgroundEraserTimer.addEventListener(TimerEvent.TIMER_COMPLETE,this.onBackgroundEraserTimerComplete);
this.backgroundEraserTimer.start();
//
this.start();
}
private function onBackgroundEraserTimerComplete(param1:TimerEvent) : void
{
addChild(this.backgroundEraser);
this.backgroundEraserTimer.removeEventListener(TimerEvent.TIMER_COMPLETE,this.onBackgroundEraserTimerComplete);
this.backgroundEraserTimer = null;
}
public function hide() : void
{
var bgService:IBackgroundService = Main.osgi.getService(IBackgroundService) as IBackgroundService;
if(bgService != null)
{
bgService.drawBg();
}
this.stopAll();
this.window = null;
this.inner = null;
for(var i:int = 0; i < rootContainer.numChildren; i++)
{
//rootContainer.getChildAt(i).destroy();
rootContainer.removeChildAt(i);
}
this.rootContainer = null;
this.cameraContainer = null;
this.camera.view.clear();
this.camera = null;
this.timer = null;
this.tank.destroy();
this.tank = null;
this.backgroundEraser = null;
this.backgroundEraserTimer = null;
Main.stage.removeEventListener(Event.ENTER_FRAME,this.onRender);
}
private function onMouseDown(e:MouseEvent) : void
{
if(this.autoRotationTimer.running)
{
this.autoRotationTimer.stop();
}
if(this.timer.running)
{
this.stop();
}
if(this.slowdownTimer.running)
{
this.slowdownTimer.stop();
}
this.resetRate();
this.holdMouseX = Main.stage.mouseX;
this.lastMouseX = this.holdMouseX;
this.prelastMouseX = this.holdMouseX;
this.holdAngle = this.cameraContainer.rotationZ;
Main.writeToConsole("TankPreview onMouseMove holdAngle: " + this.holdAngle.toString());
Main.stage.addEventListener(MouseEvent.MOUSE_UP,this.onMouseUp);
Main.stage.addEventListener(MouseEvent.MOUSE_MOVE,this.onMouseMove);
}
private function onMouseMove(e:MouseEvent) : void
{
this.cameraContainer.rotationZ = this.holdAngle - (Main.stage.mouseX - this.holdMouseX) * 0.01;
//this.camera.render();
this.rate = (Main.stage.mouseX - this.prelastMouseX) * 0.5;
this.prelastMouseX = this.lastMouseX;
this.lastMouseX = Main.stage.mouseX;
clearInterval(this.resetRateInt);
this.resetRateInt = setInterval(this.resetRate,50);
}
private function resetRate() : void
{
this.rate = 0;
}
private function onMouseUp(e:MouseEvent) : void
{
clearInterval(this.resetRateInt);
Main.stage.removeEventListener(MouseEvent.MOUSE_UP,this.onMouseUp);
Main.stage.removeEventListener(MouseEvent.MOUSE_MOVE,this.onMouseMove);
if(Math.abs(this.rate) > 0)
{
this.slowdownTimer.reset();
this.slowdownTimer.start();
}
else
{
this.autoRotationTimer.reset();
this.autoRotationTimer.start();
}
}
private function slowDown(e:TimerEvent) : void
{
this.cameraContainer.rotationZ = this.cameraContainer.rotationZ - this.rate * 0.01;
//this.camera.render();
this.rate = this.rate * Math.exp(-0.02);
if(Math.abs(this.rate) < 0.1)
{
this.slowdownTimer.stop();
this.autoRotationTimer.reset();
this.autoRotationTimer.start();
}
}
public function resize(width:Number, height:Number, i:int = 0, j:int = 0) : void
{
this.window.width = width;
this.window.height = height;
this.window.alpha = 1;
this.inner.width = width - this.windowMargin * 2;
this.inner.height = height - this.windowMargin * 2;
this.inner.x = this.windowMargin;
this.inner.y = this.windowMargin;
var bgService:IBackgroundService = Main.osgi.getService(IBackgroundService) as IBackgroundService;
if(Main.stage.stageWidth >= 800 && !this.first_resize)
{
if(bgService != null)
{
bgService.drawBg(new Rectangle(Math.round(int(Math.max(1000,Main.stage.stageWidth)) / 3) + this.windowMargin,60 + this.windowMargin,this.inner.width,this.inner.height));
}
}
this.first_resize = false;
this.camera.view.width = width - this.windowMargin * 2 - 2;
this.camera.view.height = height - this.windowMargin * 2 - 2;
this.camera.view.x = this.windowMargin;
this.camera.view.y = this.windowMargin;
this.adjustBackgroundEraser();
this.camera.render();
}
private function adjustBackgroundEraser() : void
{
if(true)
{
this.backgroundEraser.x = this.camera.view.x;
this.backgroundEraser.y = this.camera.view.y;
this.backgroundEraser.graphics.clear();
this.backgroundEraser.graphics.beginFill(16711680);
this.backgroundEraser.graphics.drawRect(0,0,this.camera.view.width,this.camera.view.height);
this.backgroundEraser.graphics.endFill();
}
}
public function start(e:TimerEvent = null) : void
{
if(this.loadedCounter < 3)
{
this.autoRotationTimer.reset();
this.autoRotationTimer.start();
}
else
{
this.firstAutoRotation = false;
this.timer.addEventListener(TimerEvent.TIMER,this.onTimer);
this.timer.reset();
this.lastTime = getTimer();
this.timer.start();
}
}
public function onRender(e:Event) : void
{
this.camera.render();
}
public function stop() : void
{
this.timer.stop();
this.timer.removeEventListener(TimerEvent.TIMER,this.onTimer);
}
public function stopAll() : void
{
this.timer.stop();
this.timer.removeEventListener(TimerEvent.TIMER,this.onTimer);
this.slowdownTimer.stop();
this.slowdownTimer.removeEventListener(TimerEvent.TIMER,this.slowDown);
this.autoRotationTimer.stop();
this.slowdownTimer.removeEventListener(TimerEvent.TIMER_COMPLETE, this.start);
}
private function onTimer(e:TimerEvent) : void
{
var time:int = this.lastTime;
this.lastTime = getTimer();
this.cameraContainer.rotationZ = this.cameraContainer.rotationZ - this.rotationSpeed * (this.lastTime - time) * 0.001 * (Math.PI / 180);
}