Light switches work properly now; no extra lights

This commit is contained in:
ericrius1 2015-10-02 16:17:54 -07:00
parent e6e60e5e7a
commit 1ac1fcaf69
2 changed files with 12 additions and 8 deletions

View file

@ -16,10 +16,11 @@
/*global LightSwitch */ /*global LightSwitch */
(function () { (function () {
var _this;
var utilitiesScript = Script.resolvePath("../libraries/utils.js"); var utilitiesScript = Script.resolvePath("../libraries/utils.js");
Script.include(utilitiesScript); Script.include(utilitiesScript);
LightSwitch = function () { LightSwitch = function () {
_this = this;
this.switchSound = SoundCache.getSound("https://hifi-public.s3.amazonaws.com/sounds/Switches%20and%20sliders/lamp_switch_2.wav"); this.switchSound = SoundCache.getSound("https://hifi-public.s3.amazonaws.com/sounds/Switches%20and%20sliders/lamp_switch_2.wav");
} }
@ -38,23 +39,27 @@
toggleLights: function () { toggleLights: function () {
var lightData = getEntityCustomData(this.resetKey, this.entityID, {}); var lightData = getEntityCustomData(this.resetKey, this.entityID, {});
var on = lightData.on; var on = !lightData.on;
var lightType = lightData.type; var lightType = lightData.type;
var lights = Entities.findEntities(this.position, 20); var lights = Entities.findEntities(this.position, 20);
lights.forEach(function (light) { lights.forEach(function (light) {
var type = getEntityCustomData(this.resetKey, light, {}).type; var type = getEntityCustomData(_this.resetKey, light, {}).type;
if (type === lightType) { if (type === lightType && JSON.stringify(light) !== JSON.stringify(_this.entityID)) {
Entities.editEntity(light, { Entities.editEntity(light, {
visible: on visible: on
}); });
} }
}); });
this.flipSwitch(); this.flipSwitch();
Audio.playSound(this.switchSound, {
volume: 0.5,
position: this.position
});
setEntityCustomData(this.resetKey, this.entityID, { setEntityCustomData(this.resetKey, this.entityID, {
on: !on on: on,
type: lightType, type: lightType,
resetMe: true resetMe: true
}); });
@ -79,7 +84,6 @@
this.resetKey = "resetMe"; this.resetKey = "resetMe";
//The light switch is static, so just cache its position once //The light switch is static, so just cache its position once
this.position = Entities.getEntityProperties(this.entityID, "position").position; this.position = Entities.getEntityProperties(this.entityID, "position").position;
this.lightType = getEntityCustomData(this.resetKey, this.entityID, {}).type;
} }
}; };

View file

@ -218,7 +218,7 @@ function createLights() {
var lightSwitchGarage = Entities.addEntity({ var lightSwitchGarage = Entities.addEntity({
type: "Model", type: "Model",
modelURL: modelURL, modelURL: modelURL,
name: "Garage", name: "Light Switch Garage",
script: scriptURL, script: scriptURL,
position: { position: {
x: 545.62, x: 545.62,