107 lines
3.5 KiB
JavaScript
107 lines
3.5 KiB
JavaScript
|
|
/*
|
|
|
|
Minigolf Cup sound player.
|
|
|
|
by Piper.Peppercorn October 2016
|
|
|
|
Plays a sound when a ball falls into the cup.
|
|
|
|
Then prepares the ball to self-delete.
|
|
|
|
Handles multiple balls if more than one player is active.
|
|
|
|
Distributed under the CreativeCommons license
|
|
|
|
*/
|
|
|
|
|
|
(function(){
|
|
const BALL_IN_CUP_SOUND_URL = "http://hifi-content.s3.amazonaws.com/caitlyn/production/minigolf%20course/mg_ball_in_cup_sound.wav";
|
|
const VICTORY_SOUND_URL = "http://hifi-content.s3.amazonaws.com/caitlyn/production/minigolf%20course/victory2.wav";
|
|
const SOUND_VOLUME = 1.0; // Range of 0.0 silent to 1.0 full volume.
|
|
const ONE_SECOND = 1000;
|
|
const INTERVAL = ONE_SECOND * 0.5;
|
|
const ZERO_GRAVITY = {"x": 0.0, "y": 0.0, "z": 0.0}
|
|
const CUP_VERTICAL_POSITION_ADJUSTMENT = 0.015; // Ensure balls don't trigger until they have fallen into the cup.
|
|
const DYNAMIC = 1;
|
|
const STATIC = 0;
|
|
var _this = this;
|
|
var timer;
|
|
var sensorBox;
|
|
|
|
|
|
var main = function() {
|
|
var validEntities = getValidEntitiesInSensorBox(sensorBox, "Sphere", DYNAMIC);
|
|
while (validEntities.length > 0) {
|
|
var entity = validEntities.pop();
|
|
triggerSoundAtEntity(
|
|
_this.BALL_SOUND,
|
|
SOUND_VOLUME,
|
|
entity.position
|
|
);
|
|
triggerSoundAtEntity(
|
|
_this.VICTORY_SOUND,
|
|
SOUND_VOLUME,
|
|
entity.position
|
|
);
|
|
// Deactivate the ball set to vanish after one minute.
|
|
deactivateBall(entity);
|
|
}
|
|
};
|
|
|
|
// Helper functions
|
|
|
|
var getValidEntitiesInSensorBox = function(sensor, entityType, dynamicState) {
|
|
var entitiesInBox = Entities.findEntitiesInBox(sensor.corner, sensor.size);
|
|
var validated = [];
|
|
for (var ent in entitiesInBox) {
|
|
var currentEntity = Entities.getEntityProperties(entitiesInBox[ent]);
|
|
if (currentEntity.type == entityType && currentEntity.dynamic == dynamicState &&
|
|
(currentEntity.clientOnly == 0 || currentEntity.owningAvatarID == MyAvatar.sessionUUID)) {
|
|
validated.push(currentEntity);
|
|
}
|
|
}
|
|
return validated;
|
|
};
|
|
|
|
|
|
var setupSensorBox = function(objectID) {
|
|
var boxProperties = Entities.getEntityProperties(objectID);
|
|
var boxDimensions = boxProperties.dimensions;
|
|
var pos = boxProperties.position;
|
|
var boxCorner = {
|
|
x: pos.x - (boxDimensions.x * 0.5),
|
|
y: pos.y - (boxDimensions.y * 0.5) - CUP_VERTICAL_POSITION_ADJUSTMENT,
|
|
z: pos.z - (boxDimensions.z * 0.5)
|
|
};
|
|
return {"corner": boxCorner, "size": boxDimensions};
|
|
};
|
|
|
|
|
|
var deactivateBall = function(ballEntity) {
|
|
var newProps = {
|
|
gravity: ZERO_GRAVITY,
|
|
dynamic: STATIC,
|
|
lifetime: ballEntity.age + ONE_SECOND
|
|
};
|
|
Entities.editEntity(ballEntity.id, newProps);
|
|
};
|
|
|
|
|
|
var triggerSoundAtEntity = function(sound, soundVolume, entityPosition) {
|
|
var soundProps = {
|
|
volume: soundVolume,
|
|
position: entityPosition
|
|
};
|
|
Audio.playSound(sound, soundProps);
|
|
};
|
|
|
|
|
|
_this.preload = function(ID) {
|
|
_this.BALL_SOUND = SoundCache.getSound(BALL_IN_CUP_SOUND_URL);
|
|
_this.VICTORY_SOUND = SoundCache.getSound(VICTORY_SOUND_URL);
|
|
sensorBox = setupSensorBox(ID);
|
|
timer = Script.setInterval(main, INTERVAL);
|
|
};
|
|
})
|