ObjectData = 
{
	MotionModel = 
	{
		DefaultAnimation = [[normal]],
		Motions = 
		{
			motion3 = 
			{
				Animations = 
				{
					anim1 = 
					{
						Path = [[DATA:Animations\Whale\whale_toolbar.anim]],
						Name = [[whale(toolbar)]],
						MinFreq = 1.000000,
						MaxFreq = 1.000000,
					},
				},
				Name = [[toolbar]],
			},
			motion2 = 
			{
				Animations = 
				{
					anim1 = 
					{
						Path = [[DATA:Animations\Whale\whale_idle.anim]],
						Name = [[whale_(normal)]],
						MinFreq = 1.000000,
						MaxFreq = 1.000000,
					},
				},
				Name = [[normal]],
			},
			motion1 = 
			{
				Animations = 
				{
					anim1 = 
					{
						Path = [[DATA:Animations\Whale\whale_shooting.anim]],
						Name = [[whale_(shooting_and_eating_animation)]],
						MinFreq = 1.000000,
						MaxFreq = 1.000000,
					},
				},
				Name = [[eat]],
			},
			motion4 = 
			{
				Animations = 
				{
					anim1 = 
					{
						Path = [[DATA:Animations\Whale\whale_alt_collision.anim]],
						Name = [[whale_alt]],
						MinFreq = 1.000000,
						MaxFreq = 1.000000,
					},
				},
				Name = [[alt_collision]],
			},
		},
	},
	Extensions = 
	{
		FlippableExtension = 
		{
		},
		EaterExtension = 
		{
			radius = 55.000000,
			frames_to_eat = 30.000000,
			excess_frames = 4.000000,
			length_forward = -30.000000,
			sucks_food = true
		},
		LevelEditorExtension = 
		{
			flip_icon_offset = {-30,-65},
			flippable = true,
		},
		ClickableExtension = 
		{
		},
		HoldingExtension = 
		{
		},
		PositionExtension = 
		{
			foreground = false,
			background = true,
		},
		PhysicsExtension = 
		{
		},
		AltCollisionExtension = 
		{
		},
	},
}

-- Begin Scripting -- 

help_text_header = "$00665"
help_text = "$00666"
alt_collision_on = false

function OnInit()	 	
	this:EnablePhysics( false )	 	
	this:EnableCollisions( false )	 	
	-- offset to get to around the blowhole area 	
	g_displacement = Vector2(27.5, -60)
	
	sound_table = 
	{
		eat = 
		{
			{ "whale_(shooting_and_eating_animation)", 2, "Whale Eating 1" },		
			{ "whale_(shooting_and_eating_animation)", 53, "Whale Eating 2" },		
			{ "whale_(shooting_and_eating_animation)", 95, "Whale Eating 3" },		
			{ "whale_(shooting_and_eating_animation)", 123, "Whale Grinning" }
		}
	}

	isReloaded = true
	
	CART_EJECTION_HASH = World_HashString("cart_ejection")
end

function OnCreatorUpdate()
	-- Creates an alt collision for the whale only when placing it on the screen.
	if not alt_collision then
		this:SetAltCollision("whale_alt")
		alt_collision = true
	end
end

function Update() 	

	Profile_Start("whale")
	
	local my_pos = this:GetPosition()
	local facing = this:GetFacing()

	local motion_model = this:GetMotionModel()
	if( World_IsSimulating() and motion_model:GetCurrentMotionName() == "eat") then

		local current_frame = motion_model:GetCurrentMotion():GetCurrentAnim():GetCurrentFrameIndex()
	
		if ( current_frame == 59 ) then
			World_Alert(this:GetPosition(), 400)
			
			--whale's default is opposite facing default so flip x
			facing.x = -facing.x
			local displacement = Vector2(g_displacement.x*facing.x, g_displacement.y)
	
			local pos = this:GetPosition()
			pos.x = pos.x + displacement.x
			pos.y = pos.y + displacement.y
			
			local impulse_objs = {}
			
			--go through objects to see if they're in the whale
			for object in World_GetObjects() do
			
				if this:IsHolding( object ) then
					
					-- If a flying object is sucked, disable its flying extension.
					local fe = object:GetFlyingExtension()
					if fe then
						fe:SetState(FlyingExtension.FS_Disabled)
						object:EnablePhysics( true )
					end
					
					table.insert( impulse_objs, object) 
					
				end
				
			end
	
			--only splurt if there was something to shoot out
			if table.getn( impulse_objs ) > 0 then
				if this:GetFlipped() then
					this:CreateEffect( "whale/splurt_left" )
				else
					this:CreateEffect( "whale/splurt" )
				end
			end
			
			this:ReleaseAll()
			
			for i in impulse_objs do
	
				local impulse_object = impulse_objs[i]
				
				impulse_object:ForcePosition( pos )
				impulse_object:SetVisibility( true );
				impulse_object:EnableCollisions( true );
				impulse_object:SetUpdate( true );
				
				if( impulse_object:IsWalker() ) then
					
					we = impulse_object:GetWalkingExtension()
					--Is there an easy way to use the names of the states instead of the values?
					we:Reset()
					
				end

				impulse_object:ApplyImpulse(displacement * 250)
				
				--don't set facing if it's a cart_ejection
				if impulse_object:GetBlueprintHash() ~= CART_EJECTION_HASH then
					if( this:GetFlipped() ) then
						impulse_object:SetFlipped( false )
						impulse_object:ForceFacing(this:GetFacing() * -1)
					else
						impulse_object:SetFlipped( true )
						impulse_object:ForceFacing(this:GetFacing())
					end
				end
			end

		elseif ( current_frame >= 60 ) then
			isReloaded = true
		end
	end
	
	if World_IsSimulating() then
		
		local sound = DetermineSound( 	motion_model:GetCurrentMotionName(), 
			motion_model:GetCurrentMotion():GetCurrentAnimName(),
			motion_model:GetCurrentMotion():GetCurrentAnim():GetCurrentFrameIndex() )

		if sound ~= nil then
			Sound_CreateSound( sound, false, 0, this:GetPosition() )
		end
			
	end
	
	Profile_Stop("whale")

end  

function OnClick1() 	
	if ( this:GetMotionModel():GetCurrentMotionName() ~= "eat" ) then
		this:SetMotion("eat")
		this:CreateEffect("whale/suck")
		isReloaded = false
	elseif isReloaded then
		-- Sets the frame to the point before firing.
		this:GetMotionModel():GetCurrentMotion():GetCurrentAnim():SetCurrentFrameIndex(1)
		this:CreateEffect("whale/suck")
		isReloaded = false
	end

	World_Alert(this:GetPosition(), 400)
end 

function DetermineSound( motion_name, anim_name, frame )

	local local_table = sound_table[ motion_name ]
	if local_table == nil then
		return nil
	end
	
	--search in subtree
	for i in local_table do
		if local_table[i][2] == frame and local_table[i][1] == anim_name then
			return local_table[i][3]
		end
	end

	--not found
	return nil
	
end 