-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLocation.pde
239 lines (202 loc) · 5.75 KB
/
Location.pde
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
public class Location extends Clickable {
public LinkedList<CardInfo> cards = new LinkedList();
public int i = -1;
protected boolean queened = false;
protected int len;
protected int p1Scor = 0;
protected int p2Scor = 0;
public static final color bigger_score_col = #4ade80;
public static final int space = 200;
public static final int gap = 100;
// ? Numbers are wierd because Processing moment
public int y_score_above = Snap.ch - Card.h - 20;
public int y_score_bellow = Snap.ch + Card.h + 20;
// Cards
public static final int cards_gap = 4;
public static final int cards_space = 70;
public static final int cards_size = 40;
public int cards_y_above = y_score_above + cards_space;
public int cards_y_bellow = y_score_bellow - cards_space;
public int cards_h = (int) (cards_size * Card._ratio);
protected void _setup() {
x = m.cw + space * i;
y = m.ch;
w = Card.w;
h = Card.h;
}
protected void _update() {
// Draw the image
imageMode(CENTER);
image(
getTop(),
x,
y,
Card.w,
Card.h
);
// show the current players cards score bellow the card
// show the other players score above the card
textAlign(CENTER);
textSize(40);
if (curTurn == 1) {
push();
if (p1Scor > p2Scor) {
fill(bigger_score_col);
}
text("" + p1Scor, x, y_score_bellow);
pop();
text("" + p2Scor, x, y_score_above);
} else if (curTurn == 2) {
push();
if (p2Scor > p1Scor) {
fill(bigger_score_col);
}
text("" + p2Scor, x, y_score_bellow);
pop();
text("" + p1Scor, x, y_score_above);
}
// Draw the icons
for(int i=-1; i<3; i++) {
int x = this.x + i*(cards_gap + cards_size) - cards_size / 2;
int index = i + 1;
boolean exists = index < len;
shapeMode(CENTER);
imageMode(CENTER);
if (exists) {
CardInfo cc = cards.get(index);
if (cc.player.num == m.curTurn) {
image(a.getCard(cc), x, cards_y_bellow, cards_size, cards_h);
} else {
image(a.getCard(cc), x, cards_y_above, cards_size, cards_h);
}
} else {
shape(a.cardEmpty, x, cards_y_bellow, cards_size, cards_size);
}
}
// If the location was clicked on
// and the card index is a valid card
// and the curent turn is -1 (to prevent crashes)
// and the number of cards already on the location is bellow the limit
// ! OR the card is a special card and WILL NOT be added to the pile
if (
clicked &&
m.curCardIndex >= 0 &&
curTurn != -1 &&
(cards.size() < 4 || m.curPlayer.hand.cards.get(curCardIndex).num >= Card.jack)
) {
// handle adding the card to that location based on the current player
Card c = m.curPlayer.hand.cards.get(curCardIndex);
if (handleCard(c)) {
// Update global variables
m.curPlayer.hand.cards.remove(curCardIndex);
m.curCardIndex = -1;
numTurns--;
// Play a place sound effect
a.place.jump(0);
} else {
// Play an error sound effect
a.error.jump(0);
}
}
}
/**
* If the card is one of the special cards do something
* @returns weather the curent card index and num turns should be updated
*/
private boolean handleCard(Card c) {
int len = cards.size();
int lastI = len - 1;
// Is a normal card so just add it to the pile
if (c.num >= 1 && c.num <= 10) {
if (len > 0) {
Card prev = cards.get(lastI).card;
if (prev.num == Card.ace && !prev.bad) {
c.bad = true;
c.num = 1;
}
}
cards.add(new CardInfo(c, m.curPlayer));
return true;
}
// Now if the card is special
if (len <= 0) return false;
if(c.num == Card.queen) {
Card c2 = cards.get(lastI).card;
c2.num -= 5;
c2.queened = true;
queened = true;
} else if (c.num == Card.king) {
cards.remove(lastI);
} else if (c.num == Card.jack) {
Location loc = randLocation();
// This card cannot be placed
// because there is nowhere to the card to move to!
if (loc == null) {
return false;
}
loc.cards.add(cards.remove(lastI));
}
return true;
}
/**
* Returns a random location that is not the current one
* and that has less than 4 placed cards on it
*/
protected Location randLocation() {
// Doesnt include -2 or 2
int index = int(random(-2, 2));
Location loc = getLoc(index);
final int maxTries = 15;
int tries = 0;
// The jack cannot move a cawrd to the same location or
// a location with 4 or more cards (already full)
while((index == i || loc.cards.size() >= 4) && tries < maxTries) {
index = int(random(-1, 2));
loc = getLoc(index);
tries++;
}
if (tries >= maxTries) {
return null;
}
return getLoc(index);
}
protected Location getLoc(int i) {
if (i == -1) {
return m.l1;
} else if (i == 0) {
return m.l2;
} else if (i == 1) {
return m.l3;
} else {
// This shoundn't happen
return null;
}
}
protected void preUpdate() {
super.preUpdate();
calc();
}
public void calc() {
if (cards.size() != len || queened) {
p1Scor = 0;
p2Scor = 0;
for(CardInfo c : cards) {
if (c.player.num == 1) {
p1Scor += c.card.num;
} else if (c.player.num == 2) {
p2Scor += c.card.num;
}
}
len = cards.size();
queened = false;
}
}
private PImage getTop() {
if (cards.size() == 0) {
return a.back;
}
Card c = (cards.get(cards.size() - 1)).card;
return a.getCard(c);
}
public Location(Snap app, int i) { super(app); this.i = i; }
}