From 0a46db60acc5cbc1936e4e56ce6ce39527e8d2a7 Mon Sep 17 00:00:00 2001 From: ericrius1 Date: Fri, 11 Sep 2015 12:01:01 -0700 Subject: [PATCH] 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);