﻿function CTurret(point, name) {
    this.init(point, name);
    this.setMarker();
    
    //Initial parameters
    this.upgradePrice = 3;
    this.sellPrice = 2;
    this.damagePower = CTurret.damagePower;
    this.range = CTurret.range; //Range in meters
    this.splashRange = CTurret.splashRange;
}
CTurret.inheritsFrom(Turret);

//Instance methods
CTurret.prototype.upgrade = function() {
    this.level++;
    switch (this.level) {
        case 2:
            this.upgradePrice += 3;
            this.splashRange += 25;
            this.sellPrice += 1;
            break;
        case 3:
            this.upgradePrice += 3;
            this.range += 25;
            this.sellPrice += 1;
            break;
        case 4:
            this.upgradePrice += 3;
            this.range += 25;
			this.damagePower += 0.5;
            this.sellPrice += 1;
            break;
    }
}

CTurret.prototype.attackTargets = function() {
    this.setAttacking(true);
    //Splash damange handled in HSEngine.. move here?
	this.onAttack.fire();
}

CTurret.prototype.onClick = function(latlng) {
    infoHTML =
    this.name + " Level " + this.level +
    "<Div Style='text-align:left'>" +
    "<BR />Damage: " + this.damagePower +
    "<BR />Range: " + this.range + " meters" +
	"<BR />Splash damage range: " + this.splashRange + " meters" +
    "<BR /><a href=\"javascript:game.removeTurret('" + this.name + "');\">Sell this turret for $" + this.sellPrice + "</a>";

    if (this.level < this.constructor.maxLevel)
        infoHTML += "<BR /><a href=\"javascript:game.upgradeTurret('" + this.name + "');\">Upgrade this turret for $" + this.upgradePrice + "</a>";

    infoHTML += this.constructor.info;
    infoHTML += "</Div>";
    this.overlayMarker.openInfoWindowHtml(infoHTML);
}

CTurret.prototype.setMarker = function() {
    oldMarker = this.marker;
    //console.log("Setting turret: " + this.curFaceDir + " and " + this.isAttacking);
    switch (this.curFaceDir) {
        case "nw":
            if (this.isAttacking)
                this.marker = new GMarker(this.point, { icon: this.constructor.facingNWFiringIcon, title: this.name, zIndexProcess: function(){return -50;}});
            else
                this.marker = new GMarker(this.point, { icon: this.constructor.facingNWIcon, title: this.name, zIndexProcess: function(){return -50;} });
            break;
        case "sw":
            if (this.isAttacking)
                this.marker = new GMarker(this.point, { icon: this.constructor.facingSWFiringIcon, title: this.name, zIndexProcess: function(){return -50;} });
            else
                this.marker = new GMarker(this.point, { icon: this.constructor.facingSWIcon, title: this.name, zIndexProcess: function(){return -50;} });
            break;
        case "ne":
            if (this.isAttacking)
                this.marker = new GMarker(this.point, { icon: this.constructor.facingNEFiringIcon, title: this.name, zIndexProcess: function(){return -50;} });
            else
                this.marker = new GMarker(this.point, { icon: this.constructor.facingNEIcon, title: this.name, zIndexProcess: function(){return -50;} });
            break;
        case "se":
            if (this.isAttacking)
                this.marker = new GMarker(this.point, { icon: this.constructor.facingSEFiringIcon, title: this.name, zIndexProcess: function(){return -50;} });
            else
                this.marker = new GMarker(this.point, { icon: this.constructor.facingSEIcon, title: this.name, zIndexProcess: function(){return -50;} });
            break;
    }
    GEvent.bind(this.marker, "click", this, this.onClick);
    this.onChangeIcon.fire(oldMarker);
}

//Static properties
CTurret.price = 15;
CTurret.splashRange = 25;
CTurret.maxLevel = 4;
CTurret.damagePower = 1;
CTurret.range = 275;
CTurret.info = "<BR />This turret causes splash damage";

//Static icon properties
var smallMapIcon = new GIcon(G_DEFAULT_ICON);
smallMapIcon.image = "http://labs.ribaudio.com/herosquad/img/cturretnw.png";
smallMapIcon.shadow = "";
smallMapIcon.iconSize = new GSize(20, 20);
smallMapIcon.iconAnchor = new GPoint(10, 15);
CTurret.smallMapIcon = smallMapIcon;

var facingNWIcon = new GIcon(G_DEFAULT_ICON);
facingNWIcon.image = "http://labs.ribaudio.com/herosquad/img/cturretnw.png";
facingNWIcon.shadow = "";
facingNWIcon.iconSize = new GSize(70, 70);
facingNWIcon.iconAnchor = new GPoint(35, 42);
facingNWIcon.imageMap = [0, 0, 69, 0, 69, 69, 0, 69];
CTurret.facingNWIcon = facingNWIcon;

CTurret.facingNEIcon = new GIcon(CTurret.facingNWIcon, "http://labs.ribaudio.com/herosquad/img/cturretne.png");
CTurret.facingSWIcon = new GIcon(CTurret.facingNWIcon, "http://labs.ribaudio.com/herosquad/img/cturretsw.png");
CTurret.facingSEIcon = new GIcon(CTurret.facingNWIcon, "http://labs.ribaudio.com/herosquad/img/cturretse.png");

CTurret.facingNWFiringIcon = new GIcon(CTurret.facingNWIcon, "http://labs.ribaudio.com/herosquad/img/cturretnwfiring.png");
CTurret.facingNEFiringIcon = new GIcon(CTurret.facingNWIcon, "http://labs.ribaudio.com/herosquad/img/cturretnefiring.png");
CTurret.facingSWFiringIcon = new GIcon(CTurret.facingNWIcon, "http://labs.ribaudio.com/herosquad/img/cturretswfiring.png");
CTurret.facingSEFiringIcon = new GIcon(CTurret.facingNWIcon, "http://labs.ribaudio.com/herosquad/img/cturretnefiring.png");