From 1975343496bb934300698abf5ca9b2e5342b09d6 Mon Sep 17 00:00:00 2001 From: Thijs Wenker Date: Thu, 15 Sep 2016 17:33:00 -0700 Subject: [PATCH] Add butane lighter --- tutorial/lighter/butaneLighter.js | 188 ++++++++++++++++++++ tutorial/lighter/createButaneLighter.js | 219 ++++++++++++++++++++++++ 2 files changed, 407 insertions(+) create mode 100644 tutorial/lighter/butaneLighter.js create mode 100644 tutorial/lighter/createButaneLighter.js diff --git a/tutorial/lighter/butaneLighter.js b/tutorial/lighter/butaneLighter.js new file mode 100644 index 0000000000..2592d8ec4a --- /dev/null +++ b/tutorial/lighter/butaneLighter.js @@ -0,0 +1,188 @@ +// +// Created by Thijs Wenker on September 14, 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 +// + +(function() { + var _this; + + function getResourceURL(file) { + return 'http://hifi-content.s3.amazonaws.com/DomainContent/Tutorial/' + file; + }; + + const LIGHTER_ON_SOUND_URL = getResourceURL('Sounds/lighter_on.wav'); + const BUTANE_SOUND_URL = getResourceURL('Sounds/butane.wav'); + + // TODO: fix this in the client, changing the sound volume while a sound is playing doesn't seem to work right now + const DYNAMIC_SOUND_VOLUME = false; + const BUTANE_MIN_SOUND_VOLUME = 0.05; + + const FLAME_LENGTH = 0.05; + + const BUTANE_SOUND_SETTINGS = { + volume: 0.4, + loop: true, + playbackGap: 1000, + playbackGapRange: 1000 + }; + + const LIGHTER_ON_SOUND_SETTINGS = { + volume: 0.5, + loop: false + }; + + function RemoteLogSender(channel, identifier) { + this.channel = channel; + this.identifier = identifier; + } + + RemoteLogSender.prototype = { + channel: null, + identifier: null, + debug: function(message) { + Messages.sendLocalMessage(this.channel, JSON.stringify({ + message: '[DEBUG ' + this.identifier + '] ' + message + })); + } + }; + + var remoteLogSender = null; + + function debugPrint(message) { + if (remoteLogSender !== null) { + remoteLogSender.debug(message); + } + } + + ButaneLighter = function() { + _this = this; + _this.triggerValue = 0.0; // be sure to set this value in the constructor, otherwise it will be a shared value + _this.isFiring = false; + } + + ButaneLighter.prototype = { + entityID: null, + lighterOnSound: null, + butaneSound: null, + lighterOnSoundInjector: null, + butaneSoundInjector: null, + butaneSoundInjectorOptions: null, + lighterParticleEntity: null, + buttonBeingPressed: null, + triggerValue: null, + isFiring: null, + getLighterParticleEntity: function() { + var result = null; + Entities.getChildrenIDs(_this.entityID).forEach(function(entity) { + var name = Entities.getEntityProperties(entity, ['name']).name; + if (name === 'lighter_particle') { + result = entity; + } + }); + return result; + }, + preload: function(entityID) { + _this.entityID = entityID; + _this.lighterOnSound = SoundCache.getSound(LIGHTER_ON_SOUND_URL); + _this.butaneSound = SoundCache.getSound(BUTANE_SOUND_URL); + var properties = Entities.getEntityProperties(_this.entityID, ['userData']); + try { + var userData = JSON.parse(properties.userData); + if (userData['debug'] !== undefined && userData['debug']['sessionUUID'] === MyAvatar.sessionUUID && + userData['debug']['channel'] !== undefined) + { + remoteLogSender = new RemoteLogSender(userData['debug']['channel'], _this.entityID); + remoteLogSender.debug('Debugger initialized'); + } + } catch (e) { + //failed to detect if we have a debugger + } + debugPrint(_this.getLighterParticleEntity()); + }, + startEquip: function(entityID, args) { + _this.lighterParticleEntity = _this.getLighterParticleEntity(); + }, + continueEquip: function(entityID, args) { + _this.triggerValue = Controller.getValue(args[0] === 'left' ? Controller.Standard.LT : Controller.Standard.RT); + if (_this.triggerValue > 0.2) { + if (!_this.isFiring) { + _this.startFiring(); + } + _this.tryToIgnite(); + _this.updateButaneSound() + return; + } + _this.stopFiring(); + }, + startFiring: function() { + if (_this.isFiring) { + return; + } + _this.isFiring = true; + if (_this.lighterOnSound.downloaded) { + // We don't want to override the default volume setting, so lets clone the default SETTINGS object + var lighterOnOptions = JSON.parse(JSON.stringify(LIGHTER_ON_SOUND_SETTINGS)); + lighterOnOptions['position'] = Entities.getEntityProperties(_this.entityID, ['position']).position; + _this.lighterOnSoundInjector = Audio.playSound(_this.lighterOnSound, lighterOnOptions); + } + if (_this.butaneSound.downloaded) { + _this.butaneSoundInjectorOptions = JSON.parse(JSON.stringify(BUTANE_SOUND_SETTINGS)); + _this.butaneSoundInjectorOptions['position'] = Entities.getEntityProperties(_this.lighterParticleEntity, ['position']).position; + if (DYNAMIC_SOUND_VOLUME) { + _this.butaneSoundInjectorOptions['volume'] = BUTANE_MIN_SOUND_VOLUME; + } + _this.butaneSoundInjector = Audio.playSound(_this.butaneSound, _this.butaneSoundInjectorOptions); + } + Entities.editEntity(_this.lighterParticleEntity, {isEmitting: _this.isFiring}); + + }, + stopFiring: function() { + if (!_this.isFiring) { + return; + } + _this.isFiring = false; + Entities.editEntity(_this.lighterParticleEntity, {isEmitting: _this.isFiring}); + _this.stopButaneSound(); + }, + tryToIgnite: function() { + var flameProperties = Entities.getEntityProperties(_this.lighterParticleEntity, ['position', 'rotation']); + var pickRay = { + origin: flameProperties.position, + direction: Quat.getFront(flameProperties.rotation) + } + var intersection = Entities.findRayIntersection(pickRay, true); + if (intersection.intersects) { + debugPrint(JSON.stringify(intersection)); + } + }, + releaseEquip: function(entityID, args) { + _this.stopFiring(); + // reset trigger value; + _this.triggerValue = 0.0; + }, + updateButaneSound: function() { + if (_this.butaneSoundInjector !== null && _this.butaneSoundInjector.isPlaying()) { + _this.butaneSoundInjectorOptions = _this.butaneSoundInjector.options; + _this.butaneSoundInjectorOptions['position'] = Entities.getEntityProperties(_this.entityID, ['position']).position; + if (DYNAMIC_SOUND_VOLUME) { + _this.butaneSoundInjectorOptions['volume'] = ((BUTANE_SOUND_SETTINGS.volume - BUTANE_MIN_SOUND_VOLUME) * + _this.triggerValue) + BUTANE_MIN_SOUND_VOLUME; + } + _this.butaneSoundInjector.options = _this.butaneSoundInjectorOptions; + } + }, + stopButaneSound: function() { + if (_this.butaneSoundInjector !== null && _this.butaneSoundInjector.isPlaying()) { + _this.butaneSoundInjector.stop(); + } + _this.butaneSoundInjector = null; + }, + unload: function() { + _this.stopButaneSound(); + }, + }; + return new ButaneLighter(); +}) diff --git a/tutorial/lighter/createButaneLighter.js b/tutorial/lighter/createButaneLighter.js new file mode 100644 index 0000000000..ec305ecb05 --- /dev/null +++ b/tutorial/lighter/createButaneLighter.js @@ -0,0 +1,219 @@ +// +// Created by Thijs Wenker on September 14, 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 +// + +const TEST_MODE = false; +const SCRIPT_URL = 'https://dl.dropboxusercontent.com/u/14997455/hifi/butaneLighter/butaneLighter.js?v=' + Date.now(); +//const SCRIPT_URL = Script.resolvePath("butaneLighter.js"); + +function getResourceURL(file) { + return 'http://hifi-content.s3.amazonaws.com/DomainContent/Tutorial/' + file; +}; + +//Creates an entity and returns a mixed object of the creation properties and the assigned entityID +var createEntity = function(entityProperties, parent) { + if (parent.rotation !== undefined) { + if (entityProperties.rotation !== undefined) { + entityProperties.rotation = Quat.multiply(parent.rotation, entityProperties.rotation); + } else { + entityProperties.rotation = parent.rotation; + } + } + if (parent.position !== undefined) { + var localPosition = (parent.rotation !== undefined) ? Vec3.multiplyQbyV(parent.rotation, entityProperties.position) : entityProperties.position; + entityProperties.position = Vec3.sum(localPosition, parent.position) + } + if (parent.id !== undefined) { + entityProperties.parentID = parent.id; + } + entityProperties.id = Entities.addEntity(entityProperties); + return entityProperties; +}; + +createButaneLighter = function(transform) { + var entityProperties = { + collisionsWillMove: true, + dimensions: { + x: 0.025599999353289604, + y: 0.057399999350309372, + z: 0.37419998645782471 + }, + dynamic: true, + gravity: { + x: 0, + y: -9.8, + z: 0 + }, + velocity: { + x: 0, + y: -0.01, + z: 0 + }, + modelURL: getResourceURL('Models/lighterIceCreamSandwich.fbx'), + name: 'BrutaneLighter', + shapeType: 'simple-compound', + type: 'Model', + userData: JSON.stringify({ + tag: "equip-temporary", + grabbableKey: { + invertSolidWhileHeld: true + }, + wearable: { + joints: { + RightHand: [{ + x: 0.029085848480463028, + y: 0.09807153046131134, + z: 0.03062543272972107 + }, { + x: 0.5929139256477356, + y: 0.3207578659057617, + z: 0.7151655554771423, + w: -0.18468326330184937 + }], + LeftHand: [{ + x: -0.029085848480463028, + y: 0.09807153046131134, + z: 0.03062543272972107 + }, { + x: -0.5929139256477356, + y: 0.3207578659057617, + z: 0.7151655554771423, + w: -0.18468326330184937 + }] + } + } + }), + script: SCRIPT_URL + }; + return createEntity(entityProperties, transform); +} + +function createFireParticle(butaneLighter) { + var entityProperties = { + userData: JSON.stringify({ tag: "equip-temporary" }), + accelerationSpread: { + x: 0.1, + y: 0, + z: 0.1 + }, + alpha: 0.039999999105930328, + alphaFinish: 0.039999999105930328, + alphaStart: 0.039999999105930328, + azimuthFinish: 0.039999999105930328, + azimuthStart: 0, + dimensions: { + x: 0.49194091558456421, + y: 0.49194091558456421, + z: 0.49194091558456421 + }, + emitAcceleration: { + x: 0, + y: 0, + z: 0 + }, + emitOrientation: { + w: 1, + x: -1.52587890625e-05, + y: -1.52587890625e-05, + z: -1.52587890625e-05 + }, + emitRate: 770, + emitSpeed: 0.014000000432133675, + isEmitting: false, + lifespan: 0.37000000476837158, + maxParticles: 820, + name: 'lighter_particle', + particleRadius: 0.0027000000700354576, + position: { + x: -0.00044769048690795898, + y: 0.016354814171791077, + z: 0.19217036664485931 + }, + radiusFinish: 0.0027000000700354576, + radiusSpread: 3, + radiusStart: 0.0027000000700354576, + rotation: { + w: 1, + x: -0.0001678466796875, + y: -1.52587890625e-05, + z: -1.52587890625e-05 + }, + speedSpread: 0.56999999284744263, + textures: 'atp:/textures/fire3.png', + type: 'ParticleEffect', + + + "color": { + "red": 255, + "green": 255, + "blue": 255 + }, + "isEmitting": 0, + "maxParticles": 820, + "lifespan": 0.28, + "emitRate": 1100, + "emitSpeed": 0.007, + "speedSpread": 0.5699999928474426, + "emitOrientation": { + "x": -0.0000152587890625, + "y": -0.0000152587890625, + "z": -0.0000152587890625, + "w": 1 + }, + "emitDimensions": { + "x": 0, + "y": 0, + "z": 0 + }, + "polarStart": 0, + "polarFinish": 0, + "azimuthStart": 0, + "azimuthFinish": 0.03999999910593033, + "emitAcceleration": { + "x": 0, + "y": 0, + "z": 0 + }, + "accelerationSpread": { + "x": 0, + "y": 0, + "z": 0 + }, + "particleRadius": 0.0037, + "radiusSpread": 3, + "radiusStart": 0.008, + "radiusFinish": 0.0004, + "colorSpread": { + "red": 0, + "green": 0, + "blue": 0 + }, + "colorStart": { + "red": 255, + "green": 255, + "blue": 255 + }, + "colorFinish": { + "red": 255, + "green": 255, + "blue": 255 + }, + "alpha": 0.03999999910593033, + "alphaSpread": 0, + "alphaStart": 0.141, + "alphaFinish": 0.02, + "emitterShouldTrail": 0, + "textures": "atp:/textures/fire3.png" + }; + return createEntity(entityProperties, butaneLighter); +} + +doCreateButaneLighter = function(transform) { + var butaneLighter = createButaneLighter(transform); + createFireParticle(butaneLighter); + return butaneLighter; +}