MMI.Util.ns( 'Osg.Shark' ); Osg.Shark= MMI.stdClass.extend({ __construct: function() { this._isFacingRight = true; this._score = 0; this._goodBloatSaved = 0; this._badBloatBooted = 0; this._frame = 0; this._jqelShark = jQuery( '
' + '
' + '' + '
' ); jQuery( '#gameScreen' ).append( this._jqelShark ); setTimeout( function(){ var thisShark = MMI.App.getInstance().player1; var thisSharkJqel = thisShark._jqelShark; var thisSharkJqelLeft = thisSharkJqel.position().left; var thisSharkJqelTop = thisSharkJqel.position().top; thisShark._destinationLeft = thisSharkJqelLeft; thisShark._destinationTop = thisSharkJqelTop; thisShark._maxTop = thisSharkJqel.parent().height() - thisSharkJqel.height(); thisShark._minTop = 180; thisShark._maxLeft = thisSharkJqel.parent().width() - thisSharkJqel.width() / 2; thisShark._minLeft = 0 - thisSharkJqel.width() / 2; }, 400 ); this._jqelShark.everyTime( 130, 'swim', MMI.Util.bind( this._animate, this )); }, /** * @return void */ startMoving: function( direction ) { var currentDestLeft, currentDestTop; currentDestLeft = this._destinationLeft; currentDestTop = this._destinationTop; switch(direction){ case 'left': this._destinationLeft = this._minLeft; this._isFacingRight = false; break; case 'right': this._destinationLeft = this._maxLeft; this._isFacingRight = true; break; case 'up': this._destinationTop = this._minTop; break; case 'down': this._destinationTop = this._maxTop; break; } if( currentDestLeft != this._destinationLeft || currentDestTop != this._destinationTop ){ this._moveToDestination(); } }, stopMoving: function(direction){ if( direction == 'right' || direction == 'left' ){ this._destinationLeft = this._jqelShark.position().left; } else if( direction == 'up' || direction == 'down' ){ this._destinationTop = this._jqelShark.position().top; } this._moveToDestination(); }, _moveToDestination: function(){ var moveSpeed = this._calculateSpeed(); this._jqelShark.stop(); this._flipHorizontal(); this._jqelShark.animate( { left: this._destinationLeft, top: this._destinationTop }, { duration: moveSpeed, easing: 'linear' } ); }, setBootState: function(){ this._boot(); }, _boot: function(){ var facingDirection = this.isFacingRight() ? 'right' : 'left'; this._jqelShark.stopTime( 'swim' ); this._jqelShark.css({ backgroundPosition: facingDirection + ' ' + this._jqelShark.height() * -8 + 'px' }); this._jqelShark.oneTime( 100, function(){ var _sharkInstance = MMI.App.getInstance().player1; jQuery( this ).everyTime( 130, 'swim', MMI.Util.bind( _sharkInstance._animate, _sharkInstance )); }); }, _animate: function(){ var facingDirection = this.isFacingRight() ? 'right' : 'left'; this._frame++; this._jqelShark.css({ backgroundPosition: facingDirection + ' ' + ( this._jqelShark.height() * -( this._frame % 8 )) + 'px' }); }, isFacingRight: function() { return this._isFacingRight; }, isFacingLeft: function() { return ( !this._isFacingRight ); }, /** * _calculateSpeed's existence is due to the fact that the animation function has a variable distance and thus needs a * variable animation time. * * Dis ting be hur cuz uh dat udder ting... */ _calculateSpeed: function() { var distanceToEndPoint, leftDeltaSquared, topDeltaSquared; var currentLeft = this._jqelShark.position().left; var currentTop = this._jqelShark.position().top; var baseSpeed = 300; /* Pythagoras to the rescue!!! */ leftDeltaSquared = Math.pow(( currentLeft - this._destinationLeft ), 2 ); topDeltaSquared = Math.pow(( currentTop - this._destinationTop ), 2 ); distanceToEndPoint = Math.sqrt( leftDeltaSquared + topDeltaSquared ); return distanceToEndPoint + baseSpeed; }, _flipHorizontal: function() { var direction = this.isFacingRight() ? 'right' : 'left'; this._jqelShark.css( 'background-position', direction + ' top' ); }, increaseScore: function( scoreType ) { switch(scoreType){ case 'booted': this._badBloatBooted++; break; case 'saved': this._goodBloatSaved++; break; case 'score': this._score += 5; break; case 'cheat': this._score = 200; break; } this._updateScoreBoard(); }, decreaseScore: function() { this._score -= 5; this._updateScoreBoard(); }, getScore: function( scoreType ){ switch( scoreType ){ case 'booted': return this._badBloatBooted; break; case 'saved': return this._goodBloatSaved; break; case 'score': return this._score; break; } }, resetScore: function(){ this._score = 0; this._badBloatBooted = 0; this._goodBloatSaved = 0; this._updateScoreBoard(); }, _updateScoreBoard: function(){ MMI.App.getInstance().updateScoreBoard(); } });