
local MakePlayerCharacter = require "prefabs/player_common"


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

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


local function sanityfn(inst)
	local x, y, z = inst.Transform:GetWorldPosition()
	local delta = 0
	local ents = TheSim:FindEntities(x, y, z, 20, {"leaf"})
	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)

end

local function onbecameghost(inst)

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

local function OnHitOther(inst, other, damage)
    inst.components.thief:StealItem(other)
end

-- This initializes for both the server and client. Tags can be added here.
local common_postinit = function(inst) 
	-- Minimap icon
	inst.MiniMapEntity:SetIcon( "silver.tex" )
	inst.soundsname = "silver"
	inst.components.talker.colour = Vector3(190/255, 205/255, 229/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.components.combat.onhitotherfn = OnHitOther

    inst:AddComponent("thief")
	
	inst.components.temperature.inherentinsulation = 40
	
	inst.components.sanity.custom_rate_fn = sanityfn	
	inst.components.sanity.dapperness = -TUNING.DAPPERNESS_SMALL
	-- 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
	
end

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