import imageUrl from '../assets/space-debris.png' ; import { gameHeight } from './config' ; import { Stuff } from './stuff' ; const keys = { sheet : "space-debris-sheet" , } ; const asteroidCount = 20 ; export class Background implements Stuff { private sprites ! : { s : Phaser . GameObjects . Image ; velocity : number ; angularV : number ; } [ ] ; preload ( scene ) { scene . load . spritesheet ( keys . sheet , imageUrl , { frameWidth : 54 , frameHeight : 54 , margin : 0 , spacing : 0 , } ) ; } create ( scene ) { this . sprites = [ ] ; for ( let i = 0 ; i < asteroidCount ; i ++ ) { const x = Phaser . Math . Between ( - 64 , scene . game . scale . width ) ; const y = Phaser . Math . Between ( - 64 , scene . game . scale . height ) ; const image = scene . add . image ( x , y , keys . sheet , Math . floor ( Math . random ( ) * 4 ) ) ; image . setTint ( 0x666666 ) ; image . scale = ( 0.2 + 2 * Math . random ( ) ) * gameHeight / 1000 ; this . sprites . push ( { s : image , velocity : ( 0.1 + Math . random ( ) * 0.5 ) * gameHeight / 1000 , angularV : ( 0.5 - Math . random ( ) ) * 0.3 , } ) ; } } update ( scene ) { const debrisMaxY = scene . game . scale . height + 100 ; const debrisMinY = - 100 ; for ( let i = 0 ; i < this . sprites . length ; i ++ ) { const sprite = this . sprites [ i ] . s ; sprite . y += this . sprites [ i ] . velocity ; sprite . angle += this . sprites [ i ] . angularV ; if ( sprite . y > debrisMaxY ) { sprite . y = debrisMinY ; } } } }