Path: blob/main/public/games/files/algaes-escapade/js/lib/playable.js
1038 views
/**1* Represents a playable sprite. This is manipulated by the player object which2* determines which playable should be modified3*4* @author David North5*/6function playable()7{8//Load the variables required by gamejs.sprite.Sprite9playable.superConstructor.apply(this, [0, 0]);10this.image = gamejs.image.load('img/player.png');1112this.rect = new gamejs.Rect([0, 0]);1314/**15* @var int The amount of health the playable has16*/17var _health = 100;1819/**20* @var float The velocity left to right that the playable is experiencing21*/22var _velocityX = 0.0;2324/**25* @var float The velocity bottom to top that this playable is experiencing26*/27var _velocityY = 0.0;2829/**30* @var string How the player is currently moving (walk, jump, fall etc.)31*/32var _moveType = '';3334/**35* Sets how the player is currently moving, and generates the correct36* sprite image37*38* @param string type The type of movement39*40* @return playable41*/42this.setMovement = function( type ){43//Only change the type if it has actually changed. No need to do any44//processing otherwise45if ( type != _moveType )46{47//Get the old size so that we can modify the X and Y co-ordinates48//accordingly if the sprite changes height or width49var oldSize = this.image.getSize();5051//Set the correct sprite image depending on the type of movement52switch(type)53{54case 'walk':55this.image.crop( new gamejs.Rect([0,0], [46,55] ));56break;57case 'jump':58this.image.crop( new gamejs.Rect([0,55], [46,69] ));59break;60case 'fall':61this.image.crop( new gamejs.Rect([46,55], [46,69] ));62break;63}6465//Get and set the new sizes66var _size = this.image.getSize();67this.rect.width = _size[0];68this.rect.height = _size[1];6970//Set the new X and Y co-ordinates71this.rect.x += oldSize[0] - _size[0];72this.rect.y += oldSize[1] - _size[1];7374_moveType = type;75}7677return this;78}7980/**81* Returns the current movement type of the playable82*83* @return string84*/85this.getMovement = function(){86return _moveType;87}8889/**90* Sets the position of the object91*92* @param float x The X co-ordinate93* @param float y The Y co-ordinate94*95* @return playable96*/97this.setPosition = function(x, y){98this.setX(x);99this.setY(y);100101return this;102}103104/**105* Sets the X value of the playable (useful when initiating a new playable,106* teleporting, etc)107*108* @param float x109*110* @return playable111*/112this.setX = function( x ){113if ( typeof(x) != 'number')114{115throw 'Value for X must be a number';116}117118this.rect.x = x;119return this;120}121122/**123* Gets the current X position124*125* @return float126*/127this.getX = function(){128return this.rect.x;129}130131/**132* Sets the Y value of the playable (useful when initiating a new playable,133* teleporting, etc)134*135* @param float y136*137* @return playable138*/139this.setY = function( y ){140if ( typeof(y) != 'number')141{142throw 'Value for Y must be a number';143}144145this.rect.y = y;146return this;147}148149/**150* Gets the current Y position151*152* @return float153*/154this.getY = function(){155return this.rect.y;156}157158/**159* Sets the velocity of the playable so that the speed in one way or160* another can be set161*162* @param float x The velocity left to right163* @param float y The velocity bottom to top164*165* @return playable166*/167this.setVelocity = function( x, y ){168if ( typeof(x) != 'number')169{170throw 'Value for X must be a number';171}172173if ( typeof(y) != 'number')174{175throw 'Value for Y must be a number';176}177178_velocityX = x;179_velocityY = y;180return this;181}182183/**184* Gets the X and Y velocity for this playable object by returning an object185* with an x and y parameter186*187* @return object188*/189this.getVelocity = function(){190return { 'x':_velocityX, 'y':_velocityY };191}192193/**194* Updates the object, ready for the next draw request195*196* @param msDuration197*198* @return playable199*/200this.update = function( msDuration ){201//Set the default movement to wealking, if it hasn't already been set202if ( this.getMovement() == '' )203{204this.setMovement('walk');205}206207//If the player is falling then set that sprite up. Other movement208//types are dealt with outside this object209if ( this.getVelocity().y > 0 )210{211this.setMovement('fall');212}213214//Calculate the distance the playable has moved since the last frame215var distanceX = (this.getVelocity().x * (msDuration/1000));216var distanceY = (this.getVelocity().y * (msDuration/1000));217218//Move the playable the calculated distance219this.rect.moveIp( distanceX, distanceY );220221return this;222}223}224225//Extend the playable object so that the parent is the sprite226gamejs.utils.objects.extend(playable, gamejs.sprite.Sprite);227228229