require "prefabutil"

local prefabs =
{
	"rowboat_wake",
	"boat_hit_fx",
	"boat_hit_fx_raft_log",
	"boat_hit_fx_raft_bamboo",
	"boat_hit_fx_rowboat",
	"boat_hit_fx_cargoboat",
	"boat_hit_fx_armoured",
	"flotsam_armoured",
	"flotsam_bamboo",
	"flotsam_cargo",
	"flotsam_lograft",
	"flotsam_rowboat",
	"flotsam_surfboard",
}

local assets =
{
	--自己改的
    Asset("ANIM", "anim/sword_3.zip"),
    Asset("ANIM", "anim/swap_sword_3.zip"),
	Asset("ANIM", "anim/toso_sword_fly.zip"),
	
	Asset("ANIM", "anim/raft_surfboard_build.zip"),
	Asset("ANIM", "anim/boat_hud_raft.zip"), 
	Asset("ANIM", "anim/boat_inspect_raft.zip"), 
	--Asset("ANIM", "anim/ryondae.zip"),
	--Asset("ANIM", "anim/flotsam_ryondae_build.zip"),
	Asset("IMAGE","images/inventoryimages/sword_3.tex"),
	Asset("ATLAS","images/inventoryimages/sword_3.xml")
}



--攻击特效
local function onAttackf(inst, owner, target)



end
--在剑上
local function onboataa(inst)

	if inst.components.drivable.driver and GetPlayer():GetIsOnWater() and GetPlayer().components.tosocast then
		local driver = inst.components.drivable.driver
		driver.Physics:SetMotorVelOverride(0, 5, 0)
        --driver.components.talker:Say("什么鬼")
	elseif inst.components.drivable.driver and not GetPlayer():GetIsOnWater() and GetPlayer().components.tosocast then
		local driver = inst.components.drivable.driver
        --driver.components.talker:Say("什么鬼lllll")
		driver.Physics:SetMotorVelOverride(0, 5, 0)
	end

end


--加入装备函数
local function onequip(inst, owner)

    owner.AnimState:OverrideSymbol("swap_object", "swap_sword_3", "swap_sword_3")
    owner.AnimState:Show("ARM_carry")
    owner.AnimState:Hide("ARM_normal")

	
end

local function onunequip(inst, owner)

    owner.AnimState:Hide("ARM_carry")
    owner.AnimState:Show("ARM_normal")

end



local function boat_perish(inst)
	--if inst.components.drivable.driver and GetPlayer():GetIsOnWater() and GetPlayer().components.yiyucast then
    --inst.Physics:SetMotorVelOverride(0, 10, 0)
	--end
	if inst.components.drivable.driver and GetPlayer():GetIsOnWater() and GetPlayer().components.tosocast then
		local driver = inst.components.drivable.driver
		--driver.Physics:SetMotorVelOverride(0, 0, -10)
        --driver.components.talker:Say("什么鬼")
		driver.components.driver:OnDismount(true)
		local reborn = SpawnPrefab("amulet")
		GetPlayer().components.inventory:GiveItem(reborn)
		GetPlayer().components.inventory:Equip(reborn)
		driver.components.health:Kill("drowning")
		inst.SoundEmitter:PlaySound(inst.sinksound)
		--inst:Remove()
	elseif inst.components.drivable.driver and not GetPlayer():GetIsOnWater() and GetPlayer().components.tosocast then
		local driver = inst.components.drivable.driver
        --driver.components.talker:Say("什么鬼lllll")
		--driver.Physics:SetMotorVelOverride(0, 0, 10)
		driver.components.driver:OnDismount(true)
		--inst:Remove()
	end
end

local function onmounted(inst)
end

local function ondismounted(inst)
end 

local function onopen(inst)
	if inst.components.drivable.driver == nil then
		inst.SoundEmitter:PlaySound("dontstarve_DLC002/common/HUD_boat_inventory_open")
	end
end

local function onclose(inst)
	if inst.components.drivable.driver == nil then
		inst.SoundEmitter:PlaySound("dontstarve_DLC002/common/HUD_boat_inventory_close")
	end
end

