nonce = function() end

local DAMAGE = 100
local TEXTURE = Engine.load_texture(_modpath .. "Drake2.png")
local BUSTER_TEXTURE = Engine.load_texture(_modpath .. "Drake.png")
local AUDIO = Engine.load_audio(_modpath .. "DrakeSummon.ogg")
local TEXTURE_EFFECT = Engine.load_texture(_modpath .. "effect.png")
local ANIMPATH_EFFECT = _modpath .. "effect.animation"
local AUDIO_DAMAGE = Engine.load_audio(_modpath .. "hit.ogg")
local AUDIORELEASE = Engine.load_audio(_modpath .. "Release.ogg")


function package_init(package)
    package:declare_package_id("DJ.DrakeRage")
    package:set_icon_texture(Engine.load_texture(_modpath .. "icon.png"))
    package:set_preview_texture(Engine.load_texture(_modpath .. "preview.png"))
    package:set_codes({"Y"})

    local props = package:get_card_props()
    props.shortname = "DrakeRge"
    props.damage = DAMAGE
    props.time_freeze = true
    props.skip_time_freeze_intro = true
    props.element = Element.Fire
    props.description = "Release Inner Flames"
    props.long_description = "Drake powers up from fire sources!"
    props.card_class = CardClass.Standard
    props.limit = 1
end



function card_create_action(actor, props)
    local cur_tile = actor:get_current_tile()
    if actor:get_element() == Element.Fire and cur_tile:get_state() == TileState.Lava then
    print("V3")
    local action = Battle.CardAction.new(actor, "PLAYER_SHOOTING")
    local frame1 = { 1, 1.86}
    
   

    local frame_sequence = make_frame_data({ frame1})
    action:override_animation_frames(frame_sequence)
    action:set_lockout(make_animation_lockout())



    action.execute_func = function(self, user)
        local buster = self:add_attachment("BUSTER")
        buster:sprite():set_texture(BUSTER_TEXTURE, true)
        buster:sprite():set_layer(-1)

        
        

        local buster_anim = buster:get_animation()
        buster_anim:load(_modpath .. "DrakeBuster.animation")
        buster_anim:set_state("DEFAULT3")
        Engine.play_audio(AUDIO, AudioPriority.Highest)

        buster_anim:on_frame(4, function()
            
            Engine.play_audio(AUDIORELEASE, AudioPriority.Low)
            local cannonshot = create_zap("DEFAULT", user, props)
            local tile = user:get_tile(user:get_facing(), 0)
            actor:get_field():spawn(cannonshot, tile)
        end)

        buster_anim:on_frame(5, function()
           
            Engine.play_audio(AUDIORELEASE, AudioPriority.Low)
            local cannonshot = create_zap("DEFAULT", user, props)
            local tile = user:get_tile(user:get_facing(), 0)
            actor:get_field():spawn(cannonshot, tile)
        end)

        buster_anim:on_frame(6, function()
            
            Engine.play_audio(AUDIORELEASE, AudioPriority.Low)
            local cannonshot = create_zap("DEFAULT", user, props)
            local tile = user:get_tile(user:get_facing(), 0)
            actor:get_field():spawn(cannonshot, tile)
        end)
    end
    return action

elseif actor:get_element() == Element.Fire then
    print("V2(fire)")
    local action = Battle.CardAction.new(actor, "PLAYER_SHOOTING")
    local frame1 = { 1, 1.46}
    
   

    local frame_sequence = make_frame_data({ frame1})
    action:override_animation_frames(frame_sequence)
    action:set_lockout(make_animation_lockout())



    action.execute_func = function(self, user)
        local buster = self:add_attachment("BUSTER")
        buster:sprite():set_texture(BUSTER_TEXTURE, true)
        buster:sprite():set_layer(-1)

        
        

        local buster_anim = buster:get_animation()
        buster_anim:load(_modpath .. "DrakeBuster.animation")
        buster_anim:set_state("DEFAULT2")
        Engine.play_audio(AUDIO, AudioPriority.Highest)
        buster_anim:on_frame(4, function()
            
            Engine.play_audio(AUDIORELEASE, AudioPriority.Low)
            local cannonshot = create_zap("DEFAULT", user, props)
            local tile = user:get_tile(user:get_facing(), 0)
            actor:get_field():spawn(cannonshot, tile)

            buster_anim:on_frame(5, function()
                
                Engine.play_audio(AUDIORELEASE, AudioPriority.Low)
                local cannonshot = create_zap("DEFAULT", user, props)
                local tile = user:get_tile(user:get_facing(), 0)
                actor:get_field():spawn(cannonshot, tile)
            end)
        end)
    end
    return action


elseif cur_tile and cur_tile:get_state() == TileState.Lava then
    
    print("V2(Lava)")
    local action = Battle.CardAction.new(actor, "PLAYER_SHOOTING")
    local frame1 = { 1, 1.46}
    
   

    local frame_sequence = make_frame_data({ frame1})
    action:override_animation_frames(frame_sequence)
    action:set_lockout(make_animation_lockout())



    action.execute_func = function(self, user)
        local buster = self:add_attachment("BUSTER")
        buster:sprite():set_texture(BUSTER_TEXTURE, true)
        buster:sprite():set_layer(-1)

        
        

        local buster_anim = buster:get_animation()
        buster_anim:load(_modpath .. "DrakeBuster.animation")
        buster_anim:set_state("DEFAULT2")
        Engine.play_audio(AUDIO, AudioPriority.Highest)
        buster_anim:on_frame(4, function()
            
            Engine.play_audio(AUDIORELEASE, AudioPriority.Low)
            local cannonshot = create_zap("DEFAULT", user, props)
            local tile = user:get_tile(user:get_facing(), 0)
            actor:get_field():spawn(cannonshot, tile)
            
            buster_anim:on_frame(5, function()
                
                Engine.play_audio(AUDIORELEASE, AudioPriority.Low)
                local cannonshot = create_zap("DEFAULT", user, props)
                local tile = user:get_tile(user:get_facing(), 0)
                actor:get_field():spawn(cannonshot, tile)
            end)
        end)
    end
    return action

else
    print("V1")
    local action = Battle.CardAction.new(actor, "PLAYER_SHOOTING")
    local frame1 = { 1, 1.36}
    
   

    local frame_sequence = make_frame_data({ frame1})
    action:override_animation_frames(frame_sequence)
    action:set_lockout(make_animation_lockout())



    action.execute_func = function(self, user)
        local buster = self:add_attachment("BUSTER")
        buster:sprite():set_texture(BUSTER_TEXTURE, true)
        buster:sprite():set_layer(-1)

        
        

        local buster_anim = buster:get_animation()
        buster_anim:load(_modpath .. "DrakeBuster.animation")
        buster_anim:set_state("DEFAULT")
        Engine.play_audio(AUDIO, AudioPriority.Highest)
        buster_anim:on_frame(4, function()
            
            Engine.play_audio(AUDIORELEASE, AudioPriority.High)
            local cannonshot = create_zap("DEFAULT", user, props)
            local tile = user:get_tile(user:get_facing(), 0)
            actor:get_field():spawn(cannonshot, tile)
        end)
    end
    return action

end
end



function create_zap(animation_state, user, props)
    local spell = Battle.Spell.new(user:get_team())
    spell:set_texture(TEXTURE, true)
    spell:highlight_tile(Highlight.Solid)
    spell:set_height(16.0)
    local direction = user:get_facing()
    spell.slide_started = false
    local field = user:get_field()
    spell:sprite("Drake2.png"):set_layer(0)
    spell:set_hit_props(
        HitProps.new(
            props.damage,
            Hit.Impact | Hit.Flinch,
            Element.Fire,
            user:get_context(),
            Drag.None
        )
    )

    local anim = spell:get_animation()
    anim:load(_modpath .. "DrakeShot.animation")
    anim:set_state(animation_state)
    anim:refresh(spell:sprite())




    
    spell.update_func = function(self, dt)
        
        self:get_current_tile():attack_entities(self)

        if self:is_sliding() == false then
            if self:get_current_tile():is_edge() and self.slide_started then
                self:delete()
            end

            local dest = self:get_tile(direction, 1)
            local ref = self
            self:slide(dest, frames(2), frames(0), ActionOrder.Voluntary,
                function()
                    ref.slide_started = true
                end
            )
        end
    end

    spell.attack_func = function(self, other)
        Engine.play_audio(AUDIO_DAMAGE, AudioPriority.Highest)
        local hitfx = Battle.Artifact.new()
        local hitfxanim = hitfx:get_animation()
        hitfx:set_texture(TEXTURE_EFFECT, true)
        hitfxanim:load(ANIMPATH_EFFECT)
        hitfxanim:set_state("2")
        hitfxanim:refresh(hitfx:sprite())
        hitfxanim:on_complete(function()
            hitfx:erase()
        end)
        field:spawn(hitfx, spell:get_current_tile())
    end

    spell.collision_func = function(self, other)
        
        self:erase()
    end

    spell.delete_func = function(self)
        
    end

    spell.can_move_to_func = function(tile)
        spell:shake_camera(2, 2)
        
        return true
    end

   

    return spell
end
