Merge pull request #7424 from ericrius1/batonSoundEntityTest

Add a test case for playing a short sound from an entity using virtual baton
This commit is contained in:
James B. Pollack 2016-03-27 10:49:59 -07:00
commit 50cc632b9c
7 changed files with 310 additions and 1 deletions

View file

@ -0,0 +1,96 @@
(function() {
Script.include("../../libraries/virtualBaton.js");
Script.include("../../libraries/utils.js");
var _this = this;
this.startUpdate = function() {
print("EBL START UPDATE");
Entities.editEntity(_this.batonOwnerIndicator, {
visible: true
});
// Change color of box
Entities.editEntity(_this.entityID, {
color: randomColor()
});
_this.position = Entities.getEntityProperties(_this.entityID, "position").position;
_this.debugLightProperties.position = Vec3.sum(_this.position, {x: 0, y: 1, z: 0});
_this.debugLightProperties.color = randomColor();
var debugLight = Entities.addEntity(_this.debugLightProperties);
Script.setTimeout(function() {
Entities.deleteEntity(debugLight);
}, 500);
}
this.maybeClaim = function() {
print("EBL MAYBE CLAIM");
if (_this.isBatonOwner === true) {
_this.isBatonOwner = false;
}
Entities.editEntity(_this.batonOwnerIndicator, {
visible: false
});
baton.claim(_this.startUpdate, _this.maybeClaim);
}
this.unload = function() {
print("EBL UNLOAD");
baton.unload();
Entities.deleteEntity(_this.batonOwnerIndicator);
}
this.preload = function(entityID) {
print("EBL Preload!!");
_this.entityID = entityID;
_this.setupDebugEntities();
baton = virtualBaton({
batonName: "batonSimpleEntityScript:" + _this.entityID
});
_this.isBatonOwner = false;
_this.maybeClaim();
}
this.setupDebugEntities = function() {
_this.batonOwnerIndicator = Entities.addEntity({
type: "Box",
color: {
red: 200,
green: 10,
blue: 200
},
position: Vec3.sum(MyAvatar.position, {
x: 0,
y: 1,
z: 0
}),
dimensions: {
x: 0.5,
y: 1,
z: 0
},
parentID: MyAvatar.sessionUUID,
visible: false
});
}
_this.debugLightProperties = {
type: "Light",
name: "hifi-baton-light",
dimensions: {
x: 10,
y: 10,
z: 10
},
falloffRadius: 3,
intensity: 20,
}
});

View file

@ -0,0 +1,33 @@
var orientation = Camera.getOrientation();
orientation = Quat.safeEulerAngles(orientation);
orientation.x = 0;
orientation = Quat.fromVec3Degrees(orientation);
var center = Vec3.sum(MyAvatar.position, Vec3.multiply(3, Quat.getFront(orientation)));
// Math.random ensures no caching of script
var SCRIPT_URL = Script.resolvePath("batonSimpleEntityScript.js");
var batonBox = Entities.addEntity({
type: "Box",
name: "hifi-baton-entity",
color: {
red: 200,
green: 200,
blue: 200
},
position: center,
dimensions: {
x: 0.1,
y: 0.1,
z: 0.1
},
script: SCRIPT_URL
});
function cleanup() {
Entities.deleteEntity(batonBox);
}
Script.scriptEnding.connect(cleanup);

View file

