-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathkblackjack10.js
393 lines (347 loc) · 12.5 KB
/
kblackjack10.js
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
//
// K BlackJack
// ***********
//
// Created by Kevin Thomas and Thoys 02/25/17
// Modified by Kevin Thomas 05/03/20
//
// Copyright: (c) 2020, Kevin Thomas <kevin@mytechnotalent.com>
// Apache License, Version 2.0 (see COPYING or https://www.apache.org/licenses/LICENSE-2.0)
//
// JavaScript for the High Fidelity interface that provides a BlackJack-style card game.
//
// Playing cards included from Byron Knoll: http://code.google.com/p/vector-playing-cards
//
// Version 0.0.1 Alpha
//
// Set global camera mode so that the avatar upon loading the game goes into 1st person
Camera.mode = "first person";
// Set global position values for the card entities in respect to your avatar
const PLAYER_DISTANCE = 2.5;
// Set global position values for the card entities in respect to the dealer
const DEALER_DISTANCE = PLAYER_DISTANCE + 2.0;
// Set global card horizontal offset
const CARD_HORIZONTAL_OFFSET = 1.25;
// Set global entity rotation
const NORMAL_ROTATION = Quat.fromPitchYawRollDegrees(0, 270, 0);
// Set global dealer card first entity initial rotation
const FLIP_ROTATION = Quat.fromPitchYawRollDegrees(270, 270, 270);
// Set global card path
const CARD_PATH = "https://raw.githubusercontent.com/mytechnotalent/k_blackjack/master/models/";
// Set global sound path
const SOUND_PATH = "https://raw.githubusercontent.com/mytechnotalent/k_blackjack/master/sounds/";
// Set global deal card texture
const DEAL_TEXTURE = CARD_PATH + "deal.png";
// Set global hit me card texture
const HIT_ME_TEXTURE = CARD_PATH + "hitme.png";
// Set global stay card texture
const STAY_TEXTURE = CARD_PATH + "stay.png";
// Set global you won card texture
const YOU_WON_TEXTURE = CARD_PATH + "youwon.png";
// Set global you lost card texture
const YOU_LOST_TEXTURE = CARD_PATH + "youlost.png";
// Set global play again texture
const PLAY_AGAIN_TEXTURE = CARD_PATH + "playagain.png";
// Set global quit texture
const QUIT_TEXTURE = CARD_PATH + "quit.png";
// Create global variables
var dealHandEntity;
var hitMeEntity;
var stayEntity;
var playAgainEntity;
var youLostEntity;
var youWonEntity;
var quitEntity;
var rezzedEntities = [];
var drawnCard;
var cardEntity1;
var cardEntity2;
var cardEntity;
var selection1;
var selection2;
var selection3;
var selection4;
var excludeCard;
var dealerScore = 0;
var playerScore = 0;
var hitAgain = 0;
var newCardPath;
var stackHeight = 0;
// Create global card array with JSON objects
var CARDS = [
{filename: 'ace_of_hearts.png', value: 1, type: "hearts"},
{filename: '2_of_hearts.png', value: 2, type: "hearts"},
{filename: '3_of_hearts.png', value: 3, type: "hearts"},
{filename: '4_of_hearts.png', value: 4, type: "hearts"},
{filename: '5_of_hearts.png', value: 5, type: "hearts"},
{filename: '6_of_hearts.png', value: 6, type: "hearts"},
{filename: '7_of_hearts.png', value: 7, type: "hearts"},
{filename: '8_of_hearts.png', value: 8, type: "hearts"},
{filename: '9_of_hearts.png', value: 9, type: "hearts"},
{filename: '10_of_hearts.png', value: 10, type: "hearts"},
{filename: 'jack_of_hearts2.png', value: 10, type: "hearts"},
{filename: 'queen_of_hearts2.png', value: 10, type: "hearts"},
{filename: 'king_of_hearts2.png', value: 10, type: "hearts"},
{filename: 'ace_of_diamonds.png', value: 1, type: "diamonds"},
{filename: '2_of_diamonds.png', value: 2, type: "diamonds"},
{filename: '3_of_diamonds.png', value: 3, type: "diamonds"},
{filename: '4_of_diamonds.png', value: 4, type: "diamonds"},
{filename: '5_of_diamonds.png', value: 5, type: "diamonds"},
{filename: '6_of_diamonds.png', value: 6, type: "diamonds"},
{filename: '7_of_diamonds.png', value: 7, type: "diamonds"},
{filename: '8_of_diamonds.png', value: 8, type: "diamonds"},
{filename: '9_of_diamonds.png', value: 9, type: "diamonds"},
{filename: '10_of_diamonds.png', value: 10, type: "diamonds"},
{filename: 'jack_of_diamonds2.png', value: 10, type: "diamonds"},
{filename: 'queen_of_diamonds2.png', value: 10, type: "diamonds"},
{filename: 'king_of_diamonds2.png', value: 10, type: "diamonds"},
{filename: 'ace_of_clubs.png', value: 1, type: "clubs"},
{filename: '2_of_clubs.png', value: 2, type: "clubs"},
{filename: '3_of_clubs.png', value: 3, type: "clubs"},
{filename: '4_of_clubs.png', value: 4, type: "clubs"},
{filename: '5_of_clubs.png', value: 5, type: "clubs"},
{filename: '6_of_clubs.png', value: 6, type: "clubs"},
{filename: '7_of_clubs.png', value: 7, type: "clubs"},
{filename: '8_of_clubs.png', value: 8, type: "clubs"},
{filename: '9_of_clubs.png', value: 9, type: "clubs"},
{filename: '10_of_clubs.png', value: 10, type: "clubs"},
{filename: 'jack_of_clubs2.png', value: 10, type: "clubs"},
{filename: 'queen_of_clubs2.png', value: 10, type: "clubs"},
{filename: 'king_of_clubs2.png', value: 10, type: "clubs"},
{filename: 'ace_of_spades.png', value: 1, type: "spades"},
{filename: '2_of_spades.png', value: 2, type: "spades"},
{filename: '3_of_spades.png', value: 3, type: "spades"},
{filename: '4_of_spades.png', value: 4, type: "spades"},
{filename: '5_of_spades.png', value: 5, type: "spades"},
{filename: '6_of_spades.png', value: 6, type: "spades"},
{filename: '7_of_spades.png', value: 7, type: "spades"},
{filename: '8_of_spades.png', value: 8, type: "spades"},
{filename: '9_of_spades.png', value: 9, type: "spades"},
{filename: '10_of_spades.png', value: 10, type: "spades"},
{filename: 'jack_of_spades2.png', value: 10, type: "spades"},
{filename: 'queen_of_spades2.png', value: 10, type: "spades"},
{filename: 'king_of_spades2.png', value: 10, type: "spades"}
];
// Set global number of cards variable based on the array length
var NUMBER_OF_CARDS = CARDS.length;
// Randomize card number function
function randomCardNumber(){
return Math.floor(Math.random() * NUMBER_OF_CARDS);
}
// Create and init sound entities
var dealHandSound = SoundCache.getSound(SOUND_PATH + "shuffle.wav");
var hitMeSound = SoundCache.getSound(SOUND_PATH + "deal.wav");
var winSound = SoundCache.getSound(SOUND_PATH + "win.wav");
var loseSound = SoundCache.getSound(SOUND_PATH + "lose.wav");
// Get card properties function
function getCardProperties(cardNumber, texture, rotation, offset){
return{
type: "Model",
userData: JSON.stringify({grabbableKey:{grabbable:true}}),
modelURL: CARD_PATH + "kblackjackcard10.fbx",
dimensions: {x: 1.7249, y: 0.0224, z: 1.0110},
dynamic: true,
gravity: {x: 0, y: -1, z: 0},
velocity: {x: 0, y: -0.1, z: 0},
textures: JSON.stringify({Card: texture}),
position: getCardPosition(cardNumber, offset),
rotation: Quat.multiply(MyAvatar.orientation, rotation),
ignoreForCollisions: false,
shapeType: "box",
};
}
// Get card position function
function getCardPosition(cardNumber, offset){
return Vec3.sum(
MyAvatar.position,
Vec3.multiplyQbyV(MyAvatar.orientation, {
x: - CARD_HORIZONTAL_OFFSET + (cardNumber * CARD_HORIZONTAL_OFFSET) + (stackHeight * 0.1),
y: 0.0,
z: - offset
}
));
}
// Create deal player hand function
function dealPlayerHand(){
dealHandEntity = Entities.addEntity(getCardProperties(-1, DEAL_TEXTURE, NORMAL_ROTATION, PLAYER_DISTANCE));
}
// Create hit me function
function hitMe(){
hitMeEntity = Entities.addEntity(getCardProperties(-1, HIT_ME_TEXTURE, NORMAL_ROTATION, PLAYER_DISTANCE));
}
// Create stay function
function stay(){
stayEntity = Entities.addEntity(getCardProperties(0, STAY_TEXTURE, NORMAL_ROTATION, PLAYER_DISTANCE));
}
// Create you won function
function youWon(){
youWonEntity = Entities.addEntity(getCardProperties(0, YOU_WON_TEXTURE, NORMAL_ROTATION, DEALER_DISTANCE));
playGameSound(winSound);
}
// Create you lost function
function youLost(){
youLostEntity = Entities.addEntity(getCardProperties(0, YOU_LOST_TEXTURE, NORMAL_ROTATION, DEALER_DISTANCE));
playGameSound(loseSound);
}
// Create check play again function
function checkPlayAgain(){
playAgainEntity = Entities.addEntity(getCardProperties(-1, PLAY_AGAIN_TEXTURE, NORMAL_ROTATION, PLAYER_DISTANCE));
}
// Create check quit function
function checkQuit(){
quitEntity = Entities.addEntity(getCardProperties(0, QUIT_TEXTURE, NORMAL_ROTATION, PLAYER_DISTANCE));
}
// Delete game cards from world before the deal of each game
function deleteCardsBeforeDealOrExit(){
rezzedEntities.forEach(function(entity){
Entities.deleteEntity(entity);
});
Entities.deleteEntity(dealHandEntity);
Entities.deleteEntity(hitMeEntity);
Entities.deleteEntity(stayEntity);
if(youLostEntity){
Entities.deleteEntity(youLostEntity);
}
if(youWonEntity){
Entities.deleteEntity(youWonEntity);
}
Entities.deleteEntity(playAgainEntity);
Entities.deleteEntity(quitEntity);
}
// Delete game cards from world after the deal of each game
function deleteCardsAfterDeal(){
Entities.deleteEntity(dealHandEntity);
}
// Delete game cards from world if hit me selected
function deleteCardsIfHitMe(){
Entities.deleteEntity(stayEntity);
}
// Delete game cards from world if stay selected
function deleteCardsIfStay(){
Entities.deleteEntity(hitMeEntity);
}
// Delete game cards from world if end of game
function deleteCardsIfEndOfGame(){
Entities.deleteEntity(hitMeEntity);
Entities.deleteEntity(stayEntity);
}
// Draw card function
function drawCard(gameSound, cardNumber, rotation, offset){
var selection = 0;
do{
selection = randomCardNumber();
} while(selection == excludeCard);
playGameSound(gameSound);
newCardPath = CARD_PATH + CARDS[selection].filename;
cardEntity = Entities.addEntity(getCardProperties(cardNumber, newCardPath, rotation, offset));
rezzedEntities.push(cardEntity);
return{entity: cardEntity, number: selection};
}
// Create play game sound function
function playGameSound(gameSound){
return Audio.playSound(gameSound,{
position: getCardPosition(0),
volume: 10,
loop: false
});
}
// Create check player score on deal function
function checkPlayerScoreOnDeal(firstSelection){
print(CARDS[firstSelection].value);
playerScore = CARDS[firstSelection].value;
}
// Create check player score on hit function
function checkPlayerScoreOnHit(secondSelection){
print(CARDS[secondSelection].value);
playerScore += CARDS[secondSelection].value;
return playerScore;
}
// Create check dealer score function
function checkDealerScore(firstSelection, secondSelection){
print(CARDS[firstSelection].value);
dealerScore = CARDS[firstSelection].value + CARDS[secondSelection].value;
}
// Create final score function
function finalScore(){
if(dealerScore > 21){
Entities.editEntity(cardEntity1, {rotation: NORMAL_ROTATION});
youWon();
} else if(playerScore > 21){
Entities.editEntity(cardEntity1, {rotation: NORMAL_ROTATION});
youLost();
} else if(dealerScore < playerScore){
Entities.editEntity(cardEntity1, {rotation: NORMAL_ROTATION});
youWon();
} else{
Entities.editEntity(cardEntity1, {rotation: NORMAL_ROTATION});
youLost();
}
checkPlayAgain();
checkQuit();
}
// Create playAgain function
function playAgain(){
deleteCardsBeforeDealOrExit();
dealPlayerHand();
}
// Create quit function
function quit(){
Script.stop();
}
// Run function at runtime
dealPlayerHand();
// Mouse PlayGame event triggering randomize logic and placement of card logic
Entities.clickReleaseOnEntity.connect(function(entityID, mouseEvent){
if(dealHandEntity == entityID){
deleteCardsBeforeDealOrExit();
drawnCard = drawCard(dealHandSound, 1, NORMAL_ROTATION, PLAYER_DISTANCE);
selection1 = drawnCard.number;
cardEntity1 = drawnCard.entity;
drawnCard = drawCard(dealHandSound, 1, FLIP_ROTATION, DEALER_DISTANCE);
selection3 = drawnCard.number;
cardEntity1 = drawnCard.entity;
drawnCard = drawCard(dealHandSound, 2, NORMAL_ROTATION, DEALER_DISTANCE);
selection4 = drawnCard.number;
cardEntity2 = drawnCard.entity;
deleteCardsAfterDeal();
hitMe();
stay();
checkPlayerScoreOnDeal(selection1, selection2);
//print("Player Score: " + playerScore);
checkDealerScore(selection3, selection4);
//print("Dealer Score: " + dealerScore);
}
if(hitMeEntity == entityID){
deleteCardsIfHitMe();
drawnCard = drawCard(hitMeSound, 2, NORMAL_ROTATION, PLAYER_DISTANCE);
selection2 = drawnCard.number;
cardEntity2 = drawnCard.entity;
stackHeight++;
hitAgain = checkPlayerScoreOnHit(selection2);
//print("Player Score: " + playerScore);
//print("Dealer Score: " + dealerScore);
if(hitAgain <= 21){
deleteCardsIfStay();
hitMe();
stay();
} else{
deleteCardsIfEndOfGame();
finalScore();
}
}
if(stayEntity == entityID){
deleteCardsIfStay();
//print("Player Score: " + playerScore);
//print("Dealer Score: " + dealerScore);
deleteCardsIfEndOfGame();
finalScore();
}
if(playAgainEntity == entityID){
playAgain();
}
if(quitEntity == entityID){
quit();
}
});
// Delete cards when exiting script
Script.scriptEnding.connect(deleteCardsBeforeDealOrExit);