ObjectData = 
{
	MotionModel = 
	{
		DefaultAnimation = [[normal]],
		Motions = 
		{
			motion3 = 
			{
				Animations = 
				{
					anim1 = 
					{
						Path = [[DATA:Animations\Merch\merch.anim]],
						Name = [[merch]],
						MinFreq = 1.000000,
						MaxFreq = 1.000000,
					},
				},
				Name = [[toolbar]],
			},
			motion2 = 
			{
				Animations = 
				{
					anim1 = 
					{
						Path = [[DATA:Animations\Merch\flesh_reaper_idle_v1.anim]],
						Name = [[flesh_reaper_idle_v1]],
						MinFreq = 2.000000,
						MaxFreq = 5.000000,
					},
					anim2 = 
					{
						Path = [[DATA:Animations\Merch\flesh_reaper_idle_v2.anim]],
						Name = [[flesh_reaper_idle_v2]],
						MinFreq = 2.000000,
						MaxFreq = 5.000000,
					},
					anim3 = 
					{
						Path = [[DATA:Animations\Merch\flesh_reaper_idle_v2_bite.anim]],
						Name = [[flesh_reaper_idle_v2_bite]],
						MinFreq = 3.000000,
						MaxFreq = 6.000000,
					},
				},
				Name = [[fleshreaper]],
			},
			motion4 = 
			{
				Animations = 
				{
					anim1 = 
					{
						Path = [[DATA:Animations\Merch\merch_happy_transition.anim]],
						Name = [[merch_happy_transition]],
						MinFreq = 1.000000,
						MaxFreq = 1.000000,
					},
				},
				Name = [[merch_happy_transition]],
			},
			motion5 = 
			{
				Animations = 
				{
					anim1 = 
					{
						Path = [[DATA:Animations\Merch\merch_sad_transition.anim]],
						Name = [[merch_sad_transition]],
						MinFreq = 1.000000,
						MaxFreq = 1.000000,
					},
				},
				Name = [[merch_sad_transition]],
			},
			motion6 = 
			{
				Animations = 
				{
					anim1 = 
					{
						Path = [[DATA:Animations\Merch\merch_happy_idle.anim]],
						Name = [[merch_happy_idle]],
						MinFreq = 1.000000,
						MaxFreq = 1.000000,
					},
				},
				Name = [[merch_happy_idle]],
			},
			motion7 = 
			{
				Animations = 
				{
					anim1 = 
					{
						Path = [[DATA:Animations\Merch\merch_talking.anim]],
						Name = [[merch_talking]],
						MinFreq = 1.000000,
						MaxFreq = 1.000000,
					},
				},
				Name = [[merch_talking]],
			},
			motion8 = 
			{
				Animations = 
				{
					anim1 = 
					{
						Path = [[DATA:Animations\Merch\merch_hop_forward.anim]],
						Name = [[merch_hop_forward]],
						MinFreq = 1.000000,
						MaxFreq = 1.000000,
					},
				},
				Name = [[merch_hop_forward]],
			},
			motion9 = 
			{
				Animations = 
				{
					anim1 = 
					{
						Path = [[DATA:Animations\Merch\merch_hop_back.anim]],
						Name = [[merch_hop_back]],
						MinFreq = 1.000000,
						MaxFreq = 1.000000,
					},
				},
				Name = [[merch_hop_back]],
			},
			motion10 = 
			{
				Animations = 
				{
					anim1 = 
					{
						Path = [[DATA:Animations\Merch\flesh_reaper_biting_around.anim]],
						Name = [[flesh_reaper_biting_around]],
						MinFreq = 1.000000,
						MaxFreq = 1.000000,
					},
				},
				Name = [[fleshreaping]],
			},
			motion1 = 
			{
				Animations = 
				{
--[[					anim1 = 
					{
						Path = [[DATA:Animations\Merch\merch_sad_idle1.anim]],
						Name = [[merch_sad_idle1]],
						MinFreq = 3.000000,
						MaxFreq = 5.000000,
					},]]
					anim1 = 
					{
						Path = [[DATA:Animations\Merch\merch_sad_idle2.anim]],
						Name = [[merch_sad_idle2]],
						MinFreq = 8.000000,
						MaxFreq = 10.000000,
					},
				},
				Name = [[normal]],
			},
		},
	},
	Extensions = 
	{
		PositionExtension = 
		{
		},
		PhysicsExtension = 
		{
			mass = 1000.000000,
			static_friction = 0.700000,
			restitution = 0.300000,
			kinetic_friction = 0.300000,
		},
		LevelEditorExtension = 
		{
		},
	},
}

-- Begin Scripting --

help_text_header = "$00653"
help_text = "$00654"

function OnInit()

	this:EnablePhysics(false)

	MerchState =
	{
		Merch = 0,
		Reaper = 1
	}
	
	merch_state = MerchState.Merch
	
	MERCH_SAD_TIMER_COUNT = 24*8
	REAPER_TIMER_COUNT = 24*6
	
	REAPER_RADIUS = 70*70

	direction = 1
	
	reaper_timer = REAPER_TIMER_COUNT
	merch_sad_timer = 0
	
	MERCH_MERCH_HASH = World_HashString("merch_merch")
	MARSHOMECH_HASH = World_HashString("marshomech")
	
	merch_original_pos = this:GetPosition()
	victim_pos = nil
	
	reaper_glow = nil
	
