// 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 (function() { var OUT_OF_BOUNDS_SOUND = SoundCache.getSound(Script.resolvePath('assets/sounds/out.wav')); var SEARCH_RADIUS = 200; var LOOK_FOR_BALL_AGAIN = 1000; var GOAL_CHECKING_INTERVAL = 100; var _this; var audioVolume = 0.4; var injector; var scoreBoardPosition; var goalNet; var field; var fieldCenterPosition; var checkingInterval; var ball; var out = 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.18; } }); checkingInterval = Script.setInterval(function() { if (!ball) { _this.findBall(); } else { Script.clearInterval(checkingInterval); _this.beginCheckingForOuts(); } }, LOOK_FOR_BALL_AGAIN); }, beginCheckingForOuts: function() { checkingInterval = Script.setInterval(function() { if (!_this.isBallInsideZone() && !out) { out = true; Entities.editEntity(ball, { velocity: {x:0, y:0, z: 0} }); // print("OUT OF BOUNDS...wait to resume game"); _this.playSound(OUT_OF_BOUNDS_SOUND, 1, 0); } else if (_this.isBallInsideZone() && out) { // print("BALL IS BACK AFTER OUT...GAME ON"); out = 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; } }); }, 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; }, 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); } } }; return new Goal(); });