From df4be6c0ba8a5829f2f05678e0a3a4b9509b7dad Mon Sep 17 00:00:00 2001 From: Zach Pomerantz Date: Tue, 3 Nov 2015 10:29:41 -0800 Subject: [PATCH 1/2] Add growth script to examples --- examples/growth.js | 75 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 examples/growth.js diff --git a/examples/growth.js b/examples/growth.js new file mode 100644 index 0000000000..8a730da6c1 --- /dev/null +++ b/examples/growth.js @@ -0,0 +1,75 @@ +// +// growth.js +// +// Created by Zachary Pomerantz 11/2/2015. +// Copyright 2015 High Fidelity, Inc. +// +// Distributed under the Apache License, Version 2.0. +// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html +// +var RADIUS = 5; +var LIFETIME = 6000; + +var size = 1; +var center = MyAvatar.position; +var positions = [ + Vec3.sum(center, Vec3.multiply(RADIUS, Vec3.FRONT)), + Vec3.sum(center, Vec3.multiply(-RADIUS, Vec3.FRONT)), + Vec3.sum(center, Vec3.multiply(RADIUS, Vec3.RIGHT)), + Vec3.sum(center, Vec3.multiply(-RADIUS, Vec3.RIGHT)), + Vec3.sum(center, Vec3.multiply(RADIUS, Vec3.UP)), + Vec3.sum(center, Vec3.multiply(-RADIUS, Vec3.UP)), +]; + +var spheres = map(positions, getSphere); + +Script.update.connect(function(delta) { + size += delta; // grow by 1 unit/s + each(spheres, function(sphere) { + Entities.editEntity(sphere, { dimensions: getDimensions(size) }); + }); +}); +Script.scriptEnding.connect(function() { + each(spheres, function(sphere) { + Entities.deleteEntity(sphere); + }); +}); + +// Entity helpers + +function getSphere(position) { + return Entities.addEntity({ + type: 'Sphere', + position: position, + dimensions: getDimensions(size), + color: getColor(), + gravity: Vec3.ZERO, + lifetime: LIFETIME, + ignoreCollisions: true, + }); +} + +function getDimensions(size) { + return { x: size, y: size, z: size }; +} + +function getColor() { + return { red: getValue(), green: getValue(), blue: getValue() }; + function getValue() { return Math.floor(Math.random() * 255); } +} + +// Functional helpers + +function map(list, iterator) { + var result = []; + for (var i = 0; i < list.length; i++) { + result.push(iterator(list[i], i, list)); + } + return result; +} + +function each(list, iterator) { + for (var i = 0; i < list.length; i++) { + iterator(list[i], i, list); + } +} From 33851baa3f953c44a4a58c82a406e887fb34031d Mon Sep 17 00:00:00 2001 From: Zach Pomerantz Date: Wed, 11 Nov 2015 22:32:42 -0800 Subject: [PATCH 2/2] Add directionality to examples/growth *There is a bug in here, but it may be in Interface.* The sphere entities have a lifetime of 60s. The first time the script is run, each click fires one clickReleaseOnEntity event. If the entities expire (lifetime is reached), and the script is rerun a second time, each click will fire two clickReleaseOnEntity events. If this is repeated (waiting for expiration each time), then the N-th repetition will fire N clickReleaseOnEntity events. Note that each entity, across runs, has a unique uuid, as expected. --- examples/growth.js | 69 ++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 58 insertions(+), 11 deletions(-) diff --git a/examples/growth.js b/examples/growth.js index 8a730da6c1..8bd49d92c5 100644 --- a/examples/growth.js +++ b/examples/growth.js @@ -8,40 +8,87 @@ // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html // var RADIUS = 5; -var LIFETIME = 6000; +var LIFETIME = 60; // only keep these entities around for a minute + +var SIZE = 1; // starting size +var SIZE_MAX = 3; +var SIZE_MIN = 0.5; -var size = 1; var center = MyAvatar.position; var positions = [ Vec3.sum(center, Vec3.multiply(RADIUS, Vec3.FRONT)), Vec3.sum(center, Vec3.multiply(-RADIUS, Vec3.FRONT)), Vec3.sum(center, Vec3.multiply(RADIUS, Vec3.RIGHT)), Vec3.sum(center, Vec3.multiply(-RADIUS, Vec3.RIGHT)), - Vec3.sum(center, Vec3.multiply(RADIUS, Vec3.UP)), - Vec3.sum(center, Vec3.multiply(-RADIUS, Vec3.UP)), ]; +// Create spheres around the avatar var spheres = map(positions, getSphere); -Script.update.connect(function(delta) { - size += delta; // grow by 1 unit/s - each(spheres, function(sphere) { - Entities.editEntity(sphere, { dimensions: getDimensions(size) }); - }); +// Toggle sphere growth +each(spheres, function(sphere) { + var state = { + size: SIZE, + grow: true, // grow/shrink + state: false, // mutate/pause + }; + + // Grow or shrink on click + Script.addEventHandler(sphere, 'clickReleaseOnEntity', toggleGrowth); + // TODO: Toggle on collisions as well + + function toggleGrowth() { + if (state.state) { + stop(); + } else { + start(); + } + } + + function start() { + var verb = state.grow ? 'grow' : 'shrink'; + print('starting to '+verb+' '+sphere); + + Script.update.connect(grow); + state.state = true; + } + + function stop() { + print('stopping '+sphere); + + Script.update.disconnect(grow); + state.state = false; + state.grow = !state.grow; + } + + function grow(delta) { + if ((state.grow && state.size > SIZE_MAX) || // cannot grow more + (!state.grow && state.size < SIZE_MIN)) { // cannot shrink more + stop(); + return; + } + + state.size += (state.grow ? 1 : -1) * delta; // grow/shrink + Entities.editEntity(sphere, { dimensions: getDimensions(state.size) }); + } }); -Script.scriptEnding.connect(function() { + +Script.scriptEnding.connect(function() { // cleanup each(spheres, function(sphere) { Entities.deleteEntity(sphere); }); }); +// NOTE: Everything below this line is a helper. +// The basic logic of this example is entirely above this note. + // Entity helpers function getSphere(position) { return Entities.addEntity({ type: 'Sphere', position: position, - dimensions: getDimensions(size), + dimensions: getDimensions(SIZE), color: getColor(), gravity: Vec3.ZERO, lifetime: LIFETIME,