From 5487c4cdab049baf5543edd91eac52a1f87a0072 Mon Sep 17 00:00:00 2001 From: PhilipRosedale Date: Tue, 8 Mar 2016 19:42:11 -0800 Subject: [PATCH] Firefly script --- examples/fireflies/firefly.js | 100 ++++++++++++++++++++++++++++ examples/fireflies/makeFireflies.js | 89 +++++++++++++++++++++++++ 2 files changed, 189 insertions(+) create mode 100644 examples/fireflies/firefly.js create mode 100644 examples/fireflies/makeFireflies.js diff --git a/examples/fireflies/firefly.js b/examples/fireflies/firefly.js new file mode 100644 index 0000000000..e4b1d3f989 --- /dev/null +++ b/examples/fireflies/firefly.js @@ -0,0 +1,100 @@ +// +// A firefly which is animated by passerbys. It's physical, no gravity, periodic forces applied. +// If a firefly is found to +// + +(function () { + var entityID, + timeoutID = null, + properties, + shouldSimulate = false, + ACTIVE_CHECK_INTERVAL = 100, // milliseconds + INACTIVE_CHECK_INTERVAL = 1000, // milliseconds + MAX_DISTANCE_TO_SIMULATE = 20, // meters + LIGHT_LIFETIME = 1400, // milliseconds a firefly light will stay alive + BUMP_SPEED = 1.5, // average velocity given by a bump + BUMP_CHANCE = 0.33, + MIN_SPEED = 0.125, // below this speed, firefly gets a new bump + SPIN_SPEED = 3.5, + BRIGHTNESS = 0.25, + wantDebug = false + + function randomVector(size) { + return { x: (Math.random() - 0.5) * size, + y: (Math.random() - 0.5) * size, + z: (Math.random() - 0.5) * size }; + } + + function printDebug(message) { + if (wantDebug) { + print(message); + } + } + + function maybe() { + properties = Entities.getEntityProperties(entityID); + var speed = Vec3.length(properties.velocity); + var distance = Vec3.distance(MyAvatar.position, properties.position); + printDebug("maybe: speed: " + speed + ", distance: " + distance); + if (shouldSimulate) { + // We are simulating this firefly, so do stuff: + if (distance > MAX_DISTANCE_TO_SIMULATE) { + shouldSimulate = false; + } else if ((speed < MIN_SPEED) && (Math.random() < BUMP_CHANCE)) { + bump(); + makeLight(); + } + } else if (Vec3.length(properties.velocity) == 0.0) { + // We've found a firefly that is not being simulated, so maybe take it over + if (distance < MAX_DISTANCE_TO_SIMULATE) { + shouldSimulate = true; + } + } + timeoutID = Script.setTimeout(maybe, (shouldSimulate == true) ? ACTIVE_CHECK_INTERVAL : INACTIVE_CHECK_INTERVAL); + } + + function bump() { + // Give the firefly a little brownian hop + printDebug("bump!"); + var velocity = randomVector(BUMP_SPEED); + if (velocity.y < 0.0) { velocity.y *= -1.0 }; + Entities.editEntity(entityID, { velocity: velocity, + angularVelocity: randomVector(SPIN_SPEED) }); + } + + function makeLight() { + printDebug("make light!"); + // create a light attached to the firefly that lives for a while + Entities.addEntity({ + type: "Light", + name: "firefly light", + intensity: 4.0 * BRIGHTNESS, + falloffRadius: 8.0 * BRIGHTNESS, + dimensions: { + x: 30 * BRIGHTNESS, + y: 30 * BRIGHTNESS, + z: 30 * BRIGHTNESS + }, + position: Vec3.sum(properties.position, { x: 0, y: 0.2, z: 0 }), + parentID: entityID, + color: { + red: 150 + Math.random() * 100, + green: 100 + Math.random() * 50, + blue: 150 + Math.random() * 100 + }, + lifetime: LIGHT_LIFETIME / 1000 + }); + } + + this.preload = function (givenEntityID) { + printDebug("preload firefly..."); + entityID = givenEntityID; + timeoutID = Script.setTimeout(maybe, ACTIVE_CHECK_INTERVAL); + }; + this.unload = function () { + printDebug("unload firefly..."); + if (timeoutID !== undefined) { + Script.clearTimeout(timeoutID); + } + }; +}) \ No newline at end of file diff --git a/examples/fireflies/makeFireflies.js b/examples/fireflies/makeFireflies.js new file mode 100644 index 0000000000..e4faf54307 --- /dev/null +++ b/examples/fireflies/makeFireflies.js @@ -0,0 +1,89 @@ +// +// Created by Philip Rosedale on March 7, 2016 +// Copyright 2016 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 +// +// Make some fireflies +// + +var SIZE = 0.05; +var TYPE = "Box"; // Right now this can be "Box" or "Model" or "Sphere" +var MODEL_URL = "http://s3.amazonaws.com/hifi-public/models/content/basketball2.fbx"; +var MODEL_DIMENSION = { x: 0.3, y: 0.3, z: 0.3 }; +//var ENTITY_URL = "file:///c:/users/dev/philip/examples/fireflies/firefly.js?"+Math.random() +var ENTITY_URL = "https://s3.amazonaws.com/hifi-public/philip/firefly.js" + +var RATE_PER_SECOND = 50; // The entity server will drop data if we create things too fast. +var SCRIPT_INTERVAL = 100; +var LIFETIME = 3600; + +var NUMBER_TO_CREATE = 200; + +var GRAVITY = { x: 0, y: -1.0, z: 0 }; +var VELOCITY = { x: 0.0, y: 0, z: 0 }; +var ANGULAR_VELOCITY = { x: 1, y: 1, z: 1 }; + +var DAMPING = 0.5; +var ANGULAR_DAMPING = 0.5; + +var collidable = true; +var gravity = true; + + +var x = 0; +var z = 0; +var totalCreated = 0; + +var RANGE = 50; +var HEIGHT = 3; +var HOW_FAR_IN_FRONT_OF_ME = 1.0; + + +var center = Vec3.sum(MyAvatar.position, Vec3.multiply(HOW_FAR_IN_FRONT_OF_ME, Quat.getFront(Camera.orientation))); + + +function randomVector(range) { + return { + x: (Math.random() - 0.5) * range.x, + y: (Math.random() - 0.5) * range.y, + z: (Math.random() - 0.5) * range.z + } +} + +Vec3.print("Center: ", center); + +Script.setInterval(function () { + if (!Entities.serversExist() || !Entities.canRez() || (totalCreated > NUMBER_TO_CREATE)) { + return; + } + + var numToCreate = RATE_PER_SECOND * (SCRIPT_INTERVAL / 1000.0); + for (var i = 0; (i < numToCreate) && (totalCreated < NUMBER_TO_CREATE); i++) { + + var position = Vec3.sum(center, randomVector({ x: RANGE, y: HEIGHT, z: RANGE })); + position.y += HEIGHT / 2.0; + + Entities.addEntity({ + type: TYPE, + modelURL: MODEL_URL, + name: "firefly", + position: position, + dimensions: (TYPE == "Model") ? MODEL_DIMENSION : { x: SIZE, y: SIZE, z: SIZE }, + color: { red: 150 + Math.random() * 100, green: 100 + Math.random() * 50, blue: 0 }, + velocity: VELOCITY, + angularVelocity: Vec3.multiply(Math.random(), ANGULAR_VELOCITY), + damping: DAMPING, + angularDamping: ANGULAR_DAMPING, + gravity: (gravity ? GRAVITY : { x: 0, y: 0, z: 0}), + dynamic: collidable, + script: ENTITY_URL, + lifetime: LIFETIME + }); + + totalCreated++; + print("Firefly #" + totalCreated); + } +}, SCRIPT_INTERVAL); +