Path: blob/main/public/games/files/algaes-escapade/js/lib/player.js
1037 views
/**1* Represents a player. Players have control over multiple playables, but only2* one playable at a time.3*4* @author David North5*/6include_once(['lib/playable.js']);7function player()8{9//The maximum X velocity a player can trvel (heading right)10const MAX_X_VELOCITY = 200;1112//The minimum X velocity a player can trvel (heading left)13const MIN_X_VELOCITY = -200;1415//The minimum Y velocity a player can trvel (heading up)16const MIN_Y_VELOCITY = -350;1718/**19* @var array An array of playables at the players disposal20*/21var _playables = new gamejs.sprite.Group();2223/**24* @var float How fast the player is currently walking (minus25* figure represents left, positive right)26*/27var _walkVelocity = 0;2829/**30* @var int The number of clones created31*/32var _numClones = 0;3334//Start the player with a single playable35_playables.add( new playable() );3637/**38* @var int The current index of the playable currently under the39* players control40*/41var _currentIndex = 0;4243var _selectedImg = gamejs.image.load('img/selected.png');4445/**46* Handles player input.47*48* @param world world The world this event came from49* @param gamejs.Event event The event that fired50*/51this.handleInput = function(event){52//If a key has been pressed then check it to see if an53//action needs taking place54if ( event.type === gamejs.event.KEY_DOWN )55{56switch( event.key )57{58//The space, w, or up key denotes a jump. The player is not allowed59//to jump if they are already falling or jumping60case gamejs.event.K_SPACE:61case gamejs.event.K_w:62case gamejs.event.K_UP:63if ( this.getCurrentPlayable().getMovement() == 'walk' )64{65this.setVelocity( this.getVelocity().x, MIN_Y_VELOCITY );66this.getCurrentPlayable().setMovement('jump');67}68break;6970//The A key, or left arrow starts to move the player left71case gamejs.event.K_a:72case gamejs.event.K_LEFT:73_walkVelocity = MIN_X_VELOCITY;74break;7576//The D key, or right arrow starts to move the player right77case gamejs.event.K_d:78case gamejs.event.K_RIGHT:79_walkVelocity = MAX_X_VELOCITY;80break;8182//The C key clones a playable, so that the player can use83//that instead84case gamejs.event.K_c:85if ( _numClones < 100 )86{87this.clone();88_numClones++;89}90break;9192//The Tab key switches between the playables that the93//player can control94case gamejs.event.K_TAB:95_walkVelocity = 0;96this.moveToNext();97}98}99else if ( event.type === gamejs.event.KEY_UP )100{101switch( event.key )102{103//Letting go of direction events will stop the player dead104case gamejs.event.K_a:105case gamejs.event.K_LEFT:106case gamejs.event.K_d:107case gamejs.event.K_RIGHT:108_walkVelocity = 0;109break;110}111}112}113114/**115* Returns the number of clonmes that the player has created116*117* @return int118*/119this.getNumClones = function(){120return _numClones;121}122123/**124* Sets the velocity of the current playable125*126* @param float x The X velocity127* @param float y The Y velocity128*129* @return player130*/131this.setVelocity = function( x, y ){132var playable = this.getCurrentPlayable();133playable.setVelocity( x, y );134135return this;136}137138/**139* Returns an object taht contains the X and Y velocity of the current140* playable141*142* @return object143*/144this.getVelocity = function(){145return this.getCurrentPlayable().getVelocity();146}147148/**149* Updates all of the playable objects150*151* @param int msDuration152*153* @return player154*/155this.update = function( msDuration ){156if ( _walkVelocity )157{158this.setVelocity( _walkVelocity, this.getVelocity().y );159}160161_playables.update( msDuration );162return this;163}164165/**166* Draws all of the playable objects to the surface167*168* @param object mainSurface169*170* @return player171*/172this.draw = function( mainSurface ){173_playables.draw( mainSurface );174175var x = this.getCurrentPlayable().getX();176var y = this.getCurrentPlayable().getY();177178x += (this.getCurrentPlayable().rect.width / 2);179x -= (_selectedImg.getSize()[0] / 2);180181y -= (_selectedImg.getSize()[1] + 2);182183var rect = new gamejs.Rect([x, y])184mainSurface.blit(_selectedImg, rect);185return this;186}187188/**189* Moves to the next available playable for control. If the last playable190* is selected then the first element is sed instead191*192* @return player193*/194this.moveToNext = function(){195if ( (_currentIndex + 1) == this.getPlayables().sprites().length )196{197_currentIndex = 0;198}199else200{201_currentIndex++202}203204return this;205}206207/**208* Gets all of the playables available to the player209*210* @return array211*/212this.getPlayables = function(){213return _playables;214}215216/**217* Gets the current playable object that the player is in control of218*219* @return playable220*/221this.getCurrentPlayable = function(){222return this.getPlayables().sprites()[_currentIndex];223}224225/**226* Clones the current playable and adds it to the playables at the players227* disposal228*229* @return player230*/231this.clone = function(){232//Get the template for the clone, and create a clone to the left233//of the current playable234var template = this.getCurrentPlayable();235var clone = new playable();236var newX = ( template.getX() - (template.rect.width * 2) );237238clone.setPosition(newX, template.getY() - template.image.getSize()[1]);239240_playables.add( clone );241242return this;243}244}245246247