local function setupcontainer(inst, slots, bank, build, inspectslots, inspectbank, inspectbuild, inspectboatbadgepos, inspectboatequiproot)
	inst:AddComponent("container")
	inst.components.container:SetNumSlots(#slots)
	inst.components.container.type = "boat"
	inst.components.container.side_align_tip = -500
	inst.components.container.canbeopened = false
	inst.components.container.onopenfn = onopen
	inst.components.container.onclosefn = onclose

	inst.components.container.widgetslotpos = slots
	inst.components.container.widgetanimbank = bank
	inst.components.container.widgetanimbuild = build
	inst.components.container.widgetboatbadgepos = Vector3(0, 40, 0)
	inst.components.container.widgetequipslotroot = Vector3(-80, 40, 0)

	local boatwidgetinfo = {}
	boatwidgetinfo.widgetslotpos = inspectslots
	boatwidgetinfo.widgetanimbank = inspectbank
	boatwidgetinfo.widgetanimbuild = inspectbuild
	boatwidgetinfo.widgetboatbadgepos = inspectboatbadgepos
	boatwidgetinfo.widgetpos = Vector3(200, 0, 0)
	boatwidgetinfo.widgetequipslotroot = inspectboatequiproot
	inst.components.container.boatwidgetinfo = boatwidgetinfo
end

local function commonfn(Sim)
	local inst = CreateEntity()
	local trans = inst.entity:AddTransform()
	trans:SetFourFaced()
	inst.no_wet_prefix = true 

	inst:AddTag("boat")

	local anim = inst.entity:AddAnimState()	
	inst.entity:AddSoundEmitter()

	inst.entity:AddPhysics()
	--ceshi

	inst.Physics:SetCylinder(0.25,2)

	inst:AddComponent("inspectable")
	inst:AddComponent("drivable")

	inst:AddComponent("rowboatwakespawner")
	inst:AddComponent("boathealth")
	inst.components.boathealth:SetDepletedFn(boat_perish)

    inst:AddComponent("repairable")
    inst.components.repairable.repairmaterial = "boat"

	return inst
end

local function deploytest(inst, pt)
	local ground = GetWorld()
	local tile = GROUND.GRASS
	if ground and ground.Map then
		tile = ground.Map:GetTileAtPoint(pt:Get())
	end
	local onWater = ground.Map:IsWater(tile)
	return onWater
end

local function surfboard_ondropped(inst)
	--If this is a valid place to be deployed, auto deploy yourself.
	if inst.components.deployable and inst.components.deployable:CanDeploy(inst:GetPosition()) then
		inst.components.deployable:Deploy(inst:GetPosition(), inst)
	end
end

local function pickupfn(inst, guy)
	local board = SpawnPrefab("surfboard_item_toso")
	guy.components.inventory:GiveItem(board)
	board.components.pocket:GiveItem("toso_surfboard", inst)
	return true
end

local function ondeploy(inst, pt, deployer)
	local board = inst.components.pocket:RemoveItem("toso_surfboard") or SpawnPrefab("toso_surfboard") 
	if board then
	--之前是0
		pt = Vector3(pt.x, 0, pt.z)
		board.Physics:SetCollides(false)--设置碰撞
		board.Physics:Teleport(pt.x, pt.y, pt.z) 
		board.Physics:SetCollides(true)--设置碰撞
		inst:Remove()
		return board
	end
end

local function surfboardfn(sim)

	local inst = commonfn(sim)
	setupcontainer(inst, {}, "boat_hud_raft", "boat_hud_raft", {}, "boat_inspect_raft", "boat_inspect_raft", {x=0,y=5}, {})--设置容积

	inst.AnimState:SetBank("toso_sword_fly")
	inst.AnimState:SetBuild("toso_sword_fly")
	inst.AnimState:PlayAnimation("run_loop", true)
	
	inst.entity:AddMiniMapEntity()
	inst.MiniMapEntity:SetIcon( "sword_3.tex" )

	inst.components.boathealth.maxhealth = 460
	inst.components.boathealth:SetHealth(460, TUNING.ARMOUREDBOAT_PERISHTIME)
    inst.components.boathealth.damagesound = "dontstarve_DLC002/common/surfboard_damage"
	
	inst.landsound = "dontstarve_DLC002/common/boatjump_land_wood"--着陆声音
	inst.sinksound = "dontstarve_DLC002/common/boat_sinking_log_cargo"--沉船声音
	inst.sailsound = "common/surfboard_sail_LP"--航行声音
	inst.sailmusic = "surfing"	--航行音乐
	
	inst.components.drivable.sanitydrain = 0
	inst.components.drivable.runspeed = 3.5
	inst.components.drivable.maprevealbonus = 8
	inst.components.drivable.hitfx = "boat_hit_fx_armoured"
	inst.components.drivable.runanimation = "row_loop"
	inst.components.drivable.prerunanimation = "row_pre"
	inst.components.drivable.postrunanimation = "row_pst"
	inst.components.drivable.overridebuild = "toso_sword_fly"
	inst.components.drivable.flotsambuild = "flotsam_ryondae_build"
	inst.components.drivable.creaksound = "dontstarve_DLC002/common/boat_creaks"
	inst.components.drivable.alwayssail = true
	inst.waveboost = TUNING.SURFBOARD_WAVEBOOST
	inst.wavesanityboost = TUNING.SURFBOARD_WAVESANITYBOOST

	inst:AddComponent("characterspecific")
    inst.components.characterspecific:SetOwner("toso")
	

TheInput:AddKeyUpHandler(TUNING.TOSO_KEY4, function()

    if not IsPaused() then
    local x = true
	
	  if x then
	  --if inst.components.drivable.driver and GetPlayer():GetIsOnWater() and GetPlayer().components.tosocast then
		local driver = inst.components.drivable.driver
		if driver then
		driver.Physics:SetMotorVelOverride(0, 5, 0)
		end
		--x = false
        --driver.components.talker:Say("什么鬼")
	  else--if inst.components.drivable.driver and not GetPlayer():GetIsOnWater() and GetPlayer().components.tosocast then
		local driver = inst.components.drivable.driver
		if driver then
        --driver.components.talker:Say("什么鬼lllll")
		driver.Physics:ClearMotorVelOverride()
		driver.Physics:SetMotorVelOverride(0, 0, 0)
		end
	  end	
      x = not x
	  
    end

end)	
	--inst:DoTaskInTime(0, function() onboataa(inst) end)
	
	inst:AddComponent("pickupable")
	inst.components.pickupable:SetOnPickupFn(pickupfn)
	inst:SetInherentSceneAltAction(ACTIONS.RETRIEVE)
	local olddrive = inst.components.drivable.OnUpdate
	inst.components.drivable.OnUpdate= function(dt)
    if inst:HasTag("boat") then
        local tile = inst:GetCurrentTileType()
        if tile and not GetMap():IsWater(tile) and inst.prefab ~= "toso_surfboard" then
            if self.driver then
                self.driver.components.driver:OnDismount(nil, inst:GetPosition())
            end
            inst:onhammered(inst)
        end

    end
end
	return inst
end 

local function surfboarditemfn(Sim)

	local inst = CreateEntity()

	inst.entity:AddMiniMapEntity()
	inst.MiniMapEntity:SetIcon( "sword_3.tex" )

	inst.entity:AddTransform()
	inst.entity:AddAnimState()
	MakeInventoryPhysics(inst)

	inst:AddTag("boat")
	--加入剑属性标签
    inst:AddTag("sharp")
    inst:AddTag("weapon_sword")
	
	inst.AnimState:SetBank("sword_3") 
	inst.AnimState:SetBuild("sword_3") 
	inst.AnimState:PlayAnimation("idle")
	
    --加入剑属性	
    inst:AddComponent("weapon")
    inst.components.weapon:SetDamage(45)
    inst.components.weapon:SetRange(1.5)
    inst.components.weapon:SetOnAttack(onAttackf)
    --可装备
    inst:AddComponent("equippable")
    inst.components.equippable:SetOnEquip(onequip)
    inst.components.equippable:SetOnUnequip(onunequip)

	if not IsDLCEnabled(REIGN_OF_GIANTS) then
    MakeInventoryFloatable(inst, "idle_water", "anim")
    inst.components.equippable.poisonblocker = true
	inst:AddComponent("windproofer")
	inst.components.windproofer:SetEffectiveness(0) 
    end
	
	inst:AddComponent("inspectable")
	inst:AddComponent("inventoryitem")
    inst.components.inventoryitem.keepondeath = true
	inst.components.inventoryitem.imagename = "sword_3"
	inst.components.inventoryitem.atlasname = "images/inventoryimages/sword_3.xml"

	inst.components.inventoryitem:SetOnDroppedFn(surfboard_ondropped)

	inst:AddComponent("pocket")

	inst:AddComponent("deployable")
	inst.components.deployable.ondeploy = ondeploy
	inst.components.deployable.placer = "surfboard_placer"
	inst.components.deployable.deploydistance = 2

	inst:AddComponent("characterspecific")
    inst.components.characterspecific:SetOwner("toso")
	
	return inst
end

return 
	Prefab( "common/objects/toso_surfboard", surfboardfn, assets, prefabs),
	Prefab("common/surfboard_item_toso", surfboarditemfn, assets, prefabs),
	MakePlacer( "common/surfboard_placer", "toso_sword_fly", "toso_sword_fly", "run_loop", false, false, false, nil, nil, nil, nil, true)