require "brains/assasin_w_brain"
local brain = require "brains/assasin_w_brain"
require "stategraphs/SGassasin_w"

--local MakePlayerCharacter = require "prefabs/player_common"

local assets = 
{
    Asset("ANIM", "anim/wilson.zip"),
	Asset("ANIM", "anim/kikyou.zip"),
	--Asset("ANIM", "anim/assasin_wilson.zip"),
	Asset("SOUND", "sound/wilson.fsb"),
		Asset("SCRIPT", "scripts/prefabs/player_common.lua"),
		Asset("ANIM", "anim/swap_nightmaresword.zip"),
		Asset("ANIM", "anim/swap_datai.zip"),
		Asset("ANIM", "anim/swap_xiaotai.zip"),
		Asset("ANIM", "anim/swap_zhxing.zip"),
		Asset("ANIM", "anim/swap_sword_1.zip"),
		Asset("ANIM", "anim/swap_sword_2.zip"),
		Asset("ANIM", "anim/swap_sword_3.zip"),
		Asset("ANIM", "anim/swap_sword_6.zip"),
		Asset("ANIM", "anim/swap_sword_7.zip"),
		Asset("ANIM", "anim/backpack.zip"),
		Asset("ANIM", "anim/armor_wood.zip"),
		Asset("ANIM", "anim/armor_grass.zip"),
		Asset("ANIM", "anim/armor_marble.zip"),
		Asset("ANIM", "anim/armor_slurper.zip"),
		Asset("ANIM", "anim/armor_ruins.zip"),
		Asset("ANIM", "anim/armor_slurtleshell.zip"),
		Asset( "ANIM", "anim/player_basic.zip" ),
        Asset( "ANIM", "anim/player_idles_shiver.zip" ),
        Asset( "ANIM", "anim/player_actions.zip" ),
        Asset( "ANIM", "anim/player_actions_axe.zip" ),
        Asset( "ANIM", "anim/player_actions_pickaxe.zip" ),
        Asset( "ANIM", "anim/player_actions_shovel.zip" ),
        Asset( "ANIM", "anim/player_actions_blowdart.zip" ),
        Asset( "ANIM", "anim/player_actions_eat.zip" ),
        Asset( "ANIM", "anim/player_actions_item.zip" ),
        Asset( "ANIM", "anim/player_actions_uniqueitem.zip" ),
        Asset( "ANIM", "anim/player_actions_bugnet.zip" ),
        Asset( "ANIM", "anim/player_actions_fishing.zip" ),
        Asset( "ANIM", "anim/player_actions_boomerang.zip" ),
        Asset( "ANIM", "anim/player_bush_hat.zip" ),
        Asset( "ANIM", "anim/player_attacks.zip" ),
        Asset( "ANIM", "anim/player_idles.zip" ),
        Asset( "ANIM", "anim/player_rebirth.zip" ),
        Asset( "ANIM", "anim/player_jump.zip" ),
        Asset( "ANIM", "anim/player_amulet_resurrect.zip" ),
        Asset( "ANIM", "anim/player_teleport.zip" ),
        Asset( "ANIM", "anim/wilson_fx.zip" ),
        Asset( "ANIM", "anim/player_one_man_band.zip" ),
        Asset( "ANIM", "anim/shadow_hands.zip" ),
        Asset( "SOUND", "sound/sfx.fsb" ),
        Asset( "SOUND", "sound/maxwell.fsb" ),
		Asset("SOUND", "sound/willow.fsb"),    
        --Asset( "ANIM", "anim/beard.zip" ),
		Asset("ANIM", "anim/player_knockedout.zip"),
		
	Asset("ANIM", "anim/lantern.zip"),
	Asset("ANIM", "anim/swap_lantern.zip"),
	}

local prefabs = 
{
}

local start_inv = 
{
}

local items =
{
	AXE = "swap_axe",
	PICK = "swap_pickaxe",
	SWORD = "swap_nightmaresword",
	TORCH = "swap_lantern",
	BODY = "swap_backpack",
	HAT3 = "hat_miner",
	HAT2 = "hat_football",
	HAT1 = "hat_top",
	
}

