import { Sprite , Texture , utils } from "pixi.js" ; export class MultistateSprite extends Sprite { public stateTextures : Map < string , Texture > = new Map ( ) ; protected _color ! : number | string ; constructor ( texture ? ) { super ( texture ) ; if ( texture ) { this . stateTextures . set ( "default" , texture ) ; } } set color ( color ) { if ( typeof color == "string" ) { this . tint = utils . string2hex ( color ) ; } else { this . tint = color ; } } get color ( ) : number | string { if ( this . _color == undefined ) { return this . tint ; } return this . _color ; } public addState ( state , texture ) { this . stateTextures . set ( state , texture ) ; } public setDefaultState ( texture ) { this . stateTextures . set ( "default" , texture ) ; } public getState ( state ) { return this . stateTextures . get ( state ) ; } public hasState ( state ) { return ! ! this . stateTextures . get ( state ) ; } public setState ( state = "default" ) { var sprite = this ; let texture = sprite . stateTextures . get ( state ) ; if ( ! texture ) return ; if ( sprite . texture && sprite . texture != texture && sprite . texture . baseTexture ) { sprite . texture . baseTexture . dispose ( ) ; } sprite . texture = texture ; } destroy ( options ? ) { if ( this . texture && this . texture . baseTexture ) { this . texture . baseTexture . dispose ( ) ; } ( < any > this ) . texture = null ; super . destroy ( options ) ; } }