From fa423344305801f7bb1339f5ba02faf2cebcd2d9 Mon Sep 17 00:00:00 2001 From: ericrius1 Date: Thu, 3 Sep 2015 11:30:44 -0700 Subject: [PATCH] added userData manipulations to grab script --- examples/controllers/hydra/hydraGrab.js | 35 ++++++++++++++++++++++--- 1 file changed, 31 insertions(+), 4 deletions(-) diff --git a/examples/controllers/hydra/hydraGrab.js b/examples/controllers/hydra/hydraGrab.js index 71e4d2a07e..1c0a46f661 100644 --- a/examples/controllers/hydra/hydraGrab.js +++ b/examples/controllers/hydra/hydraGrab.js @@ -11,9 +11,13 @@ // +Script.include("https://hifi-public.s3.amazonaws.com/scripts/libraries/utils.js"); + var RIGHT_HAND_CLICK = Controller.findAction("RIGHT_HAND_CLICK"); var rightTriggerAction = RIGHT_HAND_CLICK; +var GRAB_USER_DATA_KEY = "grabKey"; + var LEFT_HAND_CLICK = Controller.findAction("LEFT_HAND_CLICK"); var leftTriggerAction = LEFT_HAND_CLICK; @@ -37,7 +41,7 @@ var INTERSECT_COLOR = { blue: 10 }; -var GRAB_RADIUS = 2; +var GRAB_RADIUS = 0.5; var GRAB_COLOR = { red: 250, @@ -154,7 +158,6 @@ controller.prototype.attemptMove = function() { var newPosition = Vec3.sum(handPosition, Vec3.multiply(direction, this.distanceToEntity)) this.distanceHolding = true; - //TO DO : USE SPRING ACTION UPDATE FOR MOVING if (this.actionID === null) { this.actionID = Entities.addAction("spring", this.grabbedEntity, { targetPosition: newPosition, @@ -184,7 +187,10 @@ controller.prototype.hidePointer = function() { controller.prototype.letGo = function() { - Entities.deleteAction(this.grabbedEntity, this.actionID); + if (this.grabbedEntity && this.actionID) { + this.deactivateEntity(this.grabbedEntity); + Entities.deleteAction(this.grabbedEntity, this.actionID); + } this.grabbedEntity = null; this.actionID = null; this.distanceHolding = false; @@ -264,11 +270,29 @@ controller.prototype.checkForInRangeObject = function() { if (grabbedEntity === null) { return false; } else { + //We are grabbing an entity, so let it know we've grabbed it this.grabbedEntity = grabbedEntity; + this.activateEntity(this.grabbedEntity); + return true; } } +controller.prototype.activateEntity = function(entity) { + var data = { + activated: true, + avatarId: MyAvatar.sessionUUID + }; + setEntityCustomData(GRAB_USER_DATA_KEY, this.grabbedEntity, data); +} + +controller.prototype.deactivateEntity = function(entity) { + var data = { + activated: false, + avatarId: null + }; + setEntityCustomData(GRAB_USER_DATA_KEY, this.grabbedEntity, data); +} controller.prototype.onActionEvent = function(action, state) { if (this.pullAction === action && state === 1) { @@ -293,7 +317,9 @@ controller.prototype.onActionEvent = function(action, state) { controller.prototype.cleanup = function() { Entities.deleteEntity(this.pointer); - Entities.deleteAction(this.grabbedEntity, this.actionID); + if (this.grabbedEntity) { + Entities.deleteAction(this.grabbedEntity, this.actionID); + } } function update() { @@ -314,6 +340,7 @@ function cleanup() { } + Script.scriptEnding.connect(cleanup); Script.update.connect(update) Controller.actionEvent.connect(onActionEvent); \ No newline at end of file