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

212 lines
9.2 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 = [];
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(SoundCache.getSound(Script.resolvePath("assets/sounds/bingoCat.wav")));
headerSounds.push(SoundCache.getSound(Script.resolvePath("assets/sounds/bingoDog.wav")));
headerSounds.push(SoundCache.getSound(Script.resolvePath("assets/sounds/bingoPlinks.wav")));
headerSounds.push(SoundCache.getSound(Script.resolvePath("assets/sounds/bingoPianoPlinks.wav")));
headerSounds.push(SoundCache.getSound(Script.resolvePath("assets/sounds/bingoBomb.wav")));
headerSounds.push(SoundCache.getSound(Script.resolvePath("assets/sounds/bingoSqueak.wav")));
headerSounds.push(SoundCache.getSound(Script.resolvePath("assets/sounds/bingoChatter.wav")));
headerSounds.push(SoundCache.getSound(Script.resolvePath("assets/sounds/bingoClang.wav")));
headerSounds.push(SoundCache.getSound(Script.resolvePath("assets/sounds/bingoGiggles.wav")));
headerSounds.push(SoundCache.getSound(Script.resolvePath("assets/sounds/bingoSplash.wav")));
headerSounds.push(SoundCache.getSound(Script.resolvePath("assets/sounds/bingoWaHaHa.wav")));
headerSounds.push(SoundCache.getSound(Script.resolvePath("assets/sounds/bingoFall.wav")));
headerSounds.push(SoundCache.getSound(Script.resolvePath("assets/sounds/bingoTwinkle.wav")));
headerSounds.push(SoundCache.getSound(Script.resolvePath("assets/sounds/bingoPopUp.wav")));
headerSounds.push(SoundCache.getSound(Script.resolvePath("assets/sounds/bingoWhistle.wav")));
headerSounds.push(SoundCache.getSound(Script.resolvePath("assets/sounds/bingoBubbles.wav")));
headerSounds.push(SoundCache.getSound(Script.resolvePath("assets/sounds/bingoHarp.wav")));
headerSounds.push(SoundCache.getSound(Script.resolvePath("assets/sounds/bingoKazoo.wav")));
headerSounds.push(SoundCache.getSound(Script.resolvePath("assets/sounds/bingoVibration.wav")));
headerSounds.push(SoundCache.getSound(Script.resolvePath("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();
while (headerSquares.indexOf(newSound !== -1)) {
newSound = _this.getRandomSound();
}
currentHeaderSounds.push(newSound);
var params = [];
params[0] = JSON.stringify(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) {
print("mouuse press");
if (mouseEvent.isLeftButton) {
// print("My id is ", MyAvatar.sessionUUID, " and the parentID of the card is ", Entities.getEntityProperties(card, 'parentID').parentID);
var bingoCardParent = Entities.getEntityProperties(bingoCard, 'parentID').parentID;
if (bingoCardParent === "{00000000-0000-0000-0000-000000000001}" || MyAvatar.sessionUUID === bingoCardParent) {
_this.clearCard();
}
}
}
};
return new BingoClear();
});