ObjectData = 
{
	MotionModel = 
	{
		DefaultAnimation = [[normal]],
		Motions = 
		{
			motion4 = 
			{
				Animations = 
				{
					anim1 = 
					{
						Path = [[DATA:Animations\Giant Marshmallow\giant_marshmallow_balloon_toolbar.anim]],
						Name = [[giant_marshmallow_balloon_toolbar]],
						MinFreq = 1.000000,
						MaxFreq = 1.000000,
					},
				},
				Name = [[toolbar]],
			},
			motion1 = 
			{
				Animations = 
				{
					anim1 = 
					{
						Path = [[DATA:Animations\Giant Marshmallow\giant_marshmallow_balloon_inflating.anim]],
						Name = [[giant_marshmallow_balloon_inflating]],
						MinFreq = 1.000000,
						MaxFreq = 1.000000,
					},
				},
				Name = [[inflating]],
			},
			motion5 = 
			{
				Animations = 
				{
					anim1 = 
					{
						Path = [[DATA:Animations\Giant Marshmallow\giant_marshmallow_balloon_inflated.anim]],
						Name = [[giant_marshmallow_balloon_inflated]],
						MinFreq = 1.000000,
						MaxFreq = 1.000000,
					},
				},
				Name = [[inflated]],
			},
			motion3 = 
			{
				Animations = 
				{
					anim6 = 
					{
						Path = [[DATA:Animations\Giant Marshmallow\giant_marshmallow_balloon_suck_tooth.anim]],
						Name = [[giant_marshmallow_balloon_suck_tooth]],
						MinFreq = 20.000000,
						MaxFreq = 30.000000,
					},
					anim4 = 
					{
						Path = [[DATA:Animations\Giant Marshmallow\giant_marshmallow_balloon_blink.anim]],
						Name = [[giant_marshmallow_balloon_blink]],
						MinFreq = 2.000000,
						MaxFreq = 2.000000,
					},
					anim5 = 
					{
						Path = [[DATA:Animations\Giant Marshmallow\giant_marshmallow_balloon_mumble.anim]],
						Name = [[giant_marshmallow_balloon_mumble]],
						MinFreq = 9.000000,
						MaxFreq = 16.000000,
					},
					anim1 = 
					{
						Path = [[DATA:Animations\Giant Marshmallow\giant_marshmallow_balloon_look_right.anim]],
						Name = [[giant_marshmallow_balloon_look_right]],
						MinFreq = 4.000000,
						MaxFreq = 12.000000,
					},
					anim3 = 
					{
						Path = [[DATA:Animations\Giant Marshmallow\giant_marshmallow_balloon_look_up.anim]],
						Name = [[giant_marshmallow_balloon_look_up]],
						MinFreq = 4.000000,
						MaxFreq = 12.000000,
					},
					anim2 = 
					{
						Path = [[DATA:Animations\Giant Marshmallow\giant_marshmallow_balloon_look_left.anim]],
						Name = [[giant_marshmallow_balloon_look_left]],
						MinFreq = 4.000000,
						MaxFreq = 12.000000,
					},
					anim7 = 
					{
						Path = [[DATA:Animations\Giant Marshmallow\giant_marshmallow_balloon_look_down.anim]],
						Name = [[giant_marshmallow_balloon_look_down]],
						MinFreq = 4.000000,
						MaxFreq = 12.000000,
					},
				},
				Name = [[normal]],
			},
			motion2 = 
			{
				Animations = 
				{
					anim1 = 
					{
						Path = [[DATA:Animations\Giant Marshmallow\giant_marshmallow_balloon_deflating.anim]],
						Name = [[giant_marshmallow_balloon_deflating]],
						MinFreq = 1.000000,
						MaxFreq = 1.000000,
					},
				},
				Name = [[deflating]],
			},
			motion6 = 
			{
				Animations = 
				{
					anim1 = 
					{
						Path = [[DATA:Animations\Giant Marshmallow\giant_marshmallow_balloon_alt.anim]],
						Name = [[giant_marshmallow_balloon_alt]],
						MinFreq = 1.000000,
						MaxFreq = 1.000000,
					},
				},
				Name = [[alt_collision]],
			},
		},
	},
	Extensions = 
	{
		EdibleExtension = 
		{
			Eaters =
			{
				"whale",
				"eets_angry",
				"squirrel",
			},
			emotion_name = [[happy]],
			emotion_value = 1001.000000,
		},
		PhysicsExtension = 
		{
			mass = 15.000000,
			static_friction = 0.950000,
			restitution = 0.500000,
			kinetic_friction = 0.450000,
		},
		LevelEditorExtension = 
		{
			rotate_icon_offset = {-60,-45},
			trash_icon_offset = {-41,26},
			rotate_divisions = 8.000000,
		},
		LightingExtension = 
		{
		},
		PositionExtension = 
		{
		},
		SuckableExtension = 
		{
		},
		ThwackableExtension = 
		{
		},
		AltCollisionExtension = 
		{
		},
	},
}-- Begin Scripting --

help_text_header = "$00631"
help_text = "$00632"

function OnInit()
	this:SetAltCollision("giant_marshmallow_balloon_alt")

	INFLATE_START = 36
	INFLATE_END = INFLATE_START + 48 --inflation anim is 48 frames long
	DEFLATE_START = INFLATE_END + 60 --float up for at least x frames
	DEFLATE_END = DEFLATE_START + 37 --deflation anim is 37 frames long
	is_inflating = false
	inflate_counter = 0
	frame = 1
