ObjectData = 
{
	MotionModel = 
	{
		DefaultAnimation = [[normal]],
		Motions = 
		{
			motion2 = 
			{
				Animations = 
				{
					anim1 = 
					{
						Path = [[DATA:Animations\Reflector\reflector_toolbar.anim]],
						Name = [[reflector_toolbar]],
						MinFreq = 1.000000,
						MaxFreq = 1.000000,
					},
				},
				Name = [[toolbar]],
			},
			motion1 = 
			{
				Animations = 
				{
					anim1 = 
					{
						Path = [[DATA:Animations\Reflector\reflector_normal.anim]],
						Name = [[normal]],
						MinFreq = 1.000000,
						MaxFreq = 1.000000,
					},
				},
				Name = [[normal]],
			},
		},
	},
	Extensions = 
	{
		PhysicsExtension = 
		{
		},
		LevelEditorExtension = 
		{
			quick_select_icon_offset = {-5, 0},
			rotate_icon_offset = {-60,-45},
			trash_icon_offset = {-41,26},
			rotate_divisions = 4.000000,
		},
		ClickableExtension = 
		{
		},
		PositionExtension = 
		{
			foreground = false,
			background = true,
		},
	},
}-- Begin Scripting --

help_text_header = "$00659"
help_text = "$00660"

function OnInit()	

	ClickState = {}
	
	--up
	ClickState[0] = 
	{
		facing =	Vector2( 1, 0 ),
		map = FlyingExtension.FS_Up
	}
	
	--right
	ClickState[1] = 
	{
		facing =	Vector2( 0, 1 ),
		map = FlyingExtension.FS_Right
	}

	--down
	ClickState[2] = 
	{
		facing =	Vector2( -1, 0 ),
		map = FlyingExtension.FS_Down
	}

	--left
	ClickState[3] = 
	{
		facing =	Vector2( 0, -1 ),
		map = FlyingExtension.FS_Left
	}

	--[[ClickState[0].map[ FlyingExtension.FS_Left ] = FlyingExtension.FS_Up
	ClickState[0].map[ FlyingExtension.FS_Up ] = FlyingExtension.FS_Up
	ClickState[0].map[ FlyingExtension.FS_Right ] = FlyingExtension.FS_Up
	ClickState[0].map[ FlyingExtension.FS_Down ] = FlyingExtension.FS_Up
	ClickState[0].map[ FlyingExtension.FS_Disabled ] = FlyingExtension.FS_Up
	
	ClickState[1].map[ FlyingExtension.FS_Left ] = FlyingExtension.FS_Right
	ClickState[1].map[ FlyingExtension.FS_Up ] = FlyingExtension.FS_Right
	ClickState[1].map[ FlyingExtension.FS_Right ] = FlyingExtension.FS_Right
	ClickState[1].map[ FlyingExtension.FS_Down ] = FlyingExtension.FS_Right
	ClickState[1].map[ FlyingExtension.FS_Disabled ] = FlyingExtension.FS_Right

	ClickState[2].map[ FlyingExtension.FS_Left ] = FlyingExtension.FS_Down
	ClickState[2].map[ FlyingExtension.FS_Up ] = FlyingExtension.FS_Down
	ClickState[2].map[ FlyingExtension.FS_Right ] = FlyingExtension.FS_Down
	ClickState[2].map[ FlyingExtension.FS_Down ] = FlyingExtension.FS_Down
	ClickState[2].map[ FlyingExtension.FS_Disabled ] = FlyingExtension.FS_Down

	ClickState[3].map[ FlyingExtension.FS_Left ] = FlyingExtension.FS_Left
	ClickState[3].map[ FlyingExtension.FS_Up ] = FlyingExtension.FS_Left
	ClickState[3].map[ FlyingExtension.FS_Right ] = FlyingExtension.FS_Left
	ClickState[3].map[ FlyingExtension.FS_Down ] = FlyingExtension.FS_Left
	ClickState[3].map[ FlyingExtension.FS_Disabled ] = FlyingExtension.FS_Left]]

	local facing = this:GetFacing() 
	for i in ClickState do
		click_facing = ClickState[i].facing
		if click_facing.x == facing.x and click_facing.y == facing.y then
			current_state = i
		end
	end
		
	this:EnablePhysics( false )
	
	TOLERANCE_SQR = 32 * 32

end

function Update()

	Profile_Start("reflector")
	
	--detect collisions
	for object in World_GetAddedObjects() do

		local flying_ext = object:GetFlyingExtension()

		if flying_ext then

			local flying_state = flying_ext:GetState()

			--if flying_state ~= FlyingExtension.FS_Disabled then
	
				local diff = object:GetPosition() - this:GetPosition()
				if diff:LengthSqr() < TOLERANCE_SQR then
	
					local new_state = ClickState[current_state].map
	
					if new_state ~= flying_state then
						this:CreateEffect("ginseng/ginseng_explode")
						object:ForcePosition( this:GetPosition() )
						flying_ext:SetState( new_state )
					end
				end
			--end
		end
	end
	
	Profile_Stop("reflector")
	
end

function OnClick1()

	current_state = current_state + 1
	if ClickState[current_state] == nil then
		current_state = 0
	end
	
	this:SetFacing( ClickState[current_state].facing )

end

