﻿function SmoothMovement(position, target, velocity){
  position = Math.round(position);
  target   = Math.round(target);
  velocity = (velocity ? Math.round(velocity) : 0);
  this.updatePosition = function(){
    position += velocity;
    if (velocity < 0){
      if (position - velocity * (velocity  - 1) / 2 < target){
        velocity++;
      }else if (position - (velocity - 1) * (velocity - 2) / 2 >= target){
        velocity--;
      }
    }else{
      if (position + velocity * (velocity + 1) / 2 > target){
        velocity--;
      }else if (position + (velocity + 1) * (velocity + 2) / 2 <= target){
        velocity++;
      }
    }
    return position;
  }
  this.changeTarget = function(newTarget){
    target = Math.round(newTarget);
  }  
  this.getPosition = function(){
    return position;
  }
  this.getVelocity = function(){
    return velocity;
  }
  this.hasStopped = function(){
    return (position == target && velocity == 0);
  }
}