function package_init(package)
    package:declare_package_id("com.d3stroy3d&darkware.player.Chargeman")
    package:set_special_description("All aboard!!!")
    package:set_speed(1.0)
    package:set_attack(1)
    package:set_charged_attack(10)
    package:set_icon_texture(Engine.load_texture(_modpath.."face.png"))
    package:set_preview_texture(Engine.load_texture(_modpath.."preview.png"))
    package:set_overworld_animation_path(_modpath.."overworld.animation")
    package:set_overworld_texture_path(_modpath.."overworld.png")
    package:set_mugshot_texture_path(_modpath.."mug.png")
    package:set_mugshot_animation_path(_modpath.."mug.animation")
	package:set_emotions_texture_path(_modpath.."emotions.png")
end

function player_init(player)
    player:set_name("Chargeman")
    player:set_health(1000)
    player:set_element(Element.Fire)
    player:set_height (60.0)
	player:set_charge_position(1,-17)	
    player:set_animation(_modpath.."chargeman.animation")
    player:set_texture(Engine.load_texture(_modpath.."chargeman_atlas.png"), true)
    player:set_fully_charged_color(Color.new(48, 40, 48, 255))
    player.normal_attack_func = create_normal_attack
    player.charged_attack_func = create_charged_attack
    player.special_attack_func = create_special_attack 
   do_thing(player)
end

  function create_normal_attack(player)
    return Battle.Buster.new(player, false, player:get_attack_level())
end

  function create_charged_attack2(player)
    local props = Battle.CardProperties:new()
    props.damage = (player:get_attack_level() * 10)
    return include("Chips/Chimney/entry.lua").card_create_action(player, props)
end

function do_thing(user)
    local c = Battle.UIComponent.new(user)
    local spr = c:create_node()

    spr:set_texture(Engine.load_texture(_folderpath.."pixel.png"))
    c:set_draw_on_ui_pass(true)

    local player = Battle.Player.from(user)
    c.update_func = function()
        local offx = 0
        local offy = 0

        if player:input_has(Input.Held.Right) then 
            offx = 1
        end

        if player:input_has(Input.Held.Left) then 
            offx = -1
        end

        if player:input_has(Input.Held.Down) then 
            offy = 1
        end

        if player:input_has(Input.Held.Up) then 
            offy = -1
        end

        --if player:input_has(Input.Pressed.Use) then 
            print(spr:get_offset().x, spr:get_offset().y)
            local t = user:get_field():tile_from_pixels(math.floor(spr:get_offset().x), math.floor(spr:get_offset().y))
            print(t)
            if t then
            	t:highlight(Highlight.Solid)
		print(" "..t:x(), t:y())
            end
        --end

        spr:set_offset(spr:get_offset().x + offx, spr:get_offset().y + offy)
        spr:set_color_mode(ColorMode.Additive)
        spr:set_color(Color.new(255, 255, 255, 255))
    end

    user:register_component(c)
end	

function create_special_attack(player)
    if Battle.Player.from(player).charged_attack_func == create_charged_attack then
		player:set_fully_charged_color(Color.new(240, 112, 0, 255))
        player.charged_attack_func = create_charged_attack2
	
    elseif Battle.Player.from(player).charged_attack_func == create_charged_attack2 then
	player:set_fully_charged_color(Color.new(48, 40, 48, 255))
        player.charged_attack_func = create_charged_attack
		
    end
end

function create_charged_attack(player)	
		local action = Battle.CardAction.new(player, "PLAYER_CS")
		local pain = ((10 * player:get_attack_level()))
		local props = HitProps.new(
			pain,
			Hit.Impact | Hit.Flinch, 
			Element.None,
			player:get_context(),
			Drag.None
		)
		local spell = create_tackle(player, props)
		local spell2 = create_kart(player, props)
		local spell3 = create_kart2(player, props)		
		action.execute_func = function(self, user)		
		    if user:get_current_tile():is_edge() or user:get_tile(user:get_facing(), 1):get_state() ~= TileState.Broken then
			player:hide()
			player:toggle_hitbox(false)
			player:share_tile(true)
			player:get_field():spawn(spell, player:get_tile())
			player:get_field():spawn(spell2, player:get_tile())	
			player:get_field():spawn(spell3, player:get_tile())			
            end 
		end
		action.action_end_func = function(self)
			player:reveal()
			player:toggle_hitbox(true)
			player:share_tile(false)
		end
		action.can_move_to_func = function(tile)
			if tile then
				if tile:is_walkable() then
					return true
				else
					return false
				end
			end
			return false
		end
		return action
	end


