// bowlingPinEntity.js // // Created by Thijs Wenker on September 21, 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 _this; Script.include("https://hifi-content.s3.amazonaws.com/lincoln/utils.js"); var BOWLING_ALLEY_PREFIX_URL = 'http://hifi-content.s3.amazonaws.com/caitlyn/production/bowlingAlley/'; var PIN_FALL_SOUNDS = [ 'bowlingPinFall_r_1.wav', 'bowlingPinFall_r_2.wav', 'bowlingPinFall_r_3.wav', 'bowlingPinFall_r_4.wav' ]; // Return all entities with properties `properties` within radius `searchRadius` var findEntities = function(position, properties, searchRadius) { searchRadius = searchRadius ? searchRadius : 100000; // var entities = Entities.findEntities({ x: 0, y: 0, z: 0 }, searchRadius); var entities = Entities.findEntities(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; } function BowlingPin() { _this = this; _this.pinFallSounds = []; PIN_FALL_SOUNDS.forEach(function(pinFallSound) { _this.pinFallSounds.push(SoundCache.getSound(BOWLING_ALLEY_PREFIX_URL + pinFallSound)); }) _this.launchSound = SoundCache.getSound("https://s3-us-west-1.amazonaws.com/hifi-content/eric/Sounds/missle+launch.wav"); _this.explosionSound = SoundCache.getSound("https://s3-us-west-1.amazonaws.com/hifi-content/eric/Sounds/fireworksExplosion.wav"); _this.timeToExplosionRange = { min: 2500, max: 4500 }; } BowlingPin.prototype = { entityID: null, pinFallSounds: null, preload: function(entityID) { _this.entityID = entityID; }, collisionWithEntity: function(entityID, otherID, collisionInfo) { var properties = Entities.getEntityProperties(otherID, ['name']); var launchPosition = Entities.getEntityProperties(_this.entityID).position; var pinPosition = Entities.getEntityProperties(entityID).position; if (properties.name.lastIndexOf("Bowling ball -") === 0) { print(JSON.stringify(collisionInfo)); Audio.playSound(_this.pinFallSounds[Math.floor(_this.pinFallSounds.length * Math.random())], { position: collisionInfo.contactPoint, volume: 1 //TODO make it based on the ball speed }); var launchLocation = findEntities(pinPosition, { 'name': 'fwbase' }, 10); for(i=0; i