Source: paddle.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 "Paddle"
////////////////////////////////////////////////////////////////////////////////

// import
var Math         = $wk$.Math,
    m_value      = Math.value,
    m_random_num = Math.randomNum; 

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

/** 
 *  @class
 *  @name $wk$.Paddle
 *  @this $wk$.Paddle
 *  @param {int}                      p_id
 *         The identifier of the paddle (i.e. the number of the paddle)
 *  @param {CanvasRenderingContext2D} p_context 
 *         The 2d context of the canvas upon which the paddle 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 Paddle(p_context, p_init, p_id)
{ var l_key = null;

  this.v_context = p_context;
  this.v_id      = p_id;

  // initialize the paddle
  /*jslint forin: true*/
  for (l_key in p_init)
  { this[l_key] = m_random_num(m_value(p_init[l_key], p_id)); }

  this.reset();
}

//------------------------------------------------------------------------------
// Constants
//------------------------------------------------------------------------------

/** @constant {int} $wk$.Paddle.LEFT  */
Paddle.LEFT  = "left";
/** @constant {int} $wk$.Paddle.RIGHT */
Paddle.RIGHT = "right";
/** @constant {int} $wk$.Paddle.UP    */
Paddle.UP    = "up";
/** @constant {int} $wk$.Paddle.DOWN  */
Paddle.DOWN  = "down";

//------------------------------------------------------------------------------
// End of Constants
//------------------------------------------------------------------------------

Paddle.prototype =
{ //----------------------------------------------------------------------------
  // Methods (Data)
  //----------------------------------------------------------------------------

  /** 
   *  The width of the paddle.              
   *  @var {Number} $wk$.Paddle#width  
   */
  /** 
   *  The he height the paddle.              
   *  @var {Number} $wk$.Paddle#height
   */
  /** 
   *  The x-position of the paddle.
   *  @var {Number} $wk$.Paddle#x  
   */
  /** 
   *  The y-position of the paddle.
   *  @var {Number} $wk$.Paddle#y
   */
  /** 
   *  The x-velocity of the paddle.
   *  @var {Number} $wk$.Paddle#vx
   */
  /** 
   *  The y-velocity of the paddle.
   *  @var {Number} $wk$.Paddle#vy 
   */
  /** 
   *  The x-acceleration of the paddle.
   *  @var {Number} $wk$.Paddle#ax
   */
  /** 
   *  The y-acceleration of the paddle.
   *  @var {Number} $wk$.Paddle#ay 
   */
    
  /** 
   *  The initial x-position of the paddle.
   *  @var {Number} $wk$.Paddle#xInit
   */
  /** 
   *  The initial y-position of the paddle.
   *  @var {Number} $wk$.Paddle#yInit
   */
  /** 
   *  The initial x-velocity of the paddle.  
   *  @var {Number} $wk$.Paddle#vxInit
   */
  /** 
   *  The initial y-velocity of the paddle.
   *  @var {Number} $wk$.Paddle#vyInit
   */
  /** 
   *  The initial x-acceleration of the paddle.  
   *  @var {Number} $wk$.Paddle#axInit
   */
  /** 
   *  The initial y-acceleration of the paddle.
   *  @var {Number} $wk$.Paddle#ayInit
   */
      
  /** 
   *  The width of the border of the paddle.
   *  @var {Number} $wk$.Paddle#borderWidth
   */
  /** 
   *  The color of the border of the paddle.
   *  @var {String} $wk$.Paddle#borderColor
   */
  /** 
   *  The color of the paddle.
   *  @var {String} $wk$.Paddle#color
   */
    
  //----------------------------------------------------------------------------
  // Methods (Logic)
  //----------------------------------------------------------------------------

  /** 
   *  Resets the paddle: 
   *    Moves the paddle to its starting position.
   * 
   *  @method
   *  @name $wk$.Paddle#reset
   */
  reset:
    function()
    { this.x  = m_random_num(this.xInit);
      this.y  = m_random_num(this.yInit);
      this.vx = 0;
      this.vy = 0;
      this.ax = m_random_num(this.axInit);
      this.ay = m_random_num(this.ayInit);
    },

  /**
   *  Starts the paddle moving in a particular direction.
   *
   *  @method
   *  @name $wk$.Paddle#start
   *  
   *  @param {int} p_direction Either <code>$wk$.Paddle.LEFT</code>,
   *                                  <code>$wk$.Paddle.RIGHT</code>,
   *                                  <code>$wk$.Paddle.UP</code>, or
   *                                  <code>$wk$.Paddle.DOWN</code>.
   */
  start:
    function(p_direction)
    { console.log(typeof this.vx)
      if (this.vx === 0 && this.vy === 0)  
      // react only if the paddle is not already moving
      { if (p_direction === Paddle.LEFT)
        { this.vx = -this.vxInit; }
        else if (p_direction === Paddle.RIGHT)
        { this.vx = +this.vxInit; }
        else if (p_direction === Paddle.UP)
        { this.vy = -this.vyInit; }
        else if (p_direction === Paddle.DOWN)
        { this.vy = +this.vyInit; }
      }
    },
  
  /**
   *  Stops the paddle moving.
   *
   *  @method
   *  @name $wk$.Paddle#stop
   */
  stop:
    function(p_direction)
    { this.vx = 0;
      this.vy = 0;
    },
    

  /** 
   *  Moves the paddle in direction (vx,vy); the step size depends on FPS. 
   * 
   *  @method
   *  @name $wk$.Paddle#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;
      if (this.vx)
      { this.vx += this.ax*p_delta_t/1000 * (this.vx > 0 ? 1 : -1); }
      if (this.vy)
      { this.vy += this.ay*p_delta_t/1000 * (this.vy > 0 ? 1 : -1); }
    },

  //----------------------------------------------------------------------------
  // Methods (View)
  //----------------------------------------------------------------------------

  /** 
   *  Draws the paddle at its current position onto a 2d context.
   *
   *  @method
   *  @name $wk$.Paddle#draw
   */
  draw:
    function()
    { this.v_context.beginPath();
      this.v_context.rect(this.x, this.y, this.width, this.height);
      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$.Paddle = Paddle;

////////////////////////////////////////////////////////////////////////////////
// End of Class "Paddle"
////////////////////////////////////////////////////////////////////////////////

}(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:57:52 GMT+0100 (MEZ) using the DocStrap template.