diff --git a/android/src/checamon/games/virtuacards/android/AndroidLauncher.java b/android/src/checamon/games/virtuacards/android/AndroidLauncher.java index c65fd75..8e73000 100644 --- a/android/src/checamon/games/virtuacards/android/AndroidLauncher.java +++ b/android/src/checamon/games/virtuacards/android/AndroidLauncher.java @@ -3,7 +3,6 @@ package checamon.games.virtuacards.android; import android.annotation.TargetApi; import android.os.Build; import android.os.Bundle; -import android.util.Log; import android.view.View; import com.badlogic.gdx.backends.android.AndroidApplication; @@ -22,7 +21,6 @@ public class AndroidLauncher extends AndroidApplication { } AndroidApplicationConfiguration config = new AndroidApplicationConfiguration(); initialize(new VirtuaCards(),config); - Log.e("Error", "Error Test"); } @TargetApi(19) private void hideVirtualButtons() { diff --git a/core/src/checamon/games/virtuacards/Card.java b/core/src/checamon/games/virtuacards/Card.java index 85aaa2f..5b89d2a 100644 --- a/core/src/checamon/games/virtuacards/Card.java +++ b/core/src/checamon/games/virtuacards/Card.java @@ -20,6 +20,7 @@ public class Card { private int id; private boolean faceUp; private boolean decked; + private int deckId; //private boolean onTable; //private float orientation; @@ -59,6 +60,8 @@ public class Card { this.backSprite.setPosition(position.getX(), position.getY()); this.id = id; this.faceUp = false; + this.decked = true; + this.deckId = 1; } @@ -155,4 +158,12 @@ public class Card { public void setDecked(boolean decked) { this.decked = decked; } + + public int getDeckId() { + return deckId; + } + + public void setDeckId(int deckId) { + this.deckId = deckId; + } } diff --git a/core/src/checamon/games/virtuacards/Deck.java b/core/src/checamon/games/virtuacards/Deck.java index d3e6d8c..5c56f8e 100644 --- a/core/src/checamon/games/virtuacards/Deck.java +++ b/core/src/checamon/games/virtuacards/Deck.java @@ -10,7 +10,6 @@ import java.util.Collections; import java.util.HashMap; - /** * Created by angelcheca on 17/11/15. */ @@ -18,7 +17,7 @@ public class Deck { private ArrayList cards; private HashMap drawOrder; private int numberOfCards; - //private ArrayList subdecks; + private int deckCounter; public Deck(Texture texture) { try { @@ -37,6 +36,7 @@ public class Deck { } this.numberOfCards = 54; + this.deckCounter = 1; }catch (Exception e) { @@ -165,7 +165,7 @@ public class Deck { } //TODO Change this to not move to the top until is sure no other cards need to move - if (i < numberOfCards - 1) + if (i >= 0 && i < numberOfCards - 1) setCardDrawOrderOnTop(i); return cardResult; } @@ -185,12 +185,25 @@ public class Deck { } } //TODO Change this to not move to the top until is sure no other cards need to move - if (i < numberOfCards - 1) + if (i >= 0 && i < numberOfCards - 1) setCardDrawOrderOnTop(i); return cardResult; } + public Integer getTouchedCardIndex(float x, float y){ + int i = numberOfCards - 1; + boolean exit = false; + + while (i >= 0 && !exit){ + if (drawOrder.get(i) > -1 && cards.get(drawOrder.get(i)).isTouched(x, y)) { + exit = true; + }else{ + i--; + } + } + return (Integer)i; + } public Card getTouchedCard(float x, float y, int top){ Card cardResult = null; @@ -226,6 +239,25 @@ public class Deck { } } } + //TODO Make sure it orders the cards up to the top + private void setCardDrawOrderOnTopSubDeck(int index, int last){ + + int i = index; + int next = 0; + int replace = drawOrder.get(index); + + while (i < last){ + if (i < last - 1) { + next = drawOrder.get(i + 1); + drawOrder.put(i,next); + i++; + }else{ + drawOrder.put(i,replace); + i++; + + } + } + } private void setRandomCardDraw(ArrayList subDeck){ ArrayList originalOrder = (ArrayList) subDeck.clone(); @@ -235,8 +267,7 @@ public class Deck { for (int i = 0; i < originalOrder.size(); i++){ replace = drawOrder.get(originalOrder.get(i)); drawOrder.put(originalOrder.get(i),drawOrder.get(subDeck.get(i))); - if (i < originalOrder.size() - 1) - drawOrder.put(subDeck.get(i),replace); + drawOrder.put(subDeck.get(i),replace); } } @@ -246,18 +277,16 @@ public class Deck { ArrayList subDeck = new ArrayList(); Card c; - int top = numberOfCards-1; int i = numberOfCards-1; int index = 0; boolean exit = false; while (i >= 0 && !exit){ - c = this.getTouchedCard(x,y,top); + c = this.getTouchedCard(x, y, i); if (c == null) exit = true; else{ subDeck.add(index,c.getId()); - top = c.getId() - 1; index++; i--; } @@ -270,73 +299,141 @@ public class Deck { } public void moveDeckTouched(float x, float y){ - Card c = getTouchedCard(x,y); Card card; - float deltaX; - float deltaY; - - Rectangle r = c.getCardRectangle(); - c.setCenter(new Point(x,y)); - Rectangle r2 = c.getCardRectangle(); - - deltaX = r2.getX() - r.getX(); - deltaY = r2.getY() - r.getY(); + int deckId = -1; + int cardDeck = -1; + int lastIndex = -1; //Move all cards in the deck - int i = c.getId() - 1; + int i = getTouchedCardIndex(x,y); + lastIndex = i; while (i >= 0){ card = cards.get(drawOrder.get(i)); - if (drawOrder.get(i) > -1 && card.isTouched(x, y)) { - card.setPosition(new Point(card.getCardSprite().getX() + deltaX, card.getCardSprite().getY() + deltaY)); - }else{ - i--; + cardDeck = card.getDeckId(); + if (cardDeck == deckId) { + card.setCenter(new Point(x, y)); + setCardDrawOrderOnTopSubDeck(i, lastIndex); + lastIndex--; } + else if (deckId == -1){ + if (drawOrder.get(i) > -1 && card.isTouched(x, y)) { + deckId = card.getDeckId(); + card.setCenter(new Point(x, y)); + } + } + i--; } } public void moveDeckTouchedDragged(float x, float y){ - Card c = getTouchedCard(x,y); Card card; - float deltaX; - float deltaY; - - Rectangle r = c.getCardRectangle(); - c.setCenter(new Point(x,y)); - Rectangle r2 = c.getCardRectangle(); - - deltaX = r2.getX() - r.getX(); - deltaY = r2.getY() - r.getY(); + int deckId = -1; + int cardDeck = -1; + int lastIndex = -1; //Move all cards in the deck - int i = c.getId() - 1; + int i = getTouchedCardIndex(x,y); + lastIndex = i; while (i >= 0){ card = cards.get(drawOrder.get(i)); - if (drawOrder.get(i) > -1 && card.isTouchedDragged(x, y)) { - card.setPosition(new Point(card.getCardSprite().getX() + deltaX, card.getCardSprite().getY() + deltaY)); + cardDeck = card.getDeckId(); + if (cardDeck == deckId) { + card.setCenter(new Point(x, y)); + setCardDrawOrderOnTopSubDeck(i, lastIndex); + lastIndex--; + } + else if (deckId == -1){ + if (drawOrder.get(i) > -1 && card.isTouchedDragged(x, y)) { + deckId = card.getDeckId(); + card.setCenter(new Point(x, y)); + } + } + i--; + } + + } + + public void setDeckPosition(int belowDeckId, int aboveDeckId, float x, float y){ + Card card; + int cardDeck = -1; + + //Move all cards in the deck + int i = numberOfCards - 1; + + while (i >= 0){ + card = cards.get(drawOrder.get(i)); + cardDeck = card.getDeckId(); + if (cardDeck == aboveDeckId) { + card.setPosition(new Point(x, y)); + card.setDeckId(belowDeckId); + } + i--; + } + + } + public Card getTouchedCardDifferentDeck(float x, float y, int deckiId){ + Card cardResult = null; + int i = numberOfCards - 1; + boolean exit = false; + + while (i >= 0 && !exit){ + cardResult = cards.get(drawOrder.get(i)); + if (drawOrder.get(i) > -1 && cardResult.isTouched(x, y) && cardResult.getDeckId() != deckiId) { + exit = true; }else{ i--; } } + if (!exit) + cardResult = null; + + return cardResult; } - public void autoDeckCard(float x, float y){ - Card c = getTouchedCard(x,y); - Card below = getTouchedCard(x,y, numberOfCards - 2); + + + public void autoDeckCard(float x, float y) { + Card c = getTouchedCard(x, y); + Card below; + if (!c.isDecked()) { + below = getTouchedCard(x, y, numberOfCards - 2); + } else { + below = getTouchedCardDifferentDeck(x, y, c.getDeckId()); + } // Detect if overlapping enough to deck automatically // by default cards are 150 x 190 - if (below != null) { + if (below != null && !c.isDecked()) { if (((c.getCardSprite().getX() <= below.getCardSprite().getX() + 35) && (c.getCardSprite().getX() >= below.getCardSprite().getX() - 35)) && ((c.getCardSprite().getY() <= below.getCardSprite().getY() + 35) && (c.getCardSprite().getY() >= below.getCardSprite().getY() - 35))) { Rectangle r = below.getCardRectangle(); c.setPosition(new Point(r.getX(), r.getY())); c.setDecked(true); - }else{ - c.setDecked(false); + if (below.getDeckId() == 0) { + deckCounter++; + below.setDecked(true); + below.setDeckId(deckCounter); + c.setDeckId(deckCounter); + } else { + c.setDeckId(below.getDeckId()); + } } + } else if (below != null && c.isDecked()) { + if (((c.getCardSprite().getX() <= below.getCardSprite().getX() + 35) && (c.getCardSprite().getX() >= below.getCardSprite().getX() - 35)) && + ((c.getCardSprite().getY() <= below.getCardSprite().getY() + 35) && (c.getCardSprite().getY() >= below.getCardSprite().getY() - 35))) { + Rectangle r = below.getCardRectangle(); + if (below.getDeckId() == 0) { + below.setDecked(true); + below.setDeckId(c.getDeckId()); + setDeckPosition(c.getDeckId(), c.getDeckId(), r.getX(), r.getY()); + } else { + setDeckPosition(below.getDeckId(), c.getDeckId(), r.getX(), r.getY()); + } + } + //TODO resetDeckIds(); complex to compute... not worth it } } @@ -347,4 +444,12 @@ public class Deck { public int getNumberOfCards() { return numberOfCards; } + + public int getDeckCounter() { + return deckCounter; + } + + public void setDeckCounter(int deckCounter) { + this.deckCounter = deckCounter; + } } diff --git a/core/src/checamon/games/virtuacards/VirtuaCardsGameScreen.java b/core/src/checamon/games/virtuacards/VirtuaCardsGameScreen.java index 584a3e7..4964150 100644 --- a/core/src/checamon/games/virtuacards/VirtuaCardsGameScreen.java +++ b/core/src/checamon/games/virtuacards/VirtuaCardsGameScreen.java @@ -15,8 +15,8 @@ public class VirtuaCardsGameScreen implements Screen, InputProcessor { private int dragCounter; private int cardCounter; private ArrayList dragBuffer; + private ArrayList cardsTouched; private VirtuaCards game; - private Deck fullDeck; @@ -24,11 +24,12 @@ public class VirtuaCardsGameScreen implements Screen, InputProcessor { public VirtuaCardsGameScreen (VirtuaCards g) { game = g; dragBuffer = new ArrayList(); + cardsTouched = new ArrayList(); dragCounter = 0; cardCounter = 0; - Gdx.input.setInputProcessor(this); Gdx.input.setCatchBackKey(true); + //Gdx.app.setLogLevel(Application.LOG_ERROR); fullDeck = new Deck(new Texture("full_french_deck.png")); @@ -38,7 +39,7 @@ public class VirtuaCardsGameScreen implements Screen, InputProcessor { //fullDeck.getCards().get(52).setPosition(new Point(300f,100f)); - fullDeck.shuffle(110f, 110f); + //fullDeck.shuffle(110f, 110f); } @@ -46,6 +47,7 @@ public class VirtuaCardsGameScreen implements Screen, InputProcessor { public void render(float delta) { Gdx.gl.glClearColor(0.2f, 0.6f, 0.2f, 0); Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); + Gdx.input.setInputProcessor(this); game.batch.begin(); @@ -55,189 +57,187 @@ public class VirtuaCardsGameScreen implements Screen, InputProcessor { } - /** - * Called when a finger or the mouse was dragged. - * - * @param screenX - * @param screenY - * @param pointer the pointer for the event. @return whether the input was processed - */ - @Override - public boolean touchDragged(int screenX, int screenY, int pointer) { - //deck - if (dragCounter == 0) { - if (fullDeck.isTouchingCard(screenX, Gdx.graphics.getHeight() - screenY)) { - fullDeck.getTouchedCard(screenX, Gdx.graphics.getHeight() - screenY).setCenter(new Point(screenX, Gdx.graphics.getHeight() - screenY)); - dragBuffer.add(dragCounter, new Point(screenX, Gdx.graphics.getHeight() - screenY)); - dragCounter++; - } - } - else if (fullDeck.isTouchingDraggedCard(screenX, Gdx.graphics.getHeight() - screenY)) { - fullDeck.getTouchedDraggedCard(screenX, Gdx.graphics.getHeight() - screenY).setCenter(new Point(screenX, Gdx.graphics.getHeight() - screenY)); + @Override + public boolean touchDragged(int screenX, int screenY, int pointer) { + Card c; + //deck - dragBuffer.add(dragCounter, new Point(screenX, Gdx.graphics.getHeight() - screenY)); - dragCounter++; + //Gdx.app.error("VirtuaCardsGameScreen", "TouchDragged"); - } - else { - dragCounter = 0; - dragBuffer.clear(); - } - cardCounter = 0; - return true; - } + if (dragCounter == 0) { + if (fullDeck.isTouchingCard(screenX, Gdx.graphics.getHeight() - screenY)) { + c = fullDeck.getTouchedCard(screenX, Gdx.graphics.getHeight() - screenY); + if (cardCounter == 2) { // Move deck of cards + if (cardsTouched.get(0).equals(cardsTouched.get(1)) && c.isDecked()) { + fullDeck.moveDeckTouched(screenX, Gdx.graphics.getHeight() - screenY); + } + else { + c.setCenter(new Point(screenX, Gdx.graphics.getHeight() - screenY)); + c.setDecked(false); + c.setDeckId(0); + } + } + else { + c.setCenter(new Point(screenX, Gdx.graphics.getHeight() - screenY)); + c.setDecked(false); + c.setDeckId(0); + } + dragBuffer.add(dragCounter, new Point(screenX, Gdx.graphics.getHeight() - screenY)); + dragCounter++; + } + } + else if (fullDeck.isTouchingDraggedCard(screenX, Gdx.graphics.getHeight() - screenY)) { + c = fullDeck.getTouchedDraggedCard(screenX, Gdx.graphics.getHeight() - screenY); + if (cardCounter == 2) { // Move deck of cards + if (cardsTouched.get(0).equals(cardsTouched.get(1)) && c.isDecked()) { + fullDeck.moveDeckTouchedDragged(screenX, Gdx.graphics.getHeight() - screenY); + } else { + c.setCenter(new Point(screenX, Gdx.graphics.getHeight() - screenY)); + c.setDecked(false); + c.setDeckId(0); + } + } + else{ + c.setCenter(new Point(screenX, Gdx.graphics.getHeight() - screenY)); + c.setDecked(false); + c.setDeckId(0); + } - /** - * Called when a key was pressed - * - */ - @Override - public boolean keyDown(int keycode) { + dragBuffer.add(dragCounter, new Point(screenX, Gdx.graphics.getHeight() - screenY)); + dragCounter++; + } + else { + dragCounter = 0; + dragBuffer.clear(); + cardsTouched.clear(); + cardCounter = 0; + } + return true; + } + + @Override + public boolean keyDown(int keycode) { if(keycode == Input.Keys.BACK) { game.setScreen(new VirtuaCardsMainMenu(game)); dispose(); } - return true; - } + return true; + } - /** - * Called when a key was released - * - */ - @Override - public boolean keyUp(int keycode) { - return false; - } + @Override + public boolean keyUp(int keycode) { + return false; + } - /** - * Called when a key was typed - * - * @param character The character - * @return whether the input was processed - */ - @Override - public boolean keyTyped(char character) { - return false; - } + @Override + public boolean keyTyped(char character) { + return false; + } - /** - * - * @param screenX The x coordinate, origin is in the upper left corner - * @param screenY The y coordinate, origin is in the upper left corner - * @param pointer the pointer for the event. - * @param button the button - * @return whether the input was processed - */ - @Override - public boolean touchDown(int screenX, int screenY, int pointer, int button) { - - dragCounter = 0; - dragBuffer.clear(); - cardCounter = 1; - - - return true; - } - - /** - * - * @param screenX - * @param screenY - * @param pointer the pointer for the event. - * @param button the button @return whether the input was processed - */ - @Override - public boolean touchUp(int screenX, int screenY, int pointer, int button) { + @Override + public boolean touchDown(int screenX, int screenY, int pointer, int button) { Card c; + dragCounter = 0; + dragBuffer.clear(); + //Gdx.app.error("VirtuaCardsGameScreen", "TouchDown"); + c = fullDeck.getTouchedCard(screenX, Gdx.graphics.getHeight() - screenY); + if (c != null){ + cardsTouched.add(cardCounter,c.getId()); + cardCounter++; + }else{ + cardsTouched.clear(); + cardCounter = 0; + } + + return true; + } + + @Override + public boolean touchUp(int screenX, int screenY, int pointer, int button) { + Card c; + //Gdx.app.error("VirtuaCardsGameScreen", "TouchUp"); if (dragCounter > 0) { c = fullDeck.getTouchedDraggedCard(screenX, Gdx.graphics.getHeight() - screenY); if (c != null) { fullDeck.autoDeckCard(screenX, Gdx.graphics.getHeight() - screenY); + if (Point.pointListInsideDoubleTouchedDrag(dragBuffer, 75, 150)) { //flip card //fullDeck.getCards().get(52).setFaceUp(true); c.toggleFaceUp(); } + + if (cardCounter > 0){ + cardCounter = 0; + cardsTouched.clear(); + } + } + }else{ + c = fullDeck.getTouchedCard(screenX, Gdx.graphics.getHeight() - screenY); + if (c != null){ + if (cardCounter == 3){ // three touches on the same card shuffles the sub deck + if (cardsTouched.get(0) == cardsTouched.get(1) && cardsTouched.get(1) == cardsTouched.get(2)){ + fullDeck.shuffle(screenX, Gdx.graphics.getHeight() - screenY); + } + cardCounter = 0; + cardsTouched.clear(); + } } } - dragCounter = 0; - dragBuffer.clear(); - cardCounter = 0; + if (cardCounter == 3){ + cardCounter = 0; + cardsTouched.clear(); + } - return true; - } + if (dragCounter > 0){ + dragCounter = 0; + dragBuffer.clear(); - /** - * Called when the mouse was moved without any buttons being pressed. Will not be called on iOS. - * - * @param screenX - * @param screenY - * @return whether the input was processed - */ - @Override - public boolean mouseMoved(int screenX, int screenY) { - return false; - } + } - /** - * Called when the mouse wheel was scrolled. Will not be called on iOS. - * - * @param amount the scroll amount, -1 or 1 depending on the direction the wheel was scrolled. - * @return whether the input was processed. - */ - @Override + return true; + } + + @Override + public boolean mouseMoved(int screenX, int screenY) { + return false; + } + + + @Override public boolean scrolled(int amount) { return false; } - /** - * Called when this screen becomes the current screen for a {@link Game}. - */ @Override public void show() { } - /** - * @param width - * @param height - * @see ApplicationListener#resize(int, int) - */ + @Override public void resize(int width, int height) { } - /** - * @see ApplicationListener#pause() - */ @Override public void pause() { } - /** - * @see ApplicationListener#resume() - */ @Override public void resume() { } - /** - * Called when this screen is no longer the current screen for a {@link Game}. - */ @Override public void hide() { } - /** - * Called when this screen should release all resources. - */ @Override public void dispose() { diff --git a/core/src/checamon/games/virtuacards/VirtuaCardsMainMenu.java b/core/src/checamon/games/virtuacards/VirtuaCardsMainMenu.java index 211101f..027f1ad 100644 --- a/core/src/checamon/games/virtuacards/VirtuaCardsMainMenu.java +++ b/core/src/checamon/games/virtuacards/VirtuaCardsMainMenu.java @@ -9,19 +9,15 @@ import com.badlogic.gdx.graphics.Pixmap; import com.badlogic.gdx.graphics.Texture; import com.badlogic.gdx.graphics.g2d.BitmapFont; import com.badlogic.gdx.math.Rectangle; -import com.badlogic.gdx.scenes.scene2d.Stage; -import com.badlogic.gdx.scenes.scene2d.Touchable; import com.badlogic.gdx.scenes.scene2d.ui.Skin; import com.badlogic.gdx.scenes.scene2d.ui.TextButton; import com.badlogic.gdx.scenes.scene2d.ui.TextButton.TextButtonStyle; -import com.badlogic.gdx.scenes.scene2d.utils.ClickListener; /** * Created by angelcheca on 28/11/15. */ public class VirtuaCardsMainMenu implements Screen, InputProcessor { final VirtuaCards game; - private boolean exitScreen; private Skin skin; private TextButton playButton; @@ -31,12 +27,10 @@ public class VirtuaCardsMainMenu implements Screen, InputProcessor { public VirtuaCardsMainMenu (final VirtuaCards g){ game = g; - exitScreen = false; skin = new Skin(); Gdx.input.setInputProcessor(this); - Pixmap pixmap = new Pixmap(400, 100, Pixmap.Format.RGBA8888); pixmap.setColor(Color.GREEN); pixmap.fill(); @@ -99,47 +93,29 @@ public class VirtuaCardsMainMenu implements Screen, InputProcessor { } game.batch.end(); - if (exitScreen) - dispose(); } - /** - * @param width - * @param height - * @see ApplicationListener#resize(int, int) - */ + @Override public void resize(int width, int height) { } - /** - * @see ApplicationListener#pause() - */ @Override public void pause() { } - /** - * @see ApplicationListener#resume() - */ @Override public void resume() { } - /** - * Called when this screen is no longer the current screen for a {@link Game}. - */ @Override public void hide() { } - /** - * Called when this screen should release all resources. - */ @Override public void dispose() { } @@ -172,7 +148,7 @@ public class VirtuaCardsMainMenu implements Screen, InputProcessor { if (buttonClicked) // play button clicked { game.setScreen(new VirtuaCardsGameScreen(game)); - exitScreen = true; + dispose(); } else { // exit button clicked r.set(exitButton.getX(), exitButton.getY(), exitButton.getWidth(), exitButton.getHeight());