// // cylinderBlock.js // // Created by David Rowe on 25 Oct 2016. // Changes made by Thijs Wenker on 13 Feb 2017 // Copyright 2016 High Fidelity, Inc. // // Shoots blocks out of a cylinder // // 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 BLOCK_MODEL_URL = "http://hifi-content.s3.amazonaws.com/alan/dev/EZ-Block.fbx?5"; var BLOCK_COMPOUND_SHAPE_URL = "http://hifi-content.s3.amazonaws.com/alan/dev/EZ-Block.obj"; var BLOCK_DIMENSIONS = { x: 1.0072927474975586, y: 1.3508138656616211, z: 1.0093965530395508 }; var BLOCK_LIFETIME = 3600; // 1 hour. var MAX_BLOCKS_PER_HOUR = 250; // Note: Can't have many more than 50 because of entity editing MTU size limit. var MUZZLE_SOUND_URL = "http://hifi-content.s3.amazonaws.com/DomainContent/Welcome%20Area/Sounds/air_gun_1_converted.wav"; var MUZZLE_SOUND_VOLUME = 0.5; var MUZZLE_SPEED = 25.0; // m/s var MUZZLE_ANGLE = 5.0; // Degrees off-center to discourage blocks falling back inside cylinder. var muzzleSound; var cylinderID; this.preload = function (entityID) { cylinderID = entityID; muzzleSound = SoundCache.getSound(MUZZLE_SOUND_URL); } this.launchBlock = function () { // Limit block creation rate. var userData = JSON.parse(Entities.getEntityProperties(cylinderID, "userData").userData); var blocksCreated = userData.hasOwnProperty("blocksCreated") ? userData.blocksCreated : []; var SECONDS_PER_HOUR = 3600; var now = Math.round(Date.now() / 1000); // Seconds rather than milliseconds to reduce userData string length. var nowMinusOneHour = now - SECONDS_PER_HOUR; while (blocksCreated.length > 0 && blocksCreated[0] < nowMinusOneHour) { blocksCreated = blocksCreated.slice(1); } if (blocksCreated.length >= MAX_BLOCKS_PER_HOUR) { return; } // Create block. blocksCreated.push(now); userData.blocksCreated = blocksCreated; Entities.editEntity(cylinderID, { userData: JSON.stringify(userData) }); var cylinder = Entities.getEntityProperties(cylinderID, ["position", "rotation", "dimensions"]); var muzzlePosition = Vec3.sum(cylinder.position, Vec3.multiplyQbyV(cylinder.rotation, { x: 0.0, y: 0.5 * cylinder.dimensions.y, Z: 0.0 })); var muzzleVelocity = Vec3.multiply(MUZZLE_SPEED, Vec3.UNIT_Y); muzzleVelocity = Vec3.multiplyQbyV(Quat.fromPitchYawRollDegrees(MUZZLE_ANGLE, 0.0, 0.0), muzzleVelocity); muzzleVelocity = Vec3.multiplyQbyV(Quat.fromPitchYawRollDegrees(0.0, Math.random() * 360.0, 0.0), muzzleVelocity); muzzleVelocity = Vec3.multiplyQbyV(cylinder.rotation, muzzleVelocity); Entities.addEntity({ type: "Model", modelURL: BLOCK_MODEL_URL, shapeType: "compound", compoundShapeURL: BLOCK_COMPOUND_SHAPE_URL, dimensions: BLOCK_DIMENSIONS, dynamic: 1, gravity: { x: 0.0, y: -9.0, z: 0.0 }, collisionsWillMove: 1, position: cylinder.position, rotation: Quat.multiply(cylinder.rotation, Quat.fromPitchYawRollDegrees(0.0, Math.random() * 360.0, 0.0)), velocity: muzzleVelocity, lifetime: BLOCK_LIFETIME }); Audio.playSound(muzzleSound, { position: muzzlePosition, orientation: cylinder.rotation, volume: MUZZLE_SOUND_VOLUME }); } this.clickDownOnEntity = function () { this.launchBlock(); } this.startNearTrigger = function () { this.launchBlock(); } this.startFarTrigger = function () { this.launchBlock(); } })