ObjectData = 
{
	MotionModel = 
	{
		DefaultAnimation = [[normal]],
		Motions = 
		{
			motion1 = 
			{
				Animations = 
				{
					anim1 = 
					{
						Path = [[DATA:Animations\sneezy sow\sneezy_sow_firing.anim]],
						Name = [[sneezy_sow_firing]],
						MinFreq = 1.000000,
						MaxFreq = 1.000000,
					},
				},
				Name = [[fire]],
			},
			motion3 = 
			{
				Animations = 
				{
					anim1 = 
					{
						Path = [[DATA:Animations\sneezy sow\sneezy_sow_toolbar.anim]],
						Name = [[sneezy_sow_toolbar]],
						MinFreq = 1.000000,
						MaxFreq = 1.000000,
					},
				},
				Name = [[toolbar]],
			},
			motion2 = 
			{
				Animations = 
				{
					anim4 = 
					{
						Path = [[DATA:Animations\sneezy sow\sneezy_sow_idle.anim]],
						Name = [[sneezy_sow_idle]],
						MinFreq = 2.000000,
						MaxFreq = 5.000000,
					},
					anim5 = 
					{
						Path = [[DATA:Animations\sneezy sow\sneezy_sow_scratch_ground.anim]],
						Name = [[sneezy_sow_scratch_ground]],
						MinFreq = 5.000000,
						MaxFreq = 10.000000,
					},
					anim1 = 
					{
						Path = [[DATA:Animations\sneezy sow\sneezy_sow_tail_wag.anim]],
						Name = [[sneezy_sow_tail_wag]],
						MinFreq = 2.000000,
						MaxFreq = 5.000000,
					},
					anim3 = 
					{
						Path = [[DATA:Animations\sneezy sow\sneezy_sow_sniff.anim]],
						Name = [[sneezy_sow_sniff]],
						MinFreq = 2.000000,
						MaxFreq = 5.000000,
					},
					anim2 = 
					{
						Path = [[DATA:Animations\sneezy sow\sneezy_sow_blink.anim]],
						Name = [[sneezy_sow_blink]],
						MinFreq = 2.000000,
						MaxFreq = 5.000000,
					},
				},
				Name = [[normal]],
			},
		},
	},
	Extensions = 
	{
		FlippableExtension = 
		{
		},
		PhysicsExtension = 
		{
		},
		LevelEditorExtension = 
		{
			flip_icon_offset = {-30,-60},
			trash_icon_offset = {-30,38},
			flippable = true,
		},
		PositionExtension = 
		{
			foreground = true,
			background = true,
		},
	},
}


-- Begin Scripting -- 

help_text_header = "$00661"
help_text = "$00662"

function OnInit()	 	

	this:EnablePhysics( false )	
	sound_table = 
	{		
		fire =
		{
			{ "sneezy_sow_firing", 60, "Sneezy Sow Firing" },
			{ "sneezy_sow_firing", 1, "Sneezy Sow Windup" },
			{ "sneezy_sow_firing", 13, "Sneezy Sow Windup 2" }
		},
	}
	
	isReloaded = true

end  

--On a particular frame create a superpig 
--need to discuss creating object, garbage collection with Jamie 
g_sprinkler_sound = nil

function Update() 	

	Profile_Start("sneezy_sow")
	
	local motion_model = this:GetMotionModel()

	for report in this:GetCollisionReports() do

		local object = World_GetObjectByID( report.id_2 )

		--check that the object is moving
		if not object:GetPositionExtension():IsBackground() and not object:GetEmotionPlatformExtension() then
			OnHit()
		end
	
	end	

	if ( motion_model:GetCurrentMotionName() == "fire" ) then
		local frame_num = motion_model:GetCurrentMotion():GetCurrentAnim():GetCurrentFrameIndex()
	
		if frame_num == 60 then 			

			this:CreateEffect( "sneezy sow/fire" )
			facing = this:GetFacing()
			pos = this:GetPosition()
			pos.x = pos.x - (facing.x * 64)
			pos.y = pos.y - (facing.y * 64)

			local obj = World_CreateObject( "superpig", pos )

			local ext = obj:GetFlyingExtension()
			
			if this:GetFlipped() == false then
				ext:SetState( FlyingExtension.FS_Left )
				obj:SetFlipped(true)
			else
				ext:SetState( FlyingExtension.FS_Right )
			end

		elseif frame_num >= 110 then
			isReloaded = true
		end 	
	end
	
	local sound = DetermineSound( 	motion_model:GetCurrentMotionName(), 
		motion_model:GetCurrentMotion():GetCurrentAnimName(),
		motion_model:GetCurrentMotion():GetCurrentAnim():GetCurrentFrameIndex() )

	if sound ~= nil then
		if g_sprinkler_sound ~= nil then
			g_sprinkler_sound:StopPatch()
			g_sprinkler_sound = nil
		end
		g_sprinkler_sound = Sound_CreateSound( sound, false, 0, this:GetPosition() )	
		--I'm not putting a scare or alert message here because there's already one in the superpig creation
	end
	
	Profile_Stop("sneezy_sow")
		
end

-- Tells the pig to shoot.
function OnHit()
	local motion_model = this:GetMotionModel()

	if (motion_model:GetCurrentMotionName() ~= "fire") then
		this:SetMotion("fire")
		isReloaded = false
	elseif (isReloaded) then
		motion_model:GetCurrentMotion():GetCurrentAnim():SetCurrentFrameIndex(1)
		isReloaded = false
	end
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


 --function OnClick1() --	this:SetMotion("fire") --end 