var EHDI = EHDI || Object.create(null); EHDI.GAME = EHDI.GAME || Object.create(null); EHDI.GAME.components = EHDI.GAME.components || Object.create(null); EHDI.GAME.components.CardInteractions = (function() { var instance; function create() { var public = {}, container, cardContainer, clickedCards = [], holdTimer, prevPos, putBackFn, viewingCard = false; var cardSelectContainer, draftingContainer, zoomed = {val: false, card: null, index: -1}; var selectedIndex; var _onBoardTl; var _onDraftTl; public.init = function(stage, cardPlacer) { EHDI.GAME.sceneManager.getRenderer().plugins.interaction.moveWhenInside = true; container = stage; cardContainer = cardPlacer; cardSelectContainer = new EHDI.aka.Container(); container.addChild(cardSelectContainer); cardSelectContainer.visible = false; var leftBtn = new EHDI.displays.Button(EHDI.Assets.images["parabuilder_btnarrow_left"], null, null, null, 0.95); leftBtn.position.set(50, 255); leftBtn.setOnClickFunction(_cardIterator.bind(null, -1)); var rightBtn = new EHDI.displays.Button(EHDI.Assets.images["parabuilder_btnarrow_right"], null, null, null, 0.95); rightBtn.position.set(460, 255); rightBtn.setOnClickFunction(_cardIterator.bind(null, 1)); var placeBtn = new EHDI.displays.ButtonContainer("PLACE", {fontFamily: "Exo-Bold", fontSize: "24px", fill: 0xFFFFFF}, EHDI.Assets.images["parabuilder_btnplace"], EHDI.Assets.images["parabuilder_btnclicked"], EHDI.Assets.images["parabuilder_btnhover"]); placeBtn.position.set(260, 540); placeBtn.setOnClickFunction(_playCard); cardSelectContainer.addChild(leftBtn); cardSelectContainer.addChild(rightBtn); cardSelectContainer.addChild(placeBtn); draftingContainer = new EHDI.aka.Container(); container.addChild(draftingContainer); draftingContainer.visible = false; var doneBtn = new EHDI.displays.ButtonContainer("DONE", {fontFamily: "Exo-Bold", fontSize: "24px", fill: 0xFFFFFF}, EHDI.Assets.images["parabuilder_btnplace"], EHDI.Assets.images["parabuilder_btnclicked"], EHDI.Assets.images["parabuilder_btnhover"], EHDI.Assets.images["parabuilder_btngrayed"]); doneBtn.position.set(512, 540); doneBtn.setOnClickFunction(_nextRound); doneBtn.disable(true); draftingContainer.doneBtn = doneBtn; draftingContainer.addChild(doneBtn); holdTimer = new EHDI.GAME.Timer(null, 0); _onBoardTl = new TimelineLite(); _onDraftTl = new TimelineLite(); } public.setOnBoardListeners = function(card, isLastCard) { card.removeAllListeners(); childIndex = -1; // EHDI.GAME.soundManager.playSFX("card_place"); // if(isLastCard) { // card.on("pointertap", _onClickBoard.bind(card)); // } else { card.on("pointertap", _zoomCardOnBoard); // } } function _zoomCardOnBoard() { _onBoardTl.progress(1, false).kill(); if(!zoomed.card || this == zoomed.card) { if(zoomed.val) { zoomed.card.toggleZoom(_onBoardTl, false); _onBoardTl.call(function() { console.log('call') cardContainer.setChildIndex(zoomed.card, zoomed.index); zoomed.val = false; zoomed.card = null; zoomed.index = -1; }); } else { zoomed.index = cardContainer.getChildIndex(this); this.toggleZoom(_onBoardTl, true); EHDI.GAME.utils.bringToFront(this); zoomed.val = true; zoomed.card = this; } } } function _onClickBoard() { var card; var index = clickedCards.indexOf(this); _onDraftTl.progress(1, false).kill(); if(this.isDrafted) { console.log("cant select drafted card") } else if(index > -1) { card = clickedCards.splice(index, 1)[0]; card.toggleZoom(_onDraftTl, false); _onDraftTl.call(function() { cardContainer.setChildIndex(card, card.childIndex); }) } else { clickedCards.push(this); if(clickedCards.length > container.round.cardsToLeave) { card = clickedCards.splice(0, 1)[0]; card.toggleZoom(_onDraftTl, false, {duration: 0.1}); _onDraftTl.call(function() { cardContainer.setChildIndex(card, card.childIndex); }); } this.childIndex = cardContainer.getChildIndex(this); this.toggleZoom(_onDraftTl, true, {x: 0.5, y: 0.5}); EHDI.GAME.utils.bringToFront(this); } if(clickedCards.length === container.round.cardsToLeave) { draftingContainer.doneBtn.disable(false); draftingContainer.doneBtn.texture = draftingContainer.doneBtn.pointerUp; } else draftingContainer.doneBtn.disable(true); } public.setOnHandListeners = function(card) { card.removeAllListeners(); card.on("pointertap", _viewCard); selectedIndex = -1; // card.on("pointerdown", _onDragStart); // card.on("pointerup", _onDragEnd); // card.on("pointermove", _onDragMove); // card.on("pointerupoutside", _onDragEnd); // card.on("mouseout", _releaseHold.bind(card)); } public.draftingMode = function() { var count = container.round.cardsToLeave; var cards = container.player.boardCards.concat(container.player.draftedCards); //remove clicks on opponent board var oppcard = container.opponent.boardCards; for(var i = 0; i < oppcard.length; i ++) oppcard[i].removeAllListeners(); for(var i = 0; i < cards.length; i++) { cards[i].removeAllListeners(); cards[i].on("pointertap", _onClickBoard.bind(cards[i])); } EHDI.GAME.utils.bringToFront(draftingContainer); container.showFooter(true, "SELECT "+count+" CARD"+ ((count > 1)? "S":"") + " FROM YOUR BOARD"); draftingContainer.visible = true; } public.removeZoomedCard = function(exceptCards) { if(zoomed.card) { if(exceptCards) { if(exceptCards.indexOf(zoomed.card) < 0) _zoomCardOnBoard.apply(zoomed.card); } else _zoomCardOnBoard.apply(zoomed.card); } // console.log(zoomed.card) } function _nextRound() { var player = container.player; var opponent = container.opponent; var cardsToLeave = []; public.removeZoomedCard(); for(var i = 0; i < clickedCards.length; i++) { clickedCards[i].toggleZoom(false); clickedCards[i].changeFrame(); } draftingContainer.visible = false; draftingContainer.doneBtn.disable(true); _finishedChoosing(); player.removeBoardCards(); opponent.chooseFromBoard(container.round.cardsToLeave); cardsToLeave = cardsToLeave.concat(opponent.draftedCards); cardsToLeave = cardsToLeave.concat(player.draftedCards); EHDI.GAME.CardManager.nextRound(cardsToLeave); container.round.val++; container.round.cardsToLeave--; } function _finishedChoosing() { var player = container.player; for(var i = 0; i < clickedCards.length; i++) { player.draftedCards.push(clickedCards[i]); } clickedCards = []; } // function _onDragStart(event) { // if(this.viewMode) { // return; // } // holdTimer = new EHDI.GAME.Timer(_viewCard.bind(this), 500); // this.data = event.data; // this.filters = [new EHDI.GAME.filters.GlowFilter(15, 2, 1, 0x239DF1, 0.1)]; // this.dragging = true; // prevPos = {x: this.position.x, y: this.position.y}; // childIndex = cardContainer.getChildIndex(this); // EHDI.GAME.utils.bringToFront(this); // } // function _onDragEnd() { // if(!this.dragging) // return; // cardContainer.setChildIndex(this, childIndex); // if(holdTimer) // holdTimer.pause(); // this.filters = null; // this.dragging = false; // this.data = null; // if(!this.viewMode) { // if(this.checkCollision(container.boardBox)) // _playCard(this); // else // TweenLite.to(this.position, 0.1, {x: prevPos.x, y: prevPos.y}); // } // } // function _onDragMove() { // if (this.dragging && !this.viewMode) { // if(holdTimer) // holdTimer.pause(); // var newPosition = this.data.getLocalPosition(this.parent); // this.x = newPosition.x; // this.y = newPosition.y; // } // } function _viewCard() { // if(zoomed.card) { // _zoomCardOnBoard.apply(zoomed.card); // } public.removeZoomedCard(container.player.boardCards); if(selectedIndex == this.handIndex) { this.filters = null; this.setViewMode(false); viewingCard = false; cardSelectContainer.removeChildAt(cardSelectContainer.children.length-1); cardSelectContainer.visible = false; container.setCardSelectionMode(false); // TweenLite.to(this.position, 0.1, {y: 500}); TweenMax.to(this.position, 0.1, {y: 500, onStart: EHDI.GAME.soundManager.playSFX, onStartParams: ["card_select"]}); selectedIndex = -1; } else { if(selectedIndex != -1) { var oldCard = container.player.getHandCardWithIndex(selectedIndex); // oldCard.filters = null; oldCard.toggleBrightness(false); cardSelectContainer.removeChildAt(cardSelectContainer.children.length-1); // TweenLite.to(oldCard.position, 0.1, {y: 500}); TweenMax.to(oldCard.position, 0.1, {y: 500, onStart: EHDI.GAME.soundManager.playSFX, onStartParams: ["card_select"]}); } viewingCard = true; prevPos = {}; prevPos.x = this.position.x; prevPos.y = this.position.y; EHDI.GAME.utils.bringToFront(cardSelectContainer); cardSelectContainer.visible = true; container.setCardSelectionMode(true); putBackFn = _putBackCard.bind(this); container.overlay.on("click", putBackFn); // var scaledCard = new EHDI.aka.Sprite(this.scaledTexture); var scaledCard = this.scaledCard; // scaledCard.anchor.set(0.5, 0.5); scaledCard.position.set(EHDI.GAME.sceneManager.getStageWidth()*0.25, 250); if(this.doubledSymbol) { scaledCard.scaledDouble.texture = this.doubledSymbol.texture; //symbolText.position.set(scaledCard.width * 0.425, 50); //symbolText.rotation = 0.2; //scaledCard.addChild(symbolText); // TweenLite.to(symbolText.scale, 0.4, {delay: 0.2, x: "+=0.2", y: "+=0.2"}); // TweenLite.to(symbolText.scale, 0.4, {delay: 0.6, x: "-=0.2", y: "-=0.2"}); // TweenMax.to(symbolText.scale, 0.4, {delay: 0.2, x: "+=0.2", y: "+=0.2"}); // TweenMax.to(symbolText.scale, 0.4, {delay: 0.6, x: "-=0.2", y: "-=0.2"}); } selectedIndex = this.handIndex; cardSelectContainer.addChild(scaledCard); // this.filters = [EHDI.GAME.GLOW_FILTER]; // this.setBrightness(1.4); this.toggleBrightness(true); // TweenLite.to(this.position, 0.2, {y: 470}); TweenMax.to(this.position, 0.2, {y: 470, onStart: EHDI.GAME.soundManager.playSFX, onStartParams: ["card_select"]}); } } function _putBackCard() { viewingCard = false; this.viewMode = false; container.overlay.off("click", putBackFn); container.overlay.visible = false; container.overlay.interactive = false; cardContainer.addChildAt(this, selectedIndex); container.removeChild(this); // TweenLite.to(this.scale, 0.1, {x: 1, y: 1}); // TweenLite.to(this.position, 0.1, {x: prevPos.x, y: prevPos.y}); TweenMax.to(this.scale, 0.1, {x: 1, y: 1}); TweenMax.to(this.position, 0.1, {x: prevPos.x, y: prevPos.y}); } function _playCard() { var player = container.player; var opponent = container.opponent; var card = player.getHandCardWithIndex(selectedIndex); var handCards = player.handCards.concat(opponent.handCards); public.removeZoomedCard(); EHDI.GAME.CardManager.setInteractivity(handCards, false); card.filters = null; card.setViewMode(false); viewingCard = false; cardSelectContainer.removeChildAt(cardSelectContainer.children.length-1); cardSelectContainer.visible = false; container.setCardSelectionMode(false); card.removeAllListeners(); opponent.skipThinkTime(); player.nextMove(card, _reveal); // opponent.showRecentMove(); // _reveal(card); // EHDI.GAME.CardManager.switchCards(); } function _reveal(playerCard, isLastCard) { var opponent = container.opponent; var oppCard = opponent.recentMove; var fn = (isLastCard)? EHDI.GAME.CardManager.beginCalculation : EHDI.GAME.CardManager.switchCards; var timeline = new TimelineLite({delay: 0.5, onComplete: fn, paused:true}); timeline.to(playerCard.scale, 0.4, {x:"+=0.1", y:"+=0.1", onComplete:playerCard.setFacedUp.bind(playerCard)}, "revealSet"); timeline.to(oppCard.scale, 0.4, {x:"+=0.1", y:"+=0.1", onComplete:opponent.showRecentMove.bind(opponent)}, "revealSet"); timeline.call(function() { playerCard.interactive = true; oppCard.interactive = true; playerCard.toggleScaled(true); oppCard.toggleScaled(true); public.removeZoomedCard(); }); EHDI.GAME.TimelineManager.add(timeline); } function _cardIterator(direction) { // cardContainer.addChildAt(oldCard, selectedIndex); // console.log(prevPos) // TweenLite.to(oldCard.scale, 0.1, {x: 1, y: 1, onComplete: oldCard.setViewMode.bind(oldCard, false)}); var index = selectedIndex + direction; if(index >= container.player.handCards.length) index = 0; else if(index < 0) index = container.player.handCards.length-1; var nextCard = container.player.getHandCardWithIndex(index); viewingCard = false; _viewCard.call(nextCard); } return public; } return { getInstance: function() { if(!instance) instance = create(); return instance; } } })();