Browse Source

improved cards restoring

Vladimir Bykov 11 years ago
parent
commit
53eba58501
1 changed files with 26 additions and 3 deletions
  1. 26 3
      apps/web/priv/static/app/js/okey/deck.js

+ 26 - 3
apps/web/priv/static/app/js/okey/deck.js

@@ -76,6 +76,8 @@ function DeckScope(scope) {
                                 x: i,
                                 x: i,
                                 y: j
                                 y: j
                             }
                             }
+
+                            card.direction = null
                         }
                         }
                     }
                     }
                 }
                 }
@@ -99,6 +101,17 @@ function DeckScope(scope) {
             }
             }
         },
         },
 
 
+        dropDiraction: function(){
+            for(var i = 0; i < 15; i++){
+                for(var j = 0; j < 2; j++){
+                    var card = this.cards[j][i]
+                    if(card){
+                        card.direction = null
+                    }
+                }
+            }
+        },
+
         render: function() {
         render: function() {
             this.each(function(card, i, j) {
             this.each(function(card, i, j) {
                 if (null != card) {
                 if (null != card) {
@@ -115,7 +128,7 @@ function DeckScope(scope) {
                 for(var j = 0; j < 2; j++){
                 for(var j = 0; j < 2; j++){
                     var card = this.cards[j][i]
                     var card = this.cards[j][i]
                     if (card && card.$el[0] != e.target && (selectedPos.x == i && selectedPos.y == j) && !card.$el.attr("animated") && scope.Card.selected.length < 2) {
                     if (card && card.$el[0] != e.target && (selectedPos.x == i && selectedPos.y == j) && !card.$el.attr("animated") && scope.Card.selected.length < 2) {
-                        var shift = e.detail.x > card.centerX() ? i - 1 : i + 1
+                        var shift = e.detail.x > card.centerX() ? i - 1 : i + 1                        
                         if(shift > 14 || shift < 0){
                         if(shift > 14 || shift < 0){
                             shift = shift > 14 ? shift - 2 : shift
                             shift = shift > 14 ? shift - 2 : shift
                             shift = 0 > shift ? shift + 2 : shift
                             shift = 0 > shift ? shift + 2 : shift
@@ -126,6 +139,9 @@ function DeckScope(scope) {
                         else if(shift > i && this.rightDeadEnd(j, i)){
                         else if(shift > i && this.rightDeadEnd(j, i)){
                             shift = i - 1
                             shift = i - 1
                         }
                         }
+                        else if(card.direction == this.direction(i, shift)){
+                            shift = card.direction == 'left' ? i - 1 : i + 1
+                        }
                         this.move({ i: i, j: j}, {i: shift, j: j})
                         this.move({ i: i, j: j}, {i: shift, j: j})
                         this.needRestore = true
                         this.needRestore = true
                         moved = true
                         moved = true
@@ -171,12 +187,14 @@ function DeckScope(scope) {
 
 
                                 var self = this
                                 var self = this
 
 
-                                ;(function (card, fst_j, j, dir_j){                                 
+                                ;(function (card, fst_j, j, dir_j, deck){                                 
                                     card.$el.transform({
                                     card.$el.transform({
                                         from: [trfs[fst_j][j].x, trfs[fst_j][j].y].join(' '),
                                         from: [trfs[fst_j][j].x, trfs[fst_j][j].y].join(' '),
                                         to: [trfs[fst_j][dir_j].x, trfs[fst_j][dir_j].y].join(' ')
                                         to: [trfs[fst_j][dir_j].x, trfs[fst_j][dir_j].y].join(' ')
                                     })
                                     })
 
 
+                                    card.direction = deck.direction(j, dir_j)
+
                                     // card.restoreCardHandler = function(){
                                     // card.restoreCardHandler = function(){
                                     //     var selectedPos = self.getSelectedPos()
                                     //     var selectedPos = self.getSelectedPos()
                                     //     if(!self.cards[fst_j][j] && (selectedPos.x != j || selectedPos.y != fst_j)){
                                     //     if(!self.cards[fst_j][j] && (selectedPos.x != j || selectedPos.y != fst_j)){
@@ -195,7 +213,7 @@ function DeckScope(scope) {
                                     //     }
                                     //     }
                                     // }
                                     // }
                                     // self.restoredCards.push(card)
                                     // self.restoredCards.push(card)
-                                }(card, fst.j, j, direction(j)))
+                                }(card, fst.j, j, direction(j), this))
 
 
                                 // if(selected){
                                 // if(selected){
                                 //     selected.dragHandler.initTrf = [trfs[fst.j][j].x, trfs[fst.j][j].y]
                                 //     selected.dragHandler.initTrf = [trfs[fst.j][j].x, trfs[fst.j][j].y]
@@ -261,6 +279,7 @@ function DeckScope(scope) {
                                 }
                                 }
                                 ;(this.cards[posY][posX] = card).pos = {x:posX,y:posY}
                                 ;(this.cards[posY][posX] = card).pos = {x:posX,y:posY}
                                 this.normalizeCards()
                                 this.normalizeCards()
+                                this.dropDiraction()
                                 this.restoredCards = null
                                 this.restoredCards = null
                                 selected = null
                                 selected = null
                             }
                             }
@@ -385,6 +404,10 @@ function DeckScope(scope) {
             return true
             return true
         },
         },
 
 
+        direction: function(from, to){
+            return from < to ? 'left' : 'right'
+        },
+
         dir: function(){
         dir: function(){
             console.group('Cards')
             console.group('Cards')
             console.group()
             console.group()