Source: collision.js

/*jslint plusplus: true, white: true, indent: 2, maxlen: 90 */
/**
 *  @author    Wolfgang Kowarschick
 *  @copyright 2013, Wolfgang Kowarschick
 *  <br/>
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted under the terms of the 
 *  Creative Commons License Attribution-NonCommercial-ShareAlike 3.0 Unported    
 *  (CC BY-NC-SA 3.0: http://creativecommons.org/licenses/by-nc-sa/3.0/). 
 */

(function($wk$)
{ "use strict"; 

////////////////////////////////////////////////////////////////////////////////
// Class "Collision"
////////////////////////////////////////////////////////////////////////////////

//------------------------------------------------------------------------------
// Constructor
//------------------------------------------------------------------------------

/** 
 *  The main method <code>handleCollision</code> detects collisions
 *  between the paddle and a wall.
 *
 *  @class
 *  @name  $wk$.Collision 
 *  @this  $wk$.Collision 
 *  @param {Object}  p_walls  The walls of the game: An object with 
 *                            four integer attributes: <code>left</code>,
 *                            <code>right</code>, <code>top</code>, and
 *                            <code>bottom</code>.
 *  @param {Paddle}  p_paddle The paddle of the game. 
 */
function Collision(p_walls, p_paddle)
{ this.v_walls  = p_walls;
  this.v_paddle = p_paddle;
}

//------------------------------------------------------------------------------
// End of constructor
//------------------------------------------------------------------------------

Collision.prototype =
{ //----------------------------------------------------------------------------
  // Methods (Logic)
  //----------------------------------------------------------------------------

  /**
   *  Detects collisions between the paddle and a wall, 
   *  between the paddle and the wall, and between the paddle and the paddle.
   *  If a collision is detected, the collision is handled. 
   *  
   *  @method
   *  @name $wk$.Collision#handleCollision
   */  
    // Detects whether the paddle hits a wall and, if so, handles the collision.
  handleCollision:
    function()
    { var l_paddle = this.v_paddle,
          l_walls  = this.v_walls;
      if (   (l_paddle.vx < 0  && l_paddle.x <= l_walls.left)
          || (l_paddle.vx > 0  && l_paddle.x >= l_walls.right - l_paddle.width)
          || (l_paddle.vy < 0  && l_paddle.y <= l_walls.top)
          || (l_paddle.vy > 0  && l_paddle.y >= l_walls.bottom - l_paddle.height)
         )
      { l_paddle.stop();
        if (l_paddle.x < l_walls.left) 
        {l_paddle.x = l_walls.left; }
        if (l_paddle.x > l_walls.right - l_paddle.width) 
        {l_paddle.x = l_walls.right - l_paddle.width; }
        if (l_paddle.y < l_walls.top) 
        {l_paddle.y = l_walls.top; }
        if (l_paddle.y > l_walls.bottom - l_paddle.height) 
        {l_paddle.y = l_walls.bottom - l_paddle.height; }
      }
    }
    
  //----------------------------------------------------------------------------
  // End of Methods
  //----------------------------------------------------------------------------
};

$wk$.Collision = Collision;

////////////////////////////////////////////////////////////////////////////////
//End of Class "Collision"
////////////////////////////////////////////////////////////////////////////////

}(this.$wk$)); 
W. Kowarschick © 2013 (CC BY-NC-SA 3.0: http://creativecommons.org/licenses/by-nc-sa/3.0/)
Documentation generated by JSDoc 3.3.0-dev on Wed Nov 13 2013 15:01:11 GMT+0100 (MEZ) using the DocStrap template.