content/hifi-content/caitlyn/scratch/unityUITests/webBridgeTest3.js
2022-02-13 22:19:19 +01:00

82 lines
2.7 KiB
JavaScript

var spawnPoint = Vec3.sum(Vec3.sum(MyAvatar.position, Vec3.multiply(1.0, Quat.getFront(MyAvatar.orientation))), {x: 0, y: 0.5, z: 0});
var forward = Quat.getFront(MyAvatar.orientation);
var right = Vec3.cross(Quat.getFront(MyAvatar.orientation), Quat.getUp(MyAvatar.orientation));
var WEB_BRIDGE_HTML = "http://coenraets.org/apps/ionic2-directory/";
var TROMBONE_URL = "https://s3.amazonaws.com/hifi-public/tony/audio/sad-trombone.wav";
var tromboneSound = SoundCache.getSound(TROMBONE_URL);
var tromboneInjector;
var SCREAM_URL = "https://s3.amazonaws.com/hifi-public/tony/audio/wilhelm-scream.wav";
var screamSound = SoundCache.getSound(SCREAM_URL);
var screamInjector;
// this should create a near-grabbable web entity
var webEntity = Entities.addEntity({
type: "Web",
sourceUrl: WEB_BRIDGE_HTML,
dimensions: {x: 0.5, y: 0.5, z: 0.1},
position: spawnPoint,
rotation: MyAvatar.orientation,
name: "web",
dynamic: true,
angularDamping: 0.9,
damping: 0.9,
gravity: {x: 0, y: 0, z: 0},
shapeType: "box",
userData: JSON.stringify({
"grabbableKey": {"grabbable": true}
})
});
// hook up to the event bridge
Entities.webEventReceived.connect(function (entityId, msg) {
if (entityId === webEntity) {
Script.print("HIFI: recv web event = " + JSON.stringify(msg));
if (msg === "updateMessage") {
print("CM: UPDATE MESSAGE RECEIVED FROM BROWSER APP");
}
if (msg === "button-1-play") {
// play sad trombone
if (tromboneSound.downloaded) {
if (tromboneInjector) {
tromboneInjector.restart();
} else {
tromboneInjector = Audio.playSound(tromboneSound, { position: spawnPoint, volume: 1.0, loop: false });
}
}
// wait until sound is finished then send a done event
Script.setTimeout(function () {
Entities.emitScriptEvent(webEntity, "button-1-done");
}, 3500);
}
if (msg === "button-2-play") {
// play scream
if (screamSound.downloaded) {
if (screamInjector) {
screamInjector.restart();
} else {
screamInjector = Audio.playSound(screamSound, { position: spawnPoint, volume: 1.0, loop: false });
}
}
// wait until sound is finished then send a done event
Script.setTimeout(function () {
Entities.emitScriptEvent(webEntity, "button-2-done");
}, 1000);
}
}
});
Script.scriptEnding.connect(function () {
Entities.deleteEntity(webEntity);
});