ObjectData = 
{
	MotionModel = 
	{
		DefaultAnimation = [[normal]],
		Motions = 
		{
			motion2 = 
			{
				Animations = 
				{
					anim1 = 
					{
						Path = [[DATA:Animations\Giant Marshmallow\gme_toolbar.anim]],
						Name = [[gme_toolbar]],
						MinFreq = 1.000000,
						MaxFreq = 1.000000,
					},
				},
				Name = [[toolbar]],
			},
			motion1 = 
			{
				Animations = 
				{
					anim1 = 
					{
						Path = [[DATA:Animations\Giant Marshmallow\gme_upset_and_mumble.anim]],
						Name = [[gme_upset_and_mumble]],
						MinFreq = 4.000000,
						MaxFreq = 12.000000,
					},
					anim3 = 
					{
						Path = [[DATA:Animations\Giant Marshmallow\gme_upset_and_mumble_look_right.anim]],
						Name = [[gme_upset_and_mumble_look_right]],
						MinFreq = 4.000000,
						MaxFreq = 12.000000,
					},
					anim2 = 
					{
						Path = [[DATA:Animations\Giant Marshmallow\gme_upset_and_mumble_look_left.anim]],
						Name = [[gme_upset_and_mumble_look_left]],
						MinFreq = 4.000000,
						MaxFreq = 12.000000,
					},
				},
				Name = [[normal]],
			},
			motion3 = 
			{
				Animations = 
				{
					anim1 = 
					{
						Path = [[DATA:Animations\Giant Marshmallow\gme_alt.anim]],
						Name = [[gme_alt]],
						MinFreq = 1.000000,
						MaxFreq = 1.000000,
					},
				},
				Name = [[alt_collision]],
			},
		},
	},
	Extensions = 
	{
		EdibleExtension = 
		{
			Eaters =
			{
				"whale",
				"eets_angry",
				"squirrel",
			},
			emotion_name = [[angry]],
			emotion_value = 1001.000000,
		},
		PhysicsExtension = 
		{
			mass = 20.000000,
			static_friction = 0.750000,
			restitution = 0.450000,
			kinetic_friction = 0.250000,
		},
		LevelEditorExtension = 
		{
			rotate_divisions = 8.000000,
		},
		PositionExtension = 
		{
		},
		ThwackableExtension = 
		{
			plays_thwack_effect = false
		},
		SuckableExtension = 
		{
		},
		AltCollisionExtension = 
		{
		},
	},
}

-- Begin Scripting --

help_text_header = "$00633"
help_text = "$00634"

function OnInit()	

	this:SetAltCollision("gme_alt")

	EXPLOSION_FORCE = 10000
	EXPLOSION_DETACH_FORCE = -5000
	EXPLODE_VELOCITY	= 6*6 

	--only apply to objects 100 pixels away
	tolerance_sqr = 100*100
	
	explosion_delay_timer = 2
	isGoingToBlowUp = false
	
	SUPERPIG_HASH = World_HashString("superpig")
	EXPLODING_MARSH_HASH = World_HashString("giant_marshmallow_exploding")
	CART_TNT_HASH = World_HashString("cart_tnt")
	BOB_HASH = World_HashString("bob")
	SNEEZY_SOW_HASH = World_HashString("sneezy_sow")
	MARSHOMECH_HASH = World_HashString("marshomech")
	
end

function OnDestroy()

	create_explosion_effects(this:GetPosition())

	-- Check for nearby objects and affect them.
	for object in World_GetObjects() do
	
		local diff = object:GetPosition() - this:GetPosition()

		--Checks for nearby items to explode/apply a force to them.
		if diff:LengthSqr() < tolerance_sqr and object:IsVisible() and object:GetID() ~= this:GetID() then

			local objectHash = object:GetBlueprintHash()

			-- Explodes nearby explodable objects.
			if objectHash == CART_TNT_HASH or objectHash == SUPERPIG_HASH or objectHash == EXPLODING_MARSH_HASH then

				local he = object:GetHoldingExtension()
		
				-- Ejects any objects inside a tnt cart before killing it.
				if he and he:IsHoldingAny() then
					he:ReleaseAll()
				end
			
				-- Tell the object to start blowing up
				object:CallFunction("BlowMeUp")

			-- Causes nearby pigs to activate.
			elseif objectHash == BOB_HASH or objectHash == SNEEZY_SOW_HASH then
				object:CallFunction("OnHit")

			-- Gets all other nearby objects and applies an outward force to them.
			else

				-- Detaches walkers from platforms.
				local we = object:GetWalkingExtension()
				if we and we:GetState() ~= WalkingExtension.WES_Falling and objectHash ~= MARSHOMECH_HASH then
					we:Reset()
				end

				local norm = diff
				if norm:LengthSqr() == 0 then
					norm = Vector2( 0, -1 )
				else
					norm:Normalize()
				end
				
				-- Apply an upward & outward force to the object.
				object:ForcePosition( object:GetPosition() )
				object:ApplyImpulse(Vector2(0, World_GetGravity().y * EXPLOSION_DETACH_FORCE))
				object:ApplyImpulse( norm * EXPLOSION_FORCE )
			end
		end
	end
end


-- Creates explosion effects.
function create_explosion_effects(objectLocation)

	FX_SpawnEffect( "cart/explode", objectLocation )
	--FX_SpawnEffect( "Superpig/explode", this:GetPosition() )
	Sound_CreateSound( "Explosion", false, 0, objectLocation )
	World_CreateExplosion(objectLocation, 90)
	World_Alert(objectLocation, 400)
	--World_Scare(objectLocation, 200, 100)
end


-- Begins the timer to explode this object.
function BlowMeUp()
	isGoingToBlowUp = true
end


function Update()

	Profile_Start("giant_marshmallow_exploding")
	
	for report in this:GetCollisionReports() do

		hit_object = World_GetObjectByID( report.id_2 )
		
		-- Explodes the bomb upon impact.
		if hit_object:GetID() ~= this:GetID() and report.relative_velocity:LengthSqr() > EXPLODE_VELOCITY then
			this:KillMe()
			Profile_Stop("giant_marshmallow_exploding")
			return
		end
	end
	
	-- Make the object blow up if it is caught in a chain explosion.
	if 	isGoingToBlowUp then
		if (explosion_delay_timer > 0) then
			explosion_delay_timer = explosion_delay_timer - 1
		else
			this:KillMe()
		end
	end
	
	Profile_Stop("giant_marshmallow_exploding")
end

