// // crunch.js // Play a crunch sound and delete an entity when it is brought close to the head ("Eaten") // // Modified by: Elisa Lupin-Jimenez // Copyright High Fidelity 2018 // // Licensed under the Apache 2.0 License // See accompanying license file or http://apache.org/ // // All assets are under CC Attribution Non-Commerical // http://creativecommons.org/licenses/ // (function () { var TELEPORT_SOUND_VOLUME = 0.40; var teleportSound; var portalDestination; var position; var CHECK_RADIUS = 0.25; // meters var LIFETIME = 10; // seconds var GRAVITY = {x: 0, y: -9.8, z: 0}; var DEBUG = false; var _entityID; var _this; function PortalSphere() { return; } PortalSphere.prototype = { isInactive: true, playSound: function(entityID) { if (teleportSound.downloaded) { if (!position) { _this.getProps(entityID); } Audio.playSound(teleportSound, { position: position, volume: TELEPORT_SOUND_VOLUME, localOnly: true }); } }, getProps: function(entityID) { var properties = Entities.getEntityProperties(entityID); if (properties) { position = properties.position; portalDestination = JSON.parse(properties.userData).location; } }, startNearGrab: function(entityID) { if (DEBUG) { print("starting portal grab"); } var editJSON = { visible: true, lifetime: -1, dynamic: true, collisionless: true, gravity: GRAVITY }; Entities.editEntity(entityID, editJSON); }, releaseGrab: function(entityID) { if (DEBUG) { print("releasing portal grab"); } var age = Entities.getEntityProperties(entityID, 'age').age; var editJSON = { lifetime: age + LIFETIME, collisionless: false }; Entities.editEntity(entityID, editJSON); }, checkIfNearHead: function() { if (_this.isInactive && HMD.active) { var position = Entities.getEntityProperties(_entityID, "position").position; var pillDistance = CHECK_RADIUS * MyAvatar.scale; if (Vec3.distance(position, MyAvatar.getJointPosition("Head")) < pillDistance || Vec3.distance(position, MyAvatar.getJointPosition("Neck")) < pillDistance) { if (DEBUG) { print("swallowing portal"); } _this.isInactive = false; _this.teleport(); } } }, teleport: function(entityID) { _this.getProps(entityID); if (portalDestination.length > 0) { _this.playSound(entityID); Window.location = "hifi://" + portalDestination; } }, mousePressOnEntity: function(entityID, mouseEvent) { if (mouseEvent.isLeftButton) { if (!HMD.active && _this.isInactive) { if (DEBUG) { print("portal has been clicked"); } _this.isInactive = false; _this.teleport(); } } }, preload: function(entityID) { _this = this; if (DEBUG) { print("loading new portal"); } try { teleportSound = SoundCache.getSound("http://s3.amazonaws.com/hifi-public/birarda/teleport.raw"); _this.getProps(entityID); } catch (err) { print("Could not retrieve sound URLs"); } Script.update.connect(_this.checkIfNearHead); _entityID = entityID; }, unload: function(entityID) { if (DEBUG) { print("unloading portal"); } Script.update.disconnect(_this.checkIfNearHead); } }; var self = new PortalSphere(); return self; });