/*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_paddles The paddles of the game.
*/
function Collision(p_walls, p_paddles)
{ this.v_walls = p_walls;
this.v_paddles = p_paddles;
}
//------------------------------------------------------------------------------
// 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
*/
handleCollision:
function()
{ var i = null,
l_paddles = this.v_paddles;
/*jslint forin: true*/
for (i in l_paddles)
{ this.m_collision_paddle_wall(l_paddles[i]); }
},
// Detects whether the paddle hits a wall and, if so, handles the collision.
m_collision_paddle_wall:
function(p_paddle)
{ var l_walls = this.v_walls;
if ( (p_paddle.vx < 0 && p_paddle.x <= l_walls.left)
|| (p_paddle.vx > 0 && p_paddle.x >= l_walls.right - p_paddle.width)
|| (p_paddle.vy < 0 && p_paddle.y <= l_walls.top)
|| (p_paddle.vy > 0 && p_paddle.y >= l_walls.bottom - p_paddle.height)
)
{ p_paddle.stop();
if (p_paddle.x < l_walls.left)
{p_paddle.x = l_walls.left; }
if (p_paddle.x > l_walls.right - p_paddle.width)
{p_paddle.x = l_walls.right - p_paddle.width; }
if (p_paddle.y < l_walls.top)
{p_paddle.y = l_walls.top; }
if (p_paddle.y > l_walls.bottom - p_paddle.height)
{p_paddle.y = l_walls.bottom - p_paddle.height; }
}
}
//----------------------------------------------------------------------------
// End of Methods
//----------------------------------------------------------------------------
};
$wk$.Collision = Collision;
////////////////////////////////////////////////////////////////////////////////
//End of Class "Collision"
////////////////////////////////////////////////////////////////////////////////
}(this.$wk$));