﻿//
//                      block.js
//
//  Simple class to represent blocks on the playing grid.
//

// color codes
var BLOCK_YELLOW    = 0;
var BLOCK_BLUE      = 1;
var BLOCK_ORANGE    = 2;
var BLOCK_PURPLE    = 3;
var BLOCK_RED       = 4;
var BLOCK_MARKER    = 5;

var blockCnt = 0;

var aBlockColors = new Array(
    BLOCK_YELLOW,
    BLOCK_BLUE,
    BLOCK_ORANGE,
    BLOCK_PURPLE,
    BLOCK_RED,
    BLOCK_MARKER 
    );
    
var aBlockSources = new Array( 
    "yellow.png", 
    "blue.png", 
    "orange.png", 
    "purple.png", 
    "red.png",
    null
    );

var g_aAllBlocks = new Array();

//
//                      Block
// 
//  Constructor
//
function Block( colorCode )
{
    this.img = aBlockSources[colorCode];
    this.color = colorCode;
    this.destroy = _blockDestroy;
    if( this.img != null )
    {
        var sXaml = '<Image Width="45" Height="25" Source="images/' + this.img + '" Stretch="Uniform"/>';
        this.handle = g_wpfctrl.createFromXaml( sXaml );
        this.name = "b" + blockCnt++;
        g_aAllBlocks[this.name] = this;
        findName( 'cnvGameGrid' ).children.add( this.handle );
    }
    else
    {
        this.handle = null;
    }
}

//
//                      _blockDestroy
//
function _blockDestroy()
{
    with( this )
    {
        if( handle != null )
        {
            g_aAllBlocks[name] = null;
            findName( 'cnvGameGrid' ).children.remove( handle );
        }
    }
}

//
//                      genRandomBlock
//
//  Not a part of the Block class per se, but included in this file because of
//  related function.
//
function genRandomBlock()
{
    var r = Math.floor(Math.random() * 5);
    return new Block( aBlockColors[r] );
}
