From 6b468ee87bcd409bd701d1c98af88f7b67f4523e Mon Sep 17 00:00:00 2001 From: James Pollack Date: Wed, 23 Sep 2015 13:10:25 -0700 Subject: [PATCH 1/5] Add the doll --- examples/toys/doll/createDoll.js | 41 +++++++++++ examples/toys/doll/doll.js | 117 +++++++++++++++++++++++++++++++ 2 files changed, 158 insertions(+) create mode 100644 examples/toys/doll/createDoll.js create mode 100644 examples/toys/doll/doll.js diff --git a/examples/toys/doll/createDoll.js b/examples/toys/doll/createDoll.js new file mode 100644 index 0000000000..fef55bd95b --- /dev/null +++ b/examples/toys/doll/createDoll.js @@ -0,0 +1,41 @@ +/*global MyAvatar, Entities, AnimationCache, SoundCache, Scene, Camera, Overlays, Audio, HMD, AvatarList, AvatarManager, Controller, UndoStack, Window, Account, GlobalServices, Script, ScriptDiscoveryService, LODManager, Menu, Vec3, Quat, AudioDevice, Paths, Clipboard, Settings, XMLHttpRequest, randFloat, randInt */ + +function createDoll() { + var modelURL = "http://hifi-public.s3.amazonaws.com/models/Bboys/bboy2/bboy2.fbx"; + var scriptURL = Script.resolvePath("doll.js"); + var center = Vec3.sum(Vec3.sum(MyAvatar.position, { + x: 0, + y: 0.5, + z: 0 + }), Vec3.multiply(0.5, Quat.getFront(Camera.getOrientation()))); + + var naturalDimensions = { + x: 1.63, + y: 1.67, + z: 0.26 + }; + var desiredDimensions = Vec3.multiply(naturalDimensions, 0.15); + var doll = Entities.addEntity({ + type: "Model", + name: "doll", + modelURL: modelURL, + script: scriptURL, + position: center, + shapeType: 'box', + dimensions: desiredDimensions, + gravity: { + x: 0, + y: 0, + z: 0 + }, + velocity: { + x: 0, + y: 0, + z: 0 + }, + collisionsWillMove: true + }); + return doll; +} + +createDoll(); \ No newline at end of file diff --git a/examples/toys/doll/doll.js b/examples/toys/doll/doll.js new file mode 100644 index 0000000000..1417798799 --- /dev/null +++ b/examples/toys/doll/doll.js @@ -0,0 +1,117 @@ +// doll.js +// +// Script Type: Entity +// Created by Eric Levin on 9/21/15. +// Copyright 2015 High Fidelity, Inc. +// +// This entity script breathes movement and sound- one might even say life- into a doll. +// This entity script plays an animation and a sound while you hold +// Distributed under the Apache License, Version 2.0. +// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html +// +/*global MyAvatar, Entities, AnimationCache, SoundCache, Scene, Camera, Overlays, Audio, HMD, AvatarList, AvatarManager, Controller, UndoStack, Window, Account, GlobalServices, Script, ScriptDiscoveryService, LODManager, Menu, Vec3, Quat, AudioDevice, Paths, Clipboard, Settings, XMLHttpRequest, randFloat, randInt */ +(function () { + Script.include("../../utilities.js"); + Script.include("../../libraries/utils.js"); + + // this is the "constructor" for the entity as a JS object we don't do much here, but we do want to remember + // our this object, so we can access it in cases where we're called without a this (like in the case of various global signals) + var Doll = function () { + this.screamSounds = [SoundCache.getSound("https://hifi-public.s3.amazonaws.com/sounds/KenDoll_1%2303.wav")]; + + }; + + Doll.prototype = { + startAnimationSetting: JSON.stringify({ + running: true, + fps: 30, + startFrame: 0, + lastFrame: 128, + startAutomatically: true + }), + stopAnimationSetting: JSON.stringify({ + running: false, + }), + audioInjector: null, + isGrabbed: false, + leftHand: false, + rightHand: false, + handIsSet: false, + setLeftHand: function () { + if (this.handIsSet === false) { + this.leftHand = true; + this.rightHand = false; + this.setHand = 'left'; + this.handIsSet = true; + } else { + this.currentHand = 'left' + } + }, + setRightHand: function () { + if (this.handIsSet === false) { + this.rightHand = true; + this.leftHand = false; + this.setHand = 'right'; + this.handIsSet = true; + } else { + this.currentHand = 'right' + } + }, + startNearGrab: function () { + if (this.isGrabbed === false) { + Entities.editEntity(this.entityID, { + animationURL: "https://hifi-public.s3.amazonaws.com/models/Bboys/zombie_scream.fbx", + animationSettings: this.startAnimationSetting + }); + + var position = Entities.getEntityProperties(this.entityID, "position").position; + this.audioInjector = Audio.playSound(this.screamSounds[randInt(0, this.screamSounds.length)], { + position: position, + volume: 0.1 + }); + this.isGrabbed = true; + } + + }, + continueNearGrab: function () { + if (this.setHand === this.currentHand) { + print('CONTINUING GRAB IN HAND') + var position = Entities.getEntityProperties(this.entityID, "position").position; + this.audioInjector.options.position = position; + }else{ + print('NOT SAME HAND GRABBING') + } + + }, + + releaseGrab: function () { + if (this.isGrabbed === true) { + this.audioInjector.stop(); + Entities.editEntity(this.entityID, { + animationURL: "http://hifi-public.s3.amazonaws.com/models/Bboys/bboy2/bboy2.fbx", + }); + this.isGrabbed = false; + } + if(this.setHand===this.currentHand){ + print('SAME HAND LET GO') + } + else{ + print('DIFFERENT HAND KEEP HOLDING') + } + + }, + + + // preload() will be called when the entity has become visible (or known) to the interface + // it gives us a chance to set our local JavaScript object up. In this case it means: + // * remembering our entityID, so we can access it in cases where we're called without an entityID + // * connecting to the update signal so we can check our grabbed state + preload: function (entityID) { + this.entityID = entityID; + + }, + }; + + // entity scripts always need to return a newly constructed object of our type + return new Doll(); +}); \ No newline at end of file From 82f178a3532a878c1a4cf74b1d4f31f08ba7dab1 Mon Sep 17 00:00:00 2001 From: James Pollack Date: Wed, 23 Sep 2015 17:43:08 -0700 Subject: [PATCH 2/5] tweaks --- examples/toys/doll/doll.js | 34 +++++++++------------------------- 1 file changed, 9 insertions(+), 25 deletions(-) diff --git a/examples/toys/doll/doll.js b/examples/toys/doll/doll.js index 1417798799..835685e6bb 100644 --- a/examples/toys/doll/doll.js +++ b/examples/toys/doll/doll.js @@ -38,24 +38,14 @@ rightHand: false, handIsSet: false, setLeftHand: function () { - if (this.handIsSet === false) { - this.leftHand = true; - this.rightHand = false; - this.setHand = 'left'; - this.handIsSet = true; - } else { + this.currentHand = 'left' - } + }, setRightHand: function () { - if (this.handIsSet === false) { - this.rightHand = true; - this.leftHand = false; - this.setHand = 'right'; - this.handIsSet = true; - } else { + this.currentHand = 'right' - } + }, startNearGrab: function () { if (this.isGrabbed === false) { @@ -70,18 +60,17 @@ volume: 0.1 }); this.isGrabbed = true; + this.initialHand = this.hand; + print('INITIAL HAND:::' +this.initialHand) } }, continueNearGrab: function () { - if (this.setHand === this.currentHand) { + print('CONTINUING GRAB IN HAND') var position = Entities.getEntityProperties(this.entityID, "position").position; this.audioInjector.options.position = position; - }else{ - print('NOT SAME HAND GRABBING') - } - + }, releaseGrab: function () { @@ -92,12 +81,7 @@ }); this.isGrabbed = false; } - if(this.setHand===this.currentHand){ - print('SAME HAND LET GO') - } - else{ - print('DIFFERENT HAND KEEP HOLDING') - } + }, From 7efd15c3a214e8b39a8864a290ff21b2898ef978 Mon Sep 17 00:00:00 2001 From: James Pollack Date: Wed, 23 Sep 2015 18:01:18 -0700 Subject: [PATCH 3/5] Update doll to stop screaming when released, use only one hand a a time, spatialize audio position --- examples/toys/doll/createDoll.js | 11 ++++++++ examples/toys/doll/doll.js | 45 +++++++++++++------------------- 2 files changed, 29 insertions(+), 27 deletions(-) diff --git a/examples/toys/doll/createDoll.js b/examples/toys/doll/createDoll.js index fef55bd95b..8cb93a15df 100644 --- a/examples/toys/doll/createDoll.js +++ b/examples/toys/doll/createDoll.js @@ -1,3 +1,14 @@ +// createDoll.js +// +// Script Type: Entity +// Created by James B. Pollack @imgntn 9/23/2015 +// Copyright 2015 High Fidelity, Inc. +// +// ATTENTION: requires that you run handController.js +// Creates a doll entity in front of you. +// Distributed under the Apache License, Version 2.0. +// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html +// /*global MyAvatar, Entities, AnimationCache, SoundCache, Scene, Camera, Overlays, Audio, HMD, AvatarList, AvatarManager, Controller, UndoStack, Window, Account, GlobalServices, Script, ScriptDiscoveryService, LODManager, Menu, Vec3, Quat, AudioDevice, Paths, Clipboard, Settings, XMLHttpRequest, randFloat, randInt */ function createDoll() { diff --git a/examples/toys/doll/doll.js b/examples/toys/doll/doll.js index 835685e6bb..12a41d5e98 100644 --- a/examples/toys/doll/doll.js +++ b/examples/toys/doll/doll.js @@ -4,8 +4,8 @@ // Created by Eric Levin on 9/21/15. // Copyright 2015 High Fidelity, Inc. // -// This entity script breathes movement and sound- one might even say life- into a doll. -// This entity script plays an animation and a sound while you hold +// ATTENTION: requires that you run handController.js +// This entity script plays an animation and a sound while you hold it. // Distributed under the Apache License, Version 2.0. // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html // @@ -14,11 +14,9 @@ Script.include("../../utilities.js"); Script.include("../../libraries/utils.js"); - // this is the "constructor" for the entity as a JS object we don't do much here, but we do want to remember - // our this object, so we can access it in cases where we're called without a this (like in the case of various global signals) + // this is the "constructor" for the entity as a JS object we don't do much here var Doll = function () { this.screamSounds = [SoundCache.getSound("https://hifi-public.s3.amazonaws.com/sounds/KenDoll_1%2303.wav")]; - }; Doll.prototype = { @@ -34,19 +32,14 @@ }), audioInjector: null, isGrabbed: false, - leftHand: false, - rightHand: false, - handIsSet: false, setLeftHand: function () { - - this.currentHand = 'left' - + this.hand = 'left'; }, + setRightHand: function () { - - this.currentHand = 'right' - + this.hand = 'right'; }, + startNearGrab: function () { if (this.isGrabbed === false) { Entities.editEntity(this.entityID, { @@ -61,38 +54,36 @@ }); this.isGrabbed = true; this.initialHand = this.hand; - print('INITIAL HAND:::' +this.initialHand) + } - }, + continueNearGrab: function () { - - print('CONTINUING GRAB IN HAND') - var position = Entities.getEntityProperties(this.entityID, "position").position; - this.audioInjector.options.position = position; - + var position = Entities.getEntityProperties(this.entityID, "position").position; + var audioOptions = { + position: position + }; + this.audioInjector.options = audioOptions; }, releaseGrab: function () { - if (this.isGrabbed === true) { + if (this.isGrabbed === true && this.hand === this.initialHand) { this.audioInjector.stop(); + Entities.editEntity(this.entityID, { + animationSettings: this.stopAnimationSetting + }); Entities.editEntity(this.entityID, { animationURL: "http://hifi-public.s3.amazonaws.com/models/Bboys/bboy2/bboy2.fbx", }); this.isGrabbed = false; } - - }, - // preload() will be called when the entity has become visible (or known) to the interface // it gives us a chance to set our local JavaScript object up. In this case it means: // * remembering our entityID, so we can access it in cases where we're called without an entityID - // * connecting to the update signal so we can check our grabbed state preload: function (entityID) { this.entityID = entityID; - }, }; From 83a8636a3668870ae6aecced4f0d7f6459fd8736 Mon Sep 17 00:00:00 2001 From: James Pollack Date: Thu, 24 Sep 2015 14:49:28 -0700 Subject: [PATCH 4/5] Remove whitespace, batch entity edits --- examples/toys/doll/createDoll.js | 19 +++++------- examples/toys/doll/doll.js | 51 ++++++++++++++++---------------- 2 files changed, 33 insertions(+), 37 deletions(-) diff --git a/examples/toys/doll/createDoll.js b/examples/toys/doll/createDoll.js index 8cb93a15df..ffd840f4ea 100644 --- a/examples/toys/doll/createDoll.js +++ b/examples/toys/doll/createDoll.js @@ -4,7 +4,6 @@ // Created by James B. Pollack @imgntn 9/23/2015 // Copyright 2015 High Fidelity, Inc. // -// ATTENTION: requires that you run handController.js // Creates a doll entity in front of you. // Distributed under the Apache License, Version 2.0. // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html @@ -13,19 +12,15 @@ function createDoll() { var modelURL = "http://hifi-public.s3.amazonaws.com/models/Bboys/bboy2/bboy2.fbx"; - var scriptURL = Script.resolvePath("doll.js"); - var center = Vec3.sum(Vec3.sum(MyAvatar.position, { - x: 0, - y: 0.5, - z: 0 - }), Vec3.multiply(0.5, Quat.getFront(Camera.getOrientation()))); - var naturalDimensions = { - x: 1.63, - y: 1.67, - z: 0.26 - }; + var scriptURL = Script.resolvePath("doll.js"); + + var center = Vec3.sum(Vec3.sum(MyAvatar.position, { x: 0, y: 0.5, z: 0 }), Vec3.multiply(0.5, Quat.getFront(Camera.getOrientation()))); + + var naturalDimensions = { x: 1.63, y: 1.67, z: 0.26}; + var desiredDimensions = Vec3.multiply(naturalDimensions, 0.15); + var doll = Entities.addEntity({ type: "Model", name: "doll", diff --git a/examples/toys/doll/doll.js b/examples/toys/doll/doll.js index 12a41d5e98..d47dd483ec 100644 --- a/examples/toys/doll/doll.js +++ b/examples/toys/doll/doll.js @@ -2,20 +2,22 @@ // // Script Type: Entity // Created by Eric Levin on 9/21/15. +// Additions by James B. Pollack @imgntn on 9/24/15 // Copyright 2015 High Fidelity, Inc. // -// ATTENTION: requires that you run handController.js // This entity script plays an animation and a sound while you hold it. // Distributed under the Apache License, Version 2.0. // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html // +// Known issues: If you pass the doll between hands, animation can get into a weird state. We want to prevent the animation from starting again when you switch hands, but when you switch mid-animation your hand at release is still the first hand and not the current hand. /*global MyAvatar, Entities, AnimationCache, SoundCache, Scene, Camera, Overlays, Audio, HMD, AvatarList, AvatarManager, Controller, UndoStack, Window, Account, GlobalServices, Script, ScriptDiscoveryService, LODManager, Menu, Vec3, Quat, AudioDevice, Paths, Clipboard, Settings, XMLHttpRequest, randFloat, randInt */ -(function () { + +(function() { Script.include("../../utilities.js"); Script.include("../../libraries/utils.js"); // this is the "constructor" for the entity as a JS object we don't do much here - var Doll = function () { + var Doll = function() { this.screamSounds = [SoundCache.getSound("https://hifi-public.s3.amazonaws.com/sounds/KenDoll_1%2303.wav")]; }; @@ -27,21 +29,20 @@ lastFrame: 128, startAutomatically: true }), - stopAnimationSetting: JSON.stringify({ - running: false, - }), + stopAnimationSetting: JSON.stringify({running: false}), audioInjector: null, isGrabbed: false, - setLeftHand: function () { + setLeftHand: function() { this.hand = 'left'; }, - setRightHand: function () { + setRightHand: function() { this.hand = 'right'; }, - startNearGrab: function () { + startNearGrab: function() { if (this.isGrabbed === false) { + Entities.editEntity(this.entityID, { animationURL: "https://hifi-public.s3.amazonaws.com/models/Bboys/zombie_scream.fbx", animationSettings: this.startAnimationSetting @@ -52,41 +53,41 @@ position: position, volume: 0.1 }); + this.isGrabbed = true; this.initialHand = this.hand; } }, - continueNearGrab: function () { - var position = Entities.getEntityProperties(this.entityID, "position").position; + continueNearGrab: function() { + var props = Entities.getEntityProperties(this.entityID, "position"); var audioOptions = { - position: position + position: props.position }; this.audioInjector.options = audioOptions; }, - releaseGrab: function () { - if (this.isGrabbed === true && this.hand === this.initialHand) { - this.audioInjector.stop(); - Entities.editEntity(this.entityID, { - animationSettings: this.stopAnimationSetting - }); + releaseGrab: function() { + if (this.isGrabbed === true && this.hand === this.initialHand) { + + this.audioInjector.stop(); + Entities.editEntity(this.entityID, { + animationSettings: this.stopAnimationSetting, animationURL: "http://hifi-public.s3.amazonaws.com/models/Bboys/bboy2/bboy2.fbx", }); - this.isGrabbed = false; - } + this.isGrabbed = false; + } }, - // preload() will be called when the entity has become visible (or known) to the interface - // it gives us a chance to set our local JavaScript object up. In this case it means: - // * remembering our entityID, so we can access it in cases where we're called without an entityID - preload: function (entityID) { + preload: function(entityID) { + // preload() will be called when the entity has become visible (or known) to the interface + // it gives us a chance to set our local JavaScript object up. In this case it means: + // * remembering our entityID, so we can access it in cases where we're called without an entityID this.entityID = entityID; }, }; - // entity scripts always need to return a newly constructed object of our type return new Doll(); }); \ No newline at end of file From 49d3b7e93bdd3dce9682560bb8825ba587ace3aa Mon Sep 17 00:00:00 2001 From: James Pollack Date: Thu, 24 Sep 2015 15:26:19 -0700 Subject: [PATCH 5/5] fix indentation --- examples/toys/doll/doll.js | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/examples/toys/doll/doll.js b/examples/toys/doll/doll.js index d47dd483ec..f97b6de378 100644 --- a/examples/toys/doll/doll.js +++ b/examples/toys/doll/doll.js @@ -56,7 +56,6 @@ this.isGrabbed = true; this.initialHand = this.hand; - } }, @@ -69,16 +68,16 @@ }, releaseGrab: function() { - if (this.isGrabbed === true && this.hand === this.initialHand) { + if (this.isGrabbed === true && this.hand === this.initialHand) { - this.audioInjector.stop(); + this.audioInjector.stop(); Entities.editEntity(this.entityID, { animationSettings: this.stopAnimationSetting, animationURL: "http://hifi-public.s3.amazonaws.com/models/Bboys/bboy2/bboy2.fbx", }); - this.isGrabbed = false; - } + this.isGrabbed = false; + } }, preload: function(entityID) {