overte-lubosz/unpublishedScripts/DomainContent/CellScience/Scripts/zoomAndMoveRandomly.js
James B. Pollack f0d8ffce65 cleanup errors
2016-02-22 10:25:56 -08:00

179 lines
No EOL
5.3 KiB
JavaScript

// Copyright 2016 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 teleport;
var portalDestination;
var animationURL;
this.entered = true;
Script.include('virtualBaton.js');
var self = this;
var baton;
var iOwn = false;
var currentInterval;
var _entityId;
function startUpdate() {
iOwn = true;
print('i am the owner ' + _entityId)
}
function stopUpdateAndReclaim() {
print('i released the object ' + _entityId)
iOwn = false;
baton.claim(startUpdate, stopUpdateAndReclaim);
}
this.preload = function(entityID) {
this.entityId = entityID;
_entityId = entityID;
this.initialize(entityID);
this.initTimeout = null;
this.minVelocity = 1;
this.maxVelocity = 5;
this.minAngularVelocity = 0.01;
this.maxAngularVelocity = 0.03;
baton = virtualBaton({
batonName: 'io.highfidelity.vesicles:' + entityID, // One winner for each entity
});
stopUpdateAndReclaim();
currentInterval = Script.setInterval(self.move, self.getTotalWait())
}
this.initialize = function(entityID) {
// print(' should initialize')
var properties = Entities.getEntityProperties(entityID);
if (properties.userData.length === 0 || properties.hasOwnProperty('userData') === false) {
self.initTimeout = Script.setTimeout(function() {
// print(' no user data yet, try again in one second')
self.initialize(entityID);
}, 1000)
} else {
// print(' has userData')
self.portalDestination = properties.userData;
animationURL = properties.modelURL;
self.soundOptions = {
stereo: true,
loop: false,
localOnly: false,
position: properties.position,
volume: 0.5
};
self.teleportSound = SoundCache.getSound("https://hifi-content.s3.amazonaws.com/DomainContent/CellScience/Audio/whoosh.wav");
// print(" portal destination is " + self.portalDestination);
}
}
this.enterEntity = function(entityID) {
//print('ENTERED A BOUNDARY ENTITY, SHOULD ZOOM', entityID)
var data = JSON.parse(Entities.getEntityProperties(this.entityId).userData);
//print('DATA IS::' + data)
if (data != null) {
print("Teleporting to (" + data.location.x + ", " + data.location.y + ", " + data.location.z + ")");
MyAvatar.position = data.location;
// if (data.hasOwnProperty('entryPoint') && data.hasOwnProperty('target')) {
// this.lookAtTarget(data.entryPoint, data.target);
// }
// else{
// }
}
}
this.lookAtTarget = function(entryPoint, target) {
//print('SHOULD LOOK AT TARGET')
var direction = Vec3.normalize(Vec3.subtract(entryPoint, target));
var pitch = Quat.angleAxis(Math.asin(-direction.y) * 180.0 / Math.PI, {
x: 1,
y: 0,
z: 0
});
var yaw = Quat.angleAxis(Math.atan2(direction.x, direction.z) * 180.0 / Math.PI, {
x: 0,
y: 1,
z: 0
});
MyAvatar.goToLocation(entryPoint, true, yaw);
MyAvatar.headYaw = 0;
}
this.leaveEntity = function(entityID) {
Entities.editEntity(entityID, {
animationURL: animationURL,
animationSettings: '{ "frameIndex": 1, "running": false }'
});
this.entered = false;
if (this.initTimeout !== null) {
Script.clearTimeout(this.initTimeout);
}
//playSound();
}
this.unload = function() {
if (this.initTimeout !== null) {
Script.clearTimeout(this.initTimeout);
}
if (baton) {
baton.release(function() {});
}
Script.clearInterval(currentInterval);
}
this.hoverEnterEntity = function(entityID) {
Entities.editEntity(entityID, {
animationURL: animationURL,
animationSettings: '{ "fps": 24, "firstFrame": 1, "lastFrame": 25, "frameIndex": 1, "running": true, "hold": true }'
});
}
this.getTotalWait = function() {
return (Math.random() * 5000) * 2;
}
this.move = function() {
if (!iOwn) {
return;
}
var magnitudeV = self.maxVelocity;
var directionV = {
x: Math.random() - 0.5,
y: Math.random() - 0.5,
z: Math.random() - 0.5
};
//print("POS magnitude is " + magnitudeV + " and direction is " + directionV.x);
var magnitudeAV = self.maxAngularVelocity;
var directionAV = {
x: Math.random() - 0.5,
y: Math.random() - 0.5,
z: Math.random() - 0.5
};
//print("ROT magnitude is " + magnitudeAV + " and direction is " + directionAV.x);
Entities.editEntity(self.entityId, {
velocity: Vec3.multiply(magnitudeV, Vec3.normalize(directionV)),
angularVelocity: Vec3.multiply(magnitudeAV, Vec3.normalize(directionAV))
});
}
})