MMI.Util.ns( 'Osg.App' ); Osg.App = MMI.App.Core.extend({ _currentScreen: 1, _currentLevel: 1, _isFirstCall: true, _queOfGoodBloat: 3, _queOfBadBloat: 5, _requiredScore: 5, player1: false, _jqelGameScreen: jQuery( '
' ), _jqelConfirmButton: jQuery( '
\n' ), _jqelMountain: jQuery( '\n ' + '\n ' + '\n ' + '\n ' + '\n' ), _jqelGoodBloatSaved: jQuery( '
0
\n' ), _jqelBadBloatBooted: jQuery( '
0
\n' ), _jqelPlayerScore: jQuery( '
0
\n' ), _jqelLevelAdvanceButton: jQuery( '
' + '
\n' ), _jqelReefWater: jQuery( '
\n' ), _jqelRiverAnim: jQuery( 'River\n' ), _jqelDpadWrapper: jQuery( '
\n' ), _jqelBackgroundMusic: jQuery( '' ), _jqelMuteButton: jQuery( '
\n' ), _jqelRestartButton: jQuery( '
\n' ), _touchButtons: { _jqelSpaceButton: jQuery( '
\n' ), _jqelUpButton: jQuery( '
\n' ), _jqelLeftButton: jQuery( '
\n' ), _jqelDownButton: jQuery( '
\n' ), _jqelRightButton: jQuery( '
\n' ) }, _reefChumLevel: 0, _bloatCounter: 0, _levels: [ { queOfGoodBloat: 3, queOfBadBloat: 5, requiredScore: 5 /*reps 1*/ }, { queOfGoodBloat: 4, queOfBadBloat: 7, requiredScore: 15 }, { queOfGoodBloat: 6, queOfBadBloat: 11, requiredScore: 35 }, { queOfGoodBloat: 10, queOfBadBloat: 16, requiredScore: 65 }], _bloats: [], _preloaderList: [ 'intro-screen2.png', 'intro-screen3.png', 'intro-screen4.png', 'intro-screen5.png', 'level-intro1.png', 'play-button.png', 'level-background.png', 'mountain.png', 'shark.png', 'muk.png', 'bad-bloat1.png', 'bad-bloat2.png', 'bad-bloat3.png', 'bad-bloat4.png', 'bad-bloat5.png', 'good-bloat1.png', 'good-bloat2.png', 'good-bloat3.png', 'level-intro2.png', 'level-intro3.png', 'level-intro4.png', 'level-complete.png', 'you-lose.png', 'epic-win.png', 'cloud.png', 'cloud2.png', 'cloud9.png', 'river.gif', 'water-animation.gif' ], __construct: function() { this._parent(); this.registerLiveEvents([ 'click', 'keydown', 'keyup', 'vmousedown', 'vmouseup' ]); this.documentLive( 'keydown' ); this.documentLive( 'keyup' ); }, /* ----------Handlers---------- */ handleDocumentReady: function( app ){ /* @todo Later this process should put all of the items that need to be appended into an array or * some such to icrease the speed of this process */ document.ontouchmove = function( e ){ e.preventDefault(); } var osgContent = jQuery( '#osg_layoutWrapper .content' ).addClass( 'visibleBlock' ); jQuery( '#osg_layoutWrapper' ).addClass( 'visibleBlock' ); jQuery( 'body' ).addClass( 'visibleBlock' ); jQuery( 'html' ).addClass( 'visibleBlock' ); osgContent.append( this._jqelGameScreen.addClass( 'visibleBlock' ), this._jqelDpadWrapper, this._jqelBackgroundMusic ); this._jqelGameScreen.append( this._jqelConfirmButton.addClass( 'visibleBlock' ), this._jqelMountain, this._jqelBadBloatBooted, this._jqelGoodBloatSaved, this._jqelPlayerScore, this._jqelLevelAdvanceButton, this._jqelReefWater, this._jqelRiverAnim ); this._clouds = { cloud: jQuery( '#cloud' ), cloud2: jQuery( '#cloud2' ), cloud9: jQuery( '#cloud9' ) }; for ( var buttonKey in this._touchButtons ){ if( this._touchButtons.hasOwnProperty( buttonKey ) ){ this._jqelDpadWrapper.append( this._touchButtons[ buttonKey ], this._jqelMuteButton, this._jqelRestartButton ); } } this._jqelBackgroundMusic[ 0 ].load(); this._jqelBackgroundMusic[ 0 ].play(); for( var imageNumber in this._preloaderList ){ this._jqelGameScreen.append( "\n" ); } }, handleConfirmButtonClicked: function( event, app, jqel, data ) { app._currentScreen = app._currentScreen % 6 + 1; if( app._currentScreen <= 4 ) { app._jqelGameScreen.removeClass( 'introScreen' + ( app._currentScreen - 1 )).addClass( 'introScreen' + app._currentScreen ); } else { app._loadLevel(); } }, handleAdvanceButtonClicked: function( event, app ){ app._currentScreen = 5; app._loadLevel(); }, handleKeydown: function( event, app, jqel, data ) { var currentKeycode; if( !event.keyCode ) { currentKeycode = event.which; } else { currentKeycode = event.keyCode; } switch( currentKeycode ){ case 37: app.player1.startMoving( 'left' ); break; case 39: app.player1.startMoving( 'right' ); break; case 38: app.player1.startMoving( 'up' ); break; case 40: app.player1.startMoving( 'down' ); break; case 32: app._detectCollision(); app.player1.setBootState(); break; } }, handleKeyup: function( event, app, jqel, data ){ var currentKeycode; if( !event.keyCode ) { currentKeycode = event.which; } else { currentKeycode = event.keyCode; } switch( currentKeycode ){ case 37: app.player1.stopMoving( 'left' ); break; case 39: app.player1.stopMoving( 'right' ); break; case 38: app.player1.stopMoving( 'up' ); break; case 40: app.player1.stopMoving( 'down' ); break; case 80: alert( 'Game Paused. Click "OK" to continue.' ); jQuery( window ).focus(); break; case 83: app._jqelBackgroundMusic[ 0 ].pause(); break; /* default: clog( 'Your code: ' + currentKeycode ); break; */ } if( event.ctrlKey && event.altKey ){ switch( currentKeycode ){ case 83: app.player1.increaseScore( 'cheat' ); break; case 49: app._currentLevel = 1; break; case 50: app._currentLevel = 2; break; case 51: app._currentLevel = 3; break; case 52: app._currentLevel = 4; break; } } }, handleButtonTouched: function( event, app, jqel, data ){ switch ( true ){ case ( data.sharkDirection != null ): app.player1.startMoving( data.sharkDirection ); break; case ( data.action == 'play' ): jQuery( this ).data( 'action', 'pause' ).removeClass( 'unmute' ).addClass( 'mute' ); app._jqelBackgroundMusic[ 0 ].play(); break; case ( data.action == 'pause' ): jQuery( this ).data( 'action', 'play' ).removeClass( 'mute' ).addClass( 'unmute' ); app._jqelBackgroundMusic[ 0 ].pause(); break; case ( data.action == 'restart' ): app._resetGame(); break; case ( data.action != null ): break; default: app._detectCollision(); app.player1.setBootState(); break; } }, handleButtonUntouched: function( event, app, jqel, data ){ if( data.sharkDirection ){ app.player1.stopMoving( data.sharkDirection ); } }, /* ----------Level methods---------- */ _loadLevel: function() { jQuery( '#gameScreen *' ).removeClass( 'visibleBlock' ); if ( this._isFirstCall ) { if( this._jqelPlayerScore.hasClass( 'levelEndScore' ) ){ this._jqelPlayerScore.removeClass( 'levelEndScore' ) } this._jqelConfirmButton.addClass( 'visibleBlock play' ); this._jqelGameScreen.removeClass().addClass( 'visibleBlock level' + this._currentLevel ); } else { this._jqelGameScreen.addClass( 'level' ); this._jqelMountain.addClass( 'visibleBlock' ); this._jqelReefWater.removeClass().addClass( 'visibleBlock' ); this._jqelBadBloatBooted.addClass( 'visibleBlock' ); this._jqelGoodBloatSaved.addClass( 'visibleBlock' ); this._jqelPlayerScore.addClass( 'visibleBlock' ); this._jqelRiverAnim.addClass( 'visibleBlock' ); if( !this.player1 ){ this.player1 = new Osg.Shark(); this.registerHandlerClass( this.player1 ); } jQuery( '#shark, #shark *' ).removeAttr( 'style' ).addClass( 'visibleBlock' ); this.player1.resetScore(); var levelKey = this._currentLevel - 1; var currentLevelData = this._levels[ levelKey ]; for( var propertyName in currentLevelData ){ if( currentLevelData.hasOwnProperty( propertyName ) ){ this[ "_" + propertyName ] = currentLevelData[ propertyName ]; } } if( typeof( window.noPlay ) == 'undefined' || window.noPlay !== true ) { setTimeout( MMI.Util.bind( this._spawnBloat, this ), 2000 ); } for( var cloudKey in this._clouds ) { if(this._clouds.hasOwnProperty( cloudKey )) { this._clouds[ cloudKey ].oneTime( Math.floor( Math.random() ) * 100, function() { MMI.App.getInstance()._animateCloud( jQuery( this )); }); } } } this._isFirstCall = false; }, _animateCloud: function( jqelCloud ) { /* @todo Base these values on the position and height/width of elements in the page*/ var leftBase = 553; var upperLimit = 0; var lowerLimit = 56; /* Pick a random position on the right side of the screen (outside the stage) between the upper and lower limits*/ var verticalPosition = Math.floor( Math.random() * 56 ); /* Position the cloud on the right hand side of the screen*/ jqelCloud.css({ top: verticalPosition, left: leftBase }); var properties = { left: -( jqelCloud.width() ) }; var animationSpeed = Math.floor( Math.random() * 10 ) + 1; animationSpeed = Math.max( 4, animationSpeed ); var animationDuration = ( 1000 * ( animationSpeed * 4 )); var options = { duration: animationDuration, easing: 'linear', complete: function() { MMI.App.getInstance()._animateCloud( jQuery( this )); } }; jqelCloud.animate( properties, options ); }, _spawnBloat: function() { var bloatArguments = { isGood: false, destructorCallback: MMI.Util.bind( this.destroyBloat, this ) }; if( this._queOfGoodBloat > 0 && ( Math.random() * 100 < 30 || this._queOfBadBloat == 0 ) ) { bloatArguments.isGood = true; this._bloats[ this._bloatCounter ] = new Osg.RiverBloat( bloatArguments ); --this._queOfGoodBloat; } else if ( this._queOfBadBloat > 0 ) { this._bloats[ this._bloatCounter ] = new Osg.RiverBloat( bloatArguments ); --this._queOfBadBloat; } if( this._queOfBadBloat + this._queOfGoodBloat <= 0 && jQuery( '.riverBloat' ).size() == 0 ) { this._endLevel(); } else { setTimeout( MMI.Util.bind( this._spawnBloat, this ), Math.random() * 3000 + 1000 ); } this._bloatCounter++; return; }, destroyBloat: function( bloatInstance ){ this._bloats.splice( bloatInstance.getListKey(), 1 ); }, chumWater: function(){ if( this._reefChumLevel < 4 ){ this._reefChumLevel += 1; this._jqelReefWater.removeClass().addClass( 'visibleBlock chumLevel' + this._reefChumLevel ); } }, _detectCollision: function(){ var isFacingRight = this.player1.isFacingRight(); var currentSharkDirection = isFacingRight ? 'right' : 'left'; var rightSideFuzz = isFacingRight ? 122 : 0; var leftSideFuzz = isFacingRight ? 0 : 122; var headPointOffset = jQuery( '#' + currentSharkDirection ).offset(); for ( var bloatKey in this._bloats ){ if( this._bloats.hasOwnProperty( bloatKey ) ){ var bloat = this._bloats[ bloatKey ]; var bloatJqel = bloat._jqelRiverBloat; var bloatJqelOffset = bloatJqel.offset(); if ( headPointOffset.left >= bloatJqelOffset.left - leftSideFuzz && headPointOffset.top >= bloatJqelOffset.top - 10 && headPointOffset.left <= bloatJqelOffset.left + bloatJqel.width() + rightSideFuzz && headPointOffset.top <= bloatJqelOffset.top + bloatJqel.height() + 10 ){ bloat.eject(); if ( bloatJqel.hasClass( 'bad' ) ){ this.player1.increaseScore( 'score' ); this.player1.increaseScore( 'booted' ); } else { this.player1.decreaseScore(); } } } } }, updateScoreBoard: function() { this._jqelBadBloatBooted.html( this.player1.getScore( 'booted' )); this._jqelGoodBloatSaved.html( this.player1.getScore( 'saved' )); this._jqelPlayerScore.html( this.player1.getScore( 'score' )); }, _endLevel: function(){ if( this._currentScreen <= 5 ) { return; } jQuery( '#gameScreen *' ).removeClass( 'visibleBlock' ); jQuery( '.riverBloat' ).remove(); this._isFirstCall = true; this._jqelLevelAdvanceButton.addClass( 'visibleBlock' ); this._reefChumLevel = 0; this._bloats = []; this._bloatCounter = 0; if( this.player1.getScore( 'score' ) >= this._requiredScore && this._currentLevel < 4 ){ this._jqelGameScreen.removeClass( 'level' ).addClass( 'levelComplete' ); this._jqelPlayerScore.addClass( 'levelEndScore visibleBlock' ); this._currentLevel++; } else if( this._currentLevel == 4 ) { this._jqelGameScreen.removeClass( 'level' ).addClass( 'epicWin' ); this._currentLevel = 1; } else { this._jqelGameScreen.removeClass( 'level' ).addClass( 'youLose' ); } return; }, _resetGame: function() { this._currentLevel = 1; this._currentScreen = 1; this._bloats = []; this._queOfGoodBloat = 0; this._queOfBadBloat = 0; jQuery( '.riverBloat' ).remove(); this._isFirstCall = true; this._reefChumLevel = 0; this._bloatCounter = 0; jQuery( '#gameScreen *' ).removeClass( 'visibleBlock' ); this._jqelConfirmButton.removeClass().addClass( 'live_click visibleBlock' ); this._jqelGameScreen.removeClass().addClass( 'visibleBlock' ); } }); window.OsgApp = new Osg.App();