﻿function Shield(xPos, yPos, holder)
{
    this.xPos = xPos;
    this.yPos = yPos;
    this.holder = holder;
    this.size = Settings.SHIELD_SIZE;
    this.shields = new Array();
    this.enabled = true;
    this.killed = 0;
    this.total = 0;
    this.init();
    EventCenter.addListener(this, Events.BOUNDARY_RESETS, "moveShield");
    EventCenter.addListener(this, Events.CHECK_SHIELD_HIT, "checkShieldHit");
    
}

Shield.prototype.init = function()
{
    var sld = this.shield = document.createElement('div');
    sld.parent = this;
    with(sld.style)
    {
        zIndex = "1";
        position = "absolute";
        top = this.yPos + "px";
        left = this.xPos + "px";
        width = this.size + "px";
        height = this.size + "px";
    }
    this.holder.appendChild(sld);
    this.buildTiles();
}

Shield.prototype.buildTiles = function()
{
    this.tiles = Settings.SHIELD_TILE_COUNT;
    var curve = Settings.SHIELD_CURVE;
    if(2 * curve > this.tiles) curve = 0;
    
    this.tileSize = this.size / this.tiles;
    
    for(var i = 0; i < this.tiles; i++)
    {
        this.shields[i] = new Array();
        for(var j = 0; j < this.tiles; j++)
        {
            if(j < curve - i || j >= this.tiles + i - curve)
            {
                this.shields[i][j] = false;
            }
            else
            {
                this.shields[i][j] = new ShieldTile(i, j, this.tileSize, this.shield);
                this.total++;
            }
        }
    }
}

Shield.prototype.moveShield = function(evt)
{
    this.xPos += evt.data.x;
    this.shield.style.left = this.xPos + "px";
}

Shield.prototype.checkShieldHit = function(evt)
{
    var xMin = this.xPos;
    var xMax = xMin + this.size;
    var yMin = this.yPos;
    var yMax = yMin + this.size;
    var x = evt.data.x;
    var y = evt.data.y;
    // see if the shot is within the shield area
    if(this.enabled)
    {
        if(y > yMin && y < yMax)
        {
            if(x > xMin && x < xMax)
            {
                // find the tile that it hit
                var j = Math.floor((x - xMin) / this.tileSize);
                var i = Math.floor((y - yMin) / this.tileSize);
                if(this.shields[i][j])
                {
                    this.shields[i][j].die();
                    this.shields[i][j] = false;
                    this.killed++;
                    // check enabled
                    if(this.killed >= this.total)
                    {
                        this.enabled = false;
                        EventCenter.removeListener(this, Events.MOVE_SHIELDS, "moveShield");
                        EventCenter.removeListener(this, Events.CHECK_SHIELD_HIT, "checkShieldHit");
                        
                    }
                    EventCenter.broadcast(Events.SHOT_HIT, evt.data.shot);
                }

            }
        }
    }
}

Shield.prototype.die = function()
{
    for(var i = 0; i < this.tiles; i++)
    {
        for(var j = 0; j < this.tiles; j++)
        {
            if(this.shields[i][j]) this.shields[i][j].die();
        }
    }
    this.shields.innerHTML = '';
    this.shields = null;
    EventCenter.removeListener(this, Events.MOVE_SHIELDS, "moveShield");
    EventCenter.removeListener(this, Events.CHECK_SHIELD_HIT, "checkShieldHit");
}
