From 3563efb584d7b56b25beee653ded7b39ddabd05d Mon Sep 17 00:00:00 2001 From: Atlante45 Date: Tue, 24 Mar 2015 21:52:53 +0100 Subject: [PATCH 1/5] extra ; --- libraries/entities/src/EntityItemID.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/entities/src/EntityItemID.cpp b/libraries/entities/src/EntityItemID.cpp index aaf6e33128..cd2202eead 100644 --- a/libraries/entities/src/EntityItemID.cpp +++ b/libraries/entities/src/EntityItemID.cpp @@ -25,7 +25,7 @@ EntityItemID::EntityItemID() : creatorTokenID(UNKNOWN_ENTITY_TOKEN), isKnownID(false) { -}; +} EntityItemID::EntityItemID(const EntityItemID& other) : id(other.id), From 92d394404463012c6c2ef9e05d774545c4efa725 Mon Sep 17 00:00:00 2001 From: Atlante45 Date: Wed, 25 Mar 2015 15:01:22 +0100 Subject: [PATCH 2/5] Use preOp function for all common checks/updates --- examples/entityScripts/lightController.js | 70 ++++++++++++++--------- 1 file changed, 44 insertions(+), 26 deletions(-) diff --git a/examples/entityScripts/lightController.js b/examples/entityScripts/lightController.js index 624fc929b3..1ff41df16c 100644 --- a/examples/entityScripts/lightController.js +++ b/examples/entityScripts/lightController.js @@ -4,6 +4,27 @@ this.lightID = null; this.sound = null; + var DEFAULT_USER_DATA = { + creatingLight: false, + lightID: null, + lightDefaultProperties: { + type: "Light", + position: { x: 0, y: 0, z: 0 }, + dimensions: { x: 5, y: 5, z: 5 }, + isSpotlight: false, + color: { red: 255, green: 48, blue: 0 }, + diffuseColor: { red: 255, green: 255, blue: 255 }, + ambientColor: { red: 255, green: 255, blue: 255 }, + specularColor: { red: 0, green: 0, blue: 0 }, + constantAttenuation: 1, + linearAttenuation: 0, + quadraticAttenuation: 0, + intensity: 10, + exponent: 0, + cutoff: 180, // in degrees + } + }; + function copyObject(object) { return JSON.parse(JSON.stringify(object)); } @@ -30,6 +51,19 @@ Entities.editEntity(entityID, { userData: JSON.stringify(userData) }); } + // Checks whether the userData is well-formed and updates it if not + this.checkUserData = function() { + var userData = getUserData(this.entityID); + if (!userData) { + userData = DEFAULT_USER_DATA; + } else if (!userData.lightDefaultProperties) { + userData.lightDefaultProperties = DEFAULT_USER_DATA.lightDefaultProperties; + } else if (typeof userData.creatingLight == 'undefined') { + userData.creatingLight = DEFAULT_USER_DATA.creatingLight; + } + updateUserData(this.entityID, userData); + } + // Download sound if needed this.maybeDownloadSound = function() { if (this.sound === null) { @@ -76,28 +110,6 @@ this.updateLightID = function() { var userData = getUserData(this.entityID); - if (!userData) { - userData = { - lightID: null, - lightDefaultProperties: { - type: "Light", - position: { x: 0, y: 0, z: 0 }, - dimensions: { x: 5, y: 5, z: 5 }, - isSpotlight: false, - color: { red: 255, green: 48, blue: 0 }, - diffuseColor: { red: 255, green: 255, blue: 255 }, - ambientColor: { red: 255, green: 255, blue: 255 }, - specularColor: { red: 0, green: 0, blue: 0 }, - constantAttenuation: 1, - linearAttenuation: 0, - quadraticAttenuation: 0, - intensity: 10, - exponent: 0, - cutoff: 180, // in degrees - } - }; - updateUserData(this.entityID, userData); - } // Find valid light if (doesEntityExistNow(this.lightID)) { @@ -168,15 +180,21 @@ updateUserData(this.entityID, userData); print("Relative properties of light entity saved."); } - - this.preload = function(entityID) { + + // This function should be called before any callback is executed + this.preOperation = function(entityID) { this.entityID = entityID; this.maybeDownloadSound(); + + this.checkUserData(); + } + + this.preload = function(entityID) { + this.preOperation(entityID); }; this.clickReleaseOnEntity = function(entityID, mouseEvent) { - this.entityID = entityID; - this.maybeDownloadSound(); + this.preOperation(entityID); if (mouseEvent.isLeftButton) { this.updateLightID(); From afb96709f19df414534ae6ec60c0e4d021a25989 Mon Sep 17 00:00:00 2001 From: Atlante45 Date: Wed, 25 Mar 2015 15:44:37 +0100 Subject: [PATCH 3/5] Only update userData when light entity is identified --- examples/entityScripts/lightController.js | 91 +++++++++++++---------- 1 file changed, 53 insertions(+), 38 deletions(-) diff --git a/examples/entityScripts/lightController.js b/examples/entityScripts/lightController.js index 1ff41df16c..f4ebd17715 100644 --- a/examples/entityScripts/lightController.js +++ b/examples/entityScripts/lightController.js @@ -1,6 +1,5 @@ (function() { this.entityID = null; - this.properties = null; this.lightID = null; this.sound = null; @@ -51,19 +50,6 @@ Entities.editEntity(entityID, { userData: JSON.stringify(userData) }); } - // Checks whether the userData is well-formed and updates it if not - this.checkUserData = function() { - var userData = getUserData(this.entityID); - if (!userData) { - userData = DEFAULT_USER_DATA; - } else if (!userData.lightDefaultProperties) { - userData.lightDefaultProperties = DEFAULT_USER_DATA.lightDefaultProperties; - } else if (typeof userData.creatingLight == 'undefined') { - userData.creatingLight = DEFAULT_USER_DATA.creatingLight; - } - updateUserData(this.entityID, userData); - } - // Download sound if needed this.maybeDownloadSound = function() { if (this.sound === null) { @@ -81,17 +67,21 @@ print("Warning: Couldn't play sound."); } } - - // Toggles the associated light entity - this.toggleLight = function() { - if (this.lightID) { - var lightProperties = Entities.getEntityProperties(this.lightID); - Entities.editEntity(this.lightID, { visible: !lightProperties.visible }); - } else { - print("Warning: No light to turn on/off"); + + // Checks whether the userData is well-formed and updates it if not + this.checkUserData = function() { + var userData = getUserData(this.entityID); + if (!userData) { + userData = DEFAULT_USER_DATA; + } else if (!userData.lightDefaultProperties) { + userData.lightDefaultProperties = DEFAULT_USER_DATA.lightDefaultProperties; + } else if (typeof userData.creatingLight == 'undefined') { + userData.creatingLight = DEFAULT_USER_DATA.creatingLight; } + updateUserData(this.entityID, userData); } + // Create a Light entity this.createLight = function(userData) { var lightProperties = copyObject(userData.lightDefaultProperties); if (lightProperties) { @@ -108,34 +98,48 @@ } } + // Tries to find a valid light, creates one otherwise this.updateLightID = function() { - var userData = getUserData(this.entityID); - // Find valid light if (doesEntityExistNow(this.lightID)) { - if (!didEntityExist(this.lightID)) { - // Light now has an ID, so update it in userData - this.lightID = getTrueID(this.lightID); - userData.lightID = this.lightID; - updateUserData(this.entityID, userData); - } return; } + var userData = getUserData(this.entityID); if (doesEntityExistNow(userData.lightID)) { - this.lightID = getTrueID(userData.lightID); + this.lightID = userData.lightID; return; } - // No valid light, create one - this.lightID = this.createLight(userData); - print("Created new light entity"); - - // Update user data with new ID + if (!userData.creatingLight) { + // No valid light, create one + userData.creatingLight = true; + updateUserData(this.entityID, userData); + this.lightID = this.createLight(userData); + this.maybeUpdateLightIDInUserData(); + print("Created new light entity"); + } + } + + this.maybeUpdateLightIDInUserData = function() { + this.lightID = getTrueID(this.lightID); + if (this.lightID.isKnownID) { + this.updateLightIDInUserData(); + } else { + var that = this; + Script.setTimeout(function() { that.maybeUpdateLightIDInUserData() }, 500); + } + } + + // Update user data with new lightID + this.updateLightIDInUserData = function() { + var userData = getUserData(this.entityID); userData.lightID = this.lightID; + userData.creatingLight = false; updateUserData(this.entityID, userData); } + // Moves light entity if the lamp entity moved this.maybeMoveLight = function() { var entityProperties = Entities.getEntityProperties(this.entityID); var lightProperties = Entities.getEntityProperties(this.lightID); @@ -151,8 +155,9 @@ } } + // Stores light entity relative position in the lamp metadata this.updateRelativeLightPosition = function() { - if (!doesEntityExistNow(this.entityID) || !doesEntityExistNow(this.lightID)) { + if (!doesEntityExistNow(this.lightID)) { print("Warning: ID invalid, couldn't save relative position."); return; } @@ -189,6 +194,17 @@ this.checkUserData(); } + // Toggles the associated light entity + this.toggleLight = function() { + if (this.lightID) { + var lightProperties = Entities.getEntityProperties(this.lightID); + Entities.editEntity(this.lightID, { visible: !lightProperties.visible }); + this.playSound(); + } else { + print("Warning: No light to turn on/off"); + } + } + this.preload = function(entityID) { this.preOperation(entityID); }; @@ -200,7 +216,6 @@ this.updateLightID(); this.maybeMoveLight(); this.toggleLight(); - this.playSound(); } else if (mouseEvent.isRightButton) { this.updateRelativeLightPosition(); } From 19886bd33c91f6c8a79b51aad7c21f8b834c354e Mon Sep 17 00:00:00 2001 From: Atlante45 Date: Wed, 25 Mar 2015 16:38:27 +0100 Subject: [PATCH 4/5] Better handling of callback --- examples/entityScripts/lightController.js | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/examples/entityScripts/lightController.js b/examples/entityScripts/lightController.js index f4ebd17715..5b2e2ae8b1 100644 --- a/examples/entityScripts/lightController.js +++ b/examples/entityScripts/lightController.js @@ -75,8 +75,6 @@ userData = DEFAULT_USER_DATA; } else if (!userData.lightDefaultProperties) { userData.lightDefaultProperties = DEFAULT_USER_DATA.lightDefaultProperties; - } else if (typeof userData.creatingLight == 'undefined') { - userData.creatingLight = DEFAULT_USER_DATA.creatingLight; } updateUserData(this.entityID, userData); } @@ -122,8 +120,8 @@ } this.maybeUpdateLightIDInUserData = function() { - this.lightID = getTrueID(this.lightID); - if (this.lightID.isKnownID) { + if (getTrueID(this.lightID).isKnownID) { + this.lightID = getTrueID(this.lightID); this.updateLightIDInUserData(); } else { var that = this; From 715fe1096f2331d2c7fcfae55752f3a92c892091 Mon Sep 17 00:00:00 2001 From: Atlante45 Date: Wed, 25 Mar 2015 17:33:27 +0100 Subject: [PATCH 5/5] Random but persistant switch sound --- examples/entityScripts/lightController.js | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/examples/entityScripts/lightController.js b/examples/entityScripts/lightController.js index 5b2e2ae8b1..e6e4998aef 100644 --- a/examples/entityScripts/lightController.js +++ b/examples/entityScripts/lightController.js @@ -2,6 +2,9 @@ this.entityID = null; this.lightID = null; this.sound = null; + this.soundURLs = ["https://hifi-public.s3.amazonaws.com/sounds/Switches%20and%20sliders/lamp_switch_1.wav", + "https://hifi-public.s3.amazonaws.com/sounds/Switches%20and%20sliders/lamp_switch_2.wav", + "https://hifi-public.s3.amazonaws.com/sounds/Switches%20and%20sliders/lamp_switch_3.wav"] var DEFAULT_USER_DATA = { creatingLight: false, @@ -21,7 +24,8 @@ intensity: 10, exponent: 0, cutoff: 180, // in degrees - } + }, + soundIndex: Math.floor(Math.random() * this.soundURLs.length) }; function copyObject(object) { @@ -53,7 +57,8 @@ // Download sound if needed this.maybeDownloadSound = function() { if (this.sound === null) { - this.sound = SoundCache.getSound("http://public.highfidelity.io/sounds/Footsteps/FootstepW3Left-12db.wav"); + var soundIndex = getUserData(this.entityID).soundIndex; + this.sound = SoundCache.getSound(this.soundURLs[soundIndex]); } } // Play switch sound @@ -75,6 +80,8 @@ userData = DEFAULT_USER_DATA; } else if (!userData.lightDefaultProperties) { userData.lightDefaultProperties = DEFAULT_USER_DATA.lightDefaultProperties; + } else if (!userData.soundIndex) { + userData.soundIndex = DEFAULT_USER_DATA.soundIndex; } updateUserData(this.entityID, userData); } @@ -187,9 +194,9 @@ // This function should be called before any callback is executed this.preOperation = function(entityID) { this.entityID = entityID; - this.maybeDownloadSound(); this.checkUserData(); + this.maybeDownloadSound(); } // Toggles the associated light entity