import { Tween } from "./Tween" ; export class TweenManager { private static tweens : Array < Tween > = [ ] ; private static tweensToDelete : Array < Tween > = [ ] ; private last : number = 0 ; protected static instance : TweenManager ; public static getInstance ( ) { if ( ! this . instance ) { this . instance = new TweenManager ( ) ; } return this . instance ; } public static builder ( target ) { return TweenManager . getInstance ( ) . createTween ( target ) ; } public static update = ( dt ) => { let deltaMS = dt ; for ( let i = 0 ; i < TweenManager . tweens . length ; i ++ ) { let tween = TweenManager . tweens [ i ] ; if ( tween . active ) { tween . update ( deltaMS ) ; } if ( tween . isEnded && tween . expire ) { tween . remove ( ) ; } } if ( TweenManager . tweensToDelete . length ) { for ( let i = 0 ; i < TweenManager . tweensToDelete . length ; i ++ ) TweenManager . remove ( TweenManager . tweensToDelete [ i ] ) ; TweenManager . tweensToDelete . length = 0 ; } } ; getTweensForTarget ( target ) { let tweens = [ ] ; for ( let i = 0 ; i < TweenManager . tweens . length ; i ++ ) { if ( TweenManager . tweens [ i ] . target === target ) tweens . push ( TweenManager . tweens [ i ] ) ; } return tweens ; } createTween ( target ) { return new Tween ( target , this ) ; } addTween ( tween ) { tween . manager = this ; TweenManager . tweens . push ( tween ) ; } removeTween ( tween ) { TweenManager . tweensToDelete . push ( tween ) ; } private static remove ( tween ) { TweenManager . tweens . remove ( tween ) ; } private getDeltaMS ( ) { if ( this . last === 0 ) this . last = Date . now ( ) ; let now = Date . now ( ) ; let deltaMS = now - this . last ; this . last = now ; return deltaMS ; } }