-
Notifications
You must be signed in to change notification settings - Fork 502
/
Copy pathattachedEntitiesManager.js
285 lines (249 loc) · 11.2 KB
/
attachedEntitiesManager.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
//
// attachedEntitiesManager.js
//
// Created by Seth Alves on 2016-1-20
// Copyright 2016 High Fidelity, Inc.
//
// This script handles messages from the grab script related to wearables, and interacts with a doppelganger.
//
// Distributed under the Apache License, Version 2.0.
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
//
Script.include("libraries/utils.js");
var DEFAULT_WEARABLE_DATA = {
joints: {}
};
var MINIMUM_DROP_DISTANCE_FROM_JOINT = 0.8;
var ATTACHED_ENTITY_SEARCH_DISTANCE = 10.0;
var ATTACHED_ENTITIES_SETTINGS_KEY = "ATTACHED_ENTITIES";
var DRESSING_ROOM_DISTANCE = 2.0;
var SHOW_TOOL_BAR = false;
// tool bar
if (SHOW_TOOL_BAR) {
var BUTTON_SIZE = 64;
var PADDING = 6;
Script.include(["libraries/toolBars.js"]);
var toolBar = new ToolBar(0, 0, ToolBar.HORIZONTAL, "highfidelity.attachedEntities.toolbar");
var lockButton = toolBar.addTool({
width: BUTTON_SIZE,
height: BUTTON_SIZE,
imageURL: Script.resolvePath("assets/images/lock.svg"),
color: {
red: 255,
green: 255,
blue: 255
},
alpha: 1,
visible: true
}, false);
}
function mousePressEvent(event) {
var clickedOverlay = Overlays.getOverlayAtPoint({
x: event.x,
y: event.y
});
if (lockButton === toolBar.clicked(clickedOverlay)) {
manager.toggleLocked();
}
}
function scriptEnding() {
if (SHOW_TOOL_BAR) {
toolBar.cleanup();
}
}
if (SHOW_TOOL_BAR) {
Controller.mousePressEvent.connect(mousePressEvent);
}
Script.scriptEnding.connect(scriptEnding);
// attached entites
function AttachedEntitiesManager() {
var clothingLocked = false;
this.subscribeToMessages = function() {
Messages.subscribe('Hifi-Object-Manipulation');
Messages.messageReceived.connect(this.handleWearableMessages);
}
this.handleWearableMessages = function(channel, message, sender) {
if (channel !== 'Hifi-Object-Manipulation') {
return;
}
var parsedMessage = null;
try {
parsedMessage = JSON.parse(message);
} catch (e) {
print('error parsing wearable message');
return;
}
if (parsedMessage.action === 'update' ||
parsedMessage.action === 'loaded') {
// ignore
} else if (parsedMessage.action === 'release') {
manager.handleEntityRelease(parsedMessage.grabbedEntity, parsedMessage.joint)
// manager.saveAttachedEntities();
} else if (parsedMessage.action === 'equip') {
// manager.saveAttachedEntities();
} else {
print('attachedEntitiesManager -- unknown actions: ' + parsedMessage.action);
}
}
this.handleEntityRelease = function(grabbedEntity, releasedFromJoint) {
// if this is still equipped, just rewrite the position information.
var grabData = getEntityCustomData('grabKey', grabbedEntity, {});
var allowedJoints = getEntityCustomData('wearable', grabbedEntity, DEFAULT_WEARABLE_DATA).joints;
var props = Entities.getEntityProperties(grabbedEntity, ["position", "parentID", "parentJointIndex"]);
if (props.parentID === Uuid.NULL || props.parentID === MyAvatar.sessionUUID) {
var bestJointName = "";
var bestJointIndex = -1;
var bestJointDistance = 0;
var bestJointOffset = null;
for (var jointName in allowedJoints) {
if ((releasedFromJoint == "LeftHand" || releasedFromJoint == "RightHand") &&
(jointName == "LeftHand" || jointName == "RightHand")) {
// don't auto-attach to a hand if a hand just dropped something
continue;
}
var jointIndex = MyAvatar.getJointIndex(jointName);
if (jointIndex >= 0) {
var jointPosition = MyAvatar.getJointPosition(jointIndex);
var distanceFromJoint = Vec3.distance(jointPosition, props.position);
if (distanceFromJoint <= MINIMUM_DROP_DISTANCE_FROM_JOINT) {
if (bestJointIndex == -1 || distanceFromJoint < bestJointDistance) {
bestJointName = jointName;
bestJointIndex = jointIndex;
bestJointDistance = distanceFromJoint;
bestJointOffset = allowedJoints[jointName];
}
}
}
}
if (bestJointIndex != -1) {
var wearProps = Entities.getEntityProperties(grabbedEntity);
wearProps.parentID = MyAvatar.sessionUUID;
wearProps.parentJointIndex = bestJointIndex;
delete wearProps.localPosition;
delete wearProps.localRotation;
var updatePresets = false;
if (bestJointOffset && bestJointOffset.constructor === Array) {
if (!clothingLocked || bestJointOffset.length < 2) {
// we're unlocked or this thing didn't have a preset position, so update it
updatePresets = true;
} else {
// don't snap the entity to the preferred position if unlocked
wearProps.localPosition = bestJointOffset[0];
wearProps.localRotation = bestJointOffset[1];
}
}
Entities.deleteEntity(grabbedEntity);
//the true boolean here after add entity adds it as an 'avatar entity', which can travel with you from server to server.
var newEntity = Entities.addEntity(wearProps, true);
if (updatePresets) {
this.updateRelativeOffsets(newEntity);
}
} else if (props.parentID != Uuid.NULL) {
// drop the entity and set it to have no parent (not on the avatar), unless it's being equipped in a hand.
if (props.parentID === MyAvatar.sessionUUID &&
(props.parentJointIndex == MyAvatar.getJointIndex("RightHand") ||
props.parentJointIndex == MyAvatar.getJointIndex("LeftHand"))) {
// this is equipped on a hand -- don't clear the parent.
} else {
var wearProps = Entities.getEntityProperties(grabbedEntity);
wearProps.parentID = Uuid.NULL;
wearProps.parentJointIndex = -1;
delete wearProps.id;
delete wearProps.created;
delete wearProps.age;
delete wearProps.ageAsText;
delete wearProps.naturalDimensions;
delete wearProps.naturalPosition;
delete wearProps.actionData;
delete wearProps.sittingPoints;
delete wearProps.boundingBox;
delete wearProps.avatarEntity;
delete wearProps.owningAvatarID;
delete wearProps.localPosition;
delete wearProps.localRotation;
Entities.deleteEntity(grabbedEntity);
Entities.addEntity(wearProps);
}
}
}
}
this.updateRelativeOffsets = function(entityID) {
// save the preferred (current) relative position and rotation into the user-data of the entity
var props = Entities.getEntityProperties(entityID);
if (props.parentID == MyAvatar.sessionUUID) {
grabData = getEntityCustomData('grabKey', entityID, {});
var wearableData = getEntityCustomData('wearable', entityID, DEFAULT_WEARABLE_DATA);
var currentJointName = MyAvatar.getJointNames()[props.parentJointIndex];
wearableData.joints[currentJointName] = [props.localPosition, props.localRotation];
setEntityCustomData('wearable', entityID, wearableData);
return true;
}
return false;
}
// this.saveAttachedEntities = function() {
// print("--- saving attached entities ---");
// saveData = [];
// var nearbyEntities = Entities.findEntities(MyAvatar.position, ATTACHED_ENTITY_SEARCH_DISTANCE);
// for (i = 0; i < nearbyEntities.length; i++) {
// var entityID = nearbyEntities[i];
// if (this.updateRelativeOffsets(entityID)) {
// var props = Entities.getEntityProperties(entityID); // refresh, because updateRelativeOffsets changed them
// this.scrubProperties(props);
// saveData.push(props);
// }
// }
// Settings.setValue(ATTACHED_ENTITIES_SETTINGS_KEY, JSON.stringify(saveData));
// }
// this.scrubProperties = function(props) {
// var toScrub = ["queryAACube", "position", "rotation",
// "created", "ageAsText", "naturalDimensions",
// "naturalPosition", "velocity", "acceleration",
// "angularVelocity", "boundingBox"];
// toScrub.forEach(function(propertyName) {
// delete props[propertyName];
// });
// // if the userData has a grabKey, clear old state
// if ("userData" in props) {
// try {
// parsedUserData = JSON.parse(props.userData);
// if ("grabKey" in parsedUserData) {
// parsedUserData.grabKey.refCount = 0;
// delete parsedUserData.grabKey["avatarId"];
// props["userData"] = JSON.stringify(parsedUserData);
// }
// } catch (e) {
// }
// }
// }
// this.loadAttachedEntities = function(grabbedEntity) {
// print("--- loading attached entities ---");
// jsonAttachmentData = Settings.getValue(ATTACHED_ENTITIES_SETTINGS_KEY);
// var loadData = [];
// try {
// loadData = JSON.parse(jsonAttachmentData);
// } catch (e) {
// print('error parsing saved attachment data');
// return;
// }
// for (i = 0; i < loadData.length; i ++) {
// var savedProps = loadData[ i ];
// var currentProps = Entities.getEntityProperties(savedProps.id);
// if (currentProps.id == savedProps.id &&
// // TODO -- also check that parentJointIndex matches?
// currentProps.parentID == MyAvatar.sessionUUID) {
// // entity is already in-world. TODO -- patch it up?
// continue;
// }
// this.scrubProperties(savedProps);
// delete savedProps["id"];
// savedProps.parentID = MyAvatar.sessionUUID; // this will change between sessions
// var loadedEntityID = Entities.addEntity(savedProps, true);
// Messages.sendMessage('Hifi-Object-Manipulation', JSON.stringify({
// action: 'loaded',
// grabbedEntity: loadedEntityID
// }));
// }
// }
}
var manager = new AttachedEntitiesManager();
manager.subscribeToMessages();