// // boom.js // // An object with this entity script will, when grabbed, apply physical forces to any nearby objects // // 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 // (function() { var entityID, wantDebug = true, BLAST_FORCE = 10, BLAST_RADIUS = 15, BLAST_STEPDOWN = 2.0, TIME_TILL_RESTORE = 5.0, readyToExplode = true, SOUNDBOOM = SoundCache.getSound("http://hifi-content.s3.amazonaws.com/alan/dev/Audio/bigboom_converted.wav"), RED = { red:255, green: 0, blue: 0 }, affected = [] function printDebug(message) { if (wantDebug) { print(message); } } function blowStuffUp(position, force) { var stuff = Entities.findEntities(position, BLAST_RADIUS); for (var i = 0; i < stuff.length; i++) { var properties = Entities.getEntityProperties(stuff[i]); if (!properties.locked && (stuff[i] != entityID)) { // Store original position affected.push({ entityID: stuff[i], position: properties.position, rotation: properties.rotation, gravity: properties.gravity, dynamic: properties.dynamic }); //Vec3.print("pos = ", affected[affected.length - 1].position); var diff = Vec3.subtract(properties.position, position); var distance = Vec3.length(diff); var velocity = Vec3.sum(properties.velocity, Vec3.multiply(force, Vec3.normalize(diff))); var angularVelocity = { x: Math.random() * force, y: Math.random() * force, z: Math.random() * force }; Entities.editEntity(stuff[i], { velocity: velocity, dynamic: true, gravity: { x: 0, y: -9, y: 0 }, angularVelocity: angularVelocity }); } } } function replaceStuff() { for (var i = 0; i < affected.length; i++) { Entities.editEntity(affected[i].entityID, { position: affected[i].position, rotation: affected[i].rotation, gravity: affected[i].gravity, dynamic: affected[i].dynamic, velocity: { x: 0, y: 0, z: 0 }, angularVelocity: { x: 0, y: 0, z: 0 } }); } affected = []; readyToExplode = true; } this.mousePressOnEntity = function() { if (!readyToExplode) { return; } printDebug("Boom!"); var properties = Entities.getEntityProperties(entityID); Entities.editEntity(entityID, { color: RED }); Audio.playSound(SOUNDBOOM, { position: properties.position, volume: 1.0, loop: false}); blowStuffUp(Vec3.sum(properties.position, { x: 0, y: -BLAST_STEPDOWN, z: 0 }), BLAST_FORCE) Script.setTimeout(replaceStuff, TIME_TILL_RESTORE * 1000); } this.preload = function(givenEntityID) { printDebug("load boom..."); entityID = givenEntityID; }; this.unload = function() { printDebug("Unload boom..."); }; });