local function ondeath(inst, data)
       local current = inst.components.inventory:GetEquippedItem(EQUIPSLOTS.HEAD)
        if current then
            inst.components.inventory:DropItem(current)
        end
	
local dark2 = SpawnPrefab("statue_transition_2")
local pos = Vector3(inst.Transform:GetWorldPosition())

end


 
 
  local function EquipHead(inst, item)
 if item then
        inst.AnimState:OverrideSymbol("swap_hat", item, "swap_hat")
        inst.AnimState:Show("HAT")
        inst.AnimState:Show("HAT_HAIR")
        inst.AnimState:Show("HAIR_NOHAT")
        inst.AnimState:Hide("HAIR")
		
end
end

 local function EquipArmor(inst, item)
 if item then
	inst.AnimState:OverrideSymbol("swap_body", item, "swap_body")
end
end
local function EquipAxe(inst, item)
	if item then
	    inst.AnimState:OverrideSymbol("swap_object", item, "swap_axe")
	    inst.AnimState:Show("ARM_carry") 
	    inst.AnimState:Hide("ARM_normal")
	end
end
local function EquipPickaxe(inst, item)
	if item then
	    inst.AnimState:OverrideSymbol("swap_object", item, "swap_pickaxe")
	    inst.AnimState:Show("ARM_carry") 
	    inst.AnimState:Hide("ARM_normal")
	end
end
local function EquipTorch(inst, item)
	if item then
	    inst.AnimState:OverrideSymbol("swap_object", item, "swap_lantern_on")
		inst.AnimState:OverrideSymbol("lantern_overlay", "swap_lantern", "lantern_overlay")
	    inst.AnimState:Show("ARM_carry") 
	    inst.AnimState:Hide("ARM_normal")
		inst.AnimState:Show("LANTERN_OVERLAY") 
       -- inst.SoundEmitter:PlaySound("dontstarve/wilson/lantern_LP", "loop")
	end
end
local function EquipItem(inst, item)
	if item then
	    inst.AnimState:OverrideSymbol("swap_object", item, "swap_nightmaresword")
	    inst.AnimState:Show("ARM_carry") 
	    inst.AnimState:Hide("ARM_normal")
			
			
	end
end
local function ShouldAcceptItem(inst, item)
  
    if item.components.equippable and item.components.equippable.equipslot == EQUIPSLOTS.HEAD then
        return true
    end
  
    if item.components.edible and inst.components.hunger and inst.components.eater then
        return inst.components.eater:CanEat(item) and inst.components.hunger:GetPercent() <= 0.9
    end
end
local function CanEatTest(inst, item)
 	if not item:HasTag("spoiled") then
			return true
end
end
local function OnRefuseItem(inst, item)
   inst.sg:GoToState("talk")
   inst.components.talker:Say("I'm Not piggy man.")
   inst.components.locomotor:Stop()
  -- inst:DoTaskInTime( 1.5, function() inst.sg:GoToState("idle") end)
   --[[if inst.components.sleeper:IsAsleep() then
    inst.components.sleeper:WakeUp()
    end]]
end


local function OnGetItemFromPlayer(inst, giver, item)
 
 --[[if inst.components.sleeper:IsAsleep() then
    inst.components.sleeper:WakeUp()
    end]]

    if item.components.edible then

    if inst.components.combat.target and inst.components.combat.target == giver then
            inst.components.combat:SetTarget(nil)
elseif giver.components.leader then
giver.components.leader:AddFollower(inst)
        end
        if inst.components.eater:Eat(item) then
		if item.components.edible.hungervalue >= 15 then
		inst.sg:GoToState("eat")
	--inst.components.talker:Say("Yam Yam")
		elseif item.components.edible.hungervalue < 15 then
		inst.sg:GoToState("quickeat")
		end    end    
		end
   --I wear hats
    if item.components.equippable and item.components.equippable.equipslot == EQUIPSLOTS.HEAD then
        local current = inst.components.inventory:GetEquippedItem(EQUIPSLOTS.HEAD)
        if current then
            inst.components.inventory:DropItem(current)
        end
        
        inst.components.inventory:Equip(item)
        inst.AnimState:Show("hat")
    end		
		
    end
	
