ObjectData = 
{
	MotionModel = 
	{
		DefaultAnimation = [[normal]],
		Motions = 
		{
			motion3 = 
			{
				Animations = 
				{
					anim1 = 
					{
						Path = [[DATA:Animations\ginseng factory\ginseng_factory_toolbar.anim]],
						Name = [[ginseng_factory_toolbar]],
						MinFreq = 1.000000,
						MaxFreq = 1.000000,
					},
				},
				Name = [[toolbar]],
			},
			motion2 = 
			{
				Animations = 
				{
					anim4 = 
					{
						Path = [[DATA:Animations\ginseng factory\ginseng_factory_idle.anim]],
						Name = [[ginseng_factory_idle]],
						MinFreq = 1.000000,
						MaxFreq = 1.000000,
					},
					anim5 = 
					{
						Path = [[DATA:Animations\ginseng factory\ginseng_factory_blink.anim]],
						Name = [[ginseng_factory_blink]],
						MinFreq = 10.000000,
						MaxFreq = 15.000000,
					},
					anim1 = 
					{
						Path = [[DATA:Animations\ginseng factory\ginseng_factory_accident.anim]],
						Name = [[ginseng_factory_accident]],
						MinFreq = 100.000000,
						MaxFreq = 120.000000,
					},
					anim3 = 
					{
						Path = [[DATA:Animations\ginseng factory\ginseng_factory_gun_fixing.anim]],
						Name = [[ginseng_factory_gun_fixing]],
						MinFreq = 80.000000,
						MaxFreq = 100.000000,
					},
					anim2 = 
					{
						Path = [[DATA:Animations\ginseng factory\ginseng_factory_look_around.anim]],
						Name = [[ginseng_factory_look_around]],
						MinFreq = 80.000000,
						MaxFreq = 100.000000,
					},
				},
				Name = [[normal]],
			},
			motion1 = 
			{
				Animations = 
				{
					anim1 = 
					{
						Path = [[DATA:Animations\ginseng factory\ginseng_factory_firing.anim]],
						Name = [[ginseng_factory_firing]],
						MinFreq = 1.000000,
						MaxFreq = 1.000000,
					},
				},
				Name = [[fire]],
			},
		},
	},
	Extensions = 
	{
		FlippableExtension = 
		{
		},
		PhysicsExtension = 
		{
		},
		LevelEditorExtension = 
		{
			quick_select_icon_offset = {0, -30},
			flip_icon_offset = {-30,-60},
			flippable = true,		
		},
		ClickableExtension = 
		{
		},
		PositionExtension = 
		{
			foreground = false,
			background = true,
		},
	},
}

-- Begin Scripting --

help_text_header = "$00635"
help_text = "$00636"

function OnInit()	

	this:EnablePhysics( false )

	isReloaded = true

end

function Update()

	Profile_Start("ginseng_factory")

	local motion_model = this:GetMotionModel()

	if( World_IsSimulating() and motion_model:GetCurrentMotionName() == "fire" ) then
		local current_frame = motion_model:GetCurrentMotion():GetCurrentAnim():GetCurrentFrameIndex()
		if current_frame == 6 then
		
			--not sure why -- effects have one frame delay
			local facing = this:GetFacing()
			pos = this:GetPosition()
			pos.x = pos.x - (facing.x * 76)
			pos.y = pos.y + 6
			
			effect = World_CreateEffect( "ginseng_factory/fire_whirl", pos )
			effect:SetFlipped( not this:GetFlipped() )
		
			--World_CreateEffect( "ginseng_factory/fire_sparks", this:GetPosition() )
			
		elseif current_frame == 7 then
			
			Sound_CreateSound("Firing Sound", false, 0, this:GetPosition() )

			World_Alert(this:GetPosition(), 400)
			--World_Scare(this:GetPosition(), 200, 100)

			local facing = this:GetFacing()
			pos = this:GetPosition()
			pos.x = pos.x - (facing.x * 40)
			pos.y = pos.y - (facing.y * 64)
			
			local obj = World_CreateObject( "ginseng", pos )
		
			local ext = obj:GetFlyingExtension()
			if this:GetFacing().x == 1 then
				ext:SetState( FlyingExtension.FS_Left )
			else
				ext:SetState( FlyingExtension.FS_Right )
			end

		elseif current_frame >= 15 then
			isReloaded = true
		end
	end
	
	Profile_Stop("ginseng_factory")
end

function OnClick1()
	
	if( this:GetMotionModel():GetCurrentMotionName() ~= "fire" ) then
		this:SetMotion("fire")
		isReloaded = false
	elseif (isReloaded) then
		-- Sets the frame to the point before firing.
		this:GetMotionModel():GetCurrentMotion():GetCurrentAnim():SetCurrentFrameIndex(5)
		isReloaded = false
	end

end

