/*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 "Ball"
////////////////////////////////////////////////////////////////////////////////
// import
var Math = $wk$.Math,
m_random = Math.random,
m_random_int = Math.randomInt,
m_random_num = Math.randomNum;
//------------------------------------------------------------------------------
// Constructor
//------------------------------------------------------------------------------
/**
* @class
*
* @name $wk$.Ball
* @this $wk$.Ball
* @param {CanvasRenderingContext2D} p_context
* The 2d context of the canvas upon which the ball is to be drawn.
* @param {Object} p_init
* An object that contains initialization values for the attributes
* <code>rInit</code>, <code>xInit</code>, <code>yInit</code>,
* <code>vxInit</code>, <code>vyInit</code>,
* <code>borderWidth</code>, <code>borderColor</code>,
* and <code>color</code>.
*/
function Ball(p_context, p_init)
{ var l_key = null;
this.v_context = p_context;
// initialize the ball
/*jslint forin: true*/
for (l_key in p_init)
{ this[l_key] = p_init[l_key]; }
this.reset();
}
//------------------------------------------------------------------------------
// End of Constructor
//------------------------------------------------------------------------------
Ball.prototype =
{ //----------------------------------------------------------------------------
// Methods (Data)
//----------------------------------------------------------------------------
/**
* The radius of the ball.
* @var {Number} $wk$.Ball#r
*/
/**
* The x-position of the ball.
* @var {Number} $wk$.Ball#x
*/
/**
* The y-position of the ball.
* @var {Number} $wk$.Ball#y
*/
/**
* The x-velocity of the ball.
* @var {Number} $wk$.Ball#vx
*/
/**
* The y-velocity of the ball.
* @var {Number} $wk$.Ball#vy
*/
/**
* The initial radius of the ball.
* @var {Number} $wk$.Ball#rInit
*/
/**
* The initial x-position of the ball.
* @var {Number} $wk$.Ball#xInit
*/
/**
* The initial y-position of the ball.
* @var {Number} $wk$.Ball#yInit
*/
/**
* The initial x-velocity of the ball.
* @var {Number} $wk$.Ball#vxInit
*/
/**
* The initial y-velocity of the ball.
* @var {Number} $wk$.Ball#vyInit
*/
/**
* The width of the border of the ball.
* @var {Number} $wk$.Ball#borderWidth
*/
/**
* The color of the border of the ball.
* @var {String} $wk$.Ball#borderColor
*/
/**
* The color of the ball.
* @var {String} $wk$.Ball#color
*/
//----------------------------------------------------------------------------
// Methods (Logic)
//----------------------------------------------------------------------------
/**
* Resets the ball:
* Moves the ball to its starting position
* and computes randomly a velocity vector.
*
* @method
* @name $wk$.Ball#reset
*/
reset:
function()
{ this.r = m_random_int(this.rInit);
this.x = m_random_int(this.xInit);
this.y = m_random_int(this.yInit);
this.vx = (m_random()<0.5?1:-1) * m_random_num(this.vxInit);
this.vy = (m_random()<0.5?1:-1) * m_random_num(this.vyInit);
},
/**
* Moves the ball in direction (vx,vy); the step size depends on FPS.
*
* @method
* @name $wk$.Ball#move
* @param {Number} p_delta_t The time elapsed since
* the last call of <code>move</code>.
*/
move:
function(p_delta_t)
{ this.x += this.vx*p_delta_t/1000;
this.y += this.vy*p_delta_t/1000;
},
//----------------------------------------------------------------------------
// Methods (View)
//----------------------------------------------------------------------------
/**
* Draws the ball at its current position onto a 2d context.
*
* @method
* @name $wk$.Ball#draw
*/
draw:
function()
{ this.v_context.beginPath();
this.v_context.arc(this.x, this.y, this.r, 0, 2*Math.PI, true);
this.v_context.lineWidth = this.borderWidth;
this.v_context.strokeStyle = this.borderColor;
this.v_context.fillStyle = this.color;
this.v_context.stroke();
this.v_context.fill();
}
//----------------------------------------------------------------------------
// End of Methods
//----------------------------------------------------------------------------
};
$wk$.Ball = Ball;
////////////////////////////////////////////////////////////////////////////////
// End of Class "Ball"
////////////////////////////////////////////////////////////////////////////////
}(this.$wk$));