Skip to content

Commit

Permalink
Merge pull request #13 from hobnob/features/bugFixes
Browse files Browse the repository at this point in the history
Moves the lever collission detection
  • Loading branch information
hobnob committed Nov 28, 2012
2 parents 6e121f1 + 5862743 commit c3b2586
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 8 deletions.
Binary file added img/selected.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
23 changes: 19 additions & 4 deletions js/lib/lever.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,14 @@ function lever()
this.image.crop( new gamejs.Rect( [0,0], [83,43] ));

var _size = this.image.getSize();
this.rect = new gamejs.Rect([0, 0], [_size[0], _size[1]]);

/**
* @var boolean Whether or the lever is being held down
*/
var _heldDown = false;

this.rect = new gamejs.Rect([0, 0], [_size[0], _size[1]]);

/**
* Overrides the setState method of the parent so that the object changes
* depending on whether it is on or off
Expand Down Expand Up @@ -73,8 +74,12 @@ function lever()
* @return lever
*/
this.handleCollision = function( playable ){
//If the player is colliding with this lever then it is held down
_heldDown = true;

if ( _hasCollided(this, playable ) )
{
//If the player is colliding with this lever then it is held down
_heldDown = true;
}

return this;
}
Expand All @@ -92,7 +97,7 @@ function lever()
//Only check the event if a key has been pushed and the player is
//colliding with the lever
if ( event.type === gamejs.event.KEY_DOWN
&& gamejs.sprite.collideRect(playable, this) )
&& _hasCollided(this, playable) )
{
//If the key was 'enter' or 'e' then set the new state of the lever
switch( event.key )
Expand All @@ -104,6 +109,16 @@ function lever()
}
}
}

var _hasCollided = function ( self, playable )
{
var _collideRect = new gamejs.Rect(
[self.rect.x + 34, self.rect.y],
[32 , _size[1]]
);

return _collideRect.collideRect(playable.rect);
}
}

//Set the parent of the lever to io
Expand Down
13 changes: 13 additions & 0 deletions js/lib/player.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ function player()
*/
var _currentIndex = 0;

var _selectedImg = gamejs.image.load('img/selected.png');

/**
* Handles player input.
*
Expand Down Expand Up @@ -170,6 +172,17 @@ function player()
*/
this.draw = function( mainSurface ){
_playables.draw( mainSurface );

var x = this.getCurrentPlayable().getX();
var y = this.getCurrentPlayable().getY();

x += (this.getCurrentPlayable().rect.width / 2);
x -= (_selectedImg.getSize()[0] / 2);

y -= (_selectedImg.getSize()[1] + 2);

var rect = new gamejs.Rect([x, y])
mainSurface.blit(_selectedImg, rect);
return this;
}

Expand Down
7 changes: 5 additions & 2 deletions js/lib/tooltip.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,11 @@ function tooltip()
tooltip.timer = null;
}

this.handleCollision = function(){
this.show();
this.handleCollision = function( playable, isCurrentPlayer){
if ( isCurrentPlayer )
{
this.show();
}
}

this.draw = function(){
Expand Down
8 changes: 7 additions & 1 deletion js/lib/world.js
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,13 @@ function world()
{
if ( typeof(colliders[i].b.handleCollision) == 'function' )
{
colliders[i].b.handleCollision( colliders[i].a );
var isCurrentPlayer = false;
if ( colliders[i].a === _p.getCurrentPlayable() )
{
isCurrentPlayer = true;
}

colliders[i].b.handleCollision( colliders[i].a, isCurrentPlayer );
}
}

Expand Down
2 changes: 1 addition & 1 deletion js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ gamejs.preload([
'img/splash-screen.png','img/new-game.png', 'img/bg.png','img/player.png',
'img/blank.png','img/switch.png','img/door.png','img/goal.png',
'img/platform-left.png','img/platform-right.png',
'img/platform-middle.png'
'img/platform-middle.png', 'img/selected.png'
]);

gamejs.ready(function() {
Expand Down

0 comments on commit c3b2586

Please sign in to comment.