
local MakePlayerCharacter = require "prefabs/player_common"


local assets = {
    Asset("SCRIPT", "scripts/prefabs/player_common.lua"),
}
local prefabs = {
	"rainbowfeather",
}

-- Custom starting items
local start_inv = {
	"rainbowfeather",
}

local function sanityfn(inst)
	local x, y, z = inst.Transform:GetWorldPosition()
	local delta = 0
	local ents = TheSim:FindEntities(x, y, z, 20, {"bird"})
	for k, v in pairs(ents) do
		if v ~= inst then
			local bonus_sanity = -TUNING.SANITYAURA_LARGE
			local distsq = math.max(inst:GetDistanceSqToInst(v), 1)
			delta = delta + bonus_sanity / distsq
		end
	end
	return delta
end


-- When the character is revived from human
local function onbecamehuman(inst)
	inst:AddTag("leaf")
end

local function onbecameghost(inst)
	inst:RemoveTag("leaf")
end

local function pickup(inst)
	if inst:HasTag("leaf") and inst.components.health.currenthealth > 0 then
		local x, y, z = inst.Transform:GetWorldPosition()
		local ents = TheSim:FindEntities(x, y, z, 3, { "_inventoryitem" }, { "INLIMBO", "NOCLICK", "catchable", "fire" })
		for i, v in ipairs(ents) do
			if v.components.inventoryitem ~= nil and
				v.components.inventoryitem.canbepickedup and
				v.components.inventoryitem.cangoincontainer and
				not v.components.inventoryitem:IsHeld() and
				inst.components.inventory:CanAcceptCount(v, 1) > 0 then

				--Amulet will only ever pick up items one at a time. Even from stacks.
				SpawnPrefab("sand_puff").Transform:SetPosition(v.Transform:GetWorldPosition())

				local v_pos = v:GetPosition()
				if v.components.stackable ~= nil then
					v = v.components.stackable:Get()
				end

				if v.components.trap ~= nil and v.components.trap:IsSprung() then
					v.components.trap:Harvest(inst)
				else
					inst.components.inventory:GiveItem(v, nil, v_pos)
				end
				return
			end
		end
	end
end

-- When loading or spawning the character
local function onload(inst)
    inst:ListenForEvent("ms_respawnedfromghost", onbecamehuman)
    inst:ListenForEvent("ms_becameghost", onbecameghost)

    if inst:HasTag("playerghost") then
        onbecameghost(inst)
    else
        onbecamehuman(inst)
    end
end
-- This initializes for both the server and client. Tags can be added here.
local common_postinit = function(inst) 
	-- Minimap icon
	inst.MiniMapEntity:SetIcon( "leaf.tex" )
	inst.soundsname = "leaf"
	inst.components.talker.colour = Vector3(22/255, 135/255, 49/255)
end

-- This initializes for the server only. Components are added here.
local master_postinit = function(inst)
	
	-- Stats	
	inst.components.health:SetMaxHealth(150)
	inst.components.hunger:SetMax(150)
	inst.components.sanity:SetMax(150)
	
	inst:AddTag("leaf")
	inst.components.temperature.inherentinsulation = -40
	
	inst.components.sanity.custom_rate_fn = sanityfn	
	-- Damage multiplier (optional)
    inst.components.combat.damagemultiplier = 1
	
	-- Hunger rate (optional)
	inst.components.hunger.hungerrate = 1 * TUNING.WILSON_HUNGER_RATE
	
	inst.OnLoad = onload
    inst.OnNewSpawn = onload
	
	inst.task = inst:DoPeriodicTask(TUNING.ORANGEAMULET_ICD*5, pickup, nil, inst)
	
end

return MakePlayerCharacter("leaf", prefabs, assets, common_postinit, master_postinit, start_inv)
