From 66e4acf65192d2d6c2798eec6ffad70719ee7827 Mon Sep 17 00:00:00 2001 From: ericrius1 Date: Thu, 10 Sep 2015 16:21:57 -0700 Subject: [PATCH 1/9] trying to move to instant reset --- examples/entityScripts/sprayPaintCan.js | 19 ++++++- examples/sprayPaintSpawner.js | 66 ++++++------------------- 2 files changed, 33 insertions(+), 52 deletions(-) diff --git a/examples/entityScripts/sprayPaintCan.js b/examples/entityScripts/sprayPaintCan.js index c42a9bd659..76ba9f7613 100644 --- a/examples/entityScripts/sprayPaintCan.js +++ b/examples/entityScripts/sprayPaintCan.js @@ -25,6 +25,10 @@ running: true }); + var totalTime = 0; + var RESET_TIME_THRESHOLD = 2; + var RESET_POSITION = {x: 549.12, y: 495.555, z: 503.77}; + this.getUserData = function() { @@ -40,8 +44,21 @@ } this.update = function(deltaTime) { - self.properties = Entities.getEntityProperties(self.entityId); self.getUserData(); + self.properties = Entities.getEntityProperties(self.entityId); + totalTime += deltaTime; + if(totalTime > RESET_TIME_THRESHOLD) { + if(!self.activated) { + print("RESET"); + Entities.editEntity(self.entityId, { + position: RESET_POSITION, + rotation: Quat.fromPitchYawRollDegrees(0, 0, 0), + angularVelocity: ZERO_VEC, + velocity: ZERO_VEC + }); + } + totalTime = 0; + } if (self.userData.grabKey && self.userData.grabKey.activated === true) { if (self.activated !== true) { Entities.editEntity(self.paintStream, { diff --git a/examples/sprayPaintSpawner.js b/examples/sprayPaintSpawner.js index bd4edbb07c..df04a4efcc 100644 --- a/examples/sprayPaintSpawner.js +++ b/examples/sprayPaintSpawner.js @@ -8,67 +8,31 @@ // Distributed under the Apache License, Version 2.0. // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html -var scriptURL = "entityScripts/sprayPaintCan.js"; +// var scriptURL = "https://hifi-public.s3.amazonaws.com/scripts/entityScripts/sprayPaintCan.js"; +var scriptURL = "file:////Users/ericlevin/hifi/examples/entityScripts/sprayPaintCan.js?=v1" + Math.random(); +var modelURL = "https://hifi-public.s3.amazonaws.com/eric/models/paintcan.fbx"; var center = Vec3.sum(MyAvatar.position, Vec3.multiply(1, Quat.getFront(Camera.getOrientation()))); -var paintGun = Entities.addEntity({ +var sprayCan = Entities.addEntity({ type: "Model", - modelURL: "https://hifi-public.s3.amazonaws.com/eric/models/sprayGun.fbx?=v4", - position: center, + name: "spraycan", + modelURL: modelURL, + position: {x: 549.12, y:495.55, z:503.77}, + rotation: {x: 0, y: 0, z: 0, w: 1}, dimensions: { - x: 0.03, - y: 0.15, - z: 0.34 + x: 0.07, + y: 0.17, + z: 0.07 }, collisionsWillMove: true, shapeType: 'box', - script: scriptURL -}); - -var whiteboard = Entities.addEntity({ - type: "Box", - position: center, - dimensions: { - x: 2, - y: 1.5, - z: .01 - }, - rotation: orientationOf(Vec3.subtract(MyAvatar.position, center)), - color: { - red: 250, - green: 250, - blue: 250 - }, - // visible: false + script: scriptURL, + gravity: {x: 0, y: -0.5, z: 0}, + velocity: {x: 0, y: -.01, z: 0} }); function cleanup() { - Entities.deleteEntity(paintGun); - Entities.deleteEntity(whiteboard); + Entities.deleteEntity(sprayCan); } - Script.scriptEnding.connect(cleanup); - - -function orientationOf(vector) { - var Y_AXIS = { - x: 0, - y: 1, - z: 0 - }; - var X_AXIS = { - x: 1, - y: 0, - z: 0 - }; - - var theta = 0.0; - - var RAD_TO_DEG = 180.0 / Math.PI; - var direction, yaw, pitch; - direction = Vec3.normalize(vector); - yaw = Quat.angleAxis(Math.atan2(direction.x, direction.z) * RAD_TO_DEG, Y_AXIS); - pitch = Quat.angleAxis(Math.asin(-direction.y) * RAD_TO_DEG, X_AXIS); - return Quat.multiply(yaw, pitch); -} From 510e253575de2c3d477187495c3dcd3ed1bff626 Mon Sep 17 00:00:00 2001 From: ericrius1 Date: Thu, 10 Sep 2015 16:26:57 -0700 Subject: [PATCH 2/9] switching to hosted script for testing --- examples/entityScripts/sprayPaintCan.js | 31 ++++++++++++------------- examples/sprayPaintSpawner.js | 4 ++-- 2 files changed, 17 insertions(+), 18 deletions(-) diff --git a/examples/entityScripts/sprayPaintCan.js b/examples/entityScripts/sprayPaintCan.js index 76ba9f7613..47da0b5171 100644 --- a/examples/entityScripts/sprayPaintCan.js +++ b/examples/entityScripts/sprayPaintCan.js @@ -25,9 +25,11 @@ running: true }); - var totalTime = 0; - var RESET_TIME_THRESHOLD = 2; - var RESET_POSITION = {x: 549.12, y: 495.555, z: 503.77}; + var RESET_POSITION = { + x: 549.12, + y: 495.555, + z: 503.77 + }; this.getUserData = function() { @@ -46,19 +48,6 @@ this.update = function(deltaTime) { self.getUserData(); self.properties = Entities.getEntityProperties(self.entityId); - totalTime += deltaTime; - if(totalTime > RESET_TIME_THRESHOLD) { - if(!self.activated) { - print("RESET"); - Entities.editEntity(self.entityId, { - position: RESET_POSITION, - rotation: Quat.fromPitchYawRollDegrees(0, 0, 0), - angularVelocity: ZERO_VEC, - velocity: ZERO_VEC - }); - } - totalTime = 0; - } if (self.userData.grabKey && self.userData.grabKey.activated === true) { if (self.activated !== true) { Entities.editEntity(self.paintStream, { @@ -73,9 +62,19 @@ animationSettings: stopSetting }); self.activated = false; + self.reset(); } } + this.reset = function() { + Entities.editEntity(self.entityId, { + position: RESET_POSITION, + rotation: Quat.fromPitchYawRollDegrees(0, 0, 0), + angularVelocity: ZERO_VEC, + velocity: ZERO_VEC + }); + } + this.sprayStream = function() { var forwardVec = Quat.getFront(self.properties.rotation); forwardVec = Vec3.multiplyQbyV(Quat.fromPitchYawRollDegrees(0, 90, 0), forwardVec); diff --git a/examples/sprayPaintSpawner.js b/examples/sprayPaintSpawner.js index df04a4efcc..ffedfb1cbf 100644 --- a/examples/sprayPaintSpawner.js +++ b/examples/sprayPaintSpawner.js @@ -8,8 +8,8 @@ // Distributed under the Apache License, Version 2.0. // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html -// var scriptURL = "https://hifi-public.s3.amazonaws.com/scripts/entityScripts/sprayPaintCan.js"; -var scriptURL = "file:////Users/ericlevin/hifi/examples/entityScripts/sprayPaintCan.js?=v1" + Math.random(); +var scriptURL = "https://hifi-public.s3.amazonaws.com/scripts/entityScripts/sprayPaintCan.js"; +// var scriptURL = "file:////Users/ericlevin/hifi/examples/entityScripts/sprayPaintCan.js?=v1" + Math.random(); var modelURL = "https://hifi-public.s3.amazonaws.com/eric/models/paintcan.fbx"; var center = Vec3.sum(MyAvatar.position, Vec3.multiply(1, Quat.getFront(Camera.getOrientation()))); From a1fd867c096887837cd9065e28bcec63b1dfaab2 Mon Sep 17 00:00:00 2001 From: ericrius1 Date: Thu, 10 Sep 2015 16:46:25 -0700 Subject: [PATCH 3/9] timer fix for reset --- examples/entityScripts/sprayPaintCan.js | 20 ++++++++++++++++---- examples/sprayPaintSpawner.js | 4 +++- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/examples/entityScripts/sprayPaintCan.js b/examples/entityScripts/sprayPaintCan.js index 47da0b5171..81cb14489d 100644 --- a/examples/entityScripts/sprayPaintCan.js +++ b/examples/entityScripts/sprayPaintCan.js @@ -25,6 +25,8 @@ running: true }); + var timeSinceLastMoved = 0; + var RESET_TIME_THRESHOLD = 5; var RESET_POSITION = { x: 549.12, y: 495.555, @@ -48,6 +50,17 @@ this.update = function(deltaTime) { self.getUserData(); self.properties = Entities.getEntityProperties(self.entityId); + + timeSinceLastMoved += deltaTime; + if (Vec3.length(self.properties.velocity) < 0.1) { + if (timeSinceLastMoved > RESET_TIME_THRESHOLD) { + self.reset(); + timeSinceLastMoved = 0; + } + } else { + timeSinceLastMoved = 0; + } + if (self.userData.grabKey && self.userData.grabKey.activated === true) { if (self.activated !== true) { Entities.editEntity(self.paintStream, { @@ -62,7 +75,6 @@ animationSettings: stopSetting }); self.activated = false; - self.reset(); } } @@ -150,9 +162,9 @@ blue: randInt(190, 250) }, dimensions: { - x: 5, - y: 5, - z: 5 + x: 50, + y: 50, + z: 50 }, lifetime: 100 }); diff --git a/examples/sprayPaintSpawner.js b/examples/sprayPaintSpawner.js index ffedfb1cbf..f049c5a1af 100644 --- a/examples/sprayPaintSpawner.js +++ b/examples/sprayPaintSpawner.js @@ -8,7 +8,9 @@ // Distributed under the Apache License, Version 2.0. // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html -var scriptURL = "https://hifi-public.s3.amazonaws.com/scripts/entityScripts/sprayPaintCan.js"; +//Just temporarily using my own bucket here so others can test the entity. Once PR is tested and merged, then the entity script will appear in its proper place in S3, and I wil switch it +var scriptURL = "https://hifi-public.s3.amazonaws.com/eric/scripts/sprayPaintCan.js"; +// var scriptURL = "https://hifi-public.s3.amazonaws.com/scripts/entityScripts/sprayPaintCan.js"; // var scriptURL = "file:////Users/ericlevin/hifi/examples/entityScripts/sprayPaintCan.js?=v1" + Math.random(); var modelURL = "https://hifi-public.s3.amazonaws.com/eric/models/paintcan.fbx"; var center = Vec3.sum(MyAvatar.position, Vec3.multiply(1, Quat.getFront(Camera.getOrientation()))); From 7969fb64cedd6b02656da2a08c8982eb79b944f5 Mon Sep 17 00:00:00 2001 From: ericrius1 Date: Thu, 10 Sep 2015 17:08:52 -0700 Subject: [PATCH 4/9] tweaking distance threshold --- examples/entityScripts/sprayPaintCan.js | 13 +++++++------ examples/sprayPaintSpawner.js | 4 ++-- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/examples/entityScripts/sprayPaintCan.js b/examples/entityScripts/sprayPaintCan.js index 81cb14489d..203be9ba20 100644 --- a/examples/entityScripts/sprayPaintCan.js +++ b/examples/entityScripts/sprayPaintCan.js @@ -27,12 +27,12 @@ var timeSinceLastMoved = 0; var RESET_TIME_THRESHOLD = 5; - var RESET_POSITION = { + var DISTANCE_FROM_HOME_THRESHOLD = .5; + var HOME_POSITION = { x: 549.12, y: 495.555, z: 503.77 }; - this.getUserData = function() { @@ -51,13 +51,14 @@ self.getUserData(); self.properties = Entities.getEntityProperties(self.entityId); - timeSinceLastMoved += deltaTime; - if (Vec3.length(self.properties.velocity) < 0.1) { + if (Vec3.length(self.properties.velocity) < 0.1 && Vec3.distance(HOME_POSITION, self.properties.position) > DISTANCE_FROM_HOME_THRESHOLD) { + timeSinceLastMoved += deltaTime; if (timeSinceLastMoved > RESET_TIME_THRESHOLD) { self.reset(); timeSinceLastMoved = 0; } - } else { + } + else { timeSinceLastMoved = 0; } @@ -80,7 +81,7 @@ this.reset = function() { Entities.editEntity(self.entityId, { - position: RESET_POSITION, + position: HOME_POSITION, rotation: Quat.fromPitchYawRollDegrees(0, 0, 0), angularVelocity: ZERO_VEC, velocity: ZERO_VEC diff --git a/examples/sprayPaintSpawner.js b/examples/sprayPaintSpawner.js index f049c5a1af..991ba25064 100644 --- a/examples/sprayPaintSpawner.js +++ b/examples/sprayPaintSpawner.js @@ -9,7 +9,7 @@ // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html //Just temporarily using my own bucket here so others can test the entity. Once PR is tested and merged, then the entity script will appear in its proper place in S3, and I wil switch it -var scriptURL = "https://hifi-public.s3.amazonaws.com/eric/scripts/sprayPaintCan.js"; +var scriptURL = "https://hifi-public.s3.amazonaws.com/eric/scripts/sprayPaintCan.js?=v1" + Math.random(); // var scriptURL = "https://hifi-public.s3.amazonaws.com/scripts/entityScripts/sprayPaintCan.js"; // var scriptURL = "file:////Users/ericlevin/hifi/examples/entityScripts/sprayPaintCan.js?=v1" + Math.random(); var modelURL = "https://hifi-public.s3.amazonaws.com/eric/models/paintcan.fbx"; @@ -30,7 +30,7 @@ var sprayCan = Entities.addEntity({ shapeType: 'box', script: scriptURL, gravity: {x: 0, y: -0.5, z: 0}, - velocity: {x: 0, y: -.01, z: 0} + velocity: {x: 0, y: -1, z: 0} }); function cleanup() { From 1991ba0eba512490b112cbeca432503eb7ddd846 Mon Sep 17 00:00:00 2001 From: ericrius1 Date: Fri, 11 Sep 2015 09:37:03 -0700 Subject: [PATCH 5/9] absolute path for now- just for testing purposes before merge --- examples/entityScripts/sprayPaintCan.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/examples/entityScripts/sprayPaintCan.js b/examples/entityScripts/sprayPaintCan.js index 203be9ba20..9ba6d75600 100644 --- a/examples/entityScripts/sprayPaintCan.js +++ b/examples/entityScripts/sprayPaintCan.js @@ -1,5 +1,7 @@ (function() { - Script.include("../libraries/utils.js"); + // Script.include("../libraries/utils.js"); + //Need absolute path for now, for testing before PR merge and s3 cloning. Will change post-merge + Script.include("https://hifi-public.s3.amazonaws.com/scripts/libraries/utils.js"); GRAB_FRAME_USER_DATA_KEY = "grabFrame"; this.userData = {}; From 6e46e3d3b67a7a592d4f652133b6aa0e0d2d3600 Mon Sep 17 00:00:00 2001 From: ericrius1 Date: Fri, 11 Sep 2015 11:24:53 -0700 Subject: [PATCH 6/9] cleanup for paintcan --- examples/entityScripts/sprayPaintCan.js | 2 +- examples/sprayPaintSpawner.js | 4 +--- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/examples/entityScripts/sprayPaintCan.js b/examples/entityScripts/sprayPaintCan.js index 9ba6d75600..8a5829c441 100644 --- a/examples/entityScripts/sprayPaintCan.js +++ b/examples/entityScripts/sprayPaintCan.js @@ -29,7 +29,7 @@ var timeSinceLastMoved = 0; var RESET_TIME_THRESHOLD = 5; - var DISTANCE_FROM_HOME_THRESHOLD = .5; + var DISTANCE_FROM_HOME_THRESHOLD = 0.5; var HOME_POSITION = { x: 549.12, y: 495.555, diff --git a/examples/sprayPaintSpawner.js b/examples/sprayPaintSpawner.js index 991ba25064..77b74e6520 100644 --- a/examples/sprayPaintSpawner.js +++ b/examples/sprayPaintSpawner.js @@ -9,9 +9,7 @@ // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html //Just temporarily using my own bucket here so others can test the entity. Once PR is tested and merged, then the entity script will appear in its proper place in S3, and I wil switch it -var scriptURL = "https://hifi-public.s3.amazonaws.com/eric/scripts/sprayPaintCan.js?=v1" + Math.random(); -// var scriptURL = "https://hifi-public.s3.amazonaws.com/scripts/entityScripts/sprayPaintCan.js"; -// var scriptURL = "file:////Users/ericlevin/hifi/examples/entityScripts/sprayPaintCan.js?=v1" + Math.random(); +var scriptURL = "https://hifi-public.s3.amazonaws.com/eric/scripts/sprayPaintCan.js?=v1"; var modelURL = "https://hifi-public.s3.amazonaws.com/eric/models/paintcan.fbx"; var center = Vec3.sum(MyAvatar.position, Vec3.multiply(1, Quat.getFront(Camera.getOrientation()))); From eabdd1f4d9f6a42d648047f870695079e626b297 Mon Sep 17 00:00:00 2001 From: ericrius1 Date: Fri, 11 Sep 2015 11:33:21 -0700 Subject: [PATCH 7/9] quick fix for particles hanging around --- examples/entityScripts/sprayPaintCan.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/entityScripts/sprayPaintCan.js b/examples/entityScripts/sprayPaintCan.js index 8a5829c441..4af404d275 100644 --- a/examples/entityScripts/sprayPaintCan.js +++ b/examples/entityScripts/sprayPaintCan.js @@ -229,7 +229,7 @@ green: 20, blue: 150 }, - lifespan: 5, + lifetime: 1000, }); } From 5c6d8da97b26c3ffb4a0fbe7b5593077d028fba2 Mon Sep 17 00:00:00 2001 From: Brad Hefta-Gaub Date: Fri, 11 Sep 2015 11:55:36 -0700 Subject: [PATCH 8/9] Revert "Make sure all entity script callbacks happen on the Entities script engine thread" --- .../src/EntityTreeRenderer.cpp | 63 ++++++------------- libraries/script-engine/src/ScriptCache.cpp | 1 - libraries/script-engine/src/ScriptEngine.cpp | 14 ----- libraries/script-engine/src/ScriptEngine.h | 2 - libraries/shared/src/RegisteredMetaTypes.cpp | 3 +- 5 files changed, 21 insertions(+), 62 deletions(-) diff --git a/libraries/entities-renderer/src/EntityTreeRenderer.cpp b/libraries/entities-renderer/src/EntityTreeRenderer.cpp index a24461da83..93a9996740 100644 --- a/libraries/entities-renderer/src/EntityTreeRenderer.cpp +++ b/libraries/entities-renderer/src/EntityTreeRenderer.cpp @@ -242,7 +242,6 @@ QScriptValue EntityTreeRenderer::loadEntityScript(EntityItemPointer entity, bool QString scriptContents = loadScriptContents(entityScript, isURL, isPending, url, reload); if (isPending && isPreload && isURL) { - //qDebug() << "attempted to load script, isPending, _waitingOnPreload.insert() url:" << url << "entityID:" << entityID; _waitingOnPreload.insert(url, entityID); } @@ -325,8 +324,7 @@ void EntityTreeRenderer::update() { QScriptValueList currentClickingEntityArgs = createMouseEventArgs(_currentClickingOnEntityID, _lastMouseEvent); QScriptValue currentClickingEntity = loadEntityScript(_currentClickingOnEntityID); if (currentClickingEntity.property("holdingClickOnEntity").isValid()) { - //qDebug() << "About to call holdingClickOnEntity() current thread:" << QThread::currentThread() << "entities thread:" << _entitiesScriptEngine->thread(); - _entitiesScriptEngine->callScriptMethod("holdingClickOnEntity", currentClickingEntity, currentClickingEntityArgs); + currentClickingEntity.property("holdingClickOnEntity").call(currentClickingEntity, currentClickingEntityArgs); } } @@ -365,9 +363,7 @@ void EntityTreeRenderer::checkEnterLeaveEntities() { QScriptValueList entityArgs = createEntityArgs(entityID); QScriptValue entityScript = loadEntityScript(entityID); if (entityScript.property("leaveEntity").isValid()) { - - //qDebug() << "About to call leaveEntity() current thread:" << QThread::currentThread() << "entities thread:" << _entitiesScriptEngine->thread(); - _entitiesScriptEngine->callScriptMethod("leaveEntity", entityScript, entityArgs); + entityScript.property("leaveEntity").call(entityScript, entityArgs); } } @@ -380,8 +376,7 @@ void EntityTreeRenderer::checkEnterLeaveEntities() { QScriptValueList entityArgs = createEntityArgs(entityID); QScriptValue entityScript = loadEntityScript(entityID); if (entityScript.property("enterEntity").isValid()) { - //qDebug() << "About to call enterEntity() current thread:" << QThread::currentThread() << "entities thread:" << _entitiesScriptEngine->thread(); - _entitiesScriptEngine->callScriptMethod("enterEntity", entityScript, entityArgs); + entityScript.property("enterEntity").call(entityScript, entityArgs); } } } @@ -400,8 +395,7 @@ void EntityTreeRenderer::leaveAllEntities() { QScriptValueList entityArgs = createEntityArgs(entityID); QScriptValue entityScript = loadEntityScript(entityID); if (entityScript.property("leaveEntity").isValid()) { - //qDebug() << "About to call leaveEntity() current thread:" << QThread::currentThread() << "entities thread:" << _entitiesScriptEngine->thread(); - _entitiesScriptEngine->callScriptMethod("leaveEntity", entityScript, entityArgs); + entityScript.property("leaveEntity").call(entityScript, entityArgs); } } _currentEntitiesInside.clear(); @@ -857,7 +851,7 @@ void EntityTreeRenderer::mousePressEvent(QMouseEvent* event, unsigned int device bool precisionPicking = !_dontDoPrecisionPicking; RayToEntityIntersectionResult rayPickResult = findRayIntersectionWorker(ray, Octree::Lock, precisionPicking); if (rayPickResult.intersects) { - qCDebug(entitiesrenderer) << "mousePressEvent over entity:" << rayPickResult.entityID; + //qCDebug(entitiesrenderer) << "mousePressEvent over entity:" << rayPickResult.entityID; QString urlString = rayPickResult.properties.getHref(); QUrl url = QUrl(urlString, QUrl::StrictMode); @@ -871,15 +865,13 @@ void EntityTreeRenderer::mousePressEvent(QMouseEvent* event, unsigned int device QScriptValueList entityScriptArgs = createMouseEventArgs(rayPickResult.entityID, event, deviceID); QScriptValue entityScript = loadEntityScript(rayPickResult.entity); if (entityScript.property("mousePressOnEntity").isValid()) { - //qDebug() << "About to call mousePressOnEntity() current thread:" << QThread::currentThread() << "entities thread:" << _entitiesScriptEngine->thread(); - _entitiesScriptEngine->callScriptMethod("mousePressOnEntity", entityScript, entityScriptArgs); + entityScript.property("mousePressOnEntity").call(entityScript, entityScriptArgs); } _currentClickingOnEntityID = rayPickResult.entityID; emit clickDownOnEntity(_currentClickingOnEntityID, MouseEvent(*event, deviceID)); if (entityScript.property("clickDownOnEntity").isValid()) { - //qDebug() << "About to call clickDownOnEntity() current thread:" << QThread::currentThread() << "entities thread:" << _entitiesScriptEngine->thread(); - _entitiesScriptEngine->callScriptMethod("clickDownOnEntity", entityScript, entityScriptArgs); + entityScript.property("clickDownOnEntity").call(entityScript, entityScriptArgs); } } else { emit mousePressOffEntity(rayPickResult, event, deviceID); @@ -905,7 +897,7 @@ void EntityTreeRenderer::mouseReleaseEvent(QMouseEvent* event, unsigned int devi QScriptValueList entityScriptArgs = createMouseEventArgs(rayPickResult.entityID, event, deviceID); QScriptValue entityScript = loadEntityScript(rayPickResult.entity); if (entityScript.property("mouseReleaseOnEntity").isValid()) { - _entitiesScriptEngine->callScriptMethod("mouseReleaseOnEntity", entityScript, entityScriptArgs); + entityScript.property("mouseReleaseOnEntity").call(entityScript, entityScriptArgs); } } @@ -917,8 +909,7 @@ void EntityTreeRenderer::mouseReleaseEvent(QMouseEvent* event, unsigned int devi QScriptValueList currentClickingEntityArgs = createMouseEventArgs(_currentClickingOnEntityID, event, deviceID); QScriptValue currentClickingEntity = loadEntityScript(_currentClickingOnEntityID); if (currentClickingEntity.property("clickReleaseOnEntity").isValid()) { - //qDebug() << "About to call clickReleaseOnEntity() current thread:" << QThread::currentThread() << "entities thread:" << _entitiesScriptEngine->thread(); - _entitiesScriptEngine->callScriptMethod("clickReleaseOnEntity", currentClickingEntity, currentClickingEntityArgs); + currentClickingEntity.property("clickReleaseOnEntity").call(currentClickingEntity, currentClickingEntityArgs); } } @@ -946,13 +937,11 @@ void EntityTreeRenderer::mouseMoveEvent(QMouseEvent* event, unsigned int deviceI // load the entity script if needed... QScriptValue entityScript = loadEntityScript(rayPickResult.entity); if (entityScript.property("mouseMoveEvent").isValid()) { - //qDebug() << "About to call mouseMoveEvent() current thread:" << QThread::currentThread() << "entities thread:" << _entitiesScriptEngine->thread(); - _entitiesScriptEngine->callScriptMethod("mouseMoveEvent", entityScript, entityScriptArgs); + entityScript.property("mouseMoveEvent").call(entityScript, entityScriptArgs); } emit mouseMoveOnEntity(rayPickResult, event, deviceID); if (entityScript.property("mouseMoveOnEntity").isValid()) { - //qDebug() << "About to call mouseMoveOnEntity() current thread:" << QThread::currentThread() << "entities thread:" << _entitiesScriptEngine->thread(); - _entitiesScriptEngine->callScriptMethod("mouseMoveOnEntity", entityScript, entityScriptArgs); + entityScript.property("mouseMoveOnEntity").call(entityScript, entityScriptArgs); } // handle the hover logic... @@ -966,9 +955,7 @@ void EntityTreeRenderer::mouseMoveEvent(QMouseEvent* event, unsigned int deviceI QScriptValue currentHoverEntity = loadEntityScript(_currentHoverOverEntityID); if (currentHoverEntity.property("hoverLeaveEntity").isValid()) { - //qDebug() << "About to call hoverLeaveEntity() current thread:" << QThread::currentThread() << "entities thread:" << _entitiesScriptEngine->thread(); - _entitiesScriptEngine->callScriptMethod("hoverLeaveEntity", currentHoverEntity, currentHoverEntityArgs); - + currentHoverEntity.property("hoverLeaveEntity").call(currentHoverEntity, currentHoverEntityArgs); } } @@ -977,8 +964,7 @@ void EntityTreeRenderer::mouseMoveEvent(QMouseEvent* event, unsigned int deviceI if (rayPickResult.entityID != _currentHoverOverEntityID) { emit hoverEnterEntity(rayPickResult.entityID, MouseEvent(*event, deviceID)); if (entityScript.property("hoverEnterEntity").isValid()) { - //qDebug() << "About to call hoverEnterEntity() current thread:" << QThread::currentThread() << "entities thread:" << _entitiesScriptEngine->thread(); - _entitiesScriptEngine->callScriptMethod("hoverEnterEntity", entityScript, entityScriptArgs); + entityScript.property("hoverEnterEntity").call(entityScript, entityScriptArgs); } } @@ -986,8 +972,7 @@ void EntityTreeRenderer::mouseMoveEvent(QMouseEvent* event, unsigned int deviceI // we should send our hover over event emit hoverOverEntity(rayPickResult.entityID, MouseEvent(*event, deviceID)); if (entityScript.property("hoverOverEntity").isValid()) { - //qDebug() << "About to call hoverOverEntity() current thread:" << QThread::currentThread() << "entities thread:" << _entitiesScriptEngine->thread(); - _entitiesScriptEngine->callScriptMethod("hoverOverEntity", entityScript, entityScriptArgs); + entityScript.property("hoverOverEntity").call(entityScript, entityScriptArgs); } // remember what we're hovering over @@ -1004,8 +989,7 @@ void EntityTreeRenderer::mouseMoveEvent(QMouseEvent* event, unsigned int deviceI QScriptValue currentHoverEntity = loadEntityScript(_currentHoverOverEntityID); if (currentHoverEntity.property("hoverLeaveEntity").isValid()) { - //qDebug() << "About to call hoverLeaveEntity() current thread:" << QThread::currentThread() << "entities thread:" << _entitiesScriptEngine->thread(); - _entitiesScriptEngine->callScriptMethod("hoverLeaveEntity", currentHoverEntity, currentHoverEntityArgs); + currentHoverEntity.property("hoverLeaveEntity").call(currentHoverEntity, currentHoverEntityArgs); } _currentHoverOverEntityID = UNKNOWN_ENTITY_ID; // makes it the unknown ID @@ -1021,8 +1005,7 @@ void EntityTreeRenderer::mouseMoveEvent(QMouseEvent* event, unsigned int deviceI QScriptValue currentClickingEntity = loadEntityScript(_currentClickingOnEntityID); if (currentClickingEntity.property("holdingClickOnEntity").isValid()) { - //qDebug() << "About to call holdingClickOnEntity() current thread:" << QThread::currentThread() << "entities thread:" << _entitiesScriptEngine->thread(); - _entitiesScriptEngine->callScriptMethod("holdingClickOnEntity", currentClickingEntity, currentClickingEntityArgs); + currentClickingEntity.property("holdingClickOnEntity").call(currentClickingEntity, currentClickingEntityArgs); } } _lastMouseEvent = MouseEvent(*event, deviceID); @@ -1077,8 +1060,7 @@ void EntityTreeRenderer::checkAndCallPreload(const EntityItemID& entityID, const QScriptValue entityScript = loadEntityScript(entityID, true, reload); // is preload! if (entityScript.property("preload").isValid()) { QScriptValueList entityArgs = createEntityArgs(entityID); - //qDebug() << "About to call preload() current thread:" << QThread::currentThread() << "entities thread:" << _entitiesScriptEngine->thread(); - _entitiesScriptEngine->callScriptMethod("preload", entityScript, entityArgs); + entityScript.property("preload").call(entityScript, entityArgs); } } } @@ -1088,8 +1070,7 @@ void EntityTreeRenderer::checkAndCallUnload(const EntityItemID& entityID) { QScriptValue entityScript = getPreviouslyLoadedEntityScript(entityID); if (entityScript.property("unload").isValid()) { QScriptValueList entityArgs = createEntityArgs(entityID); - //qDebug() << "About to call unload() current thread:" << QThread::currentThread() << "entities thread:" << _entitiesScriptEngine->thread(); - _entitiesScriptEngine->callScriptMethod("unload", entityScript, entityArgs); + entityScript.property("unload").call(entityScript, entityArgs); } } } @@ -1174,9 +1155,7 @@ void EntityTreeRenderer::entityCollisionWithEntity(const EntityItemID& idA, cons args << idA.toScriptValue(_entitiesScriptEngine); args << idB.toScriptValue(_entitiesScriptEngine); args << collisionToScriptValue(_entitiesScriptEngine, collision); - - //qDebug() << "About to call collisionWithEntity() current thread:" << QThread::currentThread() << "entities thread:" << _entitiesScriptEngine->thread(); - _entitiesScriptEngine->callScriptMethod("collisionWithEntity", entityScriptA, args); + entityScriptA.property("collisionWithEntity").call(entityScriptA, args); } emit collisionWithEntity(idB, idA, collision); @@ -1186,9 +1165,7 @@ void EntityTreeRenderer::entityCollisionWithEntity(const EntityItemID& idA, cons args << idB.toScriptValue(_entitiesScriptEngine); args << idA.toScriptValue(_entitiesScriptEngine); args << collisionToScriptValue(_entitiesScriptEngine, collision); - - //qDebug() << "About to call collisionWithEntity() current thread:" << QThread::currentThread() << "entities thread:" << _entitiesScriptEngine->thread(); - _entitiesScriptEngine->callScriptMethod("collisionWithEntity", entityScriptB, args); + entityScriptB.property("collisionWithEntity").call(entityScriptA, args); } } diff --git a/libraries/script-engine/src/ScriptCache.cpp b/libraries/script-engine/src/ScriptCache.cpp index fb523b157c..9e04cd4ec3 100644 --- a/libraries/script-engine/src/ScriptCache.cpp +++ b/libraries/script-engine/src/ScriptCache.cpp @@ -68,7 +68,6 @@ void ScriptCache::scriptDownloaded() { qCDebug(scriptengine) << "Done downloading script at:" << url.toString(); foreach(ScriptUser* user, scriptUsers) { - // FIXME - I sometimes get a crash deep below here inside of Qt while evaluating the script user->scriptContentsAvailable(url, _scriptCache[url]); } } else { diff --git a/libraries/script-engine/src/ScriptEngine.cpp b/libraries/script-engine/src/ScriptEngine.cpp index 9f061a5cdb..e401398d24 100644 --- a/libraries/script-engine/src/ScriptEngine.cpp +++ b/libraries/script-engine/src/ScriptEngine.cpp @@ -261,20 +261,6 @@ bool ScriptEngine::setScriptContents(const QString& scriptContents, const QStrin return true; } -void ScriptEngine::callScriptMethod(QString methodName, QScriptValue script, QScriptValueList args) { - if (QThread::currentThread() != thread()) { - QMetaObject::invokeMethod(this, - "callScriptMethod", - Q_ARG(QString, methodName), - Q_ARG(QScriptValue, script), - Q_ARG(QScriptValueList, args)); - return; - } - - //qDebug() << "About to call " << methodName << "() current thread : " << QThread::currentThread() << "engine thread : " << thread(); - script.property(methodName).call(script, args); -} - void ScriptEngine::loadURL(const QUrl& scriptURL, bool reload) { if (_isRunning) { return; diff --git a/libraries/script-engine/src/ScriptEngine.h b/libraries/script-engine/src/ScriptEngine.h index 2bfcecc99e..b9156c718e 100644 --- a/libraries/script-engine/src/ScriptEngine.h +++ b/libraries/script-engine/src/ScriptEngine.h @@ -65,8 +65,6 @@ public: int numArguments = -1); Q_INVOKABLE void setIsAvatar(bool isAvatar); - Q_INVOKABLE void callScriptMethod(QString methodName, QScriptValue script, QScriptValueList args); - bool isAvatar() const { return _isAvatar; } void setAvatarData(AvatarData* avatarData, const QString& objectName); diff --git a/libraries/shared/src/RegisteredMetaTypes.cpp b/libraries/shared/src/RegisteredMetaTypes.cpp index d110635f92..b2389f4db6 100644 --- a/libraries/shared/src/RegisteredMetaTypes.cpp +++ b/libraries/shared/src/RegisteredMetaTypes.cpp @@ -26,8 +26,7 @@ static int quatMetaTypeId = qRegisterMetaType(); static int xColorMetaTypeId = qRegisterMetaType(); static int pickRayMetaTypeId = qRegisterMetaType(); static int collisionMetaTypeId = qRegisterMetaType(); -static int qMapURLStringMetaTypeId = qRegisterMetaType>(); -static int qScriptValueListMetaTypeId = qRegisterMetaType("QScriptValueList"); +static int qMapURLStringMetaTypeId = qRegisterMetaType>(); void registerMetaTypes(QScriptEngine* engine) { qScriptRegisterMetaType(engine, vec4toScriptValue, vec4FromScriptValue); From 0a46db60acc5cbc1936e4e56ce6ce39527e8d2a7 Mon Sep 17 00:00:00 2001 From: ericrius1 Date: Fri, 11 Sep 2015 12:01:01 -0700 Subject: [PATCH 9/9] spawning particles on grab and deleting on release, with a lifetime, for better cleanup of particles --- examples/controllers/handControllerGrab.js | 2 +- examples/entityScripts/sprayPaintCan.js | 85 +++++++++++----------- 2 files changed, 43 insertions(+), 44 deletions(-) diff --git a/examples/controllers/handControllerGrab.js b/examples/controllers/handControllerGrab.js index 523cb4eff9..7d4380d764 100644 --- a/examples/controllers/handControllerGrab.js +++ b/examples/controllers/handControllerGrab.js @@ -144,7 +144,7 @@ controller.prototype.checkPointer = function() { Script.setTimeout(function() { var props = Entities.getEntityProperties(self.pointer); Entities.editEntity(self.pointer, { - lifetime: props.age + EXTRA_TIME + lifetime: props.age + EXTRA_TIME + LIFETIME }); self.checkPointer(); }, POINTER_CHECK_TIME); diff --git a/examples/entityScripts/sprayPaintCan.js b/examples/entityScripts/sprayPaintCan.js index 4af404d275..4407140184 100644 --- a/examples/entityScripts/sprayPaintCan.js +++ b/examples/entityScripts/sprayPaintCan.js @@ -20,13 +20,6 @@ var self = this; - var stopSetting = JSON.stringify({ - running: false - }); - var startSetting = JSON.stringify({ - running: true - }); - var timeSinceLastMoved = 0; var RESET_TIME_THRESHOLD = 5; var DISTANCE_FROM_HOME_THRESHOLD = 0.5; @@ -59,17 +52,17 @@ self.reset(); timeSinceLastMoved = 0; } - } - else { + } else { timeSinceLastMoved = 0; } if (self.userData.grabKey && self.userData.grabKey.activated === true) { if (self.activated !== true) { + //We were just grabbed, so create a particle system + self.grab(); Entities.editEntity(self.paintStream, { animationSettings: startSetting }); - self.activated = true; } //Move emitter to where entity is always when its activated self.sprayStream(); @@ -81,6 +74,45 @@ } } + this.grab = function() { + self.activated = true; + var animationSettings = JSON.stringify({ + fps: 30, + loop: true, + firstFrame: 1, + lastFrame: 10000, + running: true + }); + + this.paintStream = Entities.addEntity({ + type: "ParticleEffect", + animationSettings: animationSettings, + position: this.properties.position, + textures: "https://raw.githubusercontent.com/ericrius1/SantasLair/santa/assets/smokeparticle.png", + emitVelocity: ZERO_VEC, + emitAcceleration: ZERO_VEC, + velocitySpread: { + x: .02, + y: .02, + z: 0.02 + }, + emitRate: 100, + particleRadius: 0.01, + color: { + red: 170, + green: 20, + blue: 150 + }, + lifetime: 500, //probably wont be holding longer than this straight + }); + + } + + this.letGo = function() { + self.activated = false; + Entities.deleteEntity(this.paintStream); + } + this.reset = function() { Entities.editEntity(self.entityId, { position: HOME_POSITION, @@ -198,41 +230,8 @@ } setEntityCustomData(GRAB_FRAME_USER_DATA_KEY, this.entityId, data); } - this.initialize(); } - this.initialize = function() { - var animationSettings = JSON.stringify({ - fps: 30, - loop: true, - firstFrame: 1, - lastFrame: 10000, - running: false - }); - - this.paintStream = Entities.addEntity({ - type: "ParticleEffect", - animationSettings: animationSettings, - position: this.properties.position, - textures: "https://raw.githubusercontent.com/ericrius1/SantasLair/santa/assets/smokeparticle.png", - emitVelocity: ZERO_VEC, - emitAcceleration: ZERO_VEC, - velocitySpread: { - x: .02, - y: .02, - z: 0.02 - }, - emitRate: 100, - particleRadius: 0.01, - color: { - red: 170, - green: 20, - blue: 150 - }, - lifetime: 1000, - }); - - } this.unload = function() { Script.update.disconnect(this.update);