import * as Phaser from 'phaser' ; import { Subscription } from 'rxjs' ; export interface KeyBinding { event : string ; fun : Function ; emitter : Phaser . Events . EventEmitter ; } export abstract class BaseObject extends Phaser . GameObjects . GameObject { private subs : Subscription [ ] = [ ] ; private keyBindings : KeyBinding [ ] = [ ] ; protected constructor ( scene , type , active = true ) { super ( scene , type ) ; this . active = false ; this . init ( ) ; if ( active ) { this . active = true ; this . activate ( ) ; } } protected abstract init ( ) ; abstract preUpdate ( time , delta ) ; protected abstract activate ( ) ; protected abstract deactivate ( ) ; public setActive ( value ) { if ( this . active === value ) { return this ; } super . setActive ( value ) ; if ( value ) { this . activateInternal ( ) ; } else { this . deactivateInternal ( ) ; } return this ; } private deactivateInternal ( ) { this . keyBindings . forEach ( binding => { binding . emitter . removeListener ( binding . event , binding . fun ) ; } ) ; this . keyBindings = [ ] ; this . subs . forEach ( sub => sub . unsubscribe ( ) ) ; this . subs = [ ] ; this . deactivate ( ) ; } private activateInternal ( ) { this . activate ( ) ; } protected addKeybinding ( binding ) { if ( ! this . active ) { throw new Error ( 'tried to add keybinding on gameobject that is not active' ) ; } this . keyBindings . push ( binding ) ; binding . emitter . addListener ( binding . event , binding . fun ) ; } protected addSubscription ( sub ) { this . subs . push ( sub ) ; } }