Small changes to clean the code and differenciate between touchdragged and touchcard.
This commit is contained in:
@@ -1,3 +1,4 @@
|
|||||||
# Virtua-Cards
|
# Virtua-Cards
|
||||||
|
|
||||||
Playing with libgdx for my own learning purposes, I'm creating a simple card game.
|
Playing with libgdx for my own learning purposes, I'm creating a simple card game.
|
||||||
|
|
||||||
|
|||||||
@@ -74,6 +74,10 @@ public class Card {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public boolean isTouched(float x, float y){
|
public boolean isTouched(float x, float y){
|
||||||
|
return this.getCardRectangle().contains(x, y);
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isTouchedDragged(float x, float y){
|
||||||
Rectangle r = this.getCardRectangle();
|
Rectangle r = this.getCardRectangle();
|
||||||
float h = r.getHeight();
|
float h = r.getHeight();
|
||||||
float w = r.getWidth();
|
float w = r.getWidth();
|
||||||
@@ -104,6 +108,12 @@ public class Card {
|
|||||||
|
|
||||||
public void setPosition(Point position) {
|
public void setPosition(Point position) {
|
||||||
|
|
||||||
|
this.cardSprite.setPosition(position.getX(), position.getY());
|
||||||
|
this.position.setCoordinates(position.getX(),position.getY());
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setCenter(Point position) {
|
||||||
|
|
||||||
this.cardSprite.setCenter(position.getX(), position.getY());
|
this.cardSprite.setCenter(position.getX(), position.getY());
|
||||||
this.position.setCoordinates(position.getX(),position.getY());
|
this.position.setCoordinates(position.getX(),position.getY());
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -94,6 +94,49 @@ public class Deck {
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean isTouchingDraggedCard(float x, float y){
|
||||||
|
|
||||||
|
boolean exit = false;
|
||||||
|
boolean result = false;
|
||||||
|
int i = 0;
|
||||||
|
|
||||||
|
while (i < numberOfCards && !exit){
|
||||||
|
if (drawOrder.get(i) > -1){
|
||||||
|
if (cards.get(drawOrder.get(i)).isTouchedDragged(x, y)) {
|
||||||
|
result = true;
|
||||||
|
exit = true;
|
||||||
|
}
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
exit = true;
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Card getTouchedDraggedCard(float x, float y){
|
||||||
|
|
||||||
|
boolean exit = false;
|
||||||
|
this.result = null;
|
||||||
|
Card cardResult = null;
|
||||||
|
int i = 0;
|
||||||
|
|
||||||
|
while (i < numberOfCards && !exit){
|
||||||
|
if (drawOrder.get(i) > -1){
|
||||||
|
if (cards.get(drawOrder.get(i)).isTouchedDragged(x, y)) {
|
||||||
|
|
||||||
|
this.result = cards.get(drawOrder.get(i)).getCardSprite();
|
||||||
|
cardResult = cards.get(drawOrder.get(i));
|
||||||
|
exit = true;
|
||||||
|
}
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
exit = true;
|
||||||
|
}
|
||||||
|
return cardResult;
|
||||||
|
}
|
||||||
|
|
||||||
public Card getTouchedCard(float x, float y){
|
public Card getTouchedCard(float x, float y){
|
||||||
|
|
||||||
boolean exit = false;
|
boolean exit = false;
|
||||||
@@ -103,7 +146,7 @@ public class Deck {
|
|||||||
|
|
||||||
while (i < numberOfCards && !exit){
|
while (i < numberOfCards && !exit){
|
||||||
if (drawOrder.get(i) > -1){
|
if (drawOrder.get(i) > -1){
|
||||||
if (cards.get(drawOrder.get(i)).isTouched(x,y)) {
|
if (cards.get(drawOrder.get(i)).isTouched(x, y)) {
|
||||||
|
|
||||||
this.result = cards.get(drawOrder.get(i)).getCardSprite();
|
this.result = cards.get(drawOrder.get(i)).getCardSprite();
|
||||||
cardResult = cards.get(drawOrder.get(i));
|
cardResult = cards.get(drawOrder.get(i));
|
||||||
|
|||||||
@@ -4,77 +4,35 @@ package checamon.games.virtuacards;
|
|||||||
import com.badlogic.gdx.ApplicationAdapter;
|
import com.badlogic.gdx.ApplicationAdapter;
|
||||||
import com.badlogic.gdx.Gdx;
|
import com.badlogic.gdx.Gdx;
|
||||||
import com.badlogic.gdx.InputProcessor;
|
import com.badlogic.gdx.InputProcessor;
|
||||||
import com.badlogic.gdx.graphics.Color;
|
|
||||||
import com.badlogic.gdx.graphics.GL20;
|
import com.badlogic.gdx.graphics.GL20;
|
||||||
import com.badlogic.gdx.graphics.Texture;
|
import com.badlogic.gdx.graphics.Texture;
|
||||||
import com.badlogic.gdx.graphics.g2d.BitmapFont;
|
|
||||||
import com.badlogic.gdx.graphics.g2d.Sprite;
|
|
||||||
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
|
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
|
||||||
import com.badlogic.gdx.graphics.g2d.TextureRegion;
|
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
|
||||||
|
|
||||||
public class VirtuaCards extends ApplicationAdapter implements InputProcessor {
|
public class VirtuaCards extends ApplicationAdapter implements InputProcessor {
|
||||||
SpriteBatch batch;
|
private SpriteBatch batch;
|
||||||
Sprite sprite;
|
|
||||||
Texture img, deck1;
|
|
||||||
BitmapFont font;
|
|
||||||
String debugText;
|
|
||||||
|
|
||||||
TextureRegion deck1region;
|
|
||||||
TextureRegion[][] regions;
|
|
||||||
TextureRegion deck2region;
|
|
||||||
Sprite sprite2;
|
|
||||||
|
|
||||||
private float x, y;
|
|
||||||
private int dragCounter;
|
private int dragCounter;
|
||||||
|
private int cardCounter;
|
||||||
private ArrayList<Point> dragBuffer;
|
private ArrayList<Point> dragBuffer;
|
||||||
private Sprite touchedSprite;
|
|
||||||
|
|
||||||
Deck fullDeck;
|
private Deck fullDeck;
|
||||||
Texture deck;
|
|
||||||
|
|
||||||
int cardCounter = 0;
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void create () {
|
public void create () {
|
||||||
batch = new SpriteBatch();
|
batch = new SpriteBatch();
|
||||||
//img = new Texture("badlogic.jpg");
|
dragBuffer = new ArrayList<Point>();
|
||||||
deck1 = new Texture("deck1pic.png");
|
|
||||||
TextureRegion[][] cc = new TextureRegion(deck1).split(deck1.getWidth()/2,deck1.getHeight()/2);
|
|
||||||
deck1region = new TextureRegion(cc[0][0]);
|
|
||||||
deck2region = new TextureRegion(cc[0][1]);
|
|
||||||
|
|
||||||
try {
|
|
||||||
deck = new Texture("full_french_deck.png");
|
|
||||||
} catch (Throwable e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
font = new BitmapFont();
|
|
||||||
font.setColor(Color.BLUE);
|
|
||||||
//sprite = new Sprite(deck1);
|
|
||||||
sprite = new Sprite(deck1region);
|
|
||||||
|
|
||||||
sprite2 = new Sprite(deck2region);
|
|
||||||
sprite2.setPosition(50f, 50f);
|
|
||||||
sprite2.scale(-0.75f);
|
|
||||||
|
|
||||||
x = 0;
|
|
||||||
y = 0;
|
|
||||||
sprite.setPosition(x, y);
|
|
||||||
sprite.scale(-0.75f);
|
|
||||||
|
|
||||||
debugText = "LOADING ...";
|
|
||||||
if (deck == null)
|
|
||||||
debugText += " DECK IS NULL";
|
|
||||||
dragCounter = 0;
|
dragCounter = 0;
|
||||||
dragBuffer = new ArrayList();
|
cardCounter = 0;
|
||||||
|
|
||||||
fullDeck = new Deck(deck);
|
fullDeck = new Deck(new Texture("full_french_deck.png"));
|
||||||
touchedSprite = null;
|
|
||||||
|
|
||||||
fullDeck.getDrawOrder().put(0, 13);
|
//init drawn cards
|
||||||
|
fullDeck.getDrawOrder().put(0,11);
|
||||||
|
fullDeck.getDrawOrder().put(1,2);
|
||||||
|
fullDeck.getCards().get(2).setPosition(new Point (200f,300f));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -83,18 +41,9 @@ public class VirtuaCards extends ApplicationAdapter implements InputProcessor {
|
|||||||
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
|
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
|
||||||
Gdx.input.setInputProcessor(this);
|
Gdx.input.setInputProcessor(this);
|
||||||
batch.begin();
|
batch.begin();
|
||||||
//sprite.draw(batch);
|
|
||||||
//sprite2.draw(batch);
|
|
||||||
|
|
||||||
fullDeck.drawCards(batch);
|
fullDeck.drawCards(batch);
|
||||||
|
|
||||||
/*
|
|
||||||
if (touchedSprite != null)
|
|
||||||
touchedSprite.draw(batch);
|
|
||||||
*/
|
|
||||||
|
|
||||||
|
|
||||||
//font.draw(batch, debugText, 20, 40);
|
|
||||||
batch.end();
|
batch.end();
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -109,21 +58,25 @@ public class VirtuaCards extends ApplicationAdapter implements InputProcessor {
|
|||||||
@Override
|
@Override
|
||||||
public boolean touchDragged(int screenX, int screenY, int pointer) {
|
public boolean touchDragged(int screenX, int screenY, int pointer) {
|
||||||
//deck
|
//deck
|
||||||
if (fullDeck.isTouchingCard(screenX, Gdx.graphics.getHeight() - screenY)){
|
if (dragCounter == 0) {
|
||||||
fullDeck.getTouchedCard(screenX, Gdx.graphics.getHeight() - screenY).setPosition(new Point(screenX, Gdx.graphics.getHeight() - screenY));
|
if (fullDeck.isTouchingCard(screenX, Gdx.graphics.getHeight() - screenY)) {
|
||||||
/*touchedSprite = fullDeck.getTouchedCard(screenX, Gdx.graphics.getHeight() - screenY).getCardSprite();
|
fullDeck.getTouchedCard(screenX, Gdx.graphics.getHeight() - screenY).setCenter(new Point(screenX, Gdx.graphics.getHeight() - screenY));
|
||||||
if (touchedSprite != null)
|
|
||||||
touchedSprite.setCenter(screenX, Gdx.graphics.getHeight() - screenY);*/
|
dragBuffer.add(dragCounter, new Point(screenX, Gdx.graphics.getHeight() - screenY));
|
||||||
cardCounter++;
|
dragCounter++;
|
||||||
/*
|
|
||||||
debugText = "Card: X=" + String.valueOf(touchedSprite.getX()) + ", Y=" + String.valueOf(touchedSprite.getY()) +
|
|
||||||
"; Touch X=" + String.valueOf(screenX) + ", Y=" + String.valueOf( Gdx.graphics.getHeight() - screenY) + " DragCounter=" + String.valueOf(dragCounter) +
|
|
||||||
" DragBuffer Length: " + String.valueOf(dragBuffer.size()) + " Touch Card Counter = " + toString().valueOf(cardCounter);
|
|
||||||
*/
|
|
||||||
}
|
}
|
||||||
else if (dragCounter > 0) {
|
}
|
||||||
|
else if (fullDeck.isTouchingDraggedCard(screenX, Gdx.graphics.getHeight() - screenY)) {
|
||||||
|
fullDeck.getTouchedDraggedCard(screenX, Gdx.graphics.getHeight() - screenY).setCenter(new Point(screenX, Gdx.graphics.getHeight() - screenY));
|
||||||
|
|
||||||
|
dragBuffer.add(dragCounter, new Point(screenX, Gdx.graphics.getHeight() - screenY));
|
||||||
|
dragCounter++;
|
||||||
|
cardCounter++;
|
||||||
|
}
|
||||||
|
else {
|
||||||
dragCounter = 0;
|
dragCounter = 0;
|
||||||
dragBuffer.clear();
|
dragBuffer.clear();
|
||||||
|
cardCounter = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
@@ -169,18 +122,6 @@ public class VirtuaCards extends ApplicationAdapter implements InputProcessor {
|
|||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public boolean touchDown(int screenX, int screenY, int pointer, int button) {
|
public boolean touchDown(int screenX, int screenY, int pointer, int button) {
|
||||||
debugText = "Touch DOWN X=" + String.valueOf(screenX) + ", Y=" + String.valueOf(screenY) + " Image X=" +
|
|
||||||
String.valueOf(sprite.getX()) + " Y=" + String.valueOf(sprite.getY()) + " DragCounter=" + String.valueOf(dragCounter) + " DragBuffer Length: " + String.valueOf(dragBuffer.size());;
|
|
||||||
|
|
||||||
/*Rectangle r = sprite.getBoundingRectangle();
|
|
||||||
|
|
||||||
if (r.contains(screenX,Gdx.graphics.getHeight() - screenY)) // It is touching the deck
|
|
||||||
{
|
|
||||||
batch.begin();
|
|
||||||
sprite.draw(batch);
|
|
||||||
batch.end();
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
|
|
||||||
dragCounter = 0;
|
dragCounter = 0;
|
||||||
dragBuffer.clear();
|
dragBuffer.clear();
|
||||||
@@ -199,19 +140,7 @@ public class VirtuaCards extends ApplicationAdapter implements InputProcessor {
|
|||||||
@Override
|
@Override
|
||||||
public boolean touchUp(int screenX, int screenY, int pointer, int button) {
|
public boolean touchUp(int screenX, int screenY, int pointer, int button) {
|
||||||
|
|
||||||
if (Point.pointListInsideDoubleTouchedDrag(dragBuffer, 75, 50) && dragCounter > 0)
|
//if (Point.pointListInsideDoubleTouchedDrag(dragBuffer, 75, 50) && dragCounter > 0)
|
||||||
{//double swipe, left/right recognised
|
|
||||||
debugText = "DoubleSwipe UP X=" + String.valueOf(screenX) + ", Y=" + String.valueOf(screenY) + " Image X=" +
|
|
||||||
String.valueOf(sprite.getX()) + " Y=" + String.valueOf(sprite.getY()) + " DragCounter=" + String.valueOf(dragCounter) + " DragBuffer Length: " + String.valueOf(dragBuffer.size());
|
|
||||||
|
|
||||||
//fullDeck.getDrawOrder().put(0, 11);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
debugText = "Touch UP X=" + String.valueOf(screenX) + ", Y=" + String.valueOf(screenY) + " Image X=" +
|
|
||||||
String.valueOf(sprite.getX()) + " Y=" + String.valueOf(sprite.getY()) + " DragCounter=" + String.valueOf(dragCounter) + " DragBuffer Length: " + String.valueOf(dragBuffer.size());
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
dragCounter = 0;
|
dragCounter = 0;
|
||||||
dragBuffer.clear();
|
dragBuffer.clear();
|
||||||
|
|||||||
Reference in New Issue
Block a user