/*jslint plusplus: true, white: true, indent: 2, maxlen: 90 */
/*global $wk$, $app$*/
/**
* @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/).
*/
$wk$(this.document,
function(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.
*/
$wk$.WKcClass(
{ name: "$app$.game.snake.Item",
methods:
{ init:
function(p_x, p_y)
{ this.x = p_x;
this.y = p_y;
}
/**
* 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
*/
},
staticMethods:
{ /**
* 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).
*/
initialize:
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.
this.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;
}
}
}
});
////////////////////////////////////////////////////////////////////////////////
// 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.
*/
$wk$.WKcClass(
{ name: "$app$.game.snake.Body",
isa: "$app$.game.snake.Item",
methods: { init: function(p_x, p_y) { this.$super$(p_x, p_y); }
/**
* 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
*/
}
});
////////////////////////////////////////////////////////////////////////////////
// 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.
*/
$wk$.WKcClass(
{ name: "$app$.game.snake.Wall",
isa: "$app$.game.snake.Item",
methods: { init: function(p_x, p_y) { this.$super$(p_x, p_y); } }
});
////////////////////////////////////////////////////////////////////////////////
// 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.
*/
$wk$.WKcClass(
{ name: "$app$.game.snake.Food",
isa: "$app$.game.snake.Item",
methods: { init: function(p_x, p_y) { this.$super$(p_x, p_y); } }
});
////////////////////////////////////////////////////////////////////////////////
// End of Classes
////////////////////////////////////////////////////////////////////////////////
});