Nikatlas 6 лет назад
Родитель
Сommit
0eebc1bddd
3 измененных файлов с 61 добавлено и 5 удалено
  1. 8 0
      src/Game/views/base/Card.js
  2. 33 5
      src/Game/views/base/CardHolder.js
  3. 20 0
      src/helpers/dragAndDrop.js

+ 8 - 0
src/Game/views/base/Card.js

@@ -119,6 +119,14 @@ class Card extends GuiableContainer{
         this.sprite.on('pointerdown', (e) => fn(e));
     }
 
+    getHolder() {
+        return this._holder;
+    }
+
+    attach(holder = null) { 
+        this._holder = holder;
+    }
+
     // Animate to Position
     moveTo(point, milliseconds=1000) {
         let path = new PIXI.tween.TweenPath();

+ 33 - 5
src/Game/views/base/CardHolder.js

@@ -9,7 +9,7 @@ import EventManager from '../../services/EventManager';
 
 import config from '../../config.js';
 
-class CardHolder extends GuiableContainer{
+class CardHolder extends GuiableContainer {
     constructor(props) {
         super(props);
         let {
@@ -45,12 +45,11 @@ class CardHolder extends GuiableContainer{
         this.sprite.interactive = true;
         this.sprite.hitArea = new PIXI.Rectangle(0, 0, this.w, this.h);
         this.sprite.cursor = 'pointer';
-        let fn = () => {
+        this._placeFn = () => {
             // This is called before it is removed from the DragEnd Callback
-            EventManager.trigger('CardPlaced', [this, this._onDrop]); 
+            EventManager.trigger('CardPlaced', [this, this._onDrop]);
         };
-        this.sprite.on('mouseup', fn);
-        this.sprite.on('touchend', fn);
+        this.setEvents();
 
         this.position.set(this.x,this.y);
 
@@ -60,11 +59,40 @@ class CardHolder extends GuiableContainer{
         EventManager.on('CardDraggingFinished', () => this.removeChild(this.sprite));
     }
 
+    getCard() {
+        return this._card;
+    }
+
+    setEvents() {
+        this.sprite.on('mouseup', this._placeFn);
+        this.sprite.on('touchend',this._placeFn);
+    }
+
+    unsetEvents() {
+        this.sprite.off('mouseup', this._placeFn);
+        this.sprite.off('touchend',this._placeFn);
+    }
+
+    lock(card = null) {
+        this.unsetEvents();
+        this._locked = true;
+        this._card = card;
+        if(card) {
+            card.attach(this);
+        }
+    }
+
+    unlock() {
+        this.setEvents();
+        this._locked = false;    
+    }
+
     scaleTo(s) {
         this.scale.set(s);
         this.s = s;
         return this;
     }
+
     change(props) {
         let newdata = {
             x: this.x,

+ 20 - 0
src/helpers/dragAndDrop.js

@@ -17,12 +17,28 @@ function dragAndDrop(sprite) {
         .on('touchmove', onDragMove);
 
     sprite.placeFn = place.bind(sprite);
+
+    sprite.stopDND = () => {
+        sprite
+        // events for drag start
+        .off('mousedown', onDragStart)
+        .off('touchstart', onDragStart)
+        // events for drag end
+        .off('mouseup', onDragEnd)
+        .off('mouseupoutside', onDragEnd)
+        .off('touchend', onDragEnd)
+        .off('touchendoutside', onDragEnd)
+        //events for drag move
+        .off('mousemove', onDragMove)
+        .off('touchmove', onDragMove);
+    }
 }
 
 function place(holder, dropCallback) {
     if(this.dragging) {
         this.placedPosition = this.parent.toLocal(holder.getGlobalPosition());
         this.placedScale = new PIXI.Point(holder.scale.x, holder.scale.y);
+        holder.lock(this);
         if(dropCallback) {
             dropCallback(this);
         }
@@ -37,6 +53,10 @@ function onDragStart(event)
     this.data = event.data;
     this.dragging = true;
     
+    let holder = this.getHolder();
+    if(holder)
+        holder.unlock();
+
     this.draggingOffset = this.data.getLocalPosition(this);
 
     this.draggingInitial = new PIXI.Point(this.position.x, this.position.y);