ObjectData = 
{
	MotionModel = 
	{
		DefaultAnimation = [[normal]],
		Motions = 
		{
			motion3 = 
			{
				Animations = 
				{
					anim1 = 
					{
						Path = [[DATA:Animations\Cart\cart_ejection_toolbar.anim]],
						Name = [[cart_ejection_toolbar]],
						MinFreq = 1.000000,
						MaxFreq = 1.000000,
					},
				},
				Name = [[toolbar]],
			},
			motion2 = 
			{
				Animations = 
				{
					anim1 = 
					{
						Path = [[DATA:Animations\Cart\cart_ejection.anim]],
						Name = [[cart]],
						MinFreq = 1.000000,
						MaxFreq = 1.000000,
					},
				},
				Name = [[normal]],
			},
			motion1 = 
			{
				Animations = 
				{
					anim1 = 
					{
						Path = [[DATA:Animations\Cart\cart_alt.anim]],
						Name = [[cart_alt]],
						MinFreq = 1.000000,
						MaxFreq = 1.000000,
					},
				},
				Name = [[alt_collision]],
			},
		},
	},
	Extensions = 
	{
		PhysicsExtension = 
		{
			kinetic_friction = 0.000000,
			static_friction = 0.000000,
			restuition = 0.0,
			mass = 50.000000,
			collision_tolerance = 50,
		},
		RollingExtension = 
		{
		},
		ClickableExtension = 
		{
		},
		PositionExtension = 
		{
		},
		LevelEditorExtension = 
		{
			teleportable = true,
			rotate_divisions = 8.000000,
		},
		HoldingExtension = 
		{
		},
		AltCollisionExtension = 
		{
		},
		WalkingExtension = 
		{
			autoWalks = false,
			walking_offset = 19.000000,
			max_degrees = 55,
			collision_offet_y = 21.000000,
			speed = 2.500000,
			length = 20.000000,
			collision_offet_x = -10.000000,
			useRotation = true,
		},
		ThwackableExtension = 
		{
		},
		EdibleExtension = 
		{
			Eaters =
			{
				"whale",
			},
		},
	},
}

-- Begin Scripting --
help_text_header = "$00605"
help_text = "$00606"

function OnInit()

	--this:SetAltCollision("cart_alt")

	MERCH_MERCH_HASH = World_HashString( "merch_merch" )
	EXPLODING_MARSH_HASH = World_HashString("giant_marshmallow_exploding")
	GINSENG_HASH = World_HashString("ginseng")
	SUPERPIG_HASH = World_HashString("superpig")
	rolling_impulse = 1500

	-- The Exploding Marshmallow exploding velocity.
	EXPLODE_VELOCITY = 36
	
end

cart_rollingsound=nil

function Update()

	local he = this:GetHoldingExtension()
	
	for report in this:GetCollisionReports() do
		local object = World_GetObjectByID( report.id_2 )
		
		-- Explodes the Exploding Marshmallow if it falls into the cart too fast.
		local isExplodingMarshmallowAlive = true
		if (object:GetBlueprintHash() == EXPLODING_MARSH_HASH and report.relative_velocity:LengthSqr() > EXPLODE_VELOCITY) then
			isExplodingMarshmallowAlive = false
		end

		if object:GetEdibleExtension() and not object:GetHoldingExtension() and not he:IsHoldingAny() and object:GetVisibility()
			and isExplodingMarshmallowAlive
			and object:GetBlueprintHash() ~= GINSENG_HASH
			and object:GetBlueprintHash() ~= SUPERPIG_HASH 
			then

			if World_GetGravity().y > 0 then
				local pos = object:GetPosition()-- + Vector2( 0, object:GetHeight() / 2 )
				local my_pos = this:GetPosition() - Vector2( 0, 5 )

				if pos.y < my_pos.y then
					this:HoldObject( object )
				end
			else
				local pos = object:GetPosition() - Vector2( 0, object:GetHeight() / 2 )
				local my_pos = this:GetPosition() + Vector2( 0, 5)
				
				if pos.y > my_pos.y then
					this:HoldObject( object )
				end
			end
		end

	end
	
	local held = nil
	for object in he:GetHolds() do
		held = object
		held:EnablePhysics( false )
	end
	
	if (not hold_effect or not hold_effect:IsValid()) and held then
	
		this:CreateEffect("cart/object_inside")
		hold_effect = this:CreateEffect("cart/"..held:GetBlueprintName() )
		if held:GetBlueprintHash() == MERCH_MERCH_HASH then
			hold_effect:SetParticleAnim("DATA:Animations/merch/merch merch/"..held:GetMotionModel():GetCurrentMotion():GetCurrentAnimName()..".anim")
		end
	
	end
	
end

function OnDestroy()

	local he = this:GetHoldingExtension()

	--cache it first
	local held_object = nil
	for object in he:GetHolds() do
		
		--should only be one thing inside
		--assert( held_object == nil )
		held_object = object
		
	end
	
	this:ReleaseAll()
	
	local gravity = World_GetGravity().y
	
	--NOW apply, after release, or it won't have any effect
	local perp_dot = this:GetFacing():PerpDot()
	
	if held_object ~= nil then
		local held_object_pos = held_object:GetPosition()
	
		-- Centres Eets when Eets is ejected, since Eets' GetPosition() isn't exactly at the centre of Eets.
		if held_object:GetFlipped() and held_object:GetID() == World_GetEets():GetID() then
			--held_object_pos.x = held_object_pos.x - gravity * 12
			held_object_pos = held_object_pos - held_object:GetFacing()*gravity * 12
		else
			--held_object_pos.x = held_object_pos.x  + gravity * 12
			held_object_pos = held_object_pos + held_object:GetFacing()*gravity * 12
		end
	
		held_object:ForceFacing( this:GetFacing() * gravity )
		held_object:ForcePosition(held_object_pos + perp_dot * gravity * - 20)
		up_force = perp_dot*-10
	
		held_object:ApplyImpulse( up_force * rolling_impulse * gravity)
	end
	
end


function OnClick1()

	FX_SpawnEffect( "cart/eject_explode", this:GetPosition() )

	-- kill rolling sound when cart is destroyed
	if cart_rollingsound ~= nil then
		cart_rollingsound:StopPatch()
	end 

	this:KillMe()

end