// PD Utility functions RETURN_CHARCODE = 0x01000004; ENTER_CHARCODE = 0x01000005; SPACEBAR_CHARCODE = 32; var pd_baseUrl = "http://s3.amazonaws.com/hifi-public/PlanetDropBundle_v2/"; pd_path = function(partialPath) { return pd_baseUrl + partialPath; } printDebug = function(theMessage) { print("------------- DEBUG " + theMessage + " ----------------"); } string_of_enum = function(enumVar,value) { for (var k in enumVar) if (enumVar[k] == value) return k; return null; } vec3_add = function(a, b) { return { x:a.x + b.x, y:a.y + b.y, z:a.z + b.z }; } vec3_distSquared = function(a, b) { var result = 0; result = (b.x - a.x)*(b.x - a.x) + (b.y - a.y)*(b.y - a.y) + (b.z - a.z)*(b.z - a.z); return result; } getRandomInt = function(min, max) { return Math.floor(Math.random() * (max - min)) + min; } // Show/hide overlay, default = show overlay_show = function(overlay, isVisible) { isVisible = typeof isVisible !== 'undefined' ? isVisible : true; Overlays.editOverlay(overlay, {visible: isVisible}); }; overlay_changeText = function(overlay, message) { Overlays.editOverlay(overlay, {text: message}); } // Finds entities with userData.pdType field and returns array of [entityID, entityProps, userData struct] function pd_findEcoTargetsInRange(pos, range) { var foundEnts = Entities.findEntities(pos, range); var targets = []; //printDebug(foundEnts.length); for( i = 0; i < foundEnts.length; i++) { var entity = foundEnts[i]; var entityProps = Entities.getEntityProperties(entity); var userDataString = entityProps.userData; if (typeof(userDataString) == "undefined") continue; if (userDataString == "") continue; var userData = JSON.parse(userDataString); if (userData.pdType != "undefined") { targets.push( [entity, entityProps, userData] ); } } return targets; }; function pd_findClosestEcoTargetInRange(pos, range) { var shortestDistance = 10000; var closestIndex = -1; var allTargets = pd_findEcoTargetsInRange(pos,range); //printDebug("Num ecoTargets in range: " + allTargets.length); for (var i=0; i < allTargets.length; i++ ) { var posTarget = allTargets[i][1].position; var dist = vec3_distSquared(pos, posTarget); if (dist < shortestDistance) { shortestDistance = dist; closestIndex = i; } } if (closestIndex > -1) return allTargets[closestIndex]; return null; // Nothing in range } function entity_pos(entityID) { var props = Entities.getEntityProperties( entityID ); return props.position; } function entity_moveToPos(entityID, pos) { var props = Entities.getEntityProperties( entityID ); Entities.editEntity(entityID, {position: pos }); } function entity_show(entityID, isVisible) { isVisible = typeof isVisible !== 'undefined' ? isVisible : true; Entities.editEntity(entityID, {visible: isVisible }); } // Returns overlay object createOverlay = function(message, pos, fontSize, bgAlpha, isVisible) { isVisible = typeof isVisible !== 'undefined' ? isVisible : false; var overlay = Overlays.addOverlay("text", { alpha: 1.0, text: message, font: { size: fontSize }, x: pos.x, y: pos.y, width: 160, height:128, backgroundColor: { red: 0, green: 0, blue: 0}, backgroundAlpha: bgAlpha, color: { red: 255, green: 0, blue: 0}, topMargin: 8, visible: isVisible }); return overlay; } createImageOverlay = function(imageUrl, pos, isVisible, width, height) { printDebug("createImageOverlay"); // isVisible = typeof isVisible !== 'undefined' ? isVisible : false; var overlay = Overlays.addOverlay("image", { alpha: 1.0, imageURL: imageUrl, x: pos.x, y: pos.y, width: width, height:height, visible: isVisible }); return overlay; } changeOverlay = function(overlay, message) { Overlays.editOverlay(overlay, {text: message}); } // Returns entityID function createModelEntity(url, pos, name, dims, lifetime) { //print("CreateModelEntity"); var position = pos; var createProperties = { lifetime: lifetime, name: name, type: "Model", position: position, modelURL: url, animationIsPlaying: false, dimensions: dims, animation: { url: url, firstFrame: 0, fps: 10, currentFrame: 0, hold: false, lastFrame: 10000, loop: true, running: true, startAutomatically:true }, }; var entityID = Entities.addEntity(createProperties); //var postCreateProps = Entities.getEntityProperties(entityID); //print("Natural dimensions: " + JSON.stringify(postCreateProps.naturalDimensions)); //print("Dimensions: " + JSON.stringify(postCreateProps.dimensions)); //Entities.editEntity(entityID, { dimensions: postCreateProps.naturalDimensions}); print("Created entityID: " + entityID); return entityID; } // Used in movement control function KeyInfo(contribution) { this.motorContribution = contribution; // Vec3 contribution of this key toward motorVelocity this.releaseTime = new Date(); // time when this button was released this.pressTime = new Date(); // time when this button was pressed this.isPressed = false; }