class 'tweener'
function tweener:__init()

 self.obj=0
 self.startPosX=0
 self.finishPosX=0
 self.startPosY=0
 self.finishPosY=0

 self.direction_rotate=1
 self.speed_rotate=0
 self.distance=0
 self.rotate_position=0
 self.finishRotate=5


--MEMORY
 self.flagRotate=false

 self.flagMove=false
 self.directionX=1
 self.directionY=1
 self.completeMoveGlob=true

 self.oneItiration=true
 self.comleteRotate=true
  --CURRENT



 self.currentX=0
 self.currentY=0
 self.time=0
 self.timeRotate=0
 self.currentRotate=0

 self.currentPosition=nil
 self.speedX=0
 self.speedY=0

end

function tweener:setFlagMove(flag)
 self.flagMove=flag
end

function tweener:getFlagMove()
 return self.flagMove
end

function tweener:setCompleteMoveGlob(flagMove)
self.completeMoveGlob =flagMove
end

function tweener:getCompleteMoveGlob()
 --print('hy')
 return self.completeMoveGlob
end

function tweener:completerot()
 return self.comleteRotate
end


function tweener:getsParmsMove(obj,speed,startPos,finishPos)
 local longPath=0
 self.completeMoveGlob=false

 self.time=0
 self.obj=obj

 self.startPosX=startPos:x()
 self.finishPosX=finishPos:x()

 self.startPosY=startPos:y()
 self.finishPosY=finishPos:y()

 longPath = ((startPos:x()-finishPos:x())*(startPos:x()-finishPos:x())+(startPos:y()-finishPos:y())*(startPos:y()-finishPos:y()))^0.5
 self.speedX=speed*math.abs(startPos:x()-finishPos:x())/longPath
 self.speedY=speed*math.abs(startPos:y()-finishPos:y())/longPath

 self.currentX=self.startPosX
 self.currentY=self.startPosY
 self:getsDirection()
 --print(self.obj:name())
 self.currentPosition=Position(self.startPosX,self.startPosY)
 self.flagMove=true
end

function tweener:setParamsRotate(obj,speed,distanse,direction)

self.direction_rotate= direction
self.speed_rotate=speed
self.obj=obj
self.distance=distanse
--self.rotate_position=Position(self.obj:position():x()-self.obj:dimensions():lx()/2,self.obj:position():y()-self.obj:dimensions():ly()/2)
--self.rotate_position=obj:position()
self.rotate_position=Position((self.obj:dimensions():lx()/2),(self.obj:dimensions():ly()/2))
self.startRotate=self.obj:angle()
self.currentRotate=self.obj:angle()

if self.oneItiration then
self.finishRotate= self.obj:angle()+ self.direction_rotate*self.distance
else
self.finishRotate= self.finishRotate+ self.direction_rotate*self.distance
end

--print(self.obj:angle())
--print()
--print()
self.oneItiration=false
self.flagRotate=true
end

function tweener:getsFinishRotate()
 return self.finishRotate
end

function tweener:getsDirection()
if self.finishPosX-self.startPosX>0  then
  self.directionX=1
end
 if self.finishPosX-self.startPosX<0 then
  self.directionX=-1
 end
 if self.finishPosX-self.startPosX==0 then
  self.directionX=0
 end

 if (self.finishPosY-self.startPosY>0)  then
  self.directionY=1
 end
 if self.finishPosY-self.startPosY<0 then
  self.directionY=-1
 end
 if self.finishPosY-self.startPosY==0 then
 -- print('0')
  self.directionY=0
 end
end

function tweener:update(dt)
 self:moveObject(dt)
 self:rotateObject(dt)
end

function tweener:rotateObject(dt)
 if self.flagRotate then
  self.timeRotate=self.timeRotate+dt
  if self.direction_rotate==1 then
   if self.finishRotate-self.currentRotate>0 then
     self.currentRotate=self.startRotate+self.timeRotate*self.speed_rotate
     self.obj:set_angle(self.currentRotate,self.rotate_position,true)
     self.comleteRotate=false
   else
    self.flagRotate=false
    self.comleteRotate=true
    self.timeRotate=0
   end
  end

  if self.direction_rotate==-1 then
   if self.finishRotate-self.currentRotate<0 then
    self.currentRotate=self.startRotate-self.timeRotate*self.speed_rotate
    self.obj:set_angle(self.currentRotate,self.rotate_position,true)
    self.comleteRotate=false
   else
    self.flagRotate=false
    self.comleteRotate=true
    self.timeRotate=0
   end
  end
 end
end

function tweener:moveObject(dt)

 if self.flagMove then
  self.time=self.time+dt
  if self.directionX==-1 then
   if self.finishPosX-self.currentX <0 then
   -- self.currentX=self.currentX-self.speedX*self.time
   self.currentX= self.startPosX -self.speedX*self.time
    --print(self.currentX)
    --self.completeMoveGlob=false
   else
    self.flagMove=false
    self.completeMoveGlob=true
   end
  end

  if self.directionX==1 then
   if self.finishPosX-self.currentX >0 then
    --self.currentX=self.currentX+self.speedX*self.time
     self.currentX=self.startPosX+self.speedX*self.time
    -- print(self.currentX)
   -- self.completeMoveGlob=false
   else
    self.flagMove=false
    self.completeMoveGlob=true
   end
  end

--  if self.directionX==0 then
--  end

  if self.directionY==-1 then
    if self.finishPosY-self.currentY <0 then
    -- self.currentY=self.currentY-self.speedY*self.time
    self.currentY=self.startPosY-self.speedY*self.time
   -- self.completeMoveGlob=false
   else
    self.flagMove=false
     self.completeMoveGlob=true
   end
  end

  if self.directionY==1 then
   if self.finishPosY-self.currentY >0 then
    -- self.currentY=self.currentY+self.speedY*self.time
    self.currentY=self.startPosY+self.speedY*self.time
     --self.completeMoveGlob=false
   else
    self.flagMove=false
    self.completeMoveGlob=true
   end
  end

--   if self.directionY==0 then
--   end
  --print(self.currentY)
  --self.currentPosition=Position(self.currentX,self.currentY)
  self.obj:set_position(Position(self.currentX,self.currentY),true)
 end
end
