Source: Item.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($app$, document)
{ "use strict";
////////////////////////////////////////////////////////////////////////////////
// Class "Item"
////////////////////////////////////////////////////////////////////////////////

//------------------------------------------------------------------------------
// Constructor
//------------------------------------------------------------------------------
/** 
 *  @class
 *  @name $app$.game.snake.Item
 *  
 *  @param {int} p_x The x position of the item.
 *  @param {int} p_y The y position of the item.
 */
function Item(p_x, p_y)
{ this.x = p_x;
  this.y = p_y;
}


//------------------------------------------------------------------------------
// Static Members
//------------------------------------------------------------------------------

/**
 *  Initializes the class <code>Item</code> and all its subclasses.
 *  
 *  @method
 *  @name $app$.game.snake.Item.init
 *  
 *  @param {Object} p_init 
 *         Should contain the attributes
 *           <code>size</code> 
 *             (the size of every item; size == width == height) and
 *           <code>subclasses</code> 
 *             (an object; for every subclasses of 
 *              <code>Item</code> there should be one attribute;
 *              the name of the attribute must be equal to
 *              the name of the subclass and the value
 *              should contain proper initialization values for
 *              the subclass).
 */

Item.init =
function (p_init)
{ var l_size       = p_init.size,
      l_size2,
      l_anchor,
      l_subclasses = p_init.subclasses,
      l_subclass   = null,
      l_init,
      l_canvas,
      l_context;

  // All items have the same size. So it is stored as prototype value.
  Item.prototype.size = l_size;
  
  // Create the image for each item type. 
  /*jslint forin: true*/
  for(l_subclass in l_subclasses)
  { l_init = l_subclasses[l_subclass];
  
    l_canvas = document.createElement("canvas");
    l_canvas.width  = l_size;
    l_canvas.height = l_size;
    l_context = l_canvas.getContext("2d");

    l_context.beginPath();
    
    if (l_init.type === "circle")
    { l_anchor = l_size/2;
      l_context.arc(l_anchor, l_anchor, l_init.radius, 0, 2*Math.PI, true); 
    }
    else if (l_init.type === "square")
    { l_size2  = l_init.size;
      l_anchor = (l_size-l_size2)/2;
      l_context.rect(l_anchor, l_anchor, l_size2, l_size2);
    }

    l_context.fillStyle   = l_init.color; 
    l_context.lineWidth   = l_init.borderSize;
    l_context.strokeStyle = l_init.borderColor;
    
    l_context.stroke();
    l_context.fill();
    
    // All items of a subclass of Item share the same image.
    // So that image is stored as prototype value of the particular class.
    $app$.game.snake[l_subclass].prototype.image = l_canvas;
  }
};

//------------------------------------------------------------------------------
// Attributes and Methods
//------------------------------------------------------------------------------

Item.prototype =
{ /** 
   *  The pixel size of an item. This value is equal for all items!
   *  @var {String} $app$.game.snake.Item#size
   */
  /** 
   *  The x-position of the item.
   *  @var {Number} $app$.game.snake.Item#x  
   */
  /** 
   *  The y-position of the item.
   *  @var {Number} $app$.game.snake.Item#y  
   */
  /** 
   *  The image (a mini canvas) of the item.
   *  @var {String} $app$.game.snake.Item#image 
   */
};

$app$.game.snake.Item = Item;

////////////////////////////////////////////////////////////////////////////////
// Class "Body"
////////////////////////////////////////////////////////////////////////////////

/** 
 *  @class
 *  @name    $app$.game.snake.Body
 *  @extends $app$.game.snake.Item
 *  
 *  @param {int} p_x The x position of the item.
 *  @param {int} p_y The y position of the item.
 */
function Body(p_x, p_y)
{ Item.call(this, p_x, p_y); }


// Inherit all attributes and methods from "Item".
Body.prototype = Object.create(Item.prototype);

/** 
 *  The predecessor of the body item. Each body item of the snake 
 *  with exception of the head has another body item as predecessor.
 *  @var {Number} $app$.game.snake.Body#pred  
 */

$app$.game.snake.Body = Body;

////////////////////////////////////////////////////////////////////////////////
// Class "Wall"
////////////////////////////////////////////////////////////////////////////////

/** 
 *  @class
 *  @name    $app$.game.snake.Wall
 *  @extends $app$.game.snake.Item
 *  
 *  @param {int} p_x The x position of the item.
 *  @param {int} p_y The y position of the item.
 */
function Wall(p_x, p_y)
{ Item.call(this, p_x, p_y); }

// Inherit all attributes and methods from "Item".
Wall.prototype = Object.create(Item.prototype);

$app$.game.snake.Wall = Wall;

////////////////////////////////////////////////////////////////////////////////
// Class "Food"
////////////////////////////////////////////////////////////////////////////////

/** 
 *  @class
 *  @name    $app$.game.snake.Food
 *  @extends $app$.game.snake.Item
 *  
 *  @param {int} p_x The x position of the item.
 *  @param {int} p_y The y position of the item.
 */
function Food(p_x, p_y)
{ Item.call(this, p_x, p_y); }


// Inherit all attributes and methods from "Item".
Food.prototype = Object.create(Item.prototype);

$app$.game.snake.Food = Food;

////////////////////////////////////////////////////////////////////////////////
// End of Classes
////////////////////////////////////////////////////////////////////////////////
}(this.$app$, this.document)); 
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 Sun Dec 15 2013 11:28:44 GMT+0100 (MEZ) using the DocStrap template.