content/hifi-content/liv/dev/Bingo/bingoClear.js
2022-02-14 02:04:11 +01:00

223 lines
8.8 KiB
JavaScript

//
// bingoClear.js
//
// created by Rebecca Stankus on 10/09/18
// Copyright 2018 High Fidelity, Inc.
//
// Distributed under the Apache License, Version 2.0.
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
//
(function() {
var WHITE = { red: 255, green: 255, blue: 255 };
var BLACK = { red: 0, green: 0, blue: 0 };
var SQUARE_YELLOW = { blue: 0, green: 167, red: 179 };
var SQUARE_BLUE = { blue: 237, green: 68, red: 26 };
var SQUARE_GREEN = { blue: 43, green: 161, red: 27 };
var SQUARE_PINK = { blue: 97, green: 0, red: 158 };
var CARD_YELLOW = { blue: 66, green: 255, red: 227 };
var CARD_BLUE = { blue: 247, green: 196, red: 0 };
var CARD_GREEN = { blue: 0, green: 255, red: 30 };
var CARD_PINK = { blue: 119, green: 0, red: 255 };
var WAIT_FOR_ELEMENTS_TO_LOAD = 300;
var CLEAR_SOUND = SoundCache.getSound(Script.resolvePath("assets/sounds/bingoClear.wav?02"));
var CLEAR_CARD_AUDIO_VOLUME = 0.3;
var _this;
var headerSquares = [];
var numberSquares = [];
var winParticleEffects = [];
var bingoCard;
var headerSounds = [];
var currentHeaderSounds = [];
function contains(array, searchTarget) {
for (var index = 0; index < array.length; index++) {
if (array[index] === searchTarget) {
return true;
}
}
return false;
}
var BingoClear = function() {
_this = this;
};
BingoClear.prototype = {
preload: function(entityID){
_this.entityID = entityID;
bingoCard = Entities.getEntityProperties(_this.entityID, 'parentID').parentID;
Script.setTimeout(function() {
_this.getSquares();
if (numberSquares.length !== 25) {
print("error finding all 25 number squares. only found ", numberSquares.length);
}
}, WAIT_FOR_ELEMENTS_TO_LOAD);
headerSounds.push("assets/sounds/bingoCat.wav");
headerSounds.push("assets/sounds/bingoDog.wav");
headerSounds.push("assets/sounds/bingoPlinks.wav");
headerSounds.push("assets/sounds/bingoPianoPlinks.wav");
headerSounds.push("assets/sounds/bingoBomb.wav?009");
headerSounds.push("assets/sounds/bingoSqueak.wav");
headerSounds.push("assets/sounds/bingoChatter.wav");
headerSounds.push("assets/sounds/bingoClang.wav");
headerSounds.push("assets/sounds/bingoGiggles.wav");
headerSounds.push("assets/sounds/bingoSplash.wav");
headerSounds.push("assets/sounds/bingoWaHaHa.wav");
headerSounds.push("assets/sounds/bingoFall.wav");
headerSounds.push("assets/sounds/bingoTwinkle.wav");
headerSounds.push("assets/sounds/bingoPopUp.wav");
headerSounds.push("assets/sounds/bingoWhistle.wav");
headerSounds.push("assets/sounds/bingoBubbles.wav");
headerSounds.push("assets/sounds/bingoHarp.wav");
headerSounds.push("assets/sounds/bingoKazoo.wav");
headerSounds.push("assets/sounds/bingoVibration.wav008");
headerSounds.push("assets/sounds/bingoSpiral.wav");
},
getSquares: function() {
Entities.getChildrenIDs(bingoCard).forEach(function(bingoCardElement) {
var cardElementName = Entities.getEntityProperties(bingoCardElement, 'name').name;
if (cardElementName.indexOf("Bingo Square") !== -1) {
numberSquares.push(bingoCardElement);
// print("found a number square");
} else if (cardElementName.indexOf("Bingo Header") !== -1) {
headerSquares.push(bingoCardElement);
} else if (cardElementName.indexOf("Particle")) {
winParticleEffects.push(bingoCardElement);
}
});
},
playSound: function(sound) {
if (sound.downloaded) {
// print("sound");
Audio.playSound(sound, {
position: Entities.getEntityProperties(_this.entityID, 'position').position,
volume: 1
});
}
},
clearCard: function() {
// print("clear card");
_this.playSound(CLEAR_SOUND, CLEAR_CARD_AUDIO_VOLUME);
winParticleEffects.forEach(function(particleEffect){
Entities.editEntity(particleEffect, { locked: false });
Entities.editEntity(particleEffect, { isEmitting: false });
Entities.editEntity(particleEffect, { locked: true });
});
numberSquares.forEach(function(numberSquare) {
Entities.editEntity(numberSquare, {
locked: false
});
Entities.editEntity(numberSquare, {
backgroundColor: WHITE,
textColor: BLACK
});
Entities.editEntity(numberSquare, {
locked: true
});
});
currentHeaderSounds = [];
headerSquares.forEach(function(headerSquare) {
var newSound = _this.getRandomSound();
// print(newSound);
// print("current sounds are ", JSON.stringify(currentHeaderSounds));
if (contains(currentHeaderSounds, newSound)) {
newSound = _this.getRandomSound();
}
print("sending sound ", newSound);
currentHeaderSounds.push(newSound);
var params = [];
params[0] = newSound;
Entities.callEntityMethod(headerSquare, 'setSound', params);
Entities.editEntity(headerSquare, {
locked: false
});
Entities.editEntity(headerSquare, {
backgroundColor: _this.getRandomSquareColor()
});
Entities.editEntity(headerSquare, {
locked: true
});
});
Entities.editEntity(_this.entityID, {
locked: false
});
Entities.editEntity(_this.entityID, {
backgroundColor: _this.getRandomSquareColor()
});
Entities.editEntity(_this.entityID, {
locked: true
});
Entities.editEntity(bingoCard, {
color: _this.getRandomCardColor()
});
},
getRandomSound: function() {
var newSoundIndex = Math.floor(Math.random() * 20);
var newSound = headerSounds[newSoundIndex];
return newSound;
},
getRandomSquareColor: function() {
var colorChange = Math.floor(Math.random() * 4);
var newColor;
switch (colorChange) {
case 0:
newColor = SQUARE_YELLOW;
break;
case 1:
newColor = SQUARE_BLUE;
break;
case 2:
newColor = SQUARE_GREEN;
break;
case 3:
newColor = SQUARE_PINK;
break;
default:
newColor = SQUARE_PINK;
}
return newColor;
},
getRandomCardColor: function() {
var colorChange = Math.floor(Math.random() * 4);
var newColor;
switch (colorChange) {
case 0:
newColor = CARD_YELLOW;
break;
case 1:
newColor = CARD_BLUE;
break;
case 2:
newColor = CARD_GREEN;
break;
case 3:
newColor = CARD_PINK;
break;
default:
newColor = CARD_PINK;
}
return newColor;
},
mousePressOnEntity: function(entityID, mouseEvent) {
if (mouseEvent.isLeftButton) {
// print("My id is ", MyAvatar.sessionUUID, " and the parentID of the card is ", Entities.getEntityProperties(bingoCard, 'parentID').parentID);
var bingoCardParent = Entities.getEntityProperties(bingoCard, 'parentID').parentID;
if (bingoCardParent === "{00000000-0000-0000-0000-000000000001}" || MyAvatar.sessionUUID === bingoCardParent) {
this.clearCard();
}
}
}
};
return new BingoClear();
});