content/hifi-content/caitlyn/production/observatory-button/observatoryButtonServerEntity.js
2022-02-13 22:19:19 +01:00

98 lines
No EOL
2.5 KiB
JavaScript

(function(){
var sound = SoundCache.getSound("https://hifi-content.s3.amazonaws.com/caitlyn/production/observatory-button/ServoShrt.wav");
var playing = false;
var howLong = 1000*10;
Messages.subscribe("com.realitybloc.observatory");
var observatoryButtonEntityID = undefined;
this.preload = function(entityID) {
observatoryButtonEntityID = entityID;
}
function onMessageReceived(chan, msg) {
if (chan!="com.realitybloc.observatory") return;
if (msg!="pressed") return;
if (playing==true) return;
playing = true;
// get observatory
var observatoryEntityID = Entities.findEntitiesByName(
"Observatory", {x:0,y:0,z:0}, 1000000
)[0];
if (!observatoryEntityID) return;
var observatoryEntity = Entities.getEntityProperties(observatoryEntityID, ["position", "parentID"]);
// do animation on observatory button
Entities.editEntity(observatoryButtonEntityID, {
animation: {
frame: 0,
running: true,
loop: false,
}
});
// tell clients its playing
Messages.sendMessage("com.realitybloc.observatory", "playing");
// play sound
Audio.playSound(sound, {
position: observatoryEntity.position,
volume: 0.7
});
// make light entity
var lightEntityID = Entities.addEntity({
type: "Light",
parentID: observatoryEntity.parentID,
position: Vec3.sum(
observatoryEntity.position,
{x:0, y:-2, z:0}
),
rotation: Quat.fromPitchYawRollDegrees(45, 0, 0),
dimensions: {x:20, y:20, z:20},
color: {r:183, g:189, b:153},
intensity: 100,
falloffRadius: 2,
isSpotlight: true,
exponent: 1,
cutoff: 60,
});
// rotate light entity
var dateNow = Date.now()
var interval = Script.setInterval(function() {
Entities.editEntity(lightEntityID, {
rotation: (
Quat.fromPitchYawRollDegrees(
45, (Date.now()-dateNow)*-0.075, 0
)
)
});
}, 1000/90);
// clean up
Script.setTimeout(function() {
Script.clearInterval(interval);
Entities.deleteEntity(lightEntityID);
if (observatoryButtonEntityID) {
// stop animation
Entities.editEntity(observatoryButtonEntityID, {
animation: { running: false }
});
}
playing = false;
}, howLong);
}
// init and cleanup
Messages.messageReceived.connect(onMessageReceived);
// Script.setTimeout(function() {
// console.log("unloading");
// Messages.messageReceived.disconnect(onMessageReceived);
// Messages.unsubscribe("com.realitybloc.observatory");
// }, 1000*30);
})