@ -46,26 +46,8 @@ export default class GameApp extends NiceGame {
} ,
} ,
} ;
}
initMenuOverlays ( ) {
/ *
* Attach to each expected menu overlay
* /
this . overlays = {
systemMenu : document . querySelector ( '.menu-overlay#system-menu' ) ,
mainMenu : document . querySelector ( '.menu-overlay#main-menu' ) ,
gameControls : document . querySelector ( '.menu-overlay#game-controls' ) ,
} ;
this . startButton = this . overlays . mainMenu . querySelector ( 'button#start-button' ) ;
this . startButton . onclick = this . onStartGame . bind ( this ) ;
this . bootButton = this . overlays . systemMenu . querySelector ( 'button#boot-button' ) ;
this . bootButton . onclick = this . bootGameCartridge . bind ( this ) ;
this . gameDisplay . removeChild ( this . overlays . mainMenu ) ;
this . gameDisplay . removeChild ( this . overlays . gameControls ) ;
document . addEventListener ( 'ngsdk:game-boot' , this . onGameBoot . bind ( this ) ) ;
}
async run ( ) {
@ -75,10 +57,9 @@ export default class GameApp extends NiceGame {
) ;
}
async bootGameCartridge ( event ) {
async onGameBoot ( event ) {
NiceInputTools . cancelEvent ( event ) ;
this . gameDisplay . removeChild ( this . overlays . systemMenu ) ;
this . gameDisplay . appendChild ( this . overlays . mainMenu ) ;
/ *
@ -93,19 +74,32 @@ export default class GameApp extends NiceGame {
this . input . addButton ( 'moveRight' , '#btn-move-right' ) ;
this . input . addButton ( 'throwEgg' , '#btn-throw-egg' ) ;
this . audio . start ( ) ;
this . input . addGamepadButton ( 'system:start' , 9 ) ;
this . input . addGamepadButton ( 'system:back' , 8 ) ;
this . input . addGamepadButton ( 'throwEgg' , 0 ) ;
this . input . addGamepadButton ( 'moveLeft' , 14 ) ;
this . input . addGamepadButton ( 'moveRight' , 15 ) ;
this . startMusicStream ( '/dist/assets/audio/music/blueberries' ) ;
await this . loadGameAssets ( ) ;
this . mode = 'menu' ;
this . background . setMode ( 'story' ) ;
this . startUpdateLoop ( ) ;
}
async onStartGame ( event ) {
NiceInputTools . cancelEvent ( event ) ;
this . startNewGame ( ) ;
}
async startMusicStream ( url ) {
await this . audio . setMusicStream ( url ) ;
this . audio . playMusicStream ( ) ;
this . audio . musicVolume = 0.4 ;
}
startNewGame ( ) {
this . mode = 'loading' ;
this . gameDisplay . removeChild ( this . overlays . mainMenu ) ;
@ -126,12 +120,6 @@ export default class GameApp extends NiceGame {
this . background . setMode ( 'play' ) ;
}
async startMusicStream ( url ) {
await this . audio . setMusicStream ( url ) ;
this . audio . playMusicStream ( ) ;
this . audio . musicVolume = 0.4 ;
}
startLevel ( ) {
const now = performance . now ( ) ;
this . nextSpawnInterval = 1000 * 2 ;
@ -163,7 +151,11 @@ export default class GameApp extends NiceGame {
}
}
updateMenu ( /* elapsed, now */ ) { }
updateMenu ( /* elapsed, now */ ) {
if ( this . input . isInputPressed ( 'system:start' ) ) {
this . startNewGame ( ) ;
}
}
renderMenu ( ctx ) {
this . background . render ( ctx ) ;
@ -262,6 +254,64 @@ export default class GameApp extends NiceGame {
formation . addTier ( 7 ) ;
}
flashBorder ( color ) {
this
. createTween (
this . playfield . colors . margin . stroke ,
{
r : color . r ,
g : color . g ,
b : color . b ,
a : color . a ,
} ,
{
r : this . playfield . colors . margin . stroke . r ,
g : this . playfield . colors . margin . stroke . g ,
b : this . playfield . colors . margin . stroke . b ,
a : this . playfield . colors . margin . stroke . a ,
} ,
NiceEasing . Bounce . InOut ,
)
. duration ( 1000 )
. run ( )
; // end createTween
}
getRandomPlayfieldX ( padding ) {
const fieldWidth = this . playfield . width - ( this . playfield . margin * 2 ) - ( padding * 2 ) ;
return this . playfield . margin + padding + ( Math . random ( ) * fieldWidth ) ;
}
initMenuOverlays ( ) {
/ *
* Attach to each expected menu overlay
* /
this . addOverlay ( 'debugDisplay' , '.menu-overlay#debug-display' ) ;
this . addOverlay ( 'mainMenu' , '.menu-overlay#main-menu' ) ;
this . addOverlay ( 'gameControls' , '.menu-overlay#game-controls' ) ;
document . addEventListener ( 'ngsdk:gamepad-connected' , this . onGamepadConnected . bind ( this ) ) ;
this . startButton = this . overlays . mainMenu . querySelector ( 'button#start-button' ) ;
this . startButton . onclick = this . onStartGame . bind ( this ) ;
this . gameDisplay . removeChild ( this . overlays . mainMenu ) ;
this . overlays . mainMenu . removeAttribute ( 'hidden' ) ;
this . gameDisplay . removeChild ( this . overlays . gameControls ) ;
this . overlays . gameControls . removeAttribute ( 'hidden' ) ;
this . overlays . systemMenu . removeAttribute ( 'hidden' ) ;
}
async onGamepadConnected ( event ) {
const { gamepad } = event . detail ;
this . log . info ( 'onGamepadConnected' , 'gamepad connected' , { gamepad } ) ;
const gamepadFeedback = this . overlays . systemMenu . querySelector ( 'span#gamepad-feedback' ) ;
gamepadFeedback . textContent = 'CONNECTED' ;
}
async loadGameAssets ( ) {
const jobs = [ ] ;
@ -309,34 +359,6 @@ export default class GameApp extends NiceGame {
await Promise . all ( jobs ) ;
}
flashBorder ( color ) {
this
. createTween (
this . playfield . colors . margin . stroke ,
{
r : color . r ,
g : color . g ,
b : color . b ,
a : color . a ,
} ,
{
r : this . playfield . colors . margin . stroke . r ,
g : this . playfield . colors . margin . stroke . g ,
b : this . playfield . colors . margin . stroke . b ,
a : this . playfield . colors . margin . stroke . a ,
} ,
NiceEasing . Bounce . InOut ,
)
. duration ( 1000 )
. run ( )
; // end createTween
}
getRandomPlayfieldX ( padding ) {
const fieldWidth = this . playfield . width - ( this . playfield . margin * 2 ) - ( padding * 2 ) ;
return this . playfield . margin + padding + ( Math . random ( ) * fieldWidth ) ;
}
}
window . addEventListener ( 'load' , async ( ) => {