88 lines
3 KiB
JavaScript
88 lines
3 KiB
JavaScript
(function(){
|
|
var teleport;
|
|
var portalDestination;
|
|
var animationURL;
|
|
var position;
|
|
var rotation;
|
|
|
|
function isPositionInsideBox(position, boxProperties) {
|
|
var localPosition = Vec3.multiplyQbyV(Quat.inverse(boxProperties.rotation),
|
|
Vec3.subtract(MyAvatar.position, boxProperties.position));
|
|
var halfDimensions = Vec3.multiply(boxProperties.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;
|
|
}
|
|
|
|
function playSound(entityID) {
|
|
print("playing teleport sound");
|
|
if (teleport.downloaded) {
|
|
if (!position) {
|
|
getProps(entityID);
|
|
}
|
|
Audio.playSound(teleport, { position: position, volume: 0.40, localOnly: true });
|
|
}
|
|
}
|
|
|
|
function getProps(entityID) {
|
|
var properties = Entities.getEntityProperties(entityID);
|
|
if (properties) {
|
|
animationURL = properties.modelURL;
|
|
position = properties.position;
|
|
portalDestination = properties.userData;
|
|
rotation = properties.rotation;
|
|
}
|
|
}
|
|
|
|
function attemptTeleportation() {
|
|
if (portalDestination.length > 0) {
|
|
print("Teleporting to hifi://" + portalDestination);
|
|
Window.location = "hifi://" + portalDestination;
|
|
}
|
|
}
|
|
|
|
this.preload = function(entityID) {
|
|
print("loading teleport script");
|
|
teleport = SoundCache.getSound("http://s3.amazonaws.com/hifi-public/birarda/teleport.raw");
|
|
getProps(entityID);
|
|
if (isPositionInsideBox(MyAvatar.position, {position: position, rotation: rotation})) {
|
|
attemptTeleportation();
|
|
}
|
|
};
|
|
|
|
this.enterEntity = function(entityID) {
|
|
// get latest props in case we changed the destination
|
|
getProps(entityID);
|
|
print("enterEntity() .... The portal destination is " + portalDestination);
|
|
attemptTeleportation();
|
|
};
|
|
|
|
this.leaveEntity = function(entityID) {
|
|
print("leaveEntity() called ....");
|
|
if (!animationURL) {
|
|
getProps(entityID);
|
|
}
|
|
Entities.editEntity(entityID, {
|
|
animation: { url: animationURL, currentFrame: 1, running: false }
|
|
});
|
|
|
|
playSound(entityID);
|
|
};
|
|
|
|
this.hoverEnterEntity = function(entityID) {
|
|
print("hoverEnterEntity() called ....");
|
|
if (!animationURL) {
|
|
getProps(entityID);
|
|
}
|
|
Entities.editEntity(entityID, {
|
|
animation: { url: animationURL, fps: 24, firstFrame: 1, lastFrame: 25, currentFrame: 1, running: true, hold: true }
|
|
});
|
|
};
|
|
|
|
this.unload = function() {
|
|
print("unloading teleport script");
|
|
};
|
|
});
|