export class HarvestBar { private wGame : any | Window ; private container : HTMLDivElement ; private harvestBar : HTMLDivElement ; private harvestTimeText : HTMLDivElement ; private updateInterval : any ; private interval : number = 200 ; private cellId : number ; private duration : number ; private remainingTime : number ; constructor ( wGame ) { this . wGame = wGame ; } private createContainer ( ) { this . container = document . createElement ( 'div' ) ; this . container . id = 'harvestBarContainer' ; this . container . className = 'harvestBarContainer' ; this . wGame . foreground . rootElement . appendChild ( this . container ) ; } private createBar ( ) { this . createContainer ( ) ; const scenePos = this . wGame . isoEngine . mapRenderer . getCellSceneCoordinate ( this . cellId ) ; const pos = this . wGame . isoEngine . mapScene . convertSceneToCanvasCoordinate ( scenePos . x , scenePos . y ) ; this . harvestBar = document . createElement ( 'div' ) ; this . harvestBar . id = 'harvestBar' ; this . harvestBar . className = 'harvestBar' ; this . container . appendChild ( this . harvestBar ) ; this . container . style . left = ( pos . x - this . container . offsetWidth / 2 ) + 'px' ; this . container . style . top = ( pos . y ) + 'px' ; this . harvestTimeText = document . createElement ( 'div' ) ; this . harvestTimeText . id = 'harvestTime' ; this . harvestTimeText . className = 'harvestTimeText' ; this . wGame . foreground . rootElement . appendChild ( this . harvestTimeText ) ; this . harvestTimeText . style . left = ( pos . x - this . container . offsetWidth / 2 ) + 'px' ; this . harvestTimeText . style . top = ( pos . y ) + 'px' ; this . update ( ) ; } private show ( ) { this . createBar ( ) ; this . container . style . visibility = 'visible' ; this . updateInterval = setInterval ( ( ) => { this . remainingTime -= this . interval ; this . update ( ) ; } , this . interval ) ; } private update ( ) { const time = this . remainingTime / this . duration * 100 ; this . harvestBar . style . width = ( time > 0 ? time : '0' ) + '%' ; this . harvestTimeText . innerHTML = ( this . remainingTime > 0 ? ( this . remainingTime / 1000 ) : '0' ) + 's' ; } public harvestStarted ( cellId , duration ) { this . cellId = cellId ; this . duration = duration * 100 ; this . remainingTime = duration * 100 ; this . show ( ) ; } public destroy ( ) { clearInterval ( this . updateInterval ) ; if ( this . container && this . container . parentElement ) this . container . parentElement . removeChild ( this . container ) ; if ( this . harvestTimeText && this . harvestTimeText . parentElement ) this . harvestTimeText . parentElement . removeChild ( this . harvestTimeText ) ; return true ; } }