// // bingoSquare.js // // created by Rebecca Stankus on 10/08/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 ROW_INDEX = 13; var COLUMN_INDEX = 15; var WAIT_FOR_ELEMENTS_TO_LOAD = 300; var WHITE = {red:255,green:255,blue:255}; // White needs to be in this format to match correctly var WHITE_STRING = JSON.stringify(WHITE); var BLACK = { red: 0, green: 0, blue: 0 }; var WIN_SOUND = SoundCache.getSound(Script.resolvePath("assets/sounds/bingoWin.wav")); var SELECT_SOUND = SoundCache.getSound(Script.resolvePath("assets/sounds/bingoSelect.wav")); var DESELECT_SOUND = SoundCache.getSound(Script.resolvePath("assets/sounds/bingoDeselect.wav")); var CARD_FLASH_INTERVAL = 200; var CARD_FLASHING_TIME = 1750; var WINNER_AUDIO_VOLUME = 1; var SELECT_AUDIO_VOLUME = 0.1; var _this; var markedSquareColor; var clearCardSquare; var bingoCard; var numberSquares = []; var row; var column; var winParticleEffects = []; var flashingInterval; var white = false; var cardColor; var BingoSquare = function() { _this = this; }; BingoSquare.prototype = { preload: function(entityID){ _this.entityID = entityID; var squareProperties = Entities.getEntityProperties(_this.entityID, ['name', 'parentID']); // print(squareProperties.name); bingoCard = squareProperties.parentID; row = squareProperties.name.charAt(ROW_INDEX); // print("row: ", row); column = squareProperties.name.charAt(COLUMN_INDEX); // print("column: ", column); Script.setTimeout(function() { _this.getSquares(); if (numberSquares.length !== 25) { print("error finding all 25 number squares"); } }, WAIT_FOR_ELEMENTS_TO_LOAD); }, getSquares: function() { Entities.getChildrenIDs(bingoCard).forEach(function(bingoCardElement) { var cardElementName = Entities.getEntityProperties(bingoCardElement, 'name').name; if (cardElementName === "Bingo Clear") { clearCardSquare = bingoCardElement; } else if (cardElementName.indexOf("Bingo Square") !== -1) { numberSquares.push(bingoCardElement); } else if (cardElementName.indexOf("Particle")) { winParticleEffects.push(bingoCardElement); } }); }, colorChange: function() { markedSquareColor = Entities.getEntityProperties(clearCardSquare, 'backgroundColor').backgroundColor; if (JSON.stringify(Entities.getEntityProperties(_this.entityID, 'backgroundColor').backgroundColor) === WHITE_STRING) { _this.playSound(SELECT_SOUND, SELECT_AUDIO_VOLUME); Entities.editEntity(_this.entityID, { locked: false }); Entities.editEntity(_this.entityID, { backgroundColor: markedSquareColor, textColor: WHITE }); Entities.editEntity(_this.entityID, { locked: true }); } else { _this.playSound(DESELECT_SOUND, SELECT_AUDIO_VOLUME); Entities.editEntity(_this.entityID, { locked: false }); Entities.editEntity(_this.entityID, { backgroundColor: WHITE, textColor: BLACK }); Entities.editEntity(_this.entityID, { locked: true }); } }, playSound: function(sound, volume) { if (sound.downloaded) { // print("sound"); Audio.playSound(sound, { position: Entities.getEntityProperties(_this.entityID, 'position').position, volume: volume }); } }, mousePressOnEntity: function(entityID, mouseEvent) { // print(JSON.stringify(mouseEvent)); if (mouseEvent.isLeftButton) { // print("the card is ", bingoCard); // print("I am ", MyAvatar.sessionUUID, " The card parent id is ", Entities.getEntityProperties(bingoCard, 'parentID').parentID); var bingoCardPArent = Entities.getEntityProperties(bingoCard, 'parentID').parentID; if (bingoCardPArent === "{00000000-0000-0000-0000-000000000001}" || MyAvatar.sessionUUID === bingoCardPArent) { // print("I am holding this card"); this.colorChange(); var rowWin = true; var columnWin = true; var diagonalTopLToBottomR = true; var diagonalTopRToBottomL = true; if (row !== column) { // print("row does not match column...no diag TL to BR"); diagonalTopLToBottomR = false; } if (((row === 2 || row === 4) && (column === 2 || column === 4)) || (row === 1 || row === 5) && (column === 1 || column === 5) || (row === 3 && column === 3)) { diagonalTopRToBottomL = false; } // print("this square is ", row, " ", column); numberSquares.forEach(function(bingoSquare) { // print("rowWin: ", rowWin, " columnWin: ", columnWin, " diag TL to BR: ", diagonalTopLToBottomR, " diag TR to BL: ", diagonalTopRToBottomL); // should I bother to skip/return if same square as this? var squareProperties = Entities.getEntityProperties(bingoSquare, ['name', 'backgroundColor']); var otherRow = squareProperties.name.charAt(ROW_INDEX); var otherColumn = squareProperties.name.charAt(COLUMN_INDEX); var otherBackgroundColorString = JSON.stringify(squareProperties.backgroundColor); // print("checking square ", otherRow, " ", otherColumn); // print("this square has background color ", JSON.stringify(squareProperties.backgroundColor)); if (otherRow === row && rowWin){ // print("this row is ", row, " and the other row is ", otherRow); if (otherBackgroundColorString === WHITE_STRING) { // print("square ", otherRow, " ", otherColumn, " has background that is white-no row win"); rowWin = false; } } if (otherColumn === column && columnWin){ if (otherBackgroundColorString === WHITE_STRING) { // print("square ", otherRow, " ", otherColumn, " has background that is white-no column win"); columnWin = false; } } if (diagonalTopLToBottomR) { // print("still checking for a diag win"); if (otherRow === otherColumn) { if (otherBackgroundColorString === WHITE_STRING) { // print("square ", otherRow, " ", otherColumn, " has background that is white-no diag win"); diagonalTopLToBottomR = false; } } } if (diagonalTopRToBottomL) { // print("checking square ", otherRow, " ", otherColumn, " for diag win"); if (((otherRow === "2" || otherRow === "4") && (otherColumn === "2" || otherColumn === "4") && (otherRow !== otherColumn)) || ((otherRow === "1" || otherRow === "5") && (otherColumn === "1" || otherColumn === "5") && (otherRow !== otherColumn)) || (otherRow === "3" && otherColumn === "3")) { // print("checking square ", otherRow, " ", otherColumn, " for diag win"); if (otherBackgroundColorString === WHITE_STRING) { // print("square ", otherRow, " ", otherColumn, " has background that is white-no diag win"); diagonalTopRToBottomL = false; } } } }); if (rowWin || columnWin || diagonalTopLToBottomR || diagonalTopRToBottomL) { // print("WINNER! row win is ", rowWin, " and column win is ", columnWin, "diag TR to BL is ", diagonalTopRToBottomL, " and diag TL to BR is ", diagonalTopLToBottomR); _this.playSound(WIN_SOUND, WINNER_AUDIO_VOLUME); winParticleEffects.forEach(function(particleEffect){ Entities.editEntity(particleEffect, { locked: false }); Entities.editEntity(particleEffect, { isEmitting: true }); Entities.editEntity(particleEffect, { locked: true }); }); cardColor = Entities.getEntityProperties(bingoCard, 'color').color; flashingInterval = Script.setInterval(function() { if (white) { Entities.editEntity(bingoCard, { color: cardColor }); white = false; } else { Entities.editEntity(bingoCard, { color: WHITE }); white = true; } }, CARD_FLASH_INTERVAL); Script.setTimeout(function() { if (flashingInterval){ Script.clearInterval(flashingInterval); } if (white) { Entities.editEntity(bingoCard, { color: cardColor }); white = false; } }, CARD_FLASHING_TIME); } else { winParticleEffects.forEach(function(particleEffect){ Entities.editEntity(particleEffect, { locked: false }); Entities.editEntity(particleEffect, { isEmitting: false }); Entities.editEntity(particleEffect, { locked: true }); }); } } } }, unload: function() { if (flashingInterval){ Script.clearInterval(flashingInterval); } if (white) { Entities.editEntity(bingoCard, { color: cardColor }); white = false; } } }; return new BingoSquare(); });