end

function ReaperUpdate()

	-- Collect torsos.
	for report in this:GetCollisionReports() do
		local obj = World_GetObjectByID( report.id_2 )
		local edible = obj:GetEdibleExtension()
		local obj_blueprint = obj:GetBlueprintHash()
		
		if obj_blueprint == MARSHOMECH_HASH or (edible and obj_blueprint ~= MERCH_MERCH_HASH and obj:IsVisible() ) then
			ReapObject( obj )
		end
	end

	if this:GetMotionModel():GetCurrentMotionName() == "fleshreaping" then
	
		local motion = this:GetMotionModel():GetCurrentMotion()
		local anim = motion:GetCurrentAnim()
		local index = anim:GetCurrentFrameIndex()
		if index == 4 then
			this:ForcePosition( victim_pos )
			this:CreateEffect( "merch/reaping" )
		elseif
			index == 8 or
			index == 13 or
			index == 18 or 
			index == 23 or
			index == 27 or
			index == 31 or
			index == 35 then
			
			--move to a random location within a radius
			local randx = World_GetDeterministicDouble() - 0.5
			local randy = World_GetDeterministicDouble() - 0.5
			local pos = Vector2( randx*160, randy*160 ) + merch_original_pos
			
			this:ForcePosition( pos )
			
			--just miss this frame so it doesn't overlap so much
			if index ~= 31 then
				this:CreateEffect( "merch/reaping" )
				Sound_CreateSound( "Merch Bite "..tostring(World_GetDeterministicIntRange(1,3)), false, 0, this:GetPosition() )
			end
			
		end
	
	else
		this:ForcePosition( merch_original_pos )
	end
	
end

function ReapObject( obj )
	
	if this:GetMotionModel():GetCurrentMotionName() ~= "fleshreaping" then
		this:GetMotionModel():PushMotion("fleshreaping", false, false)
		Sound_CreateSound( "Merch Bite 1", false, 0, this:GetPosition() )
		victim_pos = obj:GetPosition()
	end
	
	obj:KillMe()
	
end

function GoInForeground()

	local pe = this:GetPositionExtension()
	
	if not pe:IsForeground() then
		pe:SetIsForeground( true )
		pe:SetIsBackground( false )
		
		this:GetMotionModel():PopMotion()
		this:GetMotionModel():PushMotion("merch_hop_forward", false, false)
	end
	
end

function GoInBackground()

	local pe = this:GetPositionExtension()
	
	if not pe:IsBackground() then
		--don't collide with things in the foreground
		pe:SetIsForeground( false )
		pe:SetIsBackground( true )
		--this:SetScale(Vector2(0.8,0.8))
		
		this:GetMotionModel():PushMotion("merch_hop_back", false, true)
	end
	
end

function MerchUpdate()
	
	--decrement the reaper timer
	if merch_sad_timer > 0 then
		merch_sad_timer = merch_sad_timer - 1
	else
		reaper_timer = reaper_timer - 1
	
		if reaper_timer == 0 then
			
			TransitionToFleshReaper()
			
			return
			
		end
		
	end
	
	local eets = World_GetEets()

	local we = eets:GetWalkingExtension()

	--check if I should let Eets pass
	local diff = eets:GetPosition() - this:GetPosition()
		
	if diff:LengthSqr() < 90*90 then

		GoInBackground()
	else
	
		GoInForeground()
		
	end
	
end

function Update()

	Profile_Start("merch")

	if merch_state == MerchState.Merch then
		MerchUpdate()
	elseif merch_state == MerchState.Reaper then
		ReaperUpdate()
	end
	
	Profile_Stop("merch")

end

function TransitionToHappyMerch()
	
	Sound_CreateSound( "Merch Poomph", false, 0, this:GetPosition() )
	--this:CreateEffect( "merch/poomph" )
	
	merch_state = MerchState.Merch
	this:GetMotionModel():SetDefaultMotion("normal")
	this:GetMotionModel():PushMotion("merch_sad_transition", false, false)
	this:GetMotionModel():PushMotion("merch_happy_idle", false, false)
	this:GetMotionModel():PushMotion("merch_happy_idle", false, false)
	this:GetMotionModel():PushMotion("merch_talking", false, false)
	this:GetMotionModel():PushMotion("merch_happy_transition", false, false)
	
	merch_sad_timer = MERCH_SAD_TIMER_COUNT
	
	if red_glow then 
		red_glow:Stop(false)
	end
	
end

function TransitionToFleshReaper()

	Sound_CreateSound( "Merch Kreee", false, 0, this:GetPosition() )
	
	this:CreateEffect( "merch/reaper_explosion" )
	this:CreateEffect( "merch/reaper" )
	red_glow = this:CreateEffect( "merch/red_glow" )
	
	GoInForeground()
	
	this:GetMotionModel():ClearMotions()
	this:GetMotionModel():PushMotion("fleshreaper", true, false)
	merch_state = MerchState.Reaper
	
end

function MerchMerchPickUp()

	reaper_timer = REAPER_TIMER_COUNT

	TransitionToHappyMerch()
	
	if not keen_insight or not keen_insight:IsValid() then
		--keen_insight:Stop( false )
		this:CreateEffect( "merch/poomph" )
		--keen_insight = this:CreateEffect( "merch/merch_pickup" )--"merch/keen_insight1" )
	end
	

end

