content/hifi-content/rebecca/NewYearCelebration/sparkler.js
2022-02-14 02:04:11 +01:00

223 lines
7.3 KiB
JavaScript

//
// sparkler.js
//
// created by Rebecca Stankus on 12/21/18
// Copyright 2018 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 _this;
var AUDIO_VOLUME_LEVEL = 0.25;
var SEARCHING_INTERVAL_MS = 100;
var EXTINGUISH_TIMER_MS = 60000;
var LIFETIME = 30;
var SPARKLER_SOUND = SoundCache.getSound(Script.resolvePath('assets/audio/sparkler.mp3'));
var injector;
var flame;
var light;
var dimensionsY;
var audioUpdate;
var clone = true;
var Sparkler = function() {
_this = this;
};
Sparkler.prototype = {
preload: function(entityID) {
_this.entityID = entityID;
var name = Entities.getEntityProperties(_this.entityID, 'name').name;
if (name.indexOf("clone") === -1) {
clone = false;
}
},
startNearGrab: function() {
Entities.editEntity(_this.entityID, { lifetime: -1 });
if (clone) {
_this.lightSparkler();
}
},
startDistanceGrab: function() {
Entities.editEntity(_this.entityID, { lifetime: -1 });
if (clone) {
_this.lightSparkler();
}
},
lightSparkler: function() {
if (flame) {
return;
}
if (light) {
Entities.deleteEntity(light);
light = null;
}
dimensionsY = Entities.getEntityProperties(_this.entityID, ['dimensions']).dimensions.y;
flame = Entities.addEntity({
type: "ParticleEffect",
parentID: _this.entityID,
localPosition: {x: 0, y: dimensionsY * 0.5, z: 0},
dimensions: { x: 1, y: 1, z: 1 },
localRotation: {
x: 0.34419792890548706,
y: -0.7092878222465515,
z: -0.08829883486032486,
w: 0.6088127493858337
},
grab: { grabbable: false},
name: "Sparkler Flame NEW",
isEmitting: true,
maxParticles: 10,
lifespan: 0.17000000178813934,
emitRate:86,
emitSpeed: 0,
speedSpread:0,
emitOrientation: {
x: -0.0000152587890625,
y: -0.0000152587890625,
z: -0.0000152587890625,
w: 1
},
emitDimensions: {x:0, y:0, z:0},
emitRadiusStart:0,
radiusFinish: 0.5,
radiusStart: 0.20000000298023224,
emitAcceleration:{
x: 0,
y: 0,
z: 0
},
particleRadius: 0.4000000059604645,
alphaStart:0,
alphaFinish:0,
emitterShouldTrail: 1,
textures: "https://www.textures.com/system/gallery/photos/FX/Fire/Fireworks/32261/Fireworks0019_1_download600.jpg",
colorStart: {
red: 0,
green: 0,
blue: 0
},
colorFinish: {
red: 0,
green: 0,
blue: 0
},
spinStart: 0,
spinFinish: 0,
spinSpread: 6.2831854820251465
}, true);
light = Entities.addEntity({
type: "Light",
parentID: _this.entityID,
dimensions: {
x: 2.8169918060302734,
y: 2.8169918060302734,
z: 2.8169918060302734
},
localRotation: {
x: 0.34419792890548706,
y: -0.7092878222465515,
z: -0.08829883486032486,
w: 0.6088127493858337
},
grab: { grabbable: false},
name: "Sparkler Light NEW",
intensity: 5,
falloffRadius: 1,
exponent: 1,
cutoff: 75
}, true);
_this.playSound();
audioUpdate = Script.setInterval(function() {
if (flame) {
_this.updateSoundPosition;
}
}, SEARCHING_INTERVAL_MS);
var name = Entities.getEntityProperties(_this.entityID, 'name').name;
if (name.indexOf("clone") !== -1) {
Script.setTimeout(function() {
Entities.deleteEntity(flame);
Entities.deleteEntity(light);
flame = null;
light = null;
if (injector) {
if (audioUpdate) {
Script.clearInterval(audioUpdate);
}
injector.stop();
}
}, EXTINGUISH_TIMER_MS);
}
},
updateSoundPosition: function() {
var newSoundPosition = Entities.getEntityProperties(flame, 'position').position;
if (injector) {
injector.options = { position: newSoundPosition };
}
},
releaseGrab: function() {
if (clone) {
if (flame) {
Entities.deleteEntity(flame);
flame = null;
}
if (light) {
Entities.deleteEntity(light);
light = null;
}
if (injector) {
if (audioUpdate) {
Script.clearInterval(audioUpdate);
}
injector.stop();
}
var age = Entities.getEntityProperties(_this.entityID, 'age').age;
Entities.editEntity(_this.entityID, { lifetime: age + LIFETIME });
}
},
playSound: function(buttonID, params) {
_this.flamePosition = Entities.getEntityProperties(flame, 'position').position;
if (SPARKLER_SOUND.downloaded) {
if (injector) {
if (audioUpdate) {
Script.clearInterval(audioUpdate);
}
injector.stop();
}
injector = Audio.playSound(SPARKLER_SOUND, {
position: _this.flamePosition,
volume: AUDIO_VOLUME_LEVEL
});
}
},
unload: function() {
if (injector) {
if (audioUpdate) {
Script.clearInterval(audioUpdate);
}
injector.stop();
}
if (flame) {
Entities.deleteEntity(flame);
}
if (light) {
Entities.deleteEntity(light);
}
this.releaseGrab();
}
};
return new Sparkler;
});