'use strict';

class TweenService {

    constructor() {
      this.game = null;
    }

    init(game) {
      this.game = game;
    }

    move(unit, path, cb) {
      var tween = this.game.add.tween(unit);

      var getOrientation = function(start, end) {
        start = start || unit.tile.indexes;
        if (start.y > end.y) {
          return 'up';
        }
        if (start.y < end.y) {
         return 'down';
        }
        if (start.x > end.x) {
         return 'left';
        }
        if (start.x < end.x) {
         return 'right';
        }
      };

      unit.animations.play('walk_' + getOrientation(unit.tile.indexes, path[0]));

      var onMoveComplete = function() {
        return function(i) {
          if (path[i + 1]) {
            unit.animations.play('walk_' + getOrientation(path[i], path[i+1]));
            return;
          }
          unit.animations.play('idle_' + getOrientation(path[i - 1], path[i]));
        };
      };

      for (var i = 0; i < path.length; i++) {
          tween.to({
            x: path[i].x * 32,
            y: path[i].y * 32
          }, 300, Phaser.Easing.Linear.None);
          tween.onComplete.add(onMoveComplete(i).bind(this, i));
      }
      tween._lastChild.onComplete.add(cb);
      tween.start();
    }

}

export default new TweenService();

@@ -159,53 +159,8 @@
 b) %7B
-%0A      var tween = this.game.add.tween(unit);
 %0A%0A  
@@ -531,38 +531,25 @@
     
-unit.animations.play('walk_' +
+var orientation =
  get
@@ -587,16 +587,66 @@
 path%5B0%5D)
+;%0A      unit.animations.play('walk_' + orientation
 );%0A%0A    
@@ -755,38 +755,29 @@
     
-unit.animations.play('walk_' +
+var nextOrientation =
  get
@@ -807,17 +807,16 @@
 th%5Bi+1%5D)
-)
 ;%0A      
@@ -825,28 +825,161 @@
     
-return;%0A          %7D%0A
+if (orientation !== nextOrientation) %7B%0A              unit.animations.stop('walk_' + orientation, true);%0A              orientation = nextOrientation;%0A    
     
@@ -1010,21 +1010,18 @@
 ay('
-idle
+walk
 _' + 
-getO
+o
 rien
@@ -1030,51 +1030,79 @@
 tion
-(path%5Bi - 1%5D, path%5Bi%5D));%0A        %7D;%0A      %7D
+);%0A            %7D%0A          %7D%0A        %7D;%0A      %7D;%0A%0A      var tweens = %5B%5D
 ;%0A%0A 
@@ -1142,24 +1142,75 @@
 gth; i++) %7B%0A
+          var tween = this.game.add.tween(unit);%0A  
           tw
@@ -1218,16 +1218,18 @@
 en.to(%7B%0A
+  
         
@@ -1263,16 +1263,18 @@
         
+  
 y: path%5B
@@ -1283,16 +1283,18 @@
 .y * 32%0A
+  
         
@@ -1321,22 +1321,25 @@
 ing.
-Linear.None);%0A
+Quadratic.Out)%0A  
     
@@ -1344,21 +1344,16 @@
         
-tween
 .onCompl
@@ -1375,17 +1375,16 @@
 omplete(
-i
 ).bind(t
@@ -1397,38 +1397,281 @@
 ));%0A
-      %7D%0A      tween._lastChild
+%0A           tweens%5Bi%5D = tween;%0A%0A           if (i %3E 0) %7B%0A             tweens%5Bi - 1%5D.chain(tweens%5Bi%5D);%0A           %7D%0A      %7D%0A%0A      tweens%5Bpath.length - 1%5D.onComplete.add(function() %7B%0A        unit.animations.play('idle_' + orientation);%0A      %7D);%0A      tweens%5Bpath.length - 1%5D
 .onC
@@ -1698,16 +1698,20 @@
    tween
+s%5B0%5D
 .start()

Fix orientation bug and fluidify