// windmill.js // Created by Timo Kandra September, 2016 // Distributed under the CreativeCommons license //The ID of the box which should detect the ball should be saved in the userData of the windmill blades thus: {detectionBox: "PUT THE BOX ID HERE WITHOUT THE CURLY BRACES"} (function(){ var _this = this; var id; var timer; const BALL_HIT_SOUND_URL = "http://hifi-content.s3.amazonaws.com/caitlyn/production/sounds/mg_golfball_hit_hard_3.wav"; const VOLUME = 1.0; var hit = function(){ var windmillUserData = JSON.parse(Entities.getEntityProperties(id).userData); var boxIdForBallDetection; if (windmillUserData.detectionBox != undefined){ boxIdForBallDetection = windmillUserData.detectionBox; }else{ boxIdForBallDetection = "0d1dc288-892b-4e6f-818f-4abcbb3ab38e"; } var boxData = Entities.getEntityProperties(boxIdForBallDetection); var box = boxData.dimensions; var b = box; var position = boxData.position; var p = position; var corner = {x:(p.x - b.x/2), y:(p.y - b.y/2), z:(p.z - b.z/2)}; var entitiesInBox = Entities.findEntitiesInBox(corner, box); for(var i in entitiesInBox){ var entity = Entities.getEntityProperties(entitiesInBox[i]); if (entity.type == "Sphere" && entity.dynamic == true){ var prop = {velocity:{x:0, y:9, z:-6.1}}; Entities.editEntity(entity.id, prop); var audioProperties = { volume: VOLUME, position: entity.position }; print(JSON.stringify(_this.BALL_HIT_SOUND)); Audio.playSound(_this.BALL_HIT_SOUND, audioProperties); //print("shoot "+ entity.id); } } }; _this.preload = function(uuID){ _this.BALL_HIT_SOUND = SoundCache.getSound(BALL_HIT_SOUND_URL); id = uuID; var prop = {angularVelocity: {x:2 * Math.PI / 3, y:0, z:0}, angularDamping: 0, rotation:{x:0, y:0, z:0, w:1}}; Entities.editEntity(id, prop); timer = Script.setInterval(hit, 1000 * (2 * Math.PI / prop.angularVelocity.x) / 4); print("loaded"); }; })