local assets =
{
	Asset("SOUND", "sound/sfx.fsb"),
	Asset("SOUND", "sound/wilson.fsb"),
	Asset("SOUND", "sound/maxwell.fsb"),
	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_cave_enter.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/player_slurtle_armor.zip"),
	Asset("ANIM", "anim/player_staff.zip"),	
	Asset("ANIM", "anim/waxwell_shadow_mod.zip"),
	Asset("ANIM", "anim/nightmaresword.zip"),
	Asset("ANIM", "anim/swap_nightmaresword.zip"),
}

local prefabs = 
{
	"nightmarefuel",
	"purplegem",
	"nightsword",
	"armor_sanity",
}

local function RetargetFn(inst)
	return FindEntity(inst, 10, 
		function(tar) return inst.components.combat:CanTarget(tar) end, nil, 
		{"prey", "bird", "butterfly", "aquatic", "shadowmaxwell", "doydoy", "rabbit", "structure", "wall", "insect"})
end

local function KeepTarget(inst, target)
    local shouldkeep = inst.components.combat:CanTarget(target)
    local onwater = target:HasTag("aquatic")
    return shouldkeep and not onwater
end

local function OnAttacked(inst, data)
    inst.components.combat:SetTarget(data.attacker)
    inst.components.combat:ShareTarget(data.attacker, 30, function(dude)
        return dude:HasTag("shadowmaxwell")
               and not dude.components.health:IsDead()
               and dude.components.follower
               and dude.components.follower.leader == inst.components.follower.leader
    end, 10)
end

local function fn(Sim)
	local inst = CreateEntity()
	local trans = inst.entity:AddTransform()
	local anim = inst.entity:AddAnimState()
	local sound = inst.entity:AddSoundEmitter()
	local shadow = inst.entity:AddDynamicShadow()
	
	shadow:SetSize(1.5,.75)
	
	inst.entity:AddTransform()
	inst.entity:AddAnimState()
	inst.entity:AddSoundEmitter()
	inst.entity:AddLightWatcher()
	inst.Transform:SetFourFaced()
	
    MakeCharacterPhysics(inst,30,.3)
	
	anim:SetBank("wilson")
	anim:SetBuild("waxwell_shadow_mod")	
	anim:PlayAnimation("idle")
	
	inst.AnimState:OverrideSymbol("swap_object", "swap_nightmaresword", "swap_nightmaresword")
    inst.AnimState:Show("ARM_carry") 
    inst.AnimState:Hide("ARM_normal") 
	
	inst:AddTag("monster")
	inst:AddTag("hostile")
    inst:AddTag("shadowmaxwell")
    inst:AddTag("scarytoprey")
	
    inst:AddComponent("locomotor")
    inst.components.locomotor.runspeed = 6
    inst.components.locomotor.walkspeed = 6
	
	inst:AddComponent("colourtweener")
	inst.components.colourtweener:StartTween({0,0,0,.5}, 0)
	
	inst:AddComponent("knownlocations")
	
    inst:AddComponent("health")
    inst.components.health:SetMaxHealth(220)
	
    inst:AddComponent("combat")
    inst.components.combat:SetDefaultDamage(40)
    inst.components.combat:SetAttackPeriod(3)
	inst.components.combat:SetRetargetFunction(1, RetargetFn)
	inst.components.combat:SetKeepTargetFunction(KeepTarget)
	
	inst:AddComponent("inspectable")
	
    inst:AddComponent("follower")
    inst.components.follower.maxfollowtime = 99999999  
	
	inst:AddComponent("inventory")
	
    inst:AddComponent("lootdropper")
	inst.components.lootdropper:AddChanceLoot("nightmarefuel",0.50)
	inst.components.lootdropper:AddChanceLoot("purplegem",0.05)
	inst.components.lootdropper:AddChanceLoot("nightsword",0.01)
	inst.components.lootdropper:AddChanceLoot("armor_sanity",0.01)
	
	local brain = require "brains/shadowmaxwellbrain"
    inst:SetBrain(brain)
	
	inst:SetStateGraph("SGshadowmaxwell")
	
	inst:DoPeriodicTask(1, function()
		if inst:GetTimeAlive() > 960 then
			inst:Remove()
		end
	end)
	
	inst:ListenForEvent("attacked", OnAttacked)
    return inst
end

STRINGS.NAMES.SHADOWMAXWELL = "Shadow Soldier"
STRINGS.CHARACTERS.GENERIC.DESCRIBE.SHADOWMAXWELL = "This is beyond my imagination."

return Prefab("common/shadowmaxwell", fn, assets, prefabs)