local function OnEat(inst, food)
inst.components.health:DoDelta(15)
inst.components.hunger:DoDelta(10)
if food.components.edible and food.components.edible.hungervalue > 20 or food.components.edible.healthvalue > 15 then
inst.components.health:DoDelta(100)
inst.components.hunger:DoDelta(50)
end end


local function OnHitfreeze(inst, data)
local other = data.target
    if other and (other:HasTag("toso") or other:HasTag("player")) then
	   inst.components.combat:SetTarget(nil)
	   inst.components.combat:GiveUp()
	end
	
end


	
local function Onhunt(inst, target)
    return inst.components.combat:CanTarget(target)
end

local function hound_hunt(inst)
local player = GetPlayer()
if player:HasTag("crazy") and not inst.time then
inst.components.talker:Say("I think you need a time for yourself..") 
inst.time = true
end

if inst.components.follower.leader and inst.follow and not inst.active_hunt then
inst.components.combat:GiveUp()
end 

    if inst.active_hunt then 
     local dist = 30
     return FindEntity(inst, dist, function(guy)
     return inst.components.combat:CanTarget(guy)
    end,
    nil,
   {"toso","player","wall","houndmound","structure","companion","yamche","beefalo","koalefant","arongb","pig","bee","rocky","bird","statue","character","abigail","smashable","veggie"})

elseif inst.peace and not inst.active_hunt then
inst.components.combat:GiveUp()

elseif not inst.active_hunt and not inst.peace then
if inst.combat_no then
inst.components.combat:SetTarget(nil)
inst.components.combat:GiveUp()
end

end
end 	



local function item_set(inst)
if inst.fight then
inst:DoTaskInTime( 15, function() inst.fight = false end)
end

if not inst.fight and GetClock():IsNight() then
inst.equipfnt(inst, inst.items["TORCH"])
inst.Light:Enable(true)
inst.torch = true
elseif not GetClock():IsNight() and inst.torch then
inst.equipfna(inst, inst.items["AXE"])
inst.Light:Enable(false)
inst.torch = false
inst.AnimState:ClearOverrideSymbol("lantern_overlay")
end
end

local function on_close(inst)
local player = GetPlayer()
inst.fighting = true
inst.combat_no = false
	inst.components.locomotor.walkspeed = (TUNING.WILSON_WALK_SPEED)
	inst.components.locomotor.runspeed = (TUNING.WILSON_RUN_SPEED)
player.components.leader:AddFollower(inst)

end 

local function far(inst)
local player = GetPlayer()
	inst.components.locomotor.walkspeed = (TUNING.WILSON_WALK_SPEED * 1.5)
	inst.components.locomotor.runspeed = (TUNING.WILSON_RUN_SPEED * 1.5)
player.components.leader:AddFollower(inst)
inst.combat_no = true
inst.fighting = false
inst.components.combat:SetTarget(nil)
inst.components.combat:GiveUp()
end


local function fn()
	local inst = CreateEntity()
	local trans = inst.entity:AddTransform()
	local anim = inst.entity:AddAnimState()
	local sound = inst.entity:AddSoundEmitter()

	inst.Transform:SetFourFaced(inst)

    MakeCharacterPhysics(inst, 1, .5) 
 	
	anim:SetBank("wilson")
	anim:SetBuild("kikyou")
	anim:PlayAnimation("idle")

    anim:Hide("ARM_carry")
    anim:Hide("hat")
    anim:Hide("hat_hair")

    inst:AddTag("scarytoprey")
    inst:AddTag("toso_role")
 
	inst.soundsname = "wilson"
	inst.talker_path_override = "dontstarve/characters/"

	--inst:AddTag("NOCLICK")
	--inst:AddTag("assasin")
	inst:AddTag("character")
	inst:AddTag("companion")
	inst:AddTag("yamcheb")
	inst:AddTag("notraptrigger")	
	inst:AddTag("ghosthound")
	
