From 51887a76e1c28b54519a608744ceac417ecb0260 Mon Sep 17 00:00:00 2001 From: ericrius1 Date: Fri, 2 Oct 2015 11:02:19 -0700 Subject: [PATCH 1/5] refactoring lights to fix problem with piling up --- examples/toys/lightSwitch.js | 1 + examples/toys/masterReset.js | 150 +++++++++++++++++++++++++++++++++-- 2 files changed, 144 insertions(+), 7 deletions(-) create mode 100644 examples/toys/lightSwitch.js diff --git a/examples/toys/lightSwitch.js b/examples/toys/lightSwitch.js new file mode 100644 index 0000000000..932e773797 --- /dev/null +++ b/examples/toys/lightSwitch.js @@ -0,0 +1 @@ +print("HEY THERE") \ No newline at end of file diff --git a/examples/toys/masterReset.js b/examples/toys/masterReset.js index 72e2d73729..a4fb40bc1a 100644 --- a/examples/toys/masterReset.js +++ b/examples/toys/masterReset.js @@ -8,7 +8,7 @@ /*global print, 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, pointInExtents, vec3equal, setEntityCustomData, getEntityCustomData */ //per script -/*global deleteAllToys, createAllToys, createGates, createPingPongBallGun, createFire, createPottedPlant, createCombinedArmChair, createBasketballHoop, createBasketBall, createSprayCan, createDoll, createWand, createDice, createCat, deleteAllToys, createFlashlight, createBlocks, createMagballs, createLightSwitches */ +/*global deleteAllToys, createAllToys, createGates, createPingPongBallGun, createFire, createPottedPlant, createCombinedArmChair, createBasketballHoop, createBasketBall, createSprayCan, createDoll, createWand, createDice, createCat, deleteAllToys, createFlashlight, createBlocks, createMagballs, createLights */ var utilitiesScript = Script.resolvePath("../libraries/utils.js"); Script.include(utilitiesScript); @@ -89,7 +89,7 @@ function createAllToys() { createFire(); // //Handles toggling of all sconce lights - createLightSwitches(); + createLights(); @@ -98,7 +98,7 @@ function createAllToys() { function deleteAllToys() { var entities = Entities.findEntities(MyAvatar.position, 100); - entities.forEach(function(entity) { + entities.forEach(function (entity) { //params: customKey, id, defaultValue var shouldReset = getEntityCustomData(resetKey, entity, {}).resetMe; if (shouldReset === true) { @@ -252,9 +252,9 @@ function createFlashlight(position) { } -function createLightSwitches() { +function createLights() { var modelURL = "http://hifi-public.s3.amazonaws.com/ryan/lightswitch.fbx?v1"; - var scriptURL = Script.resolvePath("lightSwitchHall.js?v1"); + var scriptURL = Script.resolvePath("lightSwitch.js"); var lightSwitchHall = Entities.addEntity({ type: "Model", @@ -278,12 +278,63 @@ function createLightSwitches() { z: 0.16242524981498718 } }); - setEntityCustomData(resetKey, lightSwitchHall, { resetMe: true }); - scriptURL = Script.resolvePath("lightSwitchGarage.js?v1"); + var sconceLight1 = Entities.addEntity({ + type: "Light", + position: { + x: 543.75, + y: 496.24, + z: 511.13 + }, + name: "Sconce 1 Light", + dimensions: { + x: 2.545, + y: 2.545, + z: 2.545 + }, + cutoff: 90, + color: { + red: 217, + green: 146, + blue: 24 + } + }); + + setEntityCustomData(resetKey, sconceLight1, { + resetMe: true, + lightType: "Sconce Light", + on: true + }); + + var sconceLight2 = Entities.addEntity({ + type: "Light", + position: { + x: 540.1, + y: 496.24, + z: 505.57 + }, + name: "Sconce 2 Light", + dimensions: { + x: 2.545, + y: 2.545, + z: 2.545 + }, + cutoff: 90, + color: { + red: 217, + green: 146, + blue: 24 + } + }); + + setEntityCustomData(resetKey, sconceLight2, { + resetMe: true, + lightType: "Sconce Light", + state: true + }); var lightSwitchGarage = Entities.addEntity({ type: "Model", @@ -312,6 +363,91 @@ function createLightSwitches() { resetMe: true }); + + var sconceLight3 = Entities.addEntity({ + type: "Light", + position: { + x: 545.49468994140625, + y: 496.24026489257812, + z: 500.63516235351562 + }, + + name: "Sconce 3 Light", + dimensions: { + x: 2.545, + y: 2.545, + z: 2.545 + }, + cutoff: 90, + color: { + red: 217, + green: 146, + blue: 24 + } + }); + + + + setEntityCustomData(resetKey, sconceLight3, { + resetMe: true, + lightType: "Sconce Light", + on: true + }); + + var sconceLight4 = Entities.addEntity({ + type: "Light", + position: { + x: 550.90399169921875, + y: 496.24026489257812, + z: 507.90237426757812 + }, + name: "Sconce 4 Light", + dimensions: { + x: 2.545, + y: 2.545, + z: 2.545 + }, + cutoff: 90, + color: { + red: 217, + green: 146, + blue: 24 + } + }); + + setEntityCustomData(resetKey, sconceLight4, { + resetMe: true, + lightType: "Sconce Light", + on: true + }); + + var sconceLight5 = Entities.addEntity({ + type: "Light", + position: { + x: 548.407958984375, + y: 496.24026489257812, + z: 509.5504150390625 + }, + name: "Sconce 5 Light", + dimensions: { + x: 2.545, + y: 2.545, + z: 2.545 + }, + cutoff: 90, + color: { + red: 217, + green: 146, + blue: 24 + } + }); + + setEntityCustomData(resetKey, sconceLight5, { + resetMe: true, + lightType: "Sconce Light", + on: true + }); + } function createDice() { From 4b62048fb143b6e6a188fc64fd083b0f88d897ae Mon Sep 17 00:00:00 2001 From: ericrius1 Date: Fri, 2 Oct 2015 12:12:36 -0700 Subject: [PATCH 2/5] more light changes --- examples/toys/lightSwitch.js | 53 +++++- unpublishedScripts/masterReset.js | 297 +++++++++++++++--------------- 2 files changed, 201 insertions(+), 149 deletions(-) diff --git a/examples/toys/lightSwitch.js b/examples/toys/lightSwitch.js index 932e773797..18c6d26185 100644 --- a/examples/toys/lightSwitch.js +++ b/examples/toys/lightSwitch.js @@ -1 +1,52 @@ -print("HEY THERE") \ No newline at end of file +// +// lightSwitch.js.js +// examples/entityScripts +// +// Created by Eric Levin on 10/2/15. +// Copyright 2015 High Fidelity, Inc. +// +// +// Distributed under the Apache License, Version 2.0. +// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html +// + +/*global print, 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, pointInExtents, vec3equal, setEntityCustomData, getEntityCustomData */ +//per script + +/*global LightSwitch */ + +(function () { + + var LightSwitch = function () { + this.switchSound = SoundCache.getSound("https://hifi-public.s3.amazonaws.com/sounds/Switches%20and%20sliders/lamp_switch_2.wav"); + print("SHNUUUUR") + + }; + + LightSwitch.prototype = { + + clickReleaseOnEntity: function (entityID, mouseEvent) { + if (!mouseEvent.isLeftButton) { + return; + } + this.toggleLights(); + }, + + toggleLights: function () { + print("TOGGLE LIGHTS"); + }, + + startNearGrabNonColliding: function () { + this.toggleLights(); + }, + + preload: function (entityID) { + this.entityID = entityID; + //The light switch is static, so just cache its position once + this.position = Entities.getEntityProperties(this.entityID, "position").position; + }, + }; + + // entity scripts always need to return a newly constructed object of our type + return new LightSwitch(); +}()); \ No newline at end of file diff --git a/unpublishedScripts/masterReset.js b/unpublishedScripts/masterReset.js index fe29d8a8de..40cce2582c 100644 --- a/unpublishedScripts/masterReset.js +++ b/unpublishedScripts/masterReset.js @@ -106,155 +106,10 @@ function deleteAllToys() { }); } -function createFire() { - - - var myOrientation = Quat.fromPitchYawRollDegrees(-90, 0, 0.0); - - var animationSettings = JSON.stringify({ - fps: 30, - running: true, - loop: true, - firstFrame: 1, - lastFrame: 10000 - }); - - - var fire = Entities.addEntity({ - type: "ParticleEffect", - name: "fire", - animationSettings: animationSettings, - textures: "https://hifi-public.s3.amazonaws.com/alan/Particles/Particle-Sprite-Smoke-1.png", - position: { - x: 551.45, - y: 494.82, - z: 502.05 - }, - emitRate: 100, - colorStart: { - red: 70, - green: 70, - blue: 137 - }, - color: { - red: 200, - green: 99, - blue: 42 - }, - colorFinish: { - red: 255, - green: 99, - blue: 32 - }, - radiusSpread: 0.01, - radiusStart: 0.02, - radiusEnd: 0.001, - particleRadius: 0.05, - radiusFinish: 0.0, - emitOrientation: myOrientation, - emitSpeed: 0.3, - speedSpread: 0.1, - alphaStart: 0.05, - alpha: 0.1, - alphaFinish: 0.05, - emitDimensions: { - x: 1, - y: 1, - z: 0.1 - }, - polarFinish: 0.1, - emitAcceleration: { - x: 0.0, - y: 0.0, - z: 0.0 - }, - accelerationSpread: { - x: 0.1, - y: 0.01, - z: 0.1 - }, - lifespan: 1 - }); - - - setEntityCustomData(resetKey, fire, { - resetMe: true - }); -} - - -function createCat(position) { - var scriptURL = Script.resolvePath("../examples/toys/cat.js"); - var modelURL = "http://hifi-public.s3.amazonaws.com/ryan/Dark_Cat.fbx"; - var animationURL = "http://hifi-public.s3.amazonaws.com/ryan/sleeping.fbx"; - var animationSettings = JSON.stringify({ - running: true, - }); - var cat = Entities.addEntity({ - type: "Model", - modelURL: modelURL, - name: "cat", - script: scriptURL, - animationURL: animationURL, - animationSettings: animationSettings, - position: position, - rotation: { - w: 0.35020983219146729, - x: -4.57763671875e-05, - y: 0.93664455413818359, - z: -1.52587890625e-05 - }, - dimensions: { - x: 0.15723302960395813, - y: 0.50762706995010376, - z: 0.90716040134429932 - }, - }); - - setEntityCustomData(resetKey, cat, { - resetMe: true - }); -} - -function createFlashlight(position) { - var scriptURL = Script.resolvePath('../examples/toys/flashlight/flashlight.js'); - var modelURL = "https://hifi-public.s3.amazonaws.com/models/props/flashlight.fbx"; - - var flashlight = Entities.addEntity({ - type: "Model", - modelURL: modelURL, - name: "flashlight", - script: scriptURL, - position: position, - dimensions: { - x: 0.08, - y: 0.30, - z: 0.08 - }, - collisionsWillMove: true, - gravity: { - x: 0, - y: -3.5, - z: 0 - }, - velocity: { - x: 0, - y: -0.01, - z: 0 - }, - shapeType: 'box', - }); - - setEntityCustomData(resetKey, flashlight, { - resetMe: true - }); - -} - function createLights() { - var modelURL = "http://hifi-public.s3.amazonaws.com/ryan/lightswitch.fbx?v1"; + var modelURL = "http://hifi-public.s3.amazonaws.com/ryan/lightswitch.fbx"; - var scriptURL = Script.resolvePath("../examples/toys/lightSwitchHall.js"); + var scriptURL = Script.resolvePath("../examples/toys/lightSwitch.js"); var lightSwitchHall = Entities.addEntity({ type: "Model", @@ -450,6 +305,152 @@ function createLights() { } + +function createFire() { + + + var myOrientation = Quat.fromPitchYawRollDegrees(-90, 0, 0.0); + + var animationSettings = JSON.stringify({ + fps: 30, + running: true, + loop: true, + firstFrame: 1, + lastFrame: 10000 + }); + + + var fire = Entities.addEntity({ + type: "ParticleEffect", + name: "fire", + animationSettings: animationSettings, + textures: "https://hifi-public.s3.amazonaws.com/alan/Particles/Particle-Sprite-Smoke-1.png", + position: { + x: 551.45, + y: 494.82, + z: 502.05 + }, + emitRate: 100, + colorStart: { + red: 70, + green: 70, + blue: 137 + }, + color: { + red: 200, + green: 99, + blue: 42 + }, + colorFinish: { + red: 255, + green: 99, + blue: 32 + }, + radiusSpread: 0.01, + radiusStart: 0.02, + radiusEnd: 0.001, + particleRadius: 0.05, + radiusFinish: 0.0, + emitOrientation: myOrientation, + emitSpeed: 0.3, + speedSpread: 0.1, + alphaStart: 0.05, + alpha: 0.1, + alphaFinish: 0.05, + emitDimensions: { + x: 1, + y: 1, + z: 0.1 + }, + polarFinish: 0.1, + emitAcceleration: { + x: 0.0, + y: 0.0, + z: 0.0 + }, + accelerationSpread: { + x: 0.1, + y: 0.01, + z: 0.1 + }, + lifespan: 1 + }); + + + setEntityCustomData(resetKey, fire, { + resetMe: true + }); +} + + +function createCat(position) { + var scriptURL = Script.resolvePath("../examples/toys/cat.js"); + var modelURL = "http://hifi-public.s3.amazonaws.com/ryan/Dark_Cat.fbx"; + var animationURL = "http://hifi-public.s3.amazonaws.com/ryan/sleeping.fbx"; + var animationSettings = JSON.stringify({ + running: true, + }); + var cat = Entities.addEntity({ + type: "Model", + modelURL: modelURL, + name: "cat", + script: scriptURL, + animationURL: animationURL, + animationSettings: animationSettings, + position: position, + rotation: { + w: 0.35020983219146729, + x: -4.57763671875e-05, + y: 0.93664455413818359, + z: -1.52587890625e-05 + }, + dimensions: { + x: 0.15723302960395813, + y: 0.50762706995010376, + z: 0.90716040134429932 + }, + }); + + setEntityCustomData(resetKey, cat, { + resetMe: true + }); +} + +function createFlashlight(position) { + var scriptURL = Script.resolvePath('../examples/toys/flashlight/flashlight.js'); + var modelURL = "https://hifi-public.s3.amazonaws.com/models/props/flashlight.fbx"; + + var flashlight = Entities.addEntity({ + type: "Model", + modelURL: modelURL, + name: "flashlight", + script: scriptURL, + position: position, + dimensions: { + x: 0.08, + y: 0.30, + z: 0.08 + }, + collisionsWillMove: true, + gravity: { + x: 0, + y: -3.5, + z: 0 + }, + velocity: { + x: 0, + y: -0.01, + z: 0 + }, + shapeType: 'box', + }); + + setEntityCustomData(resetKey, flashlight, { + resetMe: true + }); + +} + function createDice() { var diceProps = { type: "Model", @@ -967,4 +968,4 @@ function cleanup() { if (shouldDeleteOnEndScript) { Script.scriptEnding.connect(cleanup); -} +} \ No newline at end of file From e6e60e5e7a1b607c3b52f9d9d8fc1e34a28429b3 Mon Sep 17 00:00:00 2001 From: ericrius1 Date: Fri, 2 Oct 2015 15:58:32 -0700 Subject: [PATCH 3/5] Lights should now only be created once at start of running reset master script, no matter how many people there are in the room --- examples/toys/lightSwitch.js | 56 ++++++-- examples/toys/lightSwitchGarage.js | 202 ----------------------------- examples/toys/lightSwitchHall.js | 179 ------------------------- unpublishedScripts/masterReset.js | 68 ++++++---- 4 files changed, 89 insertions(+), 416 deletions(-) delete mode 100644 examples/toys/lightSwitchGarage.js delete mode 100644 examples/toys/lightSwitchHall.js diff --git a/examples/toys/lightSwitch.js b/examples/toys/lightSwitch.js index 18c6d26185..9a23a3b6d4 100644 --- a/examples/toys/lightSwitch.js +++ b/examples/toys/lightSwitch.js @@ -16,12 +16,12 @@ /*global LightSwitch */ (function () { + var utilitiesScript = Script.resolvePath("../libraries/utils.js"); + Script.include(utilitiesScript); - var LightSwitch = function () { + LightSwitch = function () { this.switchSound = SoundCache.getSound("https://hifi-public.s3.amazonaws.com/sounds/Switches%20and%20sliders/lamp_switch_2.wav"); - print("SHNUUUUR") - - }; + } LightSwitch.prototype = { @@ -32,21 +32,57 @@ this.toggleLights(); }, - toggleLights: function () { - print("TOGGLE LIGHTS"); - }, - startNearGrabNonColliding: function () { this.toggleLights(); }, + toggleLights: function () { + var lightData = getEntityCustomData(this.resetKey, this.entityID, {}); + var on = lightData.on; + var lightType = lightData.type; + + var lights = Entities.findEntities(this.position, 20); + lights.forEach(function (light) { + var type = getEntityCustomData(this.resetKey, light, {}).type; + if (type === lightType) { + Entities.editEntity(light, { + visible: on + }); + } + }); + + this.flipSwitch(); + + setEntityCustomData(this.resetKey, this.entityID, { + on: !on + type: lightType, + resetMe: true + }); + }, + + flipSwitch: function () { + var rotation = Entities.getEntityProperties(this.entityID, "rotation").rotation; + var axis = { + x: 0, + y: 1, + z: 0 + }; + var dQ = Quat.angleAxis(180, axis); + rotation = Quat.multiply(rotation, dQ); + + Entities.editEntity(this.entityID, { + rotation: rotation + }); + }, preload: function (entityID) { this.entityID = entityID; + this.resetKey = "resetMe"; //The light switch is static, so just cache its position once this.position = Entities.getEntityProperties(this.entityID, "position").position; - }, + this.lightType = getEntityCustomData(this.resetKey, this.entityID, {}).type; + } }; // entity scripts always need to return a newly constructed object of our type return new LightSwitch(); -}()); \ No newline at end of file +}); \ No newline at end of file diff --git a/examples/toys/lightSwitchGarage.js b/examples/toys/lightSwitchGarage.js deleted file mode 100644 index 19d33117df..0000000000 --- a/examples/toys/lightSwitchGarage.js +++ /dev/null @@ -1,202 +0,0 @@ -// -// lightSwitchGarage.js.js -// examples/entityScripts -// -// Created by Eric Levin on 9/21/15. -// Copyright 2015 High Fidelity, Inc. -// -// -// Distributed under the Apache License, Version 2.0. -// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html -// - -(function() { - - var _this; - - // 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) - LightSwitchGarage = function() { - _this = this; - - this.lightStateKey = "lightStateKey"; - this.resetKey = "resetMe"; - - this.switchSound = SoundCache.getSound("https://hifi-public.s3.amazonaws.com/sounds/Switches%20and%20sliders/lamp_switch_2.wav"); - - }; - - LightSwitchGarage.prototype = { - - clickReleaseOnEntity: function(entityID, mouseEvent) { - if (!mouseEvent.isLeftButton) { - return; - } - this.toggleLights(); - }, - - startNearGrabNonColliding: function() { - this.toggleLights(); - }, - - toggleLights: function() { - var defaultLightData = { - on: false - }; - var lightState = getEntityCustomData(this.lightStateKey, this.entityID, defaultLightData); - if (lightState.on === true) { - this.clearLights(); - } else if (lightState.on === false) { - this.createLights(); - } - - this.flipLights(); - - Audio.playSound(this.switchSound, { - volume: 0.5, - position: this.position - }); - - }, - - clearLights: function() { - var entities = Entities.findEntities(MyAvatar.position, 100); - var self = this; - entities.forEach(function(entity) { - var resetData = getEntityCustomData(self.resetKey, entity, {}) - if (resetData.resetMe === true && resetData.lightType === "Sconce Light Garage") { - Entities.deleteEntity(entity); - } - }); - - setEntityCustomData(this.lightStateKey, this.entityID, { - on: false - }); - }, - - createLights: function() { - - var sconceLight3 = Entities.addEntity({ - type: "Light", - position: { - x: 545.49468994140625, - y: 496.24026489257812, - z: 500.63516235351562 - }, - - name: "Sconce 3 Light", - dimensions: { - x: 2.545, - y: 2.545, - z: 2.545 - }, - cutoff: 90, - color: { - red: 217, - green: 146, - blue: 24 - } - }); - - setEntityCustomData(this.resetKey, sconceLight3, { - resetMe: true, - lightType: "Sconce Light Garage" - }); - - var sconceLight4 = Entities.addEntity({ - type: "Light", - position: { - x: 550.90399169921875, - y: 496.24026489257812, - z: 507.90237426757812 - }, - name: "Sconce 4 Light", - dimensions: { - x: 2.545, - y: 2.545, - z: 2.545 - }, - cutoff: 90, - color: { - red: 217, - green: 146, - blue: 24 - } - }); - - setEntityCustomData(this.resetKey, sconceLight4, { - resetMe: true, - lightType: "Sconce Light Garage" - }); - - var sconceLight5 = Entities.addEntity({ - type: "Light", - position: { - x: 548.407958984375, - y: 496.24026489257812, - z: 509.5504150390625 - }, - name: "Sconce 5 Light", - dimensions: { - x: 2.545, - y: 2.545, - z: 2.545 - }, - cutoff: 90, - color: { - red: 217, - green: 146, - blue: 24 - } - }); - - setEntityCustomData(this.resetKey, sconceLight5, { - resetMe: true, - lightType: "Sconce Light Garage" - }); - - setEntityCustomData(this.lightStateKey, this.entityID, { - on: true - }); - }, - - flipLights: function() { - // flip model to give illusion of light switch being flicked - var rotation = Entities.getEntityProperties(this.entityID, "rotation").rotation; - var axis = { - x: 0, - y: 1, - z: 0 - }; - var dQ = Quat.angleAxis(180, axis); - rotation = Quat.multiply(rotation, dQ); - - - Entities.editEntity(this.entityID, { - rotation: rotation - }); - - }, - - preload: function(entityID) { - this.entityID = entityID; - - //The light switch is static, so just cache its position once - this.position = Entities.getEntityProperties(this.entityID, "position").position; - var defaultLightData = { - on: false - }; - var lightState = getEntityCustomData(this.lightStateKey, this.entityID, defaultLightData); - - //If light is off, then we create two new lights- at the position of the sconces - if (lightState.on === false) { - this.createLights(); - this.flipLights(); - } - //If lights are on, do nothing! - }, - }; - - // entity scripts always need to return a newly constructed object of our type - return new LightSwitchGarage(); -}) diff --git a/examples/toys/lightSwitchHall.js b/examples/toys/lightSwitchHall.js deleted file mode 100644 index e1093311f9..0000000000 --- a/examples/toys/lightSwitchHall.js +++ /dev/null @@ -1,179 +0,0 @@ -// -// lightSwitchHall.js -// examples/entityScripts -// -// Created by Eric Levin on 9/21/15. -// Copyright 2015 High Fidelity, Inc. -// -// -// Distributed under the Apache License, Version 2.0. -// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html -// - -(function() { - - var _this; - - - // 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) - LightSwitchHall = function() { - _this = this; - - this.lightStateKey = "lightStateKey"; - this.resetKey = "resetMe"; - - this.switchSound = SoundCache.getSound("https://hifi-public.s3.amazonaws.com/sounds/Switches%20and%20sliders/lamp_switch_2.wav"); - }; - - LightSwitchHall.prototype = { - - clickReleaseOnEntity: function(entityId, mouseEvent) { - if (!mouseEvent.isLeftButton) { - return; - } - this.toggleLights(); - }, - - startNearGrabNonColliding: function() { - this.toggleLights(); - }, - - toggleLights: function() { - var defaultLightData = { - on: false - }; - var lightState = getEntityCustomData(this.lightStateKey, this.entityID, defaultLightData); - if (lightState.on === true) { - this.clearLights(); - } else if (lightState.on === false) { - this.createLights(); - } - - // flip model to give illusion of light switch being flicked - this.flipLights(); - - Audio.playSound(this.switchSound, { - volume: 0.5, - position: this.position - }); - - }, - - clearLights: function() { - var entities = Entities.findEntities(MyAvatar.position, 100); - var self = this; - entities.forEach(function(entity) { - var resetData = getEntityCustomData(self.resetKey, entity, {}) - if (resetData.resetMe === true && resetData.lightType === "Sconce Light Hall") { - Entities.deleteEntity(entity); - } - }); - - setEntityCustomData(this.lightStateKey, this.entityID, { - on: false - }); - }, - - createLights: function() { - var sconceLight1 = Entities.addEntity({ - type: "Light", - position: { - x: 543.75, - y: 496.24, - z: 511.13 - }, - name: "Sconce 1 Light", - dimensions: { - x: 2.545, - y: 2.545, - z: 2.545 - }, - cutoff: 90, - color: { - red: 217, - green: 146, - blue: 24 - } - }); - - setEntityCustomData(this.resetKey, sconceLight1, { - resetMe: true, - lightType: "Sconce Light Hall" - }); - - var sconceLight2 = Entities.addEntity({ - type: "Light", - position: { - x: 540.1, - y: 496.24, - z: 505.57 - }, - name: "Sconce 2 Light", - dimensions: { - x: 2.545, - y: 2.545, - z: 2.545 - }, - cutoff: 90, - color: { - red: 217, - green: 146, - blue: 24 - } - }); - - setEntityCustomData(this.resetKey, sconceLight2, { - resetMe: true, - lightType: "Sconce Light Hall" - }); - - setEntityCustomData(this.lightStateKey, this.entityID, { - on: true - }); - - }, - - flipLights: function() { - // flip model to give illusion of light switch being flicked - var rotation = Entities.getEntityProperties(this.entityID, "rotation").rotation; - var axis = { - x: 0, - y: 1, - z: 0 - }; - var dQ = Quat.angleAxis(180, axis); - rotation = Quat.multiply(rotation, dQ); - - - Entities.editEntity(this.entityID, { - rotation: rotation - }); - - }, - - // 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: - preload: function(entityID) { - this.entityID = entityID; - - //The light switch is static, so just cache its position once - this.position = Entities.getEntityProperties(this.entityID, "position").position; - var defaultLightData = { - on: false - }; - var lightState = getEntityCustomData(this.lightStateKey, this.entityID, defaultLightData); - - //If light is off, then we create two new lights- at the position of the sconces - if (lightState.on === false) { - this.createLights(); - this.flipLights(); - - } - //If lights are on, do nothing! - }, - }; - - // entity scripts always need to return a newly constructed object of our type - return new LightSwitchHall(); -}) diff --git a/unpublishedScripts/masterReset.js b/unpublishedScripts/masterReset.js index 40cce2582c..5f93218dcb 100644 --- a/unpublishedScripts/masterReset.js +++ b/unpublishedScripts/masterReset.js @@ -111,6 +111,20 @@ function createLights() { var scriptURL = Script.resolvePath("../examples/toys/lightSwitch.js"); + var rotation = { + w: 0.63280689716339111, + x: 0.63280689716339111, + y: -0.31551080942153931, + z: 0.31548023223876953 + }; + var axis = { + x: 0, + y: 1, + z: 0 + }; + var dQ = Quat.angleAxis(180, axis); + rotation = Quat.multiply(rotation, dQ); + var lightSwitchHall = Entities.addEntity({ type: "Model", modelURL: modelURL, @@ -121,12 +135,7 @@ function createLights() { y: 495.67999267578125, z: 511.00564575195312 }, - rotation: { - w: 0.63280689716339111, - x: 0.63280689716339111, - y: -0.31551080942153931, - z: 0.31548023223876953 - }, + rotation: rotation, dimensions: { x: 0.10546875, y: 0.032372996211051941, @@ -134,7 +143,9 @@ function createLights() { } }); setEntityCustomData(resetKey, lightSwitchHall, { - resetMe: true + resetMe: true, + on: true, + type: "Hall Light" }); var sconceLight1 = Entities.addEntity({ @@ -160,8 +171,7 @@ function createLights() { setEntityCustomData(resetKey, sconceLight1, { resetMe: true, - lightType: "Sconce Light", - on: true + type: "Hall Light", }); var sconceLight2 = Entities.addEntity({ @@ -187,26 +197,35 @@ function createLights() { setEntityCustomData(resetKey, sconceLight2, { resetMe: true, - lightType: "Sconce Light", - state: true + type: "Hall Light", }); + + rotation = { + w: 0.20082402229309082, + x: 0.20082402229309082, + y: -0.67800414562225342, + z: 0.67797362804412842 + }; + axis = { + x: 0, + y: 1, + z: 0 + }; + dQ = Quat.angleAxis(180, axis); + rotation = Quat.multiply(rotation, dQ); + var lightSwitchGarage = Entities.addEntity({ type: "Model", modelURL: modelURL, - name: "Light Switch Garage", + name: "Garage", script: scriptURL, position: { x: 545.62, y: 495.68, z: 500.21 }, - rotation: { - w: 0.20082402229309082, - x: 0.20082402229309082, - y: -0.67800414562225342, - z: 0.67797362804412842 - }, + rotation: rotation, dimensions: { x: 0.10546875, y: 0.032372996211051941, @@ -215,7 +234,9 @@ function createLights() { }); setEntityCustomData(resetKey, lightSwitchGarage, { - resetMe: true + resetMe: true, + on: true, + type: "Garage Light" }); @@ -245,8 +266,7 @@ function createLights() { setEntityCustomData(resetKey, sconceLight3, { resetMe: true, - lightType: "Sconce Light", - on: true + type: "Garage Light", }); var sconceLight4 = Entities.addEntity({ @@ -272,8 +292,7 @@ function createLights() { setEntityCustomData(resetKey, sconceLight4, { resetMe: true, - lightType: "Sconce Light", - on: true + type: "Garage Light", }); var sconceLight5 = Entities.addEntity({ @@ -299,8 +318,7 @@ function createLights() { setEntityCustomData(resetKey, sconceLight5, { resetMe: true, - lightType: "Sconce Light", - on: true + type: "Garage Light", }); } From 1ac1fcaf6958bfe380f96d605f2900a3aa091a50 Mon Sep 17 00:00:00 2001 From: ericrius1 Date: Fri, 2 Oct 2015 16:17:54 -0700 Subject: [PATCH 4/5] Light switches work properly now; no extra lights --- examples/toys/lightSwitch.js | 18 +++++++++++------- unpublishedScripts/masterReset.js | 2 +- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/examples/toys/lightSwitch.js b/examples/toys/lightSwitch.js index 9a23a3b6d4..9494db4479 100644 --- a/examples/toys/lightSwitch.js +++ b/examples/toys/lightSwitch.js @@ -16,10 +16,11 @@ /*global LightSwitch */ (function () { + var _this; var utilitiesScript = Script.resolvePath("../libraries/utils.js"); Script.include(utilitiesScript); - LightSwitch = function () { + _this = this; this.switchSound = SoundCache.getSound("https://hifi-public.s3.amazonaws.com/sounds/Switches%20and%20sliders/lamp_switch_2.wav"); } @@ -38,23 +39,27 @@ toggleLights: function () { var lightData = getEntityCustomData(this.resetKey, this.entityID, {}); - var on = lightData.on; + var on = !lightData.on; var lightType = lightData.type; var lights = Entities.findEntities(this.position, 20); lights.forEach(function (light) { - var type = getEntityCustomData(this.resetKey, light, {}).type; - if (type === lightType) { + var type = getEntityCustomData(_this.resetKey, light, {}).type; + if (type === lightType && JSON.stringify(light) !== JSON.stringify(_this.entityID)) { Entities.editEntity(light, { visible: on }); + } }); - this.flipSwitch(); + Audio.playSound(this.switchSound, { + volume: 0.5, + position: this.position + }); setEntityCustomData(this.resetKey, this.entityID, { - on: !on + on: on, type: lightType, resetMe: true }); @@ -79,7 +84,6 @@ this.resetKey = "resetMe"; //The light switch is static, so just cache its position once this.position = Entities.getEntityProperties(this.entityID, "position").position; - this.lightType = getEntityCustomData(this.resetKey, this.entityID, {}).type; } }; diff --git a/unpublishedScripts/masterReset.js b/unpublishedScripts/masterReset.js index 5f93218dcb..9f4543dc8f 100644 --- a/unpublishedScripts/masterReset.js +++ b/unpublishedScripts/masterReset.js @@ -218,7 +218,7 @@ function createLights() { var lightSwitchGarage = Entities.addEntity({ type: "Model", modelURL: modelURL, - name: "Garage", + name: "Light Switch Garage", script: scriptURL, position: { x: 545.62, From 38a66bc9153498f17fe0f7b8f1e61666f853bd99 Mon Sep 17 00:00:00 2001 From: ericrius1 Date: Fri, 2 Oct 2015 16:53:13 -0700 Subject: [PATCH 5/5] syntax fixes --- examples/toys/lightSwitch.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/toys/lightSwitch.js b/examples/toys/lightSwitch.js index 9494db4479..5c47108c94 100644 --- a/examples/toys/lightSwitch.js +++ b/examples/toys/lightSwitch.js @@ -1,5 +1,5 @@ // -// lightSwitch.js.js +// lightSwitch.js // examples/entityScripts // // Created by Eric Levin on 10/2/15. @@ -22,7 +22,7 @@ LightSwitch = function () { _this = this; this.switchSound = SoundCache.getSound("https://hifi-public.s3.amazonaws.com/sounds/Switches%20and%20sliders/lamp_switch_2.wav"); - } + }; LightSwitch.prototype = {