
maxFrames = 150

local LureDPos = Ogre.Vector2(0,0)
local KeyDPos = Ogre.Vector2(0,0)

local LureLastPos
local KeyLastPos

function XToFrame(x)
    return  x
end

function FrameToXY(x)
    return x, s03_0118_pool.seq_rod.self:GetPosition().y
end

function KeyPos()
    local pos = s03_0118_pool.spr_s03_0118_key.self:GetPosition()
    return Ogre.Vector2(pos.x + 0.006, pos.y + 0.0017)
    -- print(Ogre.Vector2)
end

function LurePos()  
    local x, y = FrameToXY(s03_0118_pool.seq_rod.self:GetCurrentFrame())
    
   -- print(x, y)
    local normX = x / 1280
    return s03_0118_pool.seq_rod.self:GetPosition() + Ogre.Vector2(-3.215 * (x / 1280) + 0.408, y -0.145 - normX * 0.13)
end

function LureRightDirection()
   -- print(LureDPos.y)
    return LureDPos.x>0
end

function KeyRightDirection()
   -- print(KeyDPos.y)
    return KeyDPos.y>0
end

function LureKeyRightDistance()
    return LureKeyDistance() < 0.009
end

function LureKeyDistance()
    return (LurePos() - KeyPos()):length()
end

function SetX(x)
    local newFrame = XToFrame(x)
    s03_0118_pool.seq_rod.self:SetCurrentFrame(newFrame)
 --   print(newFrame)
end

function GetX(x)
    return FrameToXY(s03_0118_pool.seq_rod.self:GetCurrentFrame())
end

local time = 0
local lastTime = GetTime()
local a = 0
local v = 0
local m = 1
local x_ = 0

local minL = 3

function OnUpdate()

    --s.s03_0118_pool_up_draging = 1
    s.s03_0118_pool_start_mx, s.s03_0118_pool_start_my = 803, 500
    
    
    local dT = GetTime() - lastTime 
    lastTime = GetTime() 

    time = GetTime() 
    local currKeyPos  = s03_0118_pool.spr_s03_0118_key.self:GetPosition()
    y = 0.6 + math.cos(time*0.5) * 0.09
    x = 0.5 + math.sin(time*0.5) * 0.3
    
    localY = math.sin(time * 5) * 0.02
    s03_0118_pool.spr_s03_0118_key.self:SetPosition(Ogre.Vector2(x, y + localY))
    s03_0118_pool.spr_s03_0118_key_shadow.self:SetPosition(Ogre.Vector2(x-0.001, y - localY * 0.2  + 0.1))
    s03_0118_pool.spr_s03_0118_key_shadow.self:SetScale(Ogre.Vector2(y*3-1.3,y*3-1.3))
    s03_0118_pool.spr_s03_0118_key.self:SetScale(Ogre.Vector2(y*3-1.3,y*3-1.3))
    
    
   -- s03_0118_pool.spr_s03_0118_key.self:DrawImage(0, 0, "spr_s03_0118_key_origin", 1)
    --s03_0118_pool.spr_s03_0118_key.self:DrawImage(0, localY, "spr_s03_0118_brush", 3)
    -- rod

    local currMX, currMY = GetMousePos()
    
    -- friction
    a = (-v * (0.7 / m))
    
    -- local a
    a = a + math.cos(time) * 10
    local currLurePos = LurePos().x  
    local center = s03_0118_pool.seq_rod.self:GetPosition().x +0.2
    local dLurePos = currLurePos - center
   -- print(dLurePos)
    a  = a + dLurePos * 30
    local direction = - 1
    -- 
    
    local dMouseX = currMX -  s.s03_0118_pool_start_mx
    
    
    
    if currMY<500 then
        if s.s03_0118_pool_start_mx > currMX  then
            direction = 1
        end
            a = a + dMouseX * 50 * dT
            
            local xScale = math.abs(dMouseX)*0.002
            if xScale>1 then
                xScale = 1
            end
            
            s03_0118_pool.spr_s03_0118_slider.self:SetScale(Ogre.Vector2(xScale, 1))
            
            if direction>0 then
                 s03_0118_pool.spr_s03_0118_slider.self:SetAngle(Ogre.Degree(0))
            else
                 s03_0118_pool.spr_s03_0118_slider.self:SetAngle(Ogre.Degree(180))
            end
    else
        s03_0118_pool.spr_s03_0118_slider.self:SetScale(Ogre.Vector2(0, 1))
    end
    
  
    v = v + a * dT
    dX = v * dT 
    x_ = x_ + dX
    
    if  x_<0 then
          x_ = 0
          a = 0
          v = 0
    end
    
    if x_ > 130  then
         x_ = 130
         a = 0
         v = 0
    end
   
  --  print("a:", a, " v:", v, " x:", x_)
    
    --print("dt:", dT)
   
  --  if s.s03_0118_pool_up_draging == 1 then
        SetX(x_)
    --end        
 
    if LureLastPos ~= nil then
        local d = LurePos() - LureLastPos
        if d~=0 then
            LureDPos = d
          --  print("ok")
        end
        
        d =  KeyPos() - KeyLastPos
         if d~=0 then
            KeyDPos = d
        end
    else

    end
    
    LureLastPos = LurePos()
    KeyLastPos  = KeyPos()
    
    -- pointer update
    s03_0118_pool.spr_s03_0118_lure_pointer.self:SetPosition(LurePos())
    s03_0118_pool.spr_s03_0118_key_pointer.self:SetPosition(KeyPos())
    if LureKeyDistance()<minL then
        minL = LureKeyDistance()
    end
    
    --
     KeyRightDirection()
    local all = LureRightDirection()  and  LureKeyRightDistance()
    --print(LureRightDirection()," ",KeyRightDirection()," ", LureKeyRightDistance(), " T " , LureRightDirection() and KeyRightDirection() and  LureKeyRightDistance())
    if all and s.s03_0118_pool_solved == 0 and s.s03_0118_pool_pool_droped == 1 then
        PlaySound("snd_03_0118_success")
        Talk("com_s03_0118_02_hero")
        TakeBigPanel("i_carkey")
        Widgets.Inventory.ShowBigPanel("i_carkey")
        s.s03_0118_pool_solved = 1
    end
    
    
 --   if LureRightDirection() and LureKeyRightDistance() then
   --     Talk("LureRightDirection() and LureKeyRightDistance()")
   -- end
    
     --if KeyRightDirection() and LureKeyRightDistance() then
     --   Talk("KeyRightDirection() and LureKeyRightDistance()")
    --end
    
    
    
end

function OnSceneEnter()
    if  s.s03_0122_ho==1 then
		SetMinigameResolver(function() 
            Delete("i_fishingrod")
            PlaySound("snd_03_0118_success")
            Talk("com_s03_0118_02_hero")
            TakeBigPanel("i_carkey")
            s.s03_0118_pool_solved = 1
		end) 
    end
    
    Video:Play("s03_0117_poll_water_anim_0", false)
    
    if s.com_s03_0118_01_hero_com == 0 then
        s.com_s03_0118_01_hero_com = 1
        Sleep(5000)
        Talk("com_s03_0118_01_hero", 1)    
    end
  
    
    if s.i_fishingrod == 1 then
       Talk("com_s03_0118_03_hero", 1) 
    end
    
    
    -- Take("i_fishingrod")
    -- Talk("com_s03_0118_03_hero")
       
end