import { WWAConsts as Consts , Coord } from "./wwa_data" ; export class CacheCanvas { public cvs : HTMLCanvasElement ; public ctx : CanvasRenderingContext2D ; private _isTransparent : boolean ; public constructor ( width , height , isTransparent ) { this . cvs = document . createElement ( "canvas" ) ; this . cvs . width = width ; this . cvs . height = height ; this . ctx = this . cvs . getContext ( "2d" , { alpha : isTransparent } ) ; this . _isTransparent = isTransparent ; } public drawCanvas ( _image , chipX , chipY , canvasX , canvasY ) { this . ctx . drawImage ( _image , Consts . CHIP_SIZE * chipX , Consts . CHIP_SIZE * chipY , Consts . CHIP_SIZE , Consts . CHIP_SIZE , canvasX , canvasY , Consts . CHIP_SIZE , Consts . CHIP_SIZE ) ; } public clear ( ) { this . clearRect ( 0 , 0 , this . cvs . width , this . cvs . height ) ; } param yLimit public clearRectWithLowerYLimit ( yLimit ) { this . clearRect ( 0 , 0 , this . cvs . width , yLimit ) ; } private clearRect ( x , y , width , height ) { if ( ! this . _isTransparent ) { this . ctx . fillStyle = "#9E9E9E" ; this . ctx . fillRect ( x , y , width , height ) ; } else { this . ctx . clearRect ( x , y , width , height ) ; } } } export class CGManager { private _ctx : CanvasRenderingContext2D ; private _isLoaded : boolean = false ; private _fileName : string ; private _loadCompleteCallBack : ( ) => void ; private _image : HTMLImageElement ; private _frameCanvas : CacheCanvas ; private _backCanvas : CacheCanvas ; private _objectCanvases : CacheCanvas [ ] ; private _effectCanvases : CacheCanvas [ ] ; public mapCache : number [ ] = void 0 ; public mapObjectCache : number [ ] = void 0 ; public mapCacheYLimit : number = 0 ; public cpPartsLog : Coord ; private _frameCoord : Coord ; private _load ( ) { this . _frameCoord ; if ( this . _isLoaded ) { return ; } this . mapCache = [ - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 ] ; this . mapObjectCache = [ - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 ] ; this . _image = new Image ( ) ; this . _image . addEventListener ( "load" , ( ) => { this . createFrame ( ) ; this . _loadCompleteCallBack ( ) ; } ) ; this . _image . addEventListener ( "error" , ( ) => { throw new Error ( "Image Load Failed!!\nfile name:" + this . _fileName ) ; } ) ; this . _image . src = this . _fileName ; this . _isLoaded = true ; } private createFrame ( ) { this . _frameCanvas . clear ( ) ; this . _frameCanvas . drawCanvas ( this . _image , this . _frameCoord . x , this . _frameCoord . y , 0 , 0 ) ; this . _frameCanvas . drawCanvas ( this . _image , this . _frameCoord . x + 2 , this . _frameCoord . y , Consts . MAP_WINDOW_WIDTH - Consts . CHIP_SIZE , 0 ) ; this . _frameCanvas . drawCanvas ( this . _image , this . _frameCoord . x , this . _frameCoord . y + 2 , 0 , Consts . MAP_WINDOW_HEIGHT - Consts . CHIP_SIZE ) ; this . _frameCanvas . drawCanvas ( this . _image , this . _frameCoord . x + 2 , this . _frameCoord . y + 2 , Consts . MAP_WINDOW_WIDTH - Consts . CHIP_SIZE , Consts . MAP_WINDOW_HEIGHT - Consts . CHIP_SIZE ) ; for ( var i = 1 ; i < Consts . H_PARTS_NUM_IN_WINDOW - 1 ; i ++ ) { this . _frameCanvas . drawCanvas ( this . _image , this . _frameCoord . x + 1 , this . _frameCoord . y , Consts . CHIP_SIZE * i , 0 ) ; this . _frameCanvas . drawCanvas ( this . _image , this . _frameCoord . x + 1 , this . _frameCoord . y + 2 , Consts . CHIP_SIZE * i , Consts . MAP_WINDOW_HEIGHT - Consts . CHIP_SIZE ) ; } for ( var i = 1 ; i < Consts . V_PARTS_NUM_IN_WINDOW - 1 ; i ++ ) { this . _frameCanvas . drawCanvas ( this . _image , this . _frameCoord . x , this . _frameCoord . y + 1 , 0 , Consts . CHIP_SIZE * i ) ; this . _frameCanvas . drawCanvas ( this . _image , this . _frameCoord . x + 2 , this . _frameCoord . y + 1 , Consts . MAP_WINDOW_WIDTH - Consts . CHIP_SIZE , Consts . CHIP_SIZE * i ) ; } } public updateEffects ( effectCoords ) { var i ; if ( ! effectCoords ) { return ; } var len = effectCoords . length ; var effectCanvas ; for ( i = 0 ; i < len ; i ++ ) { var coord = effectCoords [ i ] ; effectCanvas = this . _effectCanvases [ i ] ; if ( ! effectCanvas ) { effectCanvas = this . _effectCanvases [ i ] = new CacheCanvas ( Consts . CHIP_SIZE * Consts . V_PARTS_NUM_IN_WINDOW , Consts . CHIP_SIZE * Consts . H_PARTS_NUM_IN_WINDOW , true ) ; } else { effectCanvas . clear ( ) ; } for ( var y = 0 ; y < Consts . V_PARTS_NUM_IN_WINDOW ; y ++ ) { for ( var x = 0 ; x < Consts . H_PARTS_NUM_IN_WINDOW ; x ++ ) { effectCanvas . drawCanvas ( this . _image , coord . x , coord . y , x * Consts . CHIP_SIZE , y * Consts . CHIP_SIZE ) ; } } } } public drawFrame ( ) { this . _ctx . drawImage ( this . _frameCanvas . cvs , 0 , 0 , Consts . CHIP_SIZE * Consts . V_PARTS_NUM_IN_WINDOW , Consts . CHIP_SIZE , 0 , 0 , Consts . CHIP_SIZE * Consts . V_PARTS_NUM_IN_WINDOW , Consts . CHIP_SIZE ) ; this . _ctx . drawImage ( this . _frameCanvas . cvs , 0 , Consts . CHIP_SIZE * ( Consts . H_PARTS_NUM_IN_WINDOW - 1 ) , Consts . CHIP_SIZE * Consts . V_PARTS_NUM_IN_WINDOW , Consts . CHIP_SIZE , 0 , Consts . CHIP_SIZE * ( Consts . H_PARTS_NUM_IN_WINDOW - 1 ) , Consts . CHIP_SIZE * Consts . V_PARTS_NUM_IN_WINDOW , Consts . CHIP_SIZE ) ; this . _ctx . drawImage ( this . _frameCanvas . cvs , 0 , Consts . CHIP_SIZE , Consts . CHIP_SIZE , Consts . CHIP_SIZE * ( Consts . H_PARTS_NUM_IN_WINDOW - 2 ) , 0 , Consts . CHIP_SIZE , Consts . CHIP_SIZE , Consts . CHIP_SIZE * ( Consts . H_PARTS_NUM_IN_WINDOW - 2 ) ) ; this . _ctx . drawImage ( this . _frameCanvas . cvs , Consts . CHIP_SIZE * ( Consts . H_PARTS_NUM_IN_WINDOW - 1 ) , Consts . CHIP_SIZE , Consts . CHIP_SIZE , Consts . CHIP_SIZE * ( Consts . H_PARTS_NUM_IN_WINDOW - 2 ) , Consts . CHIP_SIZE * ( Consts . H_PARTS_NUM_IN_WINDOW - 1 ) , Consts . CHIP_SIZE , Consts . CHIP_SIZE , Consts . CHIP_SIZE * ( Consts . H_PARTS_NUM_IN_WINDOW - 2 ) ) ; } public drawEffect ( id ) { var effectCanvas = this . _effectCanvases [ id ] ; if ( ! effectCanvas ) { return ; } this . _ctx . drawImage ( effectCanvas . cvs , 0 , 0 , Consts . CHIP_SIZE * Consts . V_PARTS_NUM_IN_WINDOW , Consts . CHIP_SIZE * Consts . H_PARTS_NUM_IN_WINDOW , 0 , 0 , Consts . CHIP_SIZE * Consts . V_PARTS_NUM_IN_WINDOW , Consts . CHIP_SIZE * Consts . H_PARTS_NUM_IN_WINDOW ) ; } public drawCanvas ( chipX , chipY , canvasX , canvasY ) { if ( ! this . _isLoaded ) { throw new Error ( "No image was loaded." ) ; } this . _ctx . drawImage ( this . _image , Consts . CHIP_SIZE * chipX , Consts . CHIP_SIZE * chipY , Consts . CHIP_SIZE , Consts . CHIP_SIZE , canvasX , canvasY , Consts . CHIP_SIZE , Consts . CHIP_SIZE ) ; } public drawCanvasWithSize ( chipX , chipY , width , height , canvasX , canvasY ) { if ( ! this . _isLoaded ) { throw new Error ( "No image was loaded." ) ; } this . _ctx . drawImage ( this . _image , Consts . CHIP_SIZE * chipX , Consts . CHIP_SIZE * chipY , Consts . CHIP_SIZE * width , Consts . CHIP_SIZE * height , canvasX , canvasY , Consts . CHIP_SIZE * width , Consts . CHIP_SIZE * height ) ; } public drawCanvasWithUpperYLimit ( chipX , chipY , canvasX , canvasY , yLimit ) { if ( ! this . _isLoaded ) { throw new Error ( "No image was loaded." ) ; } var delLength = Math . max ( 0 , canvasY + Consts . CHIP_SIZE - yLimit ) ; if ( delLength >= Consts . CHIP_SIZE ) { return ; } this . _ctx . drawImage ( this . _image , Consts . CHIP_SIZE * chipX , Consts . CHIP_SIZE * chipY , Consts . CHIP_SIZE , Consts . CHIP_SIZE - delLength , canvasX , canvasY , Consts . CHIP_SIZE , Consts . CHIP_SIZE ) ; } param cacheCanvas param delLengthFunc private copyCanvas ( cacheCanvas , delLengthFunc ) { return ( chipX , chipY , canvasX , canvasY , yLimit ) => { if ( ! this . _isLoaded ) { throw new Error ( "No image was loaded." ) ; } const delLength = Math . max ( 0 , delLengthFunc ( canvasY , yLimit ) ) ; if ( delLength >= Consts . CHIP_SIZE ) { return ; } cacheCanvas . ctx . drawImage ( this . _image , Consts . CHIP_SIZE * chipX , Consts . CHIP_SIZE * chipY + delLength , Consts . CHIP_SIZE , Consts . CHIP_SIZE - delLength , canvasX , canvasY + delLength , Consts . CHIP_SIZE , Consts . CHIP_SIZE ) ; } ; } public copyBackCanvasWithUpperYLimit ( chipX , chipY , canvasX , canvasY , yLimit ) { this . copyCanvas ( this . _backCanvas , ( canvasY , yLimit ) => canvasY + Consts . CHIP_SIZE - yLimit ) ( chipX , chipY , canvasX , canvasY , yLimit ) ; } public copyBackCanvasWithLowerYLimit ( chipX , chipY , canvasX , canvasY , yLimit ) { this . copyCanvas ( this . _backCanvas , ( canvasY , yLimit ) => yLimit - canvasY ) ( chipX , chipY , canvasX , canvasY , yLimit ) ; } public copyObjectCanvasWithUpperYLimit ( frameType , chipX , chipY , canvasX , canvasY , yLimit ) { this . copyCanvas ( this . _objectCanvases [ frameType ] , ( canvasY , yLimit ) => canvasY + Consts . CHIP_SIZE - yLimit ) ( chipX , chipY , canvasX , canvasY , yLimit ) ; } public copyObjectCanvasWithLowerYLimit ( frameType , chipX , chipY , canvasX , canvasY , yLimit ) { this . copyCanvas ( this . _objectCanvases [ frameType ] , ( canvasY , yLimit ) => yLimit - canvasY ) ( chipX , chipY , canvasX , canvasY , yLimit ) ; } public drawBackCanvas ( ) { if ( ! this . _isLoaded ) { throw new Error ( "No image was loaded." ) ; } this . _ctx . drawImage ( this . _backCanvas . cvs , 0 , 0 , Consts . CHIP_SIZE * Consts . V_PARTS_NUM_IN_WINDOW , Consts . CHIP_SIZE * Consts . H_PARTS_NUM_IN_WINDOW , 0 , 0 , Consts . CHIP_SIZE * Consts . V_PARTS_NUM_IN_WINDOW , Consts . CHIP_SIZE * Consts . H_PARTS_NUM_IN_WINDOW ) ; } public drawObjectCanvas ( frameType ) { if ( ! this . _isLoaded ) { throw new Error ( "No image was loaded." ) ; } this . _ctx . drawImage ( this . _objectCanvases [ frameType ] . cvs , 0 , 0 , Consts . CHIP_SIZE * Consts . V_PARTS_NUM_IN_WINDOW , Consts . CHIP_SIZE * Consts . H_PARTS_NUM_IN_WINDOW , 0 , 0 , Consts . CHIP_SIZE * Consts . V_PARTS_NUM_IN_WINDOW , Consts . CHIP_SIZE * Consts . H_PARTS_NUM_IN_WINDOW ) ; } public clearBackCanvas ( ) { this . _backCanvas . clear ( ) ; } public clearBackCanvasWithLowerYLimit ( yLimit ) { this . _backCanvas . clearRectWithLowerYLimit ( yLimit ) ; } public clearObjectCanvases ( ) { var i ; for ( i = 0 ; i < 2 ; i ++ ) { this . _objectCanvases [ i ] . clear ( ) ; } } public clearObjectCanvasesWithLowerYLimit ( yLimit ) { var i ; for ( i = 0 ; i < 2 ; i ++ ) { this . _objectCanvases [ i ] . clearRectWithLowerYLimit ( yLimit ) ; } } public drawCanvasWithLowerYLimit ( chipX , chipY , canvasX , canvasY , yLimit ) { if ( ! this . _isLoaded ) { throw new Error ( "No image was loaded." ) ; } var delLength = Math . max ( 0 , yLimit - canvasY ) ; if ( delLength >= Consts . CHIP_SIZE ) { return ; } this . _ctx . drawImage ( this . _image , Consts . CHIP_SIZE * chipX , Consts . CHIP_SIZE * chipY + delLength , Consts . CHIP_SIZE , Consts . CHIP_SIZE - delLength , canvasX , canvasY + delLength , Consts . CHIP_SIZE , Consts . CHIP_SIZE ) ; } public clearCanvas ( x , y , w , h ) { this . _ctx . clearRect ( x , y , w , h ) ; } public drawBase ( x , y , w , h ) { this . _ctx . fillStyle = "#9E9E9E" ; this . _ctx . fillRect ( x , y , w , h ) ; } param frameCoord public setFrameImage ( frameCoord ) { this . _frameCoord = frameCoord ; this . createFrame ( ) ; } public constructor ( ctx , fileName , _frameCoord , loadCompleteCallBack ) { this . _frameCanvas = new CacheCanvas ( Consts . CHIP_SIZE * Consts . V_PARTS_NUM_IN_WINDOW , Consts . CHIP_SIZE * Consts . H_PARTS_NUM_IN_WINDOW , true ) ; this . _backCanvas = new CacheCanvas ( Consts . CHIP_SIZE * Consts . V_PARTS_NUM_IN_WINDOW , Consts . CHIP_SIZE * Consts . H_PARTS_NUM_IN_WINDOW , false ) ; this . _objectCanvases = [ ] ; this . _effectCanvases = [ ] ; var i ; for ( i = 0 ; i < 2 ; i ++ ) { this . _objectCanvases [ i ] = new CacheCanvas ( Consts . CHIP_SIZE * Consts . V_PARTS_NUM_IN_WINDOW , Consts . CHIP_SIZE * Consts . H_PARTS_NUM_IN_WINDOW , true ) ; } this . _ctx = ctx ; this . _fileName = fileName ; this . _loadCompleteCallBack = loadCompleteCallBack ; this . _load ( ) ; this . _frameCoord = _frameCoord . clone ( ) ; this . cpPartsLog = new Coord ( 0 , 0 ) ; } }