import { AssetEvent , Vector3D , AssetLibrary , URLRequest , RequestAnimationFrame , PerspectiveProjection , Keyboard } from "@awayjs/core" ; import { AnimationNodeBase } from "@awayjs/renderer" ; import { AnimatorBase , SkeletonAnimator , SkeletonClipNode , CrossfadeTransition , AnimationStateEvent } from "@awayjs/graphics" ; import { HoverController , LoaderContainer , Scene } from "@awayjs/scene" ; import { AWDParser } from "@awayjs/parsers" ; class Intermediate_AWDViewer { private _scene : Scene ; private _cameraController : HoverController ; private _animator : SkeletonAnimator ; private _timer : RequestAnimationFrame ; private _time : number = 0 ; private _lastPanAngle : number ; private _lastTiltAngle : number ; private _lastMouseX : number ; private _lastMouseY : number ; private _move : boolean ; private _stateTransition : CrossfadeTransition = new CrossfadeTransition ( 0.5 ) ; private static IDLE_NAME : string = "idle" ; /**
     * Constructor
     */ constructor ( ) { this . init ( ) ; } /**
     * Global initialise function
     */ private init ( ) { this . initEngine ( ) ; this . initObjects ( ) ; this . initListeners ( ) ; } /**
     * Initialise the engine
     */ private initEngine ( ) { this . _scene = new Scene ( ) ; this . _scene . view . backgroundColor = 0x333338 ; this . _scene . camera . projection = new PerspectiveProjection ( 70 ) ; this . _scene . camera . projection . far = 5000 ; this . _scene . camera . projection . near = 1 ; this . _cameraController = new HoverController ( this . _scene . camera , null , 0 , 0 , 150 , 10 , 90 ) ; this . _cameraController . lookAtPosition = new Vector3D ( 0 , 60 , 0 ) ; this . _cameraController . tiltAngle = 0 ; this . _cameraController . panAngle = 0 ; this . _cameraController . minTiltAngle = 5 ; this . _cameraController . maxTiltAngle = 60 ; this . _cameraController . autoUpdate = false ; } /**
     * Initialise the scene objects
     */ private initObjects ( ) { AssetLibrary . enableParser ( AWDParser ) ; var loader = new LoaderContainer ( ) ; loader . addEventListener ( AssetEvent . ASSET_COMPLETE , ( event ) => this . onAssetComplete ( event ) ) ; loader . load ( new URLRequest ( "assets/shambler.awd" ) ) ; this . _scene . root . addChild ( loader ) ; } /**
     * Initialise the listeners
     */ private initListeners ( ) { window . onresize = ( event ) => this . onResize ( event ) ; document . onmousedown = ( event ) => this . onMouseDown ( event ) ; document . onmouseup = ( event ) => this . onMouseUp ( event ) ; document . onmousemove = ( event ) => this . onMouseMove ( event ) ; document . onwheel = ( event ) => this . onWheel ( event ) ; document . onkeydown = ( event ) => this . onKeyDown ( event ) ; this . onResize ( ) ; this . _timer = new RequestAnimationFrame ( this . onEnterFrame , this ) ; this . _timer . start ( ) ; } /**
     * loader listener for asset complete events
     */ private onAssetComplete ( event ) { if ( event . asset . isAsset ( AnimatorBase ) ) { this . _animator = < SkeletonAnimator > event . asset ; this . _animator . play ( Intermediate_AWDViewer . IDLE_NAME ) ; } else if ( event . asset . isAsset ( AnimationNodeBase ) ) { var node = < SkeletonClipNode > event . asset ; if ( node . name == Intermediate_AWDViewer . IDLE_NAME ) { node . looping = true ; } else { node . looping = false ; node . addEventListener ( AnimationStateEvent . PLAYBACK_COMPLETE , ( event ) => this . onPlaybackComplete ( event ) ) ; } } } /**
     * Key down listener for animation
     */ private onKeyDown ( event ) { switch ( event . keyCode ) { case Keyboard . NUMBER_1 : this . playAction ( "attack01" ) ; break ; case Keyboard . NUMBER_2 : this . playAction ( "attack02" ) ; break ; case Keyboard . NUMBER_3 : this . playAction ( "attack03" ) ; break ; case Keyboard . NUMBER_4 : this . playAction ( "attack04" ) ; break ; case Keyboard . NUMBER_5 : this . playAction ( "attack05" ) ; } } private playAction ( name ) { this . _animator . play ( name , this . _stateTransition , 0 ) ; } private onPlaybackComplete ( event ) { if ( this . _animator . activeState != event . animationState ) return ; this . _animator . play ( Intermediate_AWDViewer . IDLE_NAME , this . _stateTransition ) ; } /**
     * Render loop
     */ private onEnterFrame ( dt ) { this . _time += dt ; this . _cameraController . update ( ) ; this . _scene . render ( ) ; } /**
     * Mouse down listener for navigation
     */ private onMouseDown ( event ) { this . _lastPanAngle = this . _cameraController . panAngle ; this . _lastTiltAngle = this . _cameraController . tiltAngle ; this . _lastMouseX = event . clientX ; this . _lastMouseY = event . clientY ; this . _move = true ; } /**
     * Mouse up listener for navigation
     */ private onMouseUp ( event ) { this . _move = false ; } private onMouseMove ( event ) { if ( this . _move ) { this . _cameraController . panAngle = 0.3 * ( event . clientX - this . _lastMouseX ) + this . _lastPanAngle ; this . _cameraController . tiltAngle = 0.3 * ( event . clientY - this . _lastMouseY ) + this . _lastTiltAngle ; } } /**
     * Mouse wheel listener for navigation
     */ private onWheel ( event ) { this . _cameraController . distance -= event . deltaY / 2 ; if ( this . _cameraController . distance < 100 ) this . _cameraController . distance = 100 ; else if ( this . _cameraController . distance > 2000 ) this . _cameraController . distance = 2000 ; } /**
     * window listener for resize events
     */ private onResize ( event = null ) { this . _scene . view . y = 0 ; this . _scene . view . x = 0 ; this . _scene . view . width = window . innerWidth ; this . _scene . view . height = window . innerHeight ; } } window . onload = function ( ) { new Intermediate_AWDViewer ( ) ; } ;