// soccerGoal.js // // Created for HiFi Hackathon on 10/26/2018 // // When the soccer ball enters a zone that is parented to the goal, it lights up the scoreboard, adds a sound effect and changes the score // // 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 SCORE_SOUND = SoundCache.getSound(Script.resolvePath('assets/sounds/score.wav')); var SEARCH_RADIUS = 200; var LIGHT_BLINK_INTERVAL = 500; var LOOK_FOR_BALL_AGAIN = 1000; var GOAL_CHECKING_INTERVAL = 100; var WHITE = {red:255,green:255,blue:255}; var RED = {red:255,green:0,blue:0}; var BLUE = {red:0,green:0,blue:255}; var _this; var audioVolume = 0.4; var injector; var scoreBoardPosition; var goalNet; var field; var fieldCenterPosition; var scoreBoard; var scoreBoardColor; var scoreText; var checkingInterval; var blinkingInterval; var ball; var goal = false; var Goal = function() { _this = this; }; Goal.prototype = { interval: null, angularVelocityLimit: 10, nameText: null, remotelyCallable: ['reset'], preload: function(entityID) { _this.entityID = entityID; goalNet = Entities.getEntityProperties(_this.entityID, 'parentID').parentID; field = Entities.getEntityProperties(goalNet, 'parentID').parentID; Entities.getChildrenIDs(field).forEach(function(childOfField) { var name = Entities.getEntityProperties(childOfField, 'name').name; if (name === "Soccer Middle Field Line") { print("FOUND CENTER FIELD MARKER"); fieldCenterPosition = Entities.getEntityProperties(childOfField, 'position').position; fieldCenterPosition.y += 0.45; } }); Entities.getChildrenIDs(goalNet).forEach(function(childOfGoalNet) { var name = Entities.getEntityProperties(childOfGoalNet, 'name').name; if (name === "Soccer Score") { scoreText = childOfGoalNet; } else if (name === "Soccer Score Backboard Blue") { scoreBoard = childOfGoalNet; scoreBoardColor = BLUE; } else if (name === "Soccer Score Backboard Red") { scoreBoard = childOfGoalNet; scoreBoardColor = RED; } }); scoreBoardPosition = Entities.getEntityProperties(_this.entityID, 'position').position; checkingInterval = Script.setInterval(function() { if (!ball) { _this.findBall(); } else { Script.clearInterval(checkingInterval); _this.beginCheckingForGoals(); } }, LOOK_FOR_BALL_AGAIN); }, beginCheckingForGoals: function() { checkingInterval = Script.setInterval(function() { if (_this.isBallInsideZone() && !goal) { goal = true; print("SCORE SCORE SCORE SCORE...cannot score now"); _this.updateScoreBoard(); } else if (!_this.isBallInsideZone() && goal) { print("BALL HAS LEFT ZONE>>>ABLE TO SCORE AGAIN"); goal = false; } }, GOAL_CHECKING_INTERVAL); }, findBall: function() { Entities.findEntities(scoreBoardPosition, SEARCH_RADIUS).forEach(function(entityWithin200MofScoreboard) { var name = Entities.getEntityProperties(entityWithin200MofScoreboard, 'name').name; if (name === "Soccer Ball") { ball = entityWithin200MofScoreboard; } }); }, reset: function() { // }, isBallInsideZone: function() { // print("CHECKING IF THE BALL IS IN THE GOAL"); var ballPosition = Entities.getEntityProperties(ball, 'position').position; var zoneProperties = Entities.getEntityProperties(_this.entityID, ["position", "dimensions", "rotation"]); var localPosition = Vec3.multiplyQbyV(Quat.inverse(zoneProperties.rotation), Vec3.subtract(ballPosition, zoneProperties.position)); var halfDimensions = Vec3.multiply(zoneProperties.dimensions, 0.5); return -halfDimensions.x <= localPosition.x && halfDimensions.x >= localPosition.x && -halfDimensions.y <= localPosition.y && halfDimensions.y >= localPosition.y && -halfDimensions.z <= localPosition.z && halfDimensions.z >= localPosition.z; }, updateScoreBoard: function() { _this.playSound(SCORE_SOUND, 1, 0); var currentScore = Entities.getEntityProperties(scoreText, 'text').text; currentScore++; var white = false; var blinks = 0; blinkingInterval = Script.setInterval(function() { blinks++; if (white) { Entities.editEntity(scoreBoard, { locked: false }); Entities.editEntity(scoreBoard, { color: scoreBoardColor }); Entities.editEntity(scoreBoard, { locked: true }); white = false; } else { Entities.editEntity(scoreBoard, { locked: false }); Entities.editEntity(scoreBoard, { color: WHITE }); Entities.editEntity(scoreBoard, { locked: true }); white = true; } if (blinks > 5) { Script.clearInterval(blinkingInterval); print("MOVING BALL TO CENTER OF FIELD AT ", JSON.stringify(fieldCenterPosition)); Entities.editEntity(ball, { position: fieldCenterPosition, velocity: {x:0, y:0, z:0} }); } }, LIGHT_BLINK_INTERVAL); Entities.editEntity(scoreText, { locked: false }); Entities.editEntity(scoreText, { text: currentScore }); Entities.editEntity(scoreText, { locked: true }); }, playSound: function(sound, volume, localOnly) { // print("sound"); if (sound.downloaded) { if (injector) { injector.stop(); } injector = Audio.playSound(sound, { position: scoreBoardPosition, volume: audioVolume }); } }, unload: function(entityID) { if (checkingInterval) { Script.clearInterval(checkingInterval); } if (blinkingInterval) { Script.clearInterval(blinkingInterval); Entities.editEntity(scoreBoard, { locked: false }); Entities.editEntity(scoreBoard, { color: scoreBoardColor }); Entities.editEntity(scoreBoard, { locked: true }); } } }; return new Goal(); });