/
CardGame.java
180 lines (150 loc) · 4.63 KB
/
CardGame.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package com.xoppa.blog.libgdx.g3d.cardgame.step5;
import static com.xoppa.blog.libgdx.Main.data;
import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.PerspectiveCamera;
import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.graphics.g3d.utils.CameraInputController;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.utils.Array;
import com.badlogic.gdx.utils.ObjectSet;
public class CardGame implements ApplicationListener {
public final static float CARD_WIDTH = 1f;
public final static float CARD_HEIGHT = CARD_WIDTH * 277f / 200f;
public final static float MINIMUM_VIEWPORT_SIZE = 5f;
public enum Suit {
Clubs("clubs", 0), Diamonds("diamonds", 1), Hearts("hearts", 2), Spades("spades", 3);
public final String name;
public final int index;
private Suit(String name, int index) {
this.name = name;
this.index = index;
}
}
public enum Pip {
Ace(1), Two(2), Three(3), Four(4), Five(5), Six(6), Seven(7), Eight(8), Nine(9), Ten(10), Jack(11), Queen(12), King(13);
public final int value;
public final int index;
private Pip(int value) {
this.value = value;
this.index = value - 1;
}
}
public static class Card {
public final Suit suit;
public final Pip pip;
private final Sprite front;
private final Sprite back;
private boolean turned;
public Card(Suit suit, Pip pip, Sprite back, Sprite front) {
back.setSize(CARD_WIDTH, CARD_HEIGHT);
front.setSize(CARD_WIDTH, CARD_HEIGHT);
this.suit = suit;
this.pip = pip;
this.back = back;
this.front = front;
}
public void setPosition(float x, float y) {
front.setPosition(x - 0.5f * front.getWidth(), y - 0.5f * front.getHeight());
back.setPosition(x - 0.5f * back.getWidth(), y - 0.5f * back.getHeight());
}
public void turn() {
turned = !turned;
}
public void draw(Batch batch) {
if (turned)
back.draw(batch);
else
front.draw(batch);
}
}
public static class CardDeck {
private final Card[][] cards;
public CardDeck(TextureAtlas atlas, int backIndex) {
cards = new Card[Suit.values().length][];
for (Suit suit : Suit.values()) {
cards[suit.index] = new Card[Pip.values().length];
for (Pip pip : Pip.values()) {
Sprite front = atlas.createSprite(suit.name, pip.value);
Sprite back = atlas.createSprite("back", backIndex);
cards[suit.index][pip.index] = new Card(suit, pip, back, front);
}
}
}
public Card getCard(Suit suit, Pip pip) {
return cards[suit.index][pip.index];
}
}
SpriteBatch spriteBatch;
TextureAtlas atlas;
Sprite front;
Sprite back;
PerspectiveCamera cam;
CardDeck deck;
ObjectSet<Card> cards;
CameraInputController camController;
@Override
public void create() {
spriteBatch = new SpriteBatch();
atlas = new TextureAtlas(data + "/carddeck.atlas");
cards = new ObjectSet<Card>();
deck = new CardDeck(atlas, 3);
Card card1 = deck.getCard(Suit.Diamonds, Pip.Queen);
card1.setPosition(-1, 0);
cards.add(card1);
Card card2 = deck.getCard(Suit.Hearts, Pip.Four);
card2.setPosition(0, 0);
cards.add(card2);
Card card3 = deck.getCard(Suit.Spades, Pip.Ace);
card3.setPosition(1, 0);
card3.turn();
cards.add(card3);
cam = new PerspectiveCamera();
cam.position.set(0, 0, 10);
cam.lookAt(0, 0, 0);
camController = new CameraInputController(cam);
Gdx.input.setInputProcessor(camController);
}
@Override
public void resize(int width, int height) {
float halfHeight = MINIMUM_VIEWPORT_SIZE * 0.5f;
if (height > width)
halfHeight *= (float)height / (float)width;
float halfFovRadians = MathUtils.degreesToRadians * cam.fieldOfView * 0.5f;
float distance = halfHeight / (float)Math.tan(halfFovRadians);
cam.viewportWidth = width;
cam.viewportHeight = height;
cam.position.set(0, 0, distance);
cam.lookAt(0, 0, 0);
cam.update();
}
@Override
public void render() {
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT);
camController.update();
spriteBatch.setProjectionMatrix(cam.combined);
spriteBatch.begin();
for (Card card : cards)
card.draw(spriteBatch);
spriteBatch.end();
}
@Override
public void pause() {
// TODO Auto-generated method stub
}
@Override
public void resume() {
// TODO Auto-generated method stub
}
@Override
public void dispose() {
spriteBatch.dispose();
atlas.dispose();
}
}