content/hifi-content/samuel/pokestop.js
2022-02-14 02:04:11 +01:00

93 lines
No EOL
3.4 KiB
JavaScript

//
// Created by Sam Gondelman on 8/10/16
// Copyright 2016 High Fidelity, Inc.
//
// This entity script handles the logic for controlling a pokestop
//
// Distributed under the Apache License, Version 2.0.
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
//
(function(){
Script.include('https://raw.githubusercontent.com/highfidelity/hifi/master/scripts/system/libraries/utils.js');
Script.include('https://raw.githubusercontent.com/highfidelity/hifi/5042561e80367ee2ebefa7d56728246e2d037e9d/scripts/developer/libraries/virtualBaton.js');
var SHADER_URL = "https://hifi-content.s3.amazonaws.com/samuel/pokestop.fs";
var SEARCH_RADIUS = 5.0;
var _this;
// Only one person should similate the pokestop at a time- so we pass a virtual baton
var baton;
var iOwn = false;
var connected = false;
Pokestop = function() {
_this = this;
};
function startUpdate() {
iOwn = true;
connected = true;
//_this.updatePokestopForOwner();
Script.update.connect(_this.update);
}
function stopUpdateAndReclaim() {
//when the baton is released;
iOwn = false;
if (connected === true) {
connected = false;
Script.update.disconnect(_this.update);
}
//hook up callbacks to the baton
baton.claim(startUpdate, stopUpdateAndReclaim);
}
Pokestop.prototype = {
//updatePokestopForOwner: function() {},
preload: function(entityID) {
_this.entityID = entityID;
// One winner for each entity
if (Entities.canRez() && Entities.canAdjustLocks) {
baton = virtualBaton({
batonName: "io.highfidelity.pokestop:" + _this.entityID
});
stopUpdateAndReclaim();
}
},
unload: function() {
baton.unload();
if (connected === true) {
connected = false;
Script.update.disconnect(_this.update);
}
},
update: function(deltaTime) {
if (iOwn === false) {
return;
}
_this.userData = getEntityUserData(_this.entityID);
var cubeTime = _this.userData.ProceduralEntity.uniforms.cubeTime;
var growTime = _this.userData.ProceduralEntity.uniforms.growTime;
var distTime = _this.userData.ProceduralEntity.uniforms.distTime;
if (AvatarList.isAvatarInRange(Entities.getEntityProperties(_this.entityID).position, SEARCH_RADIUS)) {
_this.userData.ProceduralEntity.uniforms.cubeTime = Math.min(0.25, cubeTime + deltaTime / 2.0);
_this.userData.ProceduralEntity.uniforms.growTime = Math.min(0.5, growTime + deltaTime / 2.0);
_this.userData.ProceduralEntity.uniforms.distTime += deltaTime;
} else {
_this.userData.ProceduralEntity.uniforms.cubeTime = Math.max(0.0, cubeTime - deltaTime);
_this.userData.ProceduralEntity.uniforms.growTime = Math.max(0.0, growTime - deltaTime);
_this.userData.ProceduralEntity.uniforms.distTime = Math.max(0.0, distTime - deltaTime);
}
setEntityUserData(_this.entityID, _this.userData);
}
};
return new Pokestop();
})