
CStoryMiniGame = {}

function CStoryMiniGame:GetSplinePathAndShow( i_iChoiceNum )
	local aRet = self:GetSplinePath( i_iChoiceNum )

	local oCanvasMovie = root.StoryMiniGame
	oCanvasMovie:CanvasClear()

	oCanvasMovie:CanvasDrawLine( 0, 300, 800, 300, 0xffffff00 )
	oCanvasMovie:CanvasDrawLine( 450, 0, 450, 600, 0xffffff00 )

	for i = 1, #aRet, 1 do
		oCanvasMovie:CanvasDrawCircle( aRet[ i ][ 1 ], aRet[ i ][ 2 ], 20, 0xffffff00, 32 )
		oCanvasMovie:CanvasDrawTextLine( aRet[ i ][ 1 ], aRet[ i ][ 2 ], 20, 0xffffff00, tostring( i - 1 ) )
		if ( i > 1 ) then
			if ( i == 2 or i == #aRet ) then
				oCanvasMovie:CanvasDrawLine( aRet[ i - 1 ][ 1 ], aRet[ i - 1 ][ 2 ], aRet[ i ][ 1 ], aRet[ i ][ 2 ], 0xffff0000 )
			else
				oCanvasMovie:CanvasDrawLine( aRet[ i - 1 ][ 1 ] - 1, aRet[ i - 1 ][ 2 ] - 1, aRet[ i ][ 1 ] - 1, aRet[ i ][ 2 ] - 1, 0xffff0000 )
				oCanvasMovie:CanvasDrawLine( aRet[ i - 1 ][ 1 ], aRet[ i - 1 ][ 2 ], aRet[ i ][ 1 ], aRet[ i ][ 2 ], 0xffff0000 )
				oCanvasMovie:CanvasDrawLine( aRet[ i - 1 ][ 1 ] + 1, aRet[ i - 1 ][ 2 ] + 1, aRet[ i ][ 1 ] + 1, aRet[ i ][ 2 ] + 1, 0xffff0000 )
			end
		end
	end

	return aRet
end

function CStoryMiniGame:GetSplinePath( i_iChoiceNum )
	
	local fHalfWidth = 450.0;
	local fHalfHeight = 300.0;
	
	local aRet = {}
	
	-- first and last points are spline control points that are not traversed
	-- (see http://www.mvps.org/directx/articles/catmull/ )
	
	local oLocalMovie = root.StoryMiniGame
	
	if ( oLocalMovie.splinePath ~= nil ) then
		oLocalMovie.splinePath:DeleteSelf()
	end
	
	local oPathMovie = oLocalMovie:CreateInstance( "StoryBirdPath" .. tostring( i_iChoiceNum ), "splinePath" )
	
	local bStop = false
	local iIndex = 0
	
	while ( bStop == false ) do
		local oMarkerMovie = oPathMovie[ "_path" .. tostring( iIndex ) ]
		if ( oMarkerMovie == nil ) then
			bStop = true
		else
			table.insert( aRet,	{	oMarkerMovie:GetX(),	oMarkerMovie:GetY(),	1.0 } )
			iIndex = iIndex + 1
		end
	end
	
	local fTimingBase = 0.001
	
	if ( i_iChoiceNum == 0 ) then		fTimingBase = 0.0012
	elseif ( i_iChoiceNum == 1 ) then	fTimingBase = 0.0011
	elseif ( i_iChoiceNum == 2 ) then	fTimingBase = 0.0013
	end
	
	log( "Parsed " .. iIndex .. " path entries" )
	
	aRet[ 1 ][ 3 ] = 0.0
	aRet[ 2 ][ 3 ] = 0.0
	aRet[ #aRet ][ 3 ] = 0.0
	
	for i = 3, #aRet - 1, 1 do
		local fDeltaX = aRet[ i ][ 1 ] - aRet[ i - 1 ][ 1 ]
		local fDeltaY = aRet[ i ][ 2 ] - aRet[ i - 1 ][ 2 ]
		
		local fDist = math.sqrt( fDeltaX * fDeltaX + fDeltaY * fDeltaY )
		aRet[ i ][ 3 ] = aRet[ i ][ 3 ] * fDist * fTimingBase
	end
	
	return aRet
end
