-----------------------------------------------------------------------------
--  File     : /units/UAL0216/UAL0216_script.lua
--
--  Author(s): Eddy, EbolaSoup, Resin Smoker, Optimus Prime, Vissroid 
--
--  Summary  : Aeon T2 Artillery Bot Script
--
--  Copyright © 2011 4DC_V0.8  All rights reserved.
----------------------------------------------------------------------------- 

-- Misc Lua called 
local AWalkingLandUnit = import('/lua/aeonunits.lua').AWalkingLandUnit
local EffectUtils = import('/lua/EffectUtilities.lua')
local EffectTemplate = import('/lua/EffectTemplates.lua')

-- Weapon Local lua called 
local CustomAeonWeapons = import('/mods/4DC_V0.8/lua/4D_weapons.lua')
local CannonWeapon = CustomAeonWeapons.UAL0216_CannonWeapon

UAL0216 = Class(AWalkingLandUnit) {   
   Weapons = {
       MainWeapon = Class(CannonWeapon) {  
       
            OnCreate = function(self)
                self.MainWeaponRecoilAnimator = CreateAnimator(self.unit)
                CannonWeapon.OnCreate(self) 
            end,
                             
            PlayRackRecoil = function(self, rackList)                                
                CannonWeapon.PlayRackRecoil(self, rackList)               
                if not self.unit:IsDead() and self.unit.Deployed then
                    -- Perform recoil shake
                    self.unit:ShakeCamera(2, 1, 0.5, 0.5)
                    -- Steam effect from weapon vents  
                    self.ExhaustEffects = EffectUtils.CreateBoneEffects( self.unit, 'Bot_Arm_Right1', self.unit:GetArmy(), EffectTemplate.WeaponSteam01 )
                    self.MainWeaponRecoilAnimator:PlayAnim(self:GetBlueprint().AnimationRecoil):SetRate(self:GetBlueprint().AnimationRecoilRate or 1)              
                else
                    -- Perform recoil shake
                    self.unit:ShakeCamera(0.4, 0.2, 0.1, 0.1)
                end                 
            end,                        
       },                                    
    },
    
    OnCreate = function(self,builder,layer)
        AWalkingLandUnit.OnCreate(self)                 
        -- Creating Global Variables       
        self.MyWeapon = self:GetWeaponByLabel('MainWeapon')
        self.WepBP = self.MyWeapon:GetBlueprint()
        -- Setup of the deployment animation
        self.DeployAnim = CreateAnimator(self)
        self.DeployAnim:PlayAnim(self:GetBlueprint().Display.AnimationArtyDeploy)self.DeployAnim:SetRate(0)
        self.Trash:Add(self.DeployAnim)
        -- Booleans to record unit state and prevent the toggles from being used while changing modes
        self.ModeChange = false
        self.Deployed = false
        self.Moving = false
    end,    
    
    OnScriptBitSet = function(self, bit)
        AWalkingLandUnit.OnScriptBitSet(self, bit)       
        if not self:IsDead() and bit == 1 then 
            if not self.Deployed and not self.ModeChange then
                self:ForkThread(self.Deploy)
            end
        end
    end,
    
    OnScriptBitClear = function(self, bit)
        AWalkingLandUnit.OnScriptBitClear(self, bit)
        if not self:IsDead() and bit == 1 then  
            if self.Deployed and not self.ModeChange then        
                self:ForkThread(self.UnDeploy)
            end
        end
    end, 
    
   OnMotionHorzEventChange = function(self, new, old)
       -- Updates the gloabal variable for when the Meson movement changes
       if not self:IsDead() and new == 'Stopped' then
           self.Moving = false
       elseif not self:IsDead() then
           self.Moving = true
       end              
       AWalkingLandUnit.OnMotionHorzEventChange(self, new, old)
   end,         
    
    Deploy = function(self)
        self.ModeChange = true    
        self.MyWeapon:SetEnabled(false)     
        IssueClearCommands({self})      
        while not self:IsDead() and self.Moving do
            -- Delay till the bot has stopped moving
            WaitSeconds(1)
        end
        if not self:IsDead() then 
            -- Deploy cannon for long range artillery support
            self:SetImmobile(true) 
            self.MyWeapon:ChangeProjectileBlueprint(self.WepBP.SecondaryProjectileId)                  
            self.MyWeapon:AddDamageMod(400)
            self.MyWeapon:AddDamageRadiusMod(1.6)
            self.MyWeapon:ChangeMaxRadius(60)
            self.MyWeapon:ChangeMinRadius(15)
            self.MyWeapon:ChangeRateOfFire(0.1)
            self.MyWeapon:SetFiringRandomness(1.5)            
            self.DeployAnim:SetRate(0.33)
            WaitFor(self.DeployAnim)
            if not self:IsDead() then                 
                self.Deployed = true
                self.ModeChange = false
                self.MyWeapon:SetEnabled(true)
            end
        end   
    end,
    
    UnDeploy = function(self)
        self.ModeChange = true    
        self.MyWeapon:SetEnabled(false)     
        IssueClearCommands({self})      
        while not self:IsDead() and self.Moving do
            -- Delay till the bot has stopped moving
            WaitSeconds(1)
        end
        if not self:IsDead() then     
            -- Enable bot mode
            self.MyWeapon:ChangeProjectileBlueprint(self.WepBP.ProjectileId)                  
            self.MyWeapon:AddDamageMod(-400)
            self.MyWeapon:AddDamageRadiusMod(-2.0)
            self.MyWeapon:ChangeMaxRadius(self.WepBP.MaxRadius)
            self.MyWeapon:ChangeMinRadius(self.WepBP.MinRadius)
            self.MyWeapon:ChangeRateOfFire(self.WepBP.RateOfFire)
            self.MyWeapon:SetFiringRandomness(self.WepBP.FiringRandomness)  
            self.DeployAnim:SetRate(-0.33)
            WaitFor(self.DeployAnim)
            if  not self:IsDead() then      
                self:SetImmobile(false)            
                self.Deployed = false        
                self.ModeChange = false
                self.MyWeapon:SetEnabled(true)            
            end
        end    
    end,
   
}
TypeClass = UAL0216