-----------light on
    inst.entity:AddLight()
	inst.Light:SetRadius(2.1)
    inst.Light:SetFalloff(.7)
    inst.Light:SetIntensity(.7)
    inst.Light:SetColour(185/255,175/255,170/255)
	inst.Light:Enable(false)	
	
if  IsDLCEnabled(REIGN_OF_GIANTS) or SaveGameIndex:IsModeShipwrecked() then
         inst.DLC = true
		end
local player = GetPlayer()
if player:HasTag("DLC") or inst:HasTag("DLC") then
         inst.DLC = true
		end
		
	--inst.components.eater.monsterimmune = true
	
			    inst:AddComponent("talker")
    inst.components.talker.fontsize = 30
    inst.components.talker.colour = Vector3(1, 0.6, 0.75, 1)
--	inst.Transform:SetScale(1.1,1.1,1.1)
			inst.hurtsoundoverride = "wilson"
		inst.talker_path_override = "wilson"
	
	inst:AddComponent("colourtweener")
	--inst.components.colourtweener:StartTween({.1,.1,.1,.1}, 0)
	
	--inst:DoTaskInTime(2, function() inst.hide = true end)
		
		inst:AddComponent("locomotor")
	--inst.components.locomotor.triggerscreep = false	
	
   inst:AddComponent("follower")
   
    inst:AddComponent("combat")
    inst.components.combat.playerdamagepercent = 0
    inst.components.combat.hiteffectsymbol = "torso"
	--inst.components.combat:SetRetargetFunction(0, assasin_hunt)
	inst.components.combat:SetRetargetFunction(1, hound_hunt)
    inst.components.combat:SetKeepTargetFunction(Onhunt)
    inst.components.combat:SetAttackPeriod(1.5)
    inst.components.combat:SetRange(2, 3)
	
     inst:AddComponent("playerprox")
    inst.components.playerprox:SetDist(7, 8)
    inst.components.playerprox:SetOnPlayerNear(on_close)
    inst.components.playerprox:SetOnPlayerFar(far)
  
    inst:AddComponent("health")
	inst.components.health:StartRegen(1, 10)
	inst:AddComponent("hunger")
	inst.components.hunger:SetRate(0.25)		
	inst.components.hunger:SetMax(300)
	inst:AddComponent("trader")
    inst.components.trader:SetAcceptTest(ShouldAcceptItem)
    inst.components.trader.onaccept = OnGetItemFromPlayer
    inst.components.trader.onrefuse = OnRefuseItem
	inst:AddComponent("eater")
	inst.components.eater:SetCanEatTestFn(CanEatTest)
    inst.components.eater:SetOmnivore()
	inst.components.eater.strongstomach = true
	inst.components.eater:SetOnEatFn(OnEat)
	
	inst:AddComponent("inventory")
    inst.components.inventory.dropondeath = false
    --inst:AddComponent("beard")
    --inst.components.beard.prize = "beardhair"
   
	    inst.items = items
    inst.equipfn = EquipItem
	inst.equipfn2 = EquipArmor
	inst.equipfn3 = EquipHead
	inst.equipfna = EquipAxe
	inst.equipfnp = EquipPickaxe
	inst.equipfnt = EquipTorch
	EquipItem(inst)
	EquipArmor(inst)
	EquipHead(inst)
	local player = GetPlayer()
	
	--if player:HasTag("musha") then
if player.level then --1
	inst.equipfn(inst, inst.items["SWORD"])
	inst.equipfn2(inst, inst.items["BODY"])
	inst.components.combat:SetDefaultDamage(15+player.level*2)
	inst.components.health:SetMaxHealth(300*player.level*8)
	inst.components.health:StartRegen(20, 70)
end
    inst:ListenForEvent("death", ondeath)
    --inst:ListenForEvent("attacked", inst.procfn)
	--inst:ListenForEvent("attacked", OnAttacked)
	inst:ListenForEvent("onhitother", OnHitfreeze)
	inst.item_set = inst:DoPeriodicTask(1,item_set)

  
	inst:SetBrain(brain)
	inst:SetStateGraph("SGassasin_w")
	
		return inst
end
return Prefab("common/assasin_wilson", fn, assets, prefabs)

