/*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"
////////////////////////////////////////////////////////////////////////////////
var Math = $wk$.Math; // import
//------------------------------------------------------------------------------
// Auxiliaries
//------------------------------------------------------------------------------
function m_distance(p_x1, p_y1, p_x2, p_y2)
{ var l_dx = p_x1-p_x2,
l_dy = p_y1-p_y2;
return Math.sqrt(l_dx*l_dx+l_dy*l_dy);
}
//------------------------------------------------------------------------------
// 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; }
},
/**
* @method
* @name $wk$.Collision#handleMouseClick
* @param {Object} p_mouse_pos
* @returns The index of the ball that has been hit, if any.
*/
handleMouseClick:
function(p_mouse_pos)
{ var i = null,
l_balls = this.v_balls,
l_ball,
l_hit = null,
l_m_x = p_mouse_pos.x,
l_m_y = p_mouse_pos.y;
/*jslint forin: true*/
for (i in l_balls)
{ l_ball = l_balls[i];
if (m_distance(l_ball.x, l_ball.y, l_m_x, l_m_y) <= l_ball.r)
{ l_hit = i; }
}
return l_hit;
}
//----------------------------------------------------------------------------
// End of Methods
//----------------------------------------------------------------------------
};
$wk$.Collision = Collision;
////////////////////////////////////////////////////////////////////////////////
//End of Class "Collision"
////////////////////////////////////////////////////////////////////////////////
}(this.$wk$));