// chainSpawner.js // examples // // Created by Eric Levin on 9/2/15 // Copyright 2015 High Fidelity, Inc. // // Spawns a length of chain, for all thoise times you just need some chain to get the job done. // // Distributed under the Apache License, Version 2.0. // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html /*global print, MyAvatar, Entities, AnimationCache, SoundCache, Scene, Camera, Overlays, Audio, HMD, AvatarList, AvatarManager, Controller, UndoStack, Window, Account, GlobalServices, Script, ScriptDiscoveryService, LODManager, Menu, Vec3, Quat, AudioDevice, Paths, Clipboard, Settings, XMLHttpRequest, randFloat, randInt, pointInExtents, vec3equal, setEntityCustomData, getEntityCustomData */ map = function(value, min1, max1, min2, max2) { return min2 + (max2 - min2) * ((value - min1) / (max1 - min1)); } orientationOf = function(vector) { var Y_AXIS = { x: 0, y: 1, z: 0 }; var X_AXIS = { x: 1, y: 0, z: 0 }; var theta = 0.0; var RAD_TO_DEG = 180.0 / Math.PI; var direction, yaw, pitch; direction = Vec3.normalize(vector); yaw = Quat.angleAxis(Math.atan2(direction.x, direction.z) * RAD_TO_DEG, Y_AXIS); pitch = Quat.angleAxis(Math.asin(-direction.y) * RAD_TO_DEG, X_AXIS); return Quat.multiply(yaw, pitch); } var ground, wall; var links = []; var ZERO_VEC = {x: 0, y: 0, z: 0}; // ??? var DISTANCE_IN_FRONT_OF_ME = 2; Script.include("../libraries/utils.js"); function spawnLinks() { var linkModelURL = "https://hifi-public.s3.amazonaws.com/alan/Chain/Chain-v.fbx"; var collisionModelURL = "https://hifi-public.s3.amazonaws.com/alan/Chain/Chain-cv.obj"; /* var collisionSoundURL = "https://hifi-public.s3.amazonaws.com/alan/Chain/Clank-High-2.wav", */ var numLinks = 16; var baseRotation = Quat.fromPitchYawRollDegrees(0, 90, 0); var center = Vec3.sum(MyAvatar.position, Vec3.multiplyQbyV(MyAvatar.orientation, { x: 0, y: 0.0, z: -DISTANCE_IN_FRONT_OF_ME })); for (var i = 0; i < numLinks; i++) { var position = Vec3.sum(center, {x: i * .16, y: 0, z: 0 }) if(i % 2 === 0) { rot = 90; } else{ rot = 0; } var rotation = Quat.multiply(baseRotation, Quat.fromPitchYawRollDegrees(0, 0, rot)); var link = Entities.addEntity({ type: "Model", modelURL: linkModelURL, // collisionSoundURL: collisionSoundURL, rotation: rotation, shapeType: "compound", compoundShapeURL: collisionModelURL, position: position, collisionsWillMove: true, dimensions: {x: 0.06, y: 0.25, z: 0.39}, }); links.push(link); } Script.setTimeout(function(){ links.forEach(function(link){ Entities.editEntity(link,{ velocity: {x: 0, y: -.01, z: 0}, gravity: {x: 0, y: -2.5 - Math.random() * 6, z: 0} }); }); }, 2400); } spawnLinks(); function cleanup() { Entities.deleteEntity(ground); links.forEach(function(box){ Entities.deleteEntity(box); }); dustSystems.forEach(function(dustEffect) { Entities.deleteEntity(dustEffect); }) } Script.scriptEnding.connect(cleanup);