/*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 "Plane"
////////////////////////////////////////////////////////////////////////////////
// import
var Item = $app$.game.snake.Item,
Wall = $app$.game.snake.Wall,
Food = $app$.game.snake.Food;
/**
* @class
* @name $app$.game.snake.Plane
*
* @param {Object} p_init Should contain the attributes
* <code>canvasID</code>
* (the id of the canvas tag),
* <code>colsNr</code>
* (the number of columns of the plane),
* <code>rowsNr</code>
* (the number of rows of the plane),
* <code>item</code>
* (the initialization object for the items
* that are placed on the plane).
*/
$wk$.WKcClass(
{ name: "$app$.game.snake.Plane",
methods:
{ init:
function (p_init)
{ var l_init_item = p_init.item,
l_item_size = l_init_item.size,
l_canvas = document.getElementById(p_init.canvasID);
// Initialize the canvas.
l_canvas.width = p_init.colsNr*l_item_size;
l_canvas.height = p_init.rowsNr*l_item_size;
this.v_canvas = l_canvas;
this.v_context = l_canvas.getContext("2d");
this.v_cols_nr = p_init.colsNr;
this.v_rows_nr = p_init.rowsNr;
// Initialize the class "Item".
Item.initialize(l_init_item);
this.reset();
},
/**
* Resets the plane.
*
* @method
* @name $app$.game.snake.Plane#reset
*/
reset:
function()
{ var i, j, m, n,
l_plane = [],
l_canvas = this.v_canvas,
l_rows_nr = this.v_rows_nr,
l_cols_nr = this.v_cols_nr;
// No food has been placed on the canvas.
this.foodNr = 0;
// Clear the canvas
this.v_context.clearRect(0, 0, l_canvas.width, l_canvas.height);
// Initialize the plane. Draw the wall.
this.v_plane = l_plane;
for (i=0, n=l_cols_nr, m=l_rows_nr-1; i<n; i++)
{ l_plane[i] = [];
this.addItem(new Wall(i,0));
this.addItem(new Wall(i,m));
}
for (j=1, m=l_rows_nr-1, n=l_cols_nr-1; j<m; j++)
{ this.addItem(new Wall(0,j));
this.addItem(new Wall(n,j));
}
},
/**
* Adds an item to the plane.
*
* @method
* @name $app$.game.snake.Plane#addItem
*
* @param {$app$.game.snake.Item} p_item The item to be added.
*/
addItem:
function(p_item)
{ var l_image = p_item.image,
l_item_size = p_item.size,
l_item_x = p_item.x,
l_item_y = p_item.y,
l_x = l_item_x*l_item_size,
l_y = l_item_y*l_item_size;
this.v_plane[l_item_x][l_item_y] = p_item;
this.v_context.clearRect(l_x, l_y, l_item_size, l_item_size);
this.v_context.drawImage(l_image, l_x, l_y);
},
/**
* Removes an item from the plane.
*
* @method
* @name $app$.game.snake.Plane#removeItem
*
* @param {$app$.game.snake.Item} p_item The item to be removed.
*/
removeItem:
function(p_item)
{ var l_item_size = p_item.size,
l_item_x = p_item.x,
l_item_y = p_item.y,
l_x = l_item_x*l_item_size,
l_y = l_item_y*l_item_size;
delete this.v_plane[l_item_x][l_item_y];
this.v_context.clearRect(l_x, l_y, l_item_size, l_item_size);
},
/**
* Returns the item currently being placed on a specific field of the plane.
*
* @method
* @name $app$.game.snake.Plane#getItem
*
* @param {int} p_x The x-position of the item to be fetched.
* @param {int} p_y The y-position of the item to be fetched.
* @returns {$app$.game.snake.Item}
* The item at position (<code>p_x</code>, <code>p_y</code>)
* of the field.
*/
getItem:
function(p_x, p_y)
{ return this.v_plane[p_x][p_y]; },
/**
* Places food on some empty place of the plane.
*
* @method
* @name $app$.game.snake.Plane#addFood
*/
addFood:
function()
{ var l_plane = this.v_plane,
l_cols_nr = this.v_cols_nr-1,
l_rows_nr = this.v_rows_nr-1,
l_x,
l_y;
// Find an empty filed on the plane.
do
{ l_x = 1 + Math.floor(Math.random()*l_cols_nr);
l_y = 1 + Math.floor(Math.random()*l_rows_nr);
}
while (l_plane[l_x][l_y]);
// Put food into that field.
this.addItem(new Food(l_x, l_y));
this.foodNr++;
}
}
});
////////////////////////////////////////////////////////////////////////////////
// End of Class "Plane"
////////////////////////////////////////////////////////////////////////////////
});