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 ball 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 {Ball}    p_balls The balls of the game. 
 */
function Collision(p_walls, p_balls)
{ this.v_walls = p_walls;
  this.v_balls = p_balls;
}

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

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

  /**
   *  Detects collisions between the ball and a wall, 
   *  between the paddle and the wall, and between the ball and the paddle.
   *  If a collision is detected, the collision is handled. 
   *  
   *  @method
   *  @name $wk$.Collision#handleCollision
   */  
  handleCollision:
    function()
    { var i       = null,
          l_balls = this.v_balls;
    
      /*jslint forin: true*/
      for (i in l_balls)
      { this.m_collision_ball_wall(l_balls[i]); }
    },

 // Detects whether the ball hits a wall and, if so, handles the collision.
  m_collision_ball_wall:
    function(p_ball)
    { var l_walls = this.v_walls;
      if (   (p_ball.x <= l_walls.left  + p_ball.r)
          || (p_ball.x >= l_walls.right - p_ball.r)
         )
      { p_ball.vx = -p_ball.vx; }

      if (   (p_ball.y <= l_walls.top    + p_ball.r)
          || (p_ball.y >= l_walls.bottom - p_ball.r)
         )
      { p_ball.vy = -p_ball.vy; }
    }
    
  //----------------------------------------------------------------------------
  // 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 Sat Nov 16 2013 10:53:54 GMT+0100 (MEZ) using the DocStrap template.