end

function IsInflated()
	return is_inflating
end

function Update()
	Profile_Start("giant_marshmallow_balloon")

	local gravity = World_GetGravity()
	
	if (gravity:LengthSqr() ~= 0) then
		gravity:Normalize()
	end

	INFLATE_FORCE = gravity * -700
	DEFLATE_FORCE = gravity * -200
	
	local myMotionModel  = this:GetMotionModel()

	if ( this:GetLightingExtension():IsLit() and this:IsVisible() ) then
		--wait INFLATE_START # of frames before starting to inflate
		if (inflate_counter >= 0 and inflate_counter < INFLATE_START) then
		
			inflate_counter = inflate_counter + 1

		--start inflating
		elseif (inflate_counter == INFLATE_START) then
			this:SetAltCollision("")
			myMotionModel:PushMotion("inflating", false, true)
			this:CreateEffect( "Giant_Marshmallow/inflate" )
			myMotionModel:GetCurrentMotion():GetCurrentAnim():SetCurrentFrameIndex(1)
			inflate_counter = inflate_counter + 1
			is_inflating = true
			
		--inflating
		elseif (inflate_counter > INFLATE_START and inflate_counter < INFLATE_END) then
		
			frame_index = inflate_counter - INFLATE_START + 1
			myMotionModel:GetCurrentMotion():GetCurrentAnim():SetCurrentFrameIndex(frame_index)
			inflate_counter = inflate_counter + 1

		--end inflating, start floating
		elseif (inflate_counter == INFLATE_END) then
		
			myMotionModel:PopMotion()
			myMotionModel:PushMotion("inflated", false, true)
			this:ApplyImpulse(INFLATE_FORCE)
			inflate_counter = inflate_counter + 1
						
		--floating up
		elseif (inflate_counter > INFLATE_END and inflate_counter < DEFLATE_START - 1) then
		
			this:ApplyImpulse(INFLATE_FORCE)
			inflate_counter = inflate_counter + 1

		--as long as giant_marshmallow_balloon is lit, keep floating up (do not increment inflate_counter)
		elseif (inflate_counter == DEFLATE_START - 1) then
		
			this:ApplyImpulse(INFLATE_FORCE)
			
		--end floating, start deflating
		elseif (inflate_counter == DEFLATE_START) then
		
			myMotionModel:PopMotion()
			myMotionModel:PushMotion("deflating", false, false)
			this:ApplyImpulse(DEFLATE_FORCE)
			inflate_counter = inflate_counter + 1
			
		--deflating
		elseif (inflate_counter > DEFLATE_START and inflate_counter < DEFLATE_END) then
		
			this:ApplyImpulse(DEFLATE_FORCE)
			inflate_counter = inflate_counter + 1
		
		--end deflating
		elseif (inflate_counter == DEFLATE_END) then

			--this:SetAltCollision("giant_marshmallow_balloon_alt")
			inflate_counter = 0
			is_inflating = false
			
		end

	-- The balloon is not lit.
	else
	
		--if the giant_marshmallow_balloon doesn't receive enough frames of continuous light to start inflating, reset its counter
		if not is_inflating then

			inflate_counter = 0
			
		--the giant_marshmallow_balloon has already started inflating, but there's no light hitting it
		else

			--deflate the balloon if it stops receiving light while inflating.
			if (inflate_counter > INFLATE_START and inflate_counter < INFLATE_END) then
				inflate_counter = DEFLATE_START
				local current_frame = myMotionModel:GetCurrentMotion():GetCurrentAnim():GetCurrentFrameIndex()

				if (current_frame < 38) then
					frame = 38 - current_frame
				else
					frame = 1
				end

			--end inflating, start floating
			elseif (inflate_counter == INFLATE_END) then

				myMotionModel:PopMotion()
				myMotionModel:PushMotion("inflated", false, true)
				this:ApplyImpulse(INFLATE_FORCE)
				inflate_counter = inflate_counter + 1

			--floating up
			elseif (inflate_counter > INFLATE_END and inflate_counter < DEFLATE_START) then
			
				this:ApplyImpulse(INFLATE_FORCE)
				inflate_counter = inflate_counter + 1
				
				-- Deflates the balloon once it receives no light
				if not isLit then
					inflate_counter = DEFLATE_START
				end

			--end floating, start deflating
			elseif (inflate_counter == DEFLATE_START) then

				myMotionModel:PopMotion()
				myMotionModel:PushMotion("deflating", false, false)
				myMotionModel:GetCurrentMotion():GetCurrentAnim():SetCurrentFrameIndex(frame)

				this:ApplyImpulse(DEFLATE_FORCE)
				inflate_counter = inflate_counter + 1

			--deflating
			elseif (inflate_counter > DEFLATE_START and inflate_counter < DEFLATE_END) then
			
				this:ApplyImpulse(DEFLATE_FORCE)			
				inflate_counter = inflate_counter + 1

			--end deflating
			elseif (inflate_counter == DEFLATE_END) then

				this:SetAltCollision("giant_marshmallow_balloon_alt")
				inflate_counter = 0
				is_inflating = false
			end
			
		end
		
	end
	
	Profile_Stop("giant_marshmallow_balloon")

end

