From 30841169ee2e293f48d4c9a46d91288a33de6f2b Mon Sep 17 00:00:00 2001 From: Ryan Huffman Date: Mon, 9 Nov 2015 10:04:19 -0800 Subject: [PATCH] Move functions in baseball.js to utils.js --- examples/baseball/pitching.js | 58 +---------------------------------- examples/baseball/utils.js | 46 +++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 57 deletions(-) diff --git a/examples/baseball/pitching.js b/examples/baseball/pitching.js index 05f108b165..fe585d4f4a 100644 --- a/examples/baseball/pitching.js +++ b/examples/baseball/pitching.js @@ -2,34 +2,7 @@ print("Loading pitching"); //Script.include("../libraries/line.js"); Script.include("https://raw.githubusercontent.com/huffman/hifi/line-js/examples/libraries/line.js"); Script.include("http://rawgit.com/huffman/hifi/baseball/examples/baseball/firework.js"); - -function findEntity(properties, searchRadius) { - var entities = findEntities(properties, searchRadius); - return entities.length > 0 ? entities[0] : null; -} - -// Return all entities with properties `properties` within radius `searchRadius` -function findEntities(properties, searchRadius) { - var entities = Entities.findEntities(MyAvatar.position, searchRadius); - var matchedEntities = []; - var keys = Object.keys(properties); - for (var i = 0; i < entities.length; ++i) { - var match = true; - var candidateProperties = Entities.getEntityProperties(entities[i], keys); - for (var key in properties) { - if (candidateProperties[key] != properties[key]) { - // This isn't a match, move to next entity - match = false; - break; - } - } - if (match) { - matchedEntities.push(entities[i]); - } - } - - return matchedEntities; -} +Script.include("http://rawgit.com/huffman/hifi/baseball/examples/baseball/utils.js"); var DISTANCE_BILLBOARD_NAME = "CurrentScore"; var HIGH_SCORE_BILLBOARD_NAME = "HighScore"; @@ -247,35 +220,6 @@ var BASEBALL_STATE = { }; -function shallowCopy(obj) { - var copy = {} - for (var key in obj) { - copy[key] = obj[key]; - } - return copy; -} - -function randomInt(low, high) { - return Math.floor(randomFloat(low, high)); -} - -function randomFloat(low, high) { - if (high === undefined) { - high = low; - low = 0; - } - return low + Math.random() * (high - low); -} - -function playRandomSound(sounds, options) { - if (options === undefined) { - options = { - volume: 1.0, - position: MyAvatar.position, - } - } - Audio.playSound(sounds[randomInt(sounds.length)], options); -} function vec3Mult(a, b) { return { diff --git a/examples/baseball/utils.js b/examples/baseball/utils.js index 7266666cc9..f128797345 100644 --- a/examples/baseball/utils.js +++ b/examples/baseball/utils.js @@ -33,3 +33,49 @@ getSounds = function(soundURLs) { } return sounds; }; + +playRandomSound = function(sounds, options) { + if (options === undefined) { + options = { + volume: 1.0, + position: MyAvatar.position, + } + } + return Audio.playSound(sounds[randomInt(sounds.length)], options); +} + +shallowCopy = function(obj) { + var copy = {} + for (var key in obj) { + copy[key] = obj[key]; + } + return copy; +} + +findEntity = function(properties, searchRadius) { + var entities = findEntities(properties, searchRadius); + return entities.length > 0 ? entities[0] : null; +} + +// Return all entities with properties `properties` within radius `searchRadius` +findEntities = function(properties, searchRadius) { + var entities = Entities.findEntities(MyAvatar.position, searchRadius); + var matchedEntities = []; + var keys = Object.keys(properties); + for (var i = 0; i < entities.length; ++i) { + var match = true; + var candidateProperties = Entities.getEntityProperties(entities[i], keys); + for (var key in properties) { + if (candidateProperties[key] != properties[key]) { + // This isn't a match, move to next entity + match = false; + break; + } + } + if (match) { + matchedEntities.push(entities[i]); + } + } + + return matchedEntities; +}