// // locationZoneClient.js // // Created by Robin Wilson on 2019-04-04 // Copyright 2019 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 request = Script.require('request').request; var REQUEST_URL = Script.require(Script.resolvePath('../secrets.json')).REQUEST_URL; var DEBUG = true; function onEnterEntity() { setZoneName(); var queryParamString = "type=setUserLocation"; queryParamString += "&username=" + AccountServices.username; queryParamString += "&location=" + zoneName; var uri = REQUEST_URL + "?" + queryParamString; if (DEBUG) { console.log("statusIndicator onEnterEntity: " + uri); } request({ uri: uri }, function (error, response) { if (error || !response || response.status !== "success") { console.error("Error with onEnterEntity: " + JSON.stringify(response)); } else { // successfully sent updateLocation if (DEBUG) { console.log("Entered statusIndicatorZone called: " + zoneName); } } }); } // Returns true if my avatar is inside the bouncer zone, false otherwise. var HALF = 0.5; function avatarIsInsideZone() { var properties = Entities.getEntityProperties(_entityID, ["position", "dimensions", "rotation"]); var position = properties.position; var dimensions = properties.dimensions; var avatarPosition = MyAvatar.position; var worldOffset = Vec3.subtract(avatarPosition, position); avatarPosition = Vec3.multiplyQbyV(Quat.inverse(properties.rotation), worldOffset); var minX = 0 - dimensions.x * HALF; var maxX = 0 + dimensions.x * HALF; var minY = 0 - dimensions.y * HALF; var maxY = 0 + dimensions.y * HALF; var minZ = 0 - dimensions.z * HALF; var maxZ = 0 + dimensions.z * HALF; if (avatarPosition.x >= minX && avatarPosition.x <= maxX && avatarPosition.y >= minY && avatarPosition.y <= maxY && avatarPosition.z >= minZ && avatarPosition.z <= maxZ) { if (DEBUG) { print("Avatar IS inside zone"); } return true; } else { if (DEBUG) { print("Avatar IS NOT in zone"); } return false; } } // Ensures that every avatar experiences the enterEntity method, even if they were already in the entity // when the script started. function avatarLoadedInsideZoneCheck() { function largestAxisVec(dimensions) { var max = Math.max(dimensions.x, dimensions.y, dimensions.z); return max; } var properties = Entities.getEntityProperties(_entityID, ["position", "dimensions"]); var largestDimension = largestAxisVec(properties.dimensions); var avatarsInRange = AvatarList.getAvatarsInRange(properties.position, largestDimension).filter(function(id) { return id === MyAvatar.sessionUUID; }); if (avatarsInRange.length > 0) { if (DEBUG) { print("Found avatar near zone"); } // do isInZone check if (avatarIsInsideZone()) { onEnterEntity(); } } } function setZoneName() { zoneName = Entities.getEntityProperties(entityID, ["name"]).name; } var zoneName; var entityID; function LocationZoneClient() { // blank } LocationZoneClient.prototype = { preload: function(id) { entityID = id; setZoneName(); avatarLoadedInsideZoneCheck(); }, enterEntity: enterEntity, unload: function() { } }; return new LocationZoneClient(); })();