mirror of
https://github.com/HifiExperiments/overte.git
synced 2025-08-09 17:28:13 +02:00
Only update userData when light entity is identified
This commit is contained in:
parent
92d3944044
commit
afb96709f1
1 changed files with 53 additions and 38 deletions
|
@ -1,6 +1,5 @@
|
||||||
(function() {
|
(function() {
|
||||||
this.entityID = null;
|
this.entityID = null;
|
||||||
this.properties = null;
|
|
||||||
this.lightID = null;
|
this.lightID = null;
|
||||||
this.sound = null;
|
this.sound = null;
|
||||||
|
|
||||||
|
@ -51,19 +50,6 @@
|
||||||
Entities.editEntity(entityID, { userData: JSON.stringify(userData) });
|
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
|
// Download sound if needed
|
||||||
this.maybeDownloadSound = function() {
|
this.maybeDownloadSound = function() {
|
||||||
if (this.sound === null) {
|
if (this.sound === null) {
|
||||||
|
@ -81,17 +67,21 @@
|
||||||
print("Warning: Couldn't play sound.");
|
print("Warning: Couldn't play sound.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Toggles the associated light entity
|
// Checks whether the userData is well-formed and updates it if not
|
||||||
this.toggleLight = function() {
|
this.checkUserData = function() {
|
||||||
if (this.lightID) {
|
var userData = getUserData(this.entityID);
|
||||||
var lightProperties = Entities.getEntityProperties(this.lightID);
|
if (!userData) {
|
||||||
Entities.editEntity(this.lightID, { visible: !lightProperties.visible });
|
userData = DEFAULT_USER_DATA;
|
||||||
} else {
|
} else if (!userData.lightDefaultProperties) {
|
||||||
print("Warning: No light to turn on/off");
|
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) {
|
this.createLight = function(userData) {
|
||||||
var lightProperties = copyObject(userData.lightDefaultProperties);
|
var lightProperties = copyObject(userData.lightDefaultProperties);
|
||||||
if (lightProperties) {
|
if (lightProperties) {
|
||||||
|
@ -108,34 +98,48 @@
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Tries to find a valid light, creates one otherwise
|
||||||
this.updateLightID = function() {
|
this.updateLightID = function() {
|
||||||
var userData = getUserData(this.entityID);
|
|
||||||
|
|
||||||
// Find valid light
|
// Find valid light
|
||||||
if (doesEntityExistNow(this.lightID)) {
|
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;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var userData = getUserData(this.entityID);
|
||||||
if (doesEntityExistNow(userData.lightID)) {
|
if (doesEntityExistNow(userData.lightID)) {
|
||||||
this.lightID = getTrueID(userData.lightID);
|
this.lightID = userData.lightID;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// No valid light, create one
|
if (!userData.creatingLight) {
|
||||||
this.lightID = this.createLight(userData);
|
// No valid light, create one
|
||||||
print("Created new light entity");
|
userData.creatingLight = true;
|
||||||
|
updateUserData(this.entityID, userData);
|
||||||
// Update user data with new ID
|
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.lightID = this.lightID;
|
||||||
|
userData.creatingLight = false;
|
||||||
updateUserData(this.entityID, userData);
|
updateUserData(this.entityID, userData);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Moves light entity if the lamp entity moved
|
||||||
this.maybeMoveLight = function() {
|
this.maybeMoveLight = function() {
|
||||||
var entityProperties = Entities.getEntityProperties(this.entityID);
|
var entityProperties = Entities.getEntityProperties(this.entityID);
|
||||||
var lightProperties = Entities.getEntityProperties(this.lightID);
|
var lightProperties = Entities.getEntityProperties(this.lightID);
|
||||||
|
@ -151,8 +155,9 @@
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Stores light entity relative position in the lamp metadata
|
||||||
this.updateRelativeLightPosition = function() {
|
this.updateRelativeLightPosition = function() {
|
||||||
if (!doesEntityExistNow(this.entityID) || !doesEntityExistNow(this.lightID)) {
|
if (!doesEntityExistNow(this.lightID)) {
|
||||||
print("Warning: ID invalid, couldn't save relative position.");
|
print("Warning: ID invalid, couldn't save relative position.");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -189,6 +194,17 @@
|
||||||
this.checkUserData();
|
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.preload = function(entityID) {
|
||||||
this.preOperation(entityID);
|
this.preOperation(entityID);
|
||||||
};
|
};
|
||||||
|
@ -200,7 +216,6 @@
|
||||||
this.updateLightID();
|
this.updateLightID();
|
||||||
this.maybeMoveLight();
|
this.maybeMoveLight();
|
||||||
this.toggleLight();
|
this.toggleLight();
|
||||||
this.playSound();
|
|
||||||
} else if (mouseEvent.isRightButton) {
|
} else if (mouseEvent.isRightButton) {
|
||||||
this.updateRelativeLightPosition();
|
this.updateRelativeLightPosition();
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue