// // bingoWheel.js // // Created by Rebecca Stankus on 10/16/2018 // Copyright High Fidelity 2018 // // Licensed under the Apache 2.0 License // See accompanying license file or http://apache.org/ // /* global Avatar, AccountServices */ (function() { var ANGULAR_VELOCITY = { x: 0, y: 0, z: -10 }; var ANGULAR_VELOCITY_CHECK_MS = 100; var CHECKING_INTERVAL_DELAY = 100; var USERS_ALLOWED_TO_SPIN_WHEEL = ['markb', 'ryan', 'Becky', 'MissLiviRose', 'Firebird25', 'Alan_', 'philip']; var BLIP_SOUND = SoundCache.getSound(Script.resolvePath('assets/sounds/blip.wav')); var SEARCH_RADIUS = 200; var _this; var audioVolume = 0.4; var injector; var bingoSquares = []; var bingoWallLights = []; var listCounter = 0; var angularVelocityDecrement = 0.5; var position; function shuffle(array) { var currentIndex = array.length, temporaryValue, randomIndex; while (currentIndex !== 0) { randomIndex = Math.floor(Math.random() * currentIndex); currentIndex -= 1; temporaryValue = array[currentIndex]; array[currentIndex] = array[randomIndex]; array[randomIndex] = temporaryValue; } return array; } var Wheel = function() { _this = this; }; Wheel.prototype = { interval: null, angularVelocityLimit: 10, nameText: null, remotelyCallable: ['reset'], preload: function(entityID) { _this.entityID = entityID; _this.getLights(); _this.reset(); position = Entities.getEntityProperties(_this.entityID, 'position').position; }, getLights: function() { Entities.findEntities(position, SEARCH_RADIUS).forEach(function(nearbyEntity) { var name = Entities.getEntityProperties(nearbyEntity, 'name').name; if (name.indexOf("Bingo Wall Light") !== -1) { var lightNumber = name.substring(17, name.length); // print(lightNumber); bingoWallLights[lightNumber] = nearbyEntity; } }); // print("found ", bingoWallLights.length, " number lights"); // bingoWallLights.forEach(function(light) { // print(Entities.getEntityProperties(light, 'name').name); // }); }, reset: function() { // print("resetting wheel"); var i = 1; while (i < 16) { bingoSquares.push("B " + String(i)); i++; } while (i < 31) { bingoSquares.push("I " + String(i)); i++; } while (i < 46) { bingoSquares.push("N " + String(i)); i++; } while (i < 61) { bingoSquares.push("G " + String(i)); i++; } while (i < 76) { bingoSquares.push("O " + String(i)); i++; } // print(JSON.stringify(bingoSquares)); shuffle(bingoSquares); bingoWallLights.forEach(function(light) { Entities.editEntity(light, { locked: false }); Entities.editEntity(light, { visible: false }); Entities.editEntity(light, { locked: true }); }); var position = Entities.getEntityProperties(_this.entityID, 'position').position; var bingoNumberTexts = Entities.findEntitiesByName("Bingo Wheel Number", position, 1); Entities.editEntity(bingoNumberTexts[0], { locked: false }); Entities.editEntity(bingoNumberTexts[0], { text: "BINGO", lineHeight: 1.1 }); Entities.editEntity(bingoNumberTexts[0], { locked: true }); // print(JSON.stringify(bingoSquares)); }, // startFarTrigger: function() { // print("trigger"); // _this.mousePressOnEntity; // }, mousePressOnEntity: function(entityID, mouseEvent) { if (!mouseEvent.button === "Primary") { return; } var position = Entities.getEntityProperties(_this.entityID, 'position').position; var bingoNumberTexts = Entities.findEntitiesByName("Bingo Wheel Number", position, 1); if (USERS_ALLOWED_TO_SPIN_WHEEL.indexOf(AccountServices.username) >= 0) { _this.angularVelocityLimit = -10; Entities.editEntity(_this.entityID, { angularVelocity: ANGULAR_VELOCITY }); Script.setTimeout(function() { if (_this.interval) { Script.clearInterval(_this.interval); } var finalNumber = false; var bingoCall; var callNumber; _this.interval = Script.setInterval(function() { var currentAngularVelocity = Entities.getEntityProperties( _this.entityID, 'angularVelocity').angularVelocity; // print(JSON.stringify(currentAngularVelocity.z)); if (currentAngularVelocity.z >= _this.angularVelocityLimit && currentAngularVelocity.z < 0) { Entities.editEntity(bingoNumberTexts[0], { locked: false }); // print("bingo squares number at ", listCounter, " is ", bingoSquares[listCounter]); Entities.editEntity(bingoNumberTexts[0], { text: bingoSquares[listCounter], lineHeight: 1.58 }); Entities.editEntity(bingoNumberTexts[0], { locked: true }); listCounter++; listCounter = listCounter >= bingoSquares.length ? 0 : listCounter; angularVelocityDecrement *= 1.001; _this.angularVelocityLimit += angularVelocityDecrement; } else if (currentAngularVelocity.z >= -0.1 && !finalNumber) { finalNumber = true; // print("current z velocity is ", JSON.stringify(currentAngularVelocity.z), " greater than -0.1"); bingoCall = bingoSquares.pop(); callNumber = bingoCall.substring(2, bingoCall.length); // print("call is ", bingoCall); Entities.editEntity(bingoNumberTexts[0], { locked: false }); Entities.editEntity(bingoNumberTexts[0], { text: bingoCall }); Entities.editEntity(bingoNumberTexts[0], { locked: true }); } else if (currentAngularVelocity.z >= -0.05) { // print("current z velocity is ", JSON.stringify(currentAngularVelocity.z), " greater than -0.05"); Entities.editEntity(bingoWallLights[callNumber], { locked: false }); Entities.editEntity(bingoWallLights[callNumber], { visible: true }); Entities.editEntity(bingoWallLights[callNumber], { locked: true }); if (_this.interval) { // this print is purposely left as a backup way to see call via logs print("-------------BINGO CALL IS: ", bingoCall); _this.playSound(BLIP_SOUND); Script.clearInterval(_this.interval); } } }, ANGULAR_VELOCITY_CHECK_MS); }, CHECKING_INTERVAL_DELAY); } }, playSound: function(sound) { // print("sound"); if (sound.downloaded) { if (injector) { injector.stop(); } injector = Audio.playSound(sound, { position: position, volume: audioVolume }); } }, unload: function(entityID) { if (_this.interval) { Script.clearInterval(_this.interval); } } }; return new Wheel(); });