function create_tackle(player, props)
	local spell = Battle.Obstacle.new(Team.Other)
	spell:set_facing(player:get_facing())
	spell.slide_started = false
	spell:set_texture(Engine.load_texture(_modpath.."chargeman_atlas.png"), true)
	local direction = spell:get_facing()
 	Engine.play_audio(Engine.load_audio(_modpath.."Chips/ChoCho/start.ogg", true), AudioPriority.High)
	local max_tiles = 5
	local tiles_moved = 0
	spell.attack_func = function(self)
		local tile = self:get_tile()
		local hitbox = Battle.Hitbox.new(Team.Other)
		hitbox:set_hit_props(props)
		player:get_field():spawn(hitbox, tile)
	end
	
	local anim = spell:get_animation()
	anim:load(_modpath.."chargeman.animation", true)
	anim:set_state("PLAYER_CS")
	
	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() or self:get_tile(self:get_facing(), 1):get_state() == TileState.Broken and self.slide_started  then
                self:delete()

            end 		
			
			if tiles_moved >= max_tiles then
				self:delete()
			end
            local dest = self:get_tile(direction, 1)
            local ref = self
            self:slide(dest, frames(5), frames(0), ActionOrder.Voluntary, 
                function()
                    ref.slide_started = true 
                end
            )
			
			tiles_moved = tiles_moved + 1
        end
    end
	spell.collision_func = function(self, other)
	Engine.play_audio(Engine.load_audio(_modpath.."Chips/ChoCho/hitsound.ogg", true), AudioPriority.Low)
	end

    spell.delete_func = function(self)
		self:erase()
    end

    spell.can_move_to_func = function(tile)
        if tile then
			if tile:is_walkable() then
				return true
			else
				return true
			end
		end
		return false
    end

	return spell
end

function create_kart(player, props)
	local spell = Battle.Obstacle.new(Team.Other)
	spell:set_facing(player:get_facing())
	spell.slide_started = false
	spell:set_texture(Engine.load_texture(_modpath.."chargeman_atlas.png"), true)
	local direction = spell:get_facing()
	local max_tiles = 5
	local tiles_moved = 0
	spell.attack_func = function(self)
		local tile = self:get_tile()
		local hitbox = Battle.Hitbox.new(Team.Other)
		hitbox:set_hit_props(props)
		player:get_field():spawn(hitbox, tile)
	end
	
	local anim = spell:get_animation()
	anim:load(_modpath.."chargeman.animation", true)
	anim:set_state("PLAYER_KART")
	
	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() or self:get_tile(self:get_facing(), 1):get_state() == TileState.Broken and self.slide_started  then
                self:delete()

            end 		
			
			if tiles_moved >= max_tiles then
				self:delete()
			end
            local dest = self:get_tile(direction, 1)
    if not self.delay_started then
        -- Se a contagem de quadros de atraso ainda nao comecou
        self.delay_frames = self.delay_frames or 8 -- Defina o numero de quadros de atraso desejado
        self.delay_frames = self.delay_frames - 1
        if self.delay_frames <= 0 then
            self.delay_started = true -- Inicie o atraso apos a contagem regressiva
        end
    elseif 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(spell:get_facing(), 1)
        local ref = self
        self:slide(dest, frames(5), frames(0), ActionOrder.Voluntary, 
            function()
                ref.slide_started = true 
            end
        )
		
			tiles_moved = tiles_moved + 1		
    end
			

        end
    end
	spell.collision_func = function(self, other)
	Engine.play_audio(Engine.load_audio(_modpath.."Chips/ChoCho/hitsound.ogg", true), AudioPriority.Low)
	end

    spell.delete_func = function(self)
		self:erase()
    end

    spell.can_move_to_func = function(tile)
        if tile then
			if tile:is_walkable() then
				return true
			else
				return true
			end
		end
		return false
    end

	return spell
end

function create_kart2(player, props)
	local spell = Battle.Obstacle.new(Team.Other)
	spell:set_facing(player:get_facing())
	spell.slide_started = false
	spell:set_texture(Engine.load_texture(_modpath.."chargeman_atlas.png"), true)
	local direction = spell:get_facing()
	local max_tiles = 5
	local tiles_moved = 0
	spell.attack_func = function(self)
		local tile = self:get_tile()
		local hitbox = Battle.Hitbox.new(Team.Other)
		hitbox:set_hit_props(props)
		player:get_field():spawn(hitbox, tile)
	end
	
	local anim = spell:get_animation()
	anim:load(_modpath.."chargeman.animation", true)
	anim:set_state("PLAYER_KART2")
	
	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() or self:get_tile(self:get_facing(), 1):get_state() == TileState.Broken and self.slide_started  then
                self:delete()

            end 		
			
			if tiles_moved >= max_tiles then
				self:delete()
			end
            local dest = self:get_tile(direction, 1)
    if not self.delay_started then
        -- Se a contagem de quadros de atraso ainda nao comecou
        self.delay_frames = self.delay_frames or 14 -- Defina o numero de quadros de atraso desejado
        self.delay_frames = self.delay_frames - 1
        if self.delay_frames <= 0 then
            self.delay_started = true -- Inicie o atraso apos a contagem regressiva
        end
    elseif 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(spell:get_facing(), 1)
        local ref = self
        self:slide(dest, frames(5), frames(0), ActionOrder.Voluntary, 
            function()
                ref.slide_started = true 
            end
        )
		
			tiles_moved = tiles_moved + 1		
    end
			

        end
    end
	spell.collision_func = function(self, other)
	Engine.play_audio(Engine.load_audio(_modpath.."Chips/ChoCho/hitsound.ogg", true), AudioPriority.Low)
	end

    spell.delete_func = function(self)
		self:erase()
    end

    spell.can_move_to_func = function(tile)
        if tile then
			if tile:is_walkable() then
				return true
			else
				return true
			end
		end
		return false
    end

	return spell
end
