#****************************************************************************
#**
#**  Summary  :  Aeon Shield Generator Script
#**
#****************************************************************************

local AShieldStructureUnit = import('/lua/aeonunits.lua').AShieldStructureUnit
local TShieldStructureUnit = import('/lua/terranunits.lua').TShieldStructureUnit

SAB4102 = Class(AShieldStructureUnit) {

    ShieldEffects = {
        '/effects/emitters/aeon_shield_generator_t2_01_emit.bp',
        '/effects/emitters/aeon_shield_generator_t2_02_emit.bp',
        '/effects/emitters/aeon_shield_generator_t3_03_emit.bp',
        '/effects/emitters/aeon_shield_generator_t3_04_emit.bp',
    },
    
    OnCreate = function(self)
        AShieldStructureUnit.OnCreate(self)
        self:HideBone('Flap03', true)
        self:HideBone('Flap04', true)
    end,

    OnStopBeingBuilt = function(self,builder,layer)
        AShieldStructureUnit.OnStopBeingBuilt(self,builder,layer)
	self.ShieldEffectsBag = {}
    end,

    OnShieldEnabled = function(self)
        AShieldStructureUnit.OnShieldEnabled(self)
        if not self.OrbManip1 then
            self.OrbManip1 = CreateRotator(self, 'Orb', 'x', nil, 0, 45, -45)
            self.Trash:Add(self.OrbManip1)
        end
        self.OrbManip1:SetTargetSpeed(-45)
        if not self.OrbManip2 then
            self.OrbManip2 = CreateRotator(self, 'Orb', 'z', nil, 0, 45, 45)
            self.Trash:Add(self.OrbManip2)
        end
        self.OrbManip2:SetTargetSpeed(45)

        if self.ShieldEffectsBag then
            for k, v in self.ShieldEffectsBag do
                v:Destroy()
            end
	    self.ShieldEffectsBag = {}
	end
        for k, v in self.ShieldEffects do
            table.insert( self.ShieldEffectsBag, CreateAttachedEmitter( self, 0, self:GetArmy(), v ):ScaleEmitter(0.46) )
        end
    end,

    OnShieldDisabled = function(self)
        AShieldStructureUnit.OnShieldDisabled(self)
        if self.OrbManip1 then
            self.OrbManip1:SetSpinDown(true)
            self.OrbManip1:SetTargetSpeed(0)
        end
        if self.OrbManip2 then
            self.OrbManip2:SetSpinDown(true)
            self.OrbManip2:SetTargetSpeed(0)
        end
        if self.ShieldEffectsBag then
            for k, v in self.ShieldEffectsBag do
                v:Destroy()
            end
	    self.ShieldEffectsBag = {}
	end
    end,

    OnLayerChange = function(self, new, old)
        AShieldStructureUnit.OnLayerChange(self, new, old)
        if new == 'Land' then
            self:AddBuildRestriction(categories.ALLUNITS)
            self:RequestRefreshUI()
        elseif new == 'Water' then
            self:RestoreBuildRestrictions()
            self:RequestRefreshUI()     
        end
    end,
    
    UpgradingState = State(AShieldStructureUnit.UpgradingState) {
        Main = function(self)
            self:ShowBone('Flap03', true)
            self:ShowBone('Flap04', true)
            AShieldStructureUnit.UpgradingState.Main(self)
        end,
        EnableShield = function(self)
            AShieldStructureUnit.EnableShield(self)
        end,
        
        DisableShield = function(self)
            AShieldStructureUnit.DisableShield(self)
        end,   
    },

    OnKilled = function(self, instigator, type, overkillRatio)
        AShieldStructureUnit.OnKilled(self, instigator, type, overkillRatio)
        if self.OrbManip1 then
            self.OrbManip1:Destroy()
            self.OrbManip1 = nil
        end
        if self.OrbManip2 then
            self.OrbManip2:Destroy()
            self.OrbManip2 = nil
        end
    end,    
}

TypeClass = SAB4102