@ -28,7 +28,6 @@ colorMix = function(colorA, colorB, mix) {
}
return result;
}
scaleLine = function (start, end, scale) {
var v = Vec3.subtract(end, start);
var length = Vec3.length(v);
@ -262,6 +261,16 @@ randInt = function(low, high) {
return Math.floor(randFloat(low, high));
}
randomColor = function() {
return {
red: randInt(0, 255),
green: randInt(0, 255),
blue: randInt(0, 255)
}
}
hexToRgb = function(hex) {
var result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
return result ? {

View file

@ -0,0 +1,31 @@
var orientation = Camera.getOrientation();
orientation = Quat.safeEulerAngles(orientation);
orientation.x = 0;
orientation = Quat.fromVec3Degrees(orientation);
var center = Vec3.sum(MyAvatar.position, Vec3.multiply(3, Quat.getFront(orientation)));
// Math.random ensures no caching of script
var SCRIPT_URL = Script.resolvePath("myEntityScript.js")
var myEntity = Entities.addEntity({
type: "Sphere",
color: {
red: 200,
green: 10,
blue: 200
},
position: center,
dimensions: {
x: 1,
y: 1,
z: 1
},
script: SCRIPT_URL
})
function cleanup() {
// Entities.deleteEntity(myEntity);
}
Script.scriptEnding.connect(cleanup);

View file

@ -0,0 +1,24 @@
(function() {
var _this;
MyEntity = function() {
_this = this;
};
MyEntity.prototype = {
preload: function(entityID) {
this.entityID = entityID;
var randNum = Math.random().toFixed(3);
print("EBL PRELOAD ENTITY SCRIPT!!!", randNum)
},
};
// entity scripts always need to return a newly constructed object of our type
return new MyEntity();
});

View file

@ -0,0 +1,85 @@
(function() {
Script.include("../../libraries/virtualBaton.js");
var baton;
var _this;
BatonSoundEntity = function() {
_this = this;
_this.drumSound = SoundCache.getSound("https://s3.amazonaws.com/hifi-public/sounds/Drums/deepdrum1.wav");
_this.injectorOptions = {position: MyAvatar.position, loop: false, volume: 1};
_this.soundIntervalConnected = false;
_this.batonDebugModel = Entities.addEntity({
type: "Box",
color: {red: 200, green: 10, blue: 200},
position: Vec3.sum(MyAvatar.position, {x: 0, y: 1, z: 0}),
dimensions: {x: 0.5, y: 1, z: 0},
parentID: MyAvatar.sessionUUID,
visible: false
});
};
function startUpdate() {
// We are claiming the baton! So start our clip
if (!_this.soundInjector) {
// This client hasn't created their injector yet so create one
_this.soundInjector = Audio.playSound(_this.drumSound, _this.injectorOptions);
} else {
// We already have our injector so just restart it
_this.soundInjector.restart();
}
print("EBL START UPDATE");
Entities.editEntity(_this.batonDebugModel, {visible: true});
_this.playSoundInterval = Script.setInterval(function() {
_this.soundInjector.restart();
}, _this.drumSound.duration * 1000); // Duration is in seconds so convert to ms
_this.soundIntervalConnected = true;
}
function stopUpdateAndReclaim() {
print("EBL STOP UPDATE AND RECLAIM")
// when the baton is release
if (_this.soundIntervalConnected === true) {
Script.clearInterval(_this.playSoundInterval);
_this.soundIntervalConnected = false;
print("EBL CLEAR INTERVAL")
}
Entities.editEntity(_this.batonDebugModel, {visible: false});
// hook up callbacks to the baton
baton.claim(startUpdate, stopUpdateAndReclaim);
}
BatonSoundEntity.prototype = {
preload: function(entityID) {
_this.entityID = entityID;
print("EBL PRELOAD ENTITY SCRIPT!!!");
baton = virtualBaton({
// One winner for each entity
batonName: "io.highfidelity.soundEntityBatonTest:" + _this.entityID,
// debugFlow: true
});
stopUpdateAndReclaim();
},
unload: function() {
print("EBL UNLOAD");
// baton.release();
baton.unload();
Entities.deleteEntity(_this.batonDebugModel);
if (_this.soundIntervalConnected === true) {
Script.clearInterval(_this.playSoundInterval);
_this.soundIntervalConnected = false;
_this.soundInjector.stop();
delete _this.soundInjector;
}
}
};
// entity scripts always need to return a newly constructed object of our type
return new BatonSoundEntity();
});

View file

@ -0,0 +1,31 @@
var orientation = Camera.getOrientation();
orientation = Quat.safeEulerAngles(orientation);
orientation.x = 0;
orientation = Quat.fromVec3Degrees(orientation);
var center = Vec3.sum(MyAvatar.position, Vec3.multiply(3, Quat.getFront(orientation)));
// Math.random ensures no caching of script
var SCRIPT_URL = Script.resolvePath("batonSoundTestEntityScript.js")
var soundEntity = Entities.addEntity({
type: "Box",
color: {
red: 200,
green: 10,
blue: 10
},
position: center,
dimensions: {
x: 0.1,
y: 0.1,
z: 0.1
},
script: SCRIPT_URL
});
function cleanup() {
// Entities.deleteEntity(soundEntity);
}
Script.scriptEnding.connect(cleanup);