module editor { export class DataCenter extends egret . EventDispatcher { private static m_Instance : DataCenter ; public static get Instance ( ) { if ( this . m_Instance == null ) this . m_Instance = new DataCenter ( ) ; return this . m_Instance ; } public static SAVE_REMOTE_DATA_URL : string = "http://123.207.4.125/fishformation/saveresult.php" ; public static SAVE_JSON_DATA_URL : string = "http://123.207.4.125/fishformation/savejsonresult.php" ; public static LOAD_REMOTE_DATA_URL : string = "http://123.207.4.125/fishformation/loadresult.php" ; public static Down_REMOTE_DATA_URL : string = "http://123.207.4.125/fishformation/result.csv" ; constructor ( ) { super ( ) ; } public fishResMap : { [ sign ] : egret . MovieClip ; } = { } ; public getFishRes ( sign ) { for ( let item of table . TableFishConfig . instance ( ) ) { if ( item . sign == sign ) { let groupName = item . movieClipAry [ 0 ] . groupName ; var data = RES . getRes ( groupName + "_json" ) ; var txtr = RES . getRes ( groupName + "_png" ) ; var mcFactory = new egret . MovieClipDataFactory ( data , txtr ) ; var mc = new egret . MovieClip ( mcFactory . generateMovieClipData ( ) ) ; return mc ; } } } public formationArray : Array < { sign : number ; fishs : Array < { fishid : number ; offsetX : number ; offsetY : number ; } > ; } > = [ ] ; public changedFormation ( sign , fishs ) { this . formationArray . removeFirst ( v => v . sign == sign ) ; this . formationArray . push ( { sign : sign , fishs : fishs } ) ; this . formationArray = this . formationArray . sort ( ( a , b ) => a . sign - b . sign ) ; } public removeFormation ( sign ) { this . formationArray . removeFirst ( v => v . sign == sign ) ; } public shapeData : Array < { sign : number ; shape : Array < { shapeType : number ; x : number ; y : number ; r : number ; endX : number ; endY : number ; fishNum : number ; fishId : number ; } > ; } > = [ ] ; public changedShapeData ( sign , shape ) { this . shapeData . removeFirst ( v => v . sign == sign ) ; this . shapeData . push ( { sign : sign , shape : shape } ) ; this . shapeData = this . shapeData . sort ( ( a , b ) => a . sign - b . sign ) ; editor . MainEditor . Instance . updataFormationList ( ) ; } public removeShapeData ( sign ) { this . shapeData . removeFirst ( v => v . sign == sign ) ; editor . MainEditor . Instance . updataFormationList ( ) ; } public newFormation ( sign ) { if ( sign == null || sign == NaN ) return ; if ( this . shapeData . first ( v => v . sign == sign ) ) { return ; } this . shapeData . push ( { sign : sign , shape : [ ] } ) ; this . shapeData = this . shapeData . sort ( ( a , b ) => a . sign - b . sign ) ; editor . MainEditor . Instance . updataFormationList ( ) ; } public changedFormationSign ( oldsign , newsign ) { let item = this . shapeData . first ( v => v . sign == oldsign ) ; item . sign = newsign ; this . shapeData = this . shapeData . sort ( ( a , b ) => a . sign - b . sign ) ; editor . MainEditor . Instance . updataFormationList ( ) ; } public LoadData ( ) { this . loadDataFromUrl ( DataCenter . LOAD_REMOTE_DATA_URL ) ; } public SaveData ( ) { var urlloader = new egret . URLLoader ( ) ; var req = new egret . URLRequest ( DataCenter . SAVE_REMOTE_DATA_URL ) ; egret . log ( "call saveDataToUrl, url:" + DataCenter . SAVE_REMOTE_DATA_URL ) ; req . method = egret . URLRequestMethod . POST ; req . data = this . formatResult ( ) ; urlloader . load ( req ) ; let self = this ; let onPostComplete = function ( event ) { var loader = < egret . URLLoader > event . target ; var data = loader . data ; egret . log ( "保存成功" ) ; } ; urlloader . addEventListener ( egret . Event . COMPLETE , onPostComplete , null ) ; var urlloader = new egret . URLLoader ( ) ; var req = new egret . URLRequest ( DataCenter . SAVE_JSON_DATA_URL ) ; egret . log ( "call saveDataToUrl, url:" + DataCenter . SAVE_JSON_DATA_URL ) ; req . method = egret . URLRequestMethod . POST ; let shapeData = [ ] ; for ( let item of this . shapeData ) { let data = { sign : item . sign , shape : item . shape } ; shapeData . push ( data ) ; } req . data = JSON . stringify ( shapeData ) ; urlloader . load ( req ) ; urlloader . addEventListener ( egret . Event . COMPLETE , onPostComplete , null ) ; } protected formatResult ( ) { let result = "" ; let outputStr = function ( msg ) { result = result + msg + "\n" ; } ; outputStr ( "sign,\"fishSign,offsetX,offsetY\"" ) ; for ( let formation of this . formationArray ) { let str = "\"[" ; for ( let j = 0 ; j < formation . fishs . length ; j ++ ) { let item = formation . fishs [ j ] ; str += ( "[" + item . fishid + "," + Math . floor ( item . offsetX ) + "," + Math . floor ( item . offsetY ) + "]" ) ; if ( j != formation . fishs . length - 1 ) { str += "," ; } } str += "]\"" ; outputStr ( "" + formation . sign + "," + str ) ; } return result ; } protected calcAngleAndDistance ( p1 , p2 ) { let p2p1x = p2 . x - p1 . x ; let p2p1y = p2 . y - p1 . y ; let angle = 0 ; if ( p2p1x < 0.001 && p2p1x > - 0.001 ) { if ( p2p1y < 0 ) { angle = 90 ; } else { angle = 270 ; } } else { angle = Math . atan ( p2p1y / p2p1x ) * 180 ; } let dist = Math . sqrt ( p2p1x * p2p1x + p2p1y * p2p1y ) ; return { angle : angle , dist : dist } ; } private urlloader : egret . URLLoader ; protected loadDataFromUrl ( url ) { egret . log ( "call loadDataFromUrl:" + url ) ; this . urlloader = new egret . URLLoader ( ) ; this . urlloader . dataFormat = egret . URLLoaderDataFormat . TEXT ; var urlreq = new egret . URLRequest ( ) ; urlreq . url = url ; this . urlloader . load ( urlreq ) ; this . urlloader . addEventListener ( egret . Event . COMPLETE , this . onLoadUrlComplete , this ) ; } protected onLoadUrlComplete ( event ) { this . parsePathDataFromCsvData ( this . urlloader . data ) ; } protected parsePathDataFromCsvData ( data ) { if ( data == null ) return ; this . shapeData = JSON . parse ( data ) ; this . shapeData = this . shapeData . sort ( ( a , b ) => a . sign - b . sign ) ; editor . MainEditor . Instance . updataFormationList ( ) ; egret . log ( "this.fishRegionMap:" + data ) ; } } }