-
Notifications
You must be signed in to change notification settings - Fork 53
Scripting Support
The ability to specify a script in your JanusVR room definition is what really brings JanusVR worlds to life. JanusWeb now implements the same spec as JanusVR native client, see http://janusvr.com/guide/javascriptbasics/index.html for full details.
The following classes and functions are available to user scripts, and are implemented in JanusWeb as well:
window.janus
window.room
window.player
window.Vector
window.translate(v1, v2)
window.scalarMultiply(v, n)
window.cross(v1, v2)
window.normalized(v)
window.distance(p1, p2)
window.equals(v1, v2)
window.removeKey(dict, key)
window.print(value)
window.debug(value)
window.uniqueId()
In order to expose a clean public API for user scripts, we're going to use ES6 Proxy
objects. We already have Elation Engine components to represent all the classes we need to expose, so by using a Proxy
object we can expose only the functions needed by the scripts, and provide an API that is consistent with the native JanusVR scripting engine without having to refactor engine code.
scriptobjects['janus'] = new elation.proxy(this, {
version: ['property', 'version', { readonly: true}],
//versiononline: ['property', 'versiononline', {readonly: true}],
//currentkey: ['property', 'currentkey', {readonly: true}],
//chat: ['property', 'chat.messages', {readonly: true}],
//networkstatus: ['property', 'network.status'],
//networkerror: ['property', 'network.error'],
//roomserver: ['property', 'network.server'],
playercount: ['property', 'currentroom.playercount'],
//bookmarkurl: ['property', 'bookmarks.items'],
//bookmarkthumb: ['property', 'bookmarks.items'], // FIXME - need to filter?
//playerlist: ['property', ''],
//settings: ['property', 'settings'],
userid: ['property', 'userId'],
//avatarlighting: ['property', 'settings.avatarlighting'],
currenturl: ['function', 'getCurrentURL'],
tricount: ['function', 'getTriangleCount'],
locked: ['function', 'isLocked'],
//getsetting: ['function', 'getSetting'],
//setsetting: ['function', 'setSetting'],
//roomprogress: ['function', 'currentroom.getProgress'],
launchurl: ['function', 'loadURL'],
navback: ['function', 'navigateBack'],
navforward: ['function', 'navigateForward'],
navhome: ['function', 'navigateHome'],
chatsend: ['function', 'chat.send'],
//sync: ['function', 'currentroom.sync'],
//reset: ['function', 'reset'],
//quit: ['function', 'quit'],
//focus: ['function', 'focus'],
//unfocus: ['function', 'blur'],
//saveroom: ['function', 'saveRoom'],
//roomcode: ['function', 'getRoomCode'],
//setroomcode: ['function', 'setRoomCode'],
//setuserid: ['function', 'setUsername'],
//getuserid: ['function', 'getUsername'],
//setavatarlighting: ['function', 'setAvatarLighting'],
//getavatarlighting: ['function', 'getAvatarLighting'],
//resetavatar: ['function', 'resetAvatar'],
//hasFocus: ['function', 'hasFocus']
});
scriptobjects['room'] = new elation.proxy(currentroom, {
url: ['property', 'url', { readonly: true}],
objects: ['property', 'jsobjects'],
cookies: ['property', 'cookies'],
walk_speed: ['property', 'properties.walk_speed'],
run_speed: ['property', 'properties.run_speed'],
jump_velocity: ['property', 'properties.jump_velocity'],
gravity: ['property', 'properties.gravity'],
createObject: ['function', 'spawn'],
removeObject: ['function', 'remove'],
addCookie: ['function', 'addCookie'],
playSound: ['function', 'playSound'],
getObjectById: ['function', 'getObjectById'],
onLoad: ['callback', 'room_load'],
update: ['callback', 'engine_frame'],
onCollision: ['callback', 'physics_collide'],
onClick: ['callback', 'click'],
onMouseDown: ['callback', 'mousedown'],
onMouseUp: ['callback', 'mouseup'],
onKeyDown: ['callback', 'keydown'],
onKeyUp: ['callback', 'keyup']
});
scriptobjects['player'] = new elation.proxy(player, {
pos: ['property', 'properties.position'],
//eye_pos: ['property', 'eyes.properties.position'],
head_pos: ['property', 'head.properties.position'],
//cursor_pos: ['property', 'properties.cursor_position'],
//cursor_xdir: ['property', 'properties.cursor_xdir'],
//cursor_ydir: ['property', 'properties.cursor_ydir'],
//cursor_zdir: ['property', 'properties.cursor_zdir'],
view_dir: ['property', 'vectors.view_forward'],
dir: ['property', 'vectors.forward'],
userid: ['property', 'properties.player_id'],
//url: ['property', 'currenturl'],
//hmd_enabled: ['property', 'hmd_enabled'],
//cursor_active: ['property', 'cursor_active'],
//cursor_object: ['property', 'cursor_object'],
//lookat_object: ['property', 'lookat_object'],
//lookat_pos: ['property', 'properties.lookat_position'],
//lookat_xdir: ['property', 'properties.lookat_xdir'],
//lookat_ydir: ['property', 'properties.lookat_ydir'],
//lookat_zdir: ['property', 'properties.lookat_zdir'],
});
Note: see Scripting Support 2.0 for details on the experimental new custom element scripting functionality