History

interface decides on entityIDs rather than bouncing a temporary id through the entity server

Seth Alves committed 11 years ago

Commit f9d46b01 parent f188078104 files · +490 −1235Browse files

Showing the changes to libraries/entities/src/EntityScriptingInterface.cpp (1 of 104 files). Show all

Unified · Split

libraries/entities/src/EntityScriptingInterface.cpp+34−86
@@ -20,7 +20,6 @@ · 19 unmodified lines
2020
2121
2222 EntityScriptingInterface::EntityScriptingInterface() :
23− _nextCreatorTokenID(0),
2423 _entityTree(NULL)
2524 {
2625 auto nodeList = DependencyManager::get<NodeList>();
@@ -29,7 +28,7 @@ EntityScriptingInterface::EntityScriptingInterface() : · 2 unmodified lines
2928 }
3029
3130 void EntityScriptingInterface::queueEntityMessage(PacketType packetType,
32− EntityItemID entityID, const EntityItemProperties& properties) {
31+ QUuid entityID, const EntityItemProperties& properties) {
3332 getEntityPacketSender()->queueEditEntityMessage(packetType, entityID, properties);
3433 }
3534
@@ -71,14 +70,10 @@ void bidForSimulationOwnership(EntityItemProperties& properties) { · 35 unmodified lines
7170
7271
7372
74−EntityItemID EntityScriptingInterface::addEntity(const EntityItemProperties& properties) {
75−
76− // The application will keep track of creatorTokenID
77− uint32_t creatorTokenID = EntityItemID::getNextCreatorTokenID();
73+QUuid EntityScriptingInterface::addEntity(const EntityItemProperties& properties) {
7874
7975 EntityItemProperties propertiesWithSimID = properties;
80−
81− EntityItemID id(NEW_ENTITY, creatorTokenID, false);
76+ QUuid id = QUuid::createUuid();
8277
8378 // If we have a local entity tree set, then also update it.
8479 bool success = true;
@@ -104,25 +99,8 @@ EntityItemID EntityScriptingInterface::addEntity(const EntityItemProperties& pro · 19 unmodified lines
10499 return id;
105100 }
106101
107−EntityItemID EntityScriptingInterface::identifyEntity(EntityItemID entityID) {
108− EntityItemID actualID = entityID;
109−
110− if (!entityID.isKnownID) {
111− actualID = EntityItemID::getIDfromCreatorTokenID(entityID.creatorTokenID);
112− if (actualID == UNKNOWN_ENTITY_ID) {
113− return entityID; // bailing early
114− }
115−
116− // found it!
117− entityID.id = actualID.id;
118− entityID.isKnownID = true;
119− }
120− return entityID;
121−}
122−
123−EntityItemProperties EntityScriptingInterface::getEntityProperties(EntityItemID entityID) {
102+EntityItemProperties EntityScriptingInterface::getEntityProperties(QUuid identity) {
124103 EntityItemProperties results;
125− EntityItemID identity = identifyEntity(entityID);
126104 if (_entityTree) {
127105 _entityTree->lockForRead();
128106 EntityItem* entity = const_cast<EntityItem*>(_entityTree->findEntityByEntityItemID(identity));
@@ -142,8 +120,6 @@ EntityItemProperties EntityScriptingInterface::getEntityProperties(EntityItemID · 13 unmodified lines
142120 }
143121 }
144122
145− } else {
146− results.setIsUnknownID();
147123 }
148124 _entityTree->unlock();
149125 }
@@ -151,59 +127,33 @@ EntityItemProperties EntityScriptingInterface::getEntityProperties(EntityItemID · 1 unmodified line
151127 return results;
152128 }
153129
154−EntityItemID EntityScriptingInterface::editEntity(EntityItemID entityID, const EntityItemProperties& properties) {
155− EntityItemID actualID = entityID;
156− // if the entity is unknown, attempt to look it up
157− if (!entityID.isKnownID) {
158− actualID = EntityItemID::getIDfromCreatorTokenID(entityID.creatorTokenID);
159− if (actualID.id != UNKNOWN_ENTITY_ID) {
160− entityID.id = actualID.id;
161− entityID.isKnownID = true;
162− }
163− }
164−
165− // If we have a local entity tree set, then also update it. We can do this even if we don't know
166− // the actual id, because we can edit out local entities just with creatorTokenID
130+QUuid EntityScriptingInterface::editEntity(QUuid entityID, const EntityItemProperties& properties) {
131+ // If we have a local entity tree set, then also update it.
167132 if (_entityTree) {
168133 _entityTree->lockForWrite();
169134 _entityTree->updateEntity(entityID, properties);
170135 _entityTree->unlock();
171136 }
172137
173− // if at this point, we know the id, send the update to the entity server
174− if (entityID.isKnownID) {
175− // make sure the properties has a type, so that the encode can know which properties to include
176− if (properties.getType() == EntityTypes::Unknown) {
177− EntityItem* entity = _entityTree->findEntityByEntityItemID(entityID);
178− if (entity) {
179− // we need to change the outgoing properties, so we make a copy, modify, and send.
180− EntityItemProperties modifiedProperties = properties;
181− entity->setLastBroadcast(usecTimestampNow());
182− modifiedProperties.setType(entity->getType());
183− bidForSimulationOwnership(modifiedProperties);
184− queueEntityMessage(PacketTypeEntityAddOrEdit, entityID, modifiedProperties);
185− return entityID;
186− }
138+ // make sure the properties has a type, so that the encode can know which properties to include
139+ if (properties.getType() == EntityTypes::Unknown) {
140+ EntityItem* entity = _entityTree->findEntityByEntityItemID(entityID);
141+ if (entity) {
142+ // we need to change the outgoing properties, so we make a copy, modify, and send.
143+ EntityItemProperties modifiedProperties = properties;
144+ entity->setLastBroadcast(usecTimestampNow());
145+ modifiedProperties.setType(entity->getType());
146+ bidForSimulationOwnership(modifiedProperties);
147+ queueEntityMessage(PacketTypeEntityAddOrEdit, entityID, modifiedProperties);
148+ return entityID;
187149 }
188−
189− queueEntityMessage(PacketTypeEntityAddOrEdit, entityID, properties);
190150 }
191−
151+
152+ queueEntityMessage(PacketTypeEntityAddOrEdit, entityID, properties);
192153 return entityID;
193154 }
194155
195−void EntityScriptingInterface::deleteEntity(EntityItemID entityID) {
196−
197− EntityItemID actualID = entityID;
198−
199− // if the entity is unknown, attempt to look it up
200− if (!entityID.isKnownID) {
201− actualID = EntityItemID::getIDfromCreatorTokenID(entityID.creatorTokenID);
202− if (actualID.id != UNKNOWN_ENTITY_ID) {
203− entityID.id = actualID.id;
204− entityID.isKnownID = true;
205− }
206− }
156+void EntityScriptingInterface::deleteEntity(QUuid entityID) {
207157
208158 bool shouldDelete = true;
209159
@@ -211,7 +161,7 @@ void EntityScriptingInterface::deleteEntity(EntityItemID entityID) { · 1 unmodified line
211161 if (_entityTree) {
212162 _entityTree->lockForWrite();
213163
214− EntityItem* entity = const_cast<EntityItem*>(_entityTree->findEntityByEntityItemID(actualID));
164+ EntityItem* entity = const_cast<EntityItem*>(_entityTree->findEntityByEntityItemID(entityID));
215165 if (entity) {
216166 if (entity->getLocked()) {
217167 shouldDelete = false;
@@ -224,19 +174,19 @@ void EntityScriptingInterface::deleteEntity(EntityItemID entityID) { · 6 unmodified lines
224174 }
225175
226176 // if at this point, we know the id, and we should still delete the entity, send the update to the entity server
227− if (shouldDelete && entityID.isKnownID) {
177+ if (shouldDelete) {
228178 getEntityPacketSender()->queueEraseEntityMessage(entityID);
229179 }
230180 }
231181
232−EntityItemID EntityScriptingInterface::findClosestEntity(const glm::vec3& center, float radius) const {
233− EntityItemID result(UNKNOWN_ENTITY_ID, UNKNOWN_ENTITY_TOKEN, false);
182+QUuid EntityScriptingInterface::findClosestEntity(const glm::vec3& center, float radius) const {
183+ QUuid result;
234184 if (_entityTree) {
235185 _entityTree->lockForRead();
236186 const EntityItem* closestEntity = _entityTree->findClosestEntity(center, radius);
237187 _entityTree->unlock();
238188 if (closestEntity) {
239− result = closestEntity->getEntityItemID();
189+ result = closestEntity->getID();
240190 }
241191 }
242192 return result;
@@ -251,8 +201,8 @@ void EntityScriptingInterface::dumpTree() const { · 8 unmodified lines
251201 }
252202 }
253203
254−QVector<EntityItemID> EntityScriptingInterface::findEntities(const glm::vec3& center, float radius) const {
255− QVector<EntityItemID> result;
204+QVector<QUuid> EntityScriptingInterface::findEntities(const glm::vec3& center, float radius) const {
205+ QVector<QUuid> result;
256206 if (_entityTree) {
257207 _entityTree->lockForRead();
258208 QVector<const EntityItem*> entities;
@@ -260,14 +210,14 @@ QVector<EntityItemID> EntityScriptingInterface::findEntities(const glm::vec3& ce · 1 unmodified line
260210 _entityTree->unlock();
261211
262212 foreach (const EntityItem* entity, entities) {
263− result << entity->getEntityItemID();
213+ result << entity->getID();
264214 }
265215 }
266216 return result;
267217 }
268218
269−QVector<EntityItemID> EntityScriptingInterface::findEntitiesInBox(const glm::vec3& corner, const glm::vec3& dimensions) const {
270− QVector<EntityItemID> result;
219+QVector<QUuid> EntityScriptingInterface::findEntitiesInBox(const glm::vec3& corner, const glm::vec3& dimensions) const {
220+ QVector<QUuid> result;
271221 if (_entityTree) {
272222 _entityTree->lockForRead();
273223 AABox box(corner, dimensions);
@@ -276,7 +226,7 @@ QVector<EntityItemID> EntityScriptingInterface::findEntitiesInBox(const glm::vec · 2 unmodified lines
276226 _entityTree->unlock();
277227
278228 foreach (const EntityItem* entity, entities) {
279− result << entity->getEntityItemID();
229+ result << entity->getID();
280230 }
281231 }
282232 return result;
@@ -303,7 +253,7 @@ RayToEntityIntersectionResult EntityScriptingInterface::findRayIntersectionWorke · 20 unmodified lines
303253 (void**)&intersectedEntity, lockType, &result.accurate,
304254 precisionPicking);
305255 if (result.intersects && intersectedEntity) {
306− result.entityID = intersectedEntity->getEntityItemID();
256+ result.entityID = intersectedEntity->getID();
307257 result.properties = intersectedEntity->getProperties();
308258 result.intersection = ray.origin + (ray.direction * result.distance);
309259 }
@@ -359,7 +309,7 @@ QScriptValue RayToEntityIntersectionResultToScriptValue(QScriptEngine* engine, c · 49 unmodified lines
359309 QScriptValue obj = engine->newObject();
360310 obj.setProperty("intersects", value.intersects);
361311 obj.setProperty("accurate", value.accurate);
362− QScriptValue entityItemValue = EntityItemIDtoScriptValue(engine, value.entityID);
312+ QScriptValue entityItemValue = quuidToScriptValue(engine, value.entityID);
363313 obj.setProperty("entityID", entityItemValue);
364314
365315 QScriptValue propertiesValue = EntityItemPropertiesToScriptValue(engine, value.properties);
@@ -403,9 +353,7 @@ void RayToEntityIntersectionResultFromScriptValue(const QScriptValue& object, Ra · 37 unmodified lines
403353 value.intersects = object.property("intersects").toVariant().toBool();
404354 value.accurate = object.property("accurate").toVariant().toBool();
405355 QScriptValue entityIDValue = object.property("entityID");
406− if (entityIDValue.isValid()) {
407− EntityItemIDfromScriptValue(entityIDValue, value.entityID);
408− }
356+ quuidFromScriptValue(entityIDValue, value.entityID);
409357 QScriptValue entityPropertiesValue = object.property("properties");
410358 if (entityPropertiesValue.isValid()) {
411359 EntityItemPropertiesFromScriptValue(entityPropertiesValue, value.properties);

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t