From 7f91491f04e728513410dddfa7563c95e79ece73 Mon Sep 17 00:00:00 2001 From: Ryan Huffman Date: Thu, 15 Sep 2016 13:11:50 -0700 Subject: [PATCH] Add entity scripts for tutorial --- tutorial/firePit/fire.js | 169 ++++++++++++++++++++++++++++++++++++ tutorial/firePit/flicker.js | 51 +++++++++++ tutorial/fuse.js | 74 ++++++++++++++++ tutorial/spinner.js | 66 ++++++++++++++ 4 files changed, 360 insertions(+) create mode 100644 tutorial/firePit/fire.js create mode 100644 tutorial/firePit/flicker.js create mode 100644 tutorial/fuse.js create mode 100644 tutorial/spinner.js diff --git a/tutorial/firePit/fire.js b/tutorial/firePit/fire.js new file mode 100644 index 0000000000..0747bc9f14 --- /dev/null +++ b/tutorial/firePit/fire.js @@ -0,0 +1,169 @@ +// this script turns an entity into an exploder -- anything that collides with it will be vaporized! +// +// + +(function() { + + var _this = this; + + function Fire() { + _this = this; + } + + var RED = { + red: 255, + green: 0, + blue: 0 + }; + + var ORANGE = { + red: 255, + green: 165, + blue: 0 + }; + + var YELLOW = { + red: 255, + green: 255, + blue: 0 + }; + + var GREEN = { + red: 0, + green: 255, + blue: 0 + }; + + var BLUE = { + red: 0, + green: 0, + blue: 255 + }; + + var INDIGO = { + red: 128, + green: 0, + blue: 128 + }; + + var VIOLET = { + red: 75, + green: 0, + blue: 130 + }; + + var colors = [RED, ORANGE, YELLOW, GREEN, BLUE, INDIGO, VIOLET]; + + Fire.prototype = { + preload: function(entityID) { + this.entityID = entityID; + this.EXPLOSION_SOUND = SoundCache.getSound("atp:/firepit/fire_burst.wav"); + print("IN FIRE SCRIPT"); + }, + collisionWithEntity: function(myID, otherID, collisionInfo) { + print("FIRE SCRIPT: COLLIDED"); + var otherProps = Entities.getEntityProperties(otherID); + var data = null; + print("FIRE SCRIPT: 2 COLLIDED"); + try { + print("parsing.."); + data = JSON.parse(otherProps.userData) + print("done parsing.."); + } catch (err) { + print('ERROR GETTING USERDATA!'); + } + print("HERE"); + if (data === null || "") { + return; + } else { + if (data.hasOwnProperty('hifiHomeKey')) { + if (data.hifiHomeKey.reset === true) { + print('FLAMMABLE THING, EXPLODE IT!'); + _this.playSoundAtCurrentPosition(); + _this.explodeWithColor(); + Entities.deleteEntity(otherID) + Messages.sendMessage('Entity-Exploded', JSON.stringify({ + entityID: otherID, + })); + } + } + } + }, + explodeWithColor: function() { + print('EXPLODE!') + var myProps = Entities.getEntityProperties(this.entityID); + var color = colors[Math.floor(Math.random() * colors.length)]; + var explosionParticleProperties = { + "color": color, + "isEmitting": 1, + "maxParticles": 1000, + "lifespan": 0.25, + "emitRate": 1, + "emitSpeed": 0.1, + "speedSpread": 1, + "emitOrientation": Quat.getUp(myProps.rotation), + "emitDimensions": { + "x": 0, + "y": 0, + "z": 0 + }, + "polarStart": 0, + "polarFinish": 0, + "azimuthStart": 0, + "azimuthFinish": 0, + "emitAcceleration": { + "x": 0, + "y": 0, + "z": 0 + }, + "accelerationSpread": { + "x": 0, + "y": 0, + "z": 0 + }, + "particleRadius": 0.829, + "radiusSpread": 0, + "radiusStart": 0.361, + "radiusFinish": 0.294, + "colorSpread": { + "red": 0, + "green": 0, + "blue": 0 + }, + "colorStart": { + "red": 255, + "green": 255, + "blue": 255 + }, + "colorFinish": { + "red": 255, + "green": 255, + "blue": 255 + }, + "alpha": 1, + "alphaSpread": 0, + "alphaStart": -0.2, + "alphaFinish": 0.5, + "emitterShouldTrail": 0, + "textures": "atp:/firepit/explode.png", + "type": "ParticleEffect", + lifetime: 1, + position: myProps.position + }; + + var explosion = Entities.addEntity(explosionParticleProperties); + print('explosion is: ' + explosion) + }, + playSoundAtCurrentPosition: function() { + + var audioProperties = { + volume: 0.5, + position: Entities.getEntityProperties(this.entityID).position + }; + + Audio.playSound(this.EXPLOSION_SOUND, audioProperties); + }, + } + + return new Fire(); +}); diff --git a/tutorial/firePit/flicker.js b/tutorial/firePit/flicker.js new file mode 100644 index 0000000000..43148dabba --- /dev/null +++ b/tutorial/firePit/flicker.js @@ -0,0 +1,51 @@ +(function() { + + var MINIMUM_LIGHT_INTENSITY = 50.0; + var MAXIMUM_LIGHT_INTENSITY = 200.0; + var LIGHT_FALLOFF_RADIUS = 0.1; + var LIGHT_INTENSITY_RANDOMNESS = 0.1; + + function randFloat(low, high) { + return low + Math.random() * (high - low); + } + + var _this; + + function FlickeringFlame() { + _this = this; + } + + var totalTime = 0; + var spacer = 2; + FlickeringFlame.prototype = { + preload: function(entityID) { + this.entityID = entityID; + Script.update.connect(this.update); + }, + update: function(deltaTime) { + + totalTime += deltaTime; + if (totalTime > spacer) { + var howManyAvatars = AvatarList.getAvatarIdentifiers().length; + var intensity = (MINIMUM_LIGHT_INTENSITY + (MAXIMUM_LIGHT_INTENSITY + (Math.sin(totalTime) * MAXIMUM_LIGHT_INTENSITY))); + intensity += randFloat(-LIGHT_INTENSITY_RANDOMNESS, LIGHT_INTENSITY_RANDOMNESS); + + Entities.editEntity(_this.entityID, { + intensity: intensity + }); + + spacer = Math.random(0, 100) * (2 / howManyAvatars); + totalTime = 0; + } else { + //just keep counting + } + }, + unload: function() { + Script.update.disconnect(this.update) + } + } + + return new FlickeringFlame + + +}); \ No newline at end of file diff --git a/tutorial/fuse.js b/tutorial/fuse.js new file mode 100644 index 0000000000..d09dc11bd1 --- /dev/null +++ b/tutorial/fuse.js @@ -0,0 +1,74 @@ +(function() { + var fuseSound = SoundCache.getSound("https://hifi-content.s3.amazonaws.com/DomainContent/Tutorial/Sounds/fuse.wav"); + function getChildProperties(entityID, propertyNames) { + var childEntityIDs = Entities.getChildrenIDs(entityID); + var results = {} + for (var i = 0; i < childEntityIDs.length; ++i) { + var childEntityID = childEntityIDs[i]; + var properties = Entities.getEntityProperties(childEntityID, propertyNames); + results[childEntityID] = properties; + } + return results; + } + var Fuse = function() { + }; + Fuse.prototype = { + onLit: function() { + print("LIT", this.entityID); + Entities.editEntity(this.entityID, { + animation: { + currentFrame: 0, + //"lastFrame": 130, + running: 1, + url: "https://hifi-content.s3.amazonaws.com/ozan/dev/anim/fuse/fuse.fbx", + loop: 0 + }, + }); + var injector = Audio.playSound(fuseSound, { + position: Entities.getEntityProperties(this.entityID, 'position').position, + volume: 0.7, + loop: true + }); + + var childrenProps = getChildProperties(this.entityID, ['type']); + for (var childEntityID in childrenProps) { + var props = childrenProps[childEntityID]; + if (props.type == "ParticleEffect") { + Entities.editEntity(childEntityID, { + emitRate: 140, + }); + } else if (props.type == "Light") { + Entities.editEntity(childEntityID, { + visible: true, + }); + } + } + + var self = this; + Script.setTimeout(function() { + print("BLOW UP"); + Entities.callEntityMethod("{dd13fcd5-616f-4749-ab28-2e1e8bc512e9}", "onLit"); + injector.stop(); + + var childrenProps = getChildProperties(self.entityID, ['type']); + for (var childEntityID in childrenProps) { + var props = childrenProps[childEntityID]; + if (props.type == "ParticleEffect") { + Entities.editEntity(childEntityID, { + emitRate: 0, + }); + } else if (props.type == "Light") { + Entities.editEntity(childEntityID, { + visible: false, + }); + } + } + + }, 4900); + }, + preload: function(entityID) { + this.entityID = entityID; + }, + }; + return new Fuse(); +}); diff --git a/tutorial/spinner.js b/tutorial/spinner.js new file mode 100644 index 0000000000..348e250bec --- /dev/null +++ b/tutorial/spinner.js @@ -0,0 +1,66 @@ +(function() { + var spinnerSound = SoundCache.getSound("http://hifi-content.s3.amazonaws.com/DomainContent/Tutorial/Sounds/Pinwheel.L.wav"); + var Spinner = function() { + }; + function getChildProperties(entityID, propertyNames) { + var childEntityIDs = Entities.getChildrenIDs(entityID); + var results = {} + for (var i = 0; i < childEntityIDs.length; ++i) { + var childEntityID = childEntityIDs[i]; + var properties = Entities.getEntityProperties(childEntityID, propertyNames); + results[childEntityID] = properties; + } + return results; + } + Spinner.prototype = { + onLit: function() { + print("LIT SPINNER", this.entityID); + Entities.editEntity(this.entityID, { + "angularDamping": 0.1, + "angularVelocity": { + "x": 20.471975326538086, + "y": 0, + "z": 0 + }, + }); + var injector = Audio.playSound(spinnerSound, { + position: Entities.getEntityProperties(this.entityID, 'position').position, + volume: 0.7, + loop: true + }); + + print("HERE2"); + var childrenProps = getChildProperties(this.entityID, ['type']); + for (var childEntityID in childrenProps) { + var props = childrenProps[childEntityID]; + if (props.type == "ParticleEffect") { + Entities.editEntity(childEntityID, { + emitRate: 140, + }); + } + } + Messages.sendLocalMessage("Tutorial-Spinner", "wasLit"); + + var self = this; + Script.setTimeout(function() { + print("BLOW UP"); + injector.stop(); + + print("HERE"); + var childrenProps = getChildProperties(self.entityID, ['type']); + for (var childEntityID in childrenProps) { + var props = childrenProps[childEntityID]; + if (props.type == "ParticleEffect") { + Entities.editEntity(childEntityID, { + emitRate: 0, + }); + } + } + }, 4900); + }, + preload: function(entityID) { + this.entityID = entityID; + }, + }; + return new Spinner(); +});