Path: blob/main/public/games/files/algaes-escapade/js/lib/goal.js
1038 views
/**1* The end goal of the game. All of the goals present in the game need to be2* active before the game is complete3*4* @author David North5*/6function goal()7{8//Load the variables required by gamejs.sprite.Sprite9goal.superConstructor.apply(this, [0, 0]);10this.image = gamejs.image.load('img/goal.png');1112var _size = this.image.getSize();13this.rect = new gamejs.Rect([0, 0], [_size[0], _size[1]]);1415/**16* @var boolean Whether the goal has been activated17*/18var _active = false;1920/**21* Sets the position of the object22*23* @param float x The X co-ordinate24* @param float y The Y co-ordinate25*26* @return goal27*/28this.setPosition = function(x, y){29this.rect.x = x;30this.rect.y = y;3132return this;33}3435/**36* Returns whether or not the goal has been activated37*38* @return boolean39*/40this.isActive = function(){41return _active;42}4344/**45* Updates the object, ready for the next draw request46*47* @param msDuration48*49* @return goal50*/51this.update = function(msDuration){52//The default state for thisobject is deactivated, unless a53//player has collided with it54_active = false;5556return this;57}5859/**60* Handles the collision between a playable and this object61*62* @return goal63*/64this.handleCollision = function( playable ){65//Modify the rectangle. The player shouldn't end the level until66//they are fully within the tube67var targetX = this.rect.x + (this.rect.width / 2);68targetX += (playable.rect.width / 2);6970var targetY = this.rect.y;71var rect = new gamejs.Rect([targetX, targetY], [40, 144]);7273//Check if the player has collided with this goal, and whether it74//should be activated75_active = playable.rect.collideRect(rect);7677return this;78}79}8081//Extend the playable object so that the parent is the sprite82gamejs.utils.objects.extend(goal, gamejs.sprite.Sprite);8384