69 lines
2.1 KiB
JavaScript
69 lines
2.1 KiB
JavaScript
(function() {
|
|
var messageChannel = 'MESSAGE_CHANNEL_GATE_GAME';
|
|
var SOUND_URL = 'https://hifi-content.s3.amazonaws.com/wadewatts/Ring2.wav?v=' + Date.now();
|
|
var entityID;
|
|
var sound;
|
|
|
|
function sendMessage(message) {
|
|
message.entityID = entityID;
|
|
Messages.sendMessage(
|
|
messageChannel,
|
|
JSON.stringify(message)
|
|
);
|
|
}
|
|
|
|
function onReceivedMessage(channel, message, senderID) {
|
|
try {
|
|
var data = JSON.parse(message);
|
|
} catch (e) {
|
|
print('Error parsing message: ' + message);
|
|
return;
|
|
}
|
|
// print('data: ' + JSON.stringify(data));
|
|
|
|
switch (data.type) {
|
|
case 'CHECKPOINT':
|
|
if (entityID === data.gateID) {
|
|
var position = Entities.getEntityProperties(entityID, 'position').position;
|
|
var audioProperties = {
|
|
volume: 0.1,
|
|
position: position
|
|
};
|
|
Audio.playSound(sound, audioProperties);
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
preload: function(id) {
|
|
entityID = id;
|
|
Messages.subscribe(messageChannel);
|
|
Messages.messageReceived.connect(null, onReceivedMessage);
|
|
sound = SoundCache.getSound(SOUND_URL);
|
|
sendMessage({
|
|
type: 'INIT_GATE',
|
|
gateID: entityID
|
|
});
|
|
print('Hello Gate');
|
|
},
|
|
unload: function(id) {
|
|
sendMessage({
|
|
type: 'REMOVE_GATE',
|
|
gateID: entityID
|
|
});
|
|
Messages.unsubscribe(messageChannel);
|
|
Messages.messageReceived.disconnect(null, onReceivedMessage);
|
|
},
|
|
enterEntity: function(id) {
|
|
sendMessage({
|
|
type: 'PASS_THROUGH_GATE',
|
|
avatarSessionUUID: MyAvatar.sessionUUID,
|
|
avatarDisplayName: MyAvatar.displayName,
|
|
gateID: entityID
|
|
});
|
|
}
|
|
};
|
|
})
|