Skip to content

Instantly share code, notes, and snippets.

@birarda
Last active December 3, 2016 00:28
Show Gist options
  • Save birarda/afb3df3e967f0697403b434d3a5e4d19 to your computer and use it in GitHub Desktop.
Save birarda/afb3df3e967f0697403b434d3a5e4d19 to your computer and use it in GitHub Desktop.
//
// cylinderBlock.js
//
// Created by David Rowe on 25 Oct 2016.
// Copyright 2015 High Fidelity, Inc.
//
// This script displays a progress download indicator when downloads are in progress.
//
// 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 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 muzzleSound;
var machineID;
this.preload = function (entityID) {
machineID = entityID;
muzzleSound = SoundCache.getSound(MUZZLE_SOUND_URL);
}
this.launchBall = function () {
var PITCHING_MACHINE_OUTPUT_OFFSET_PCT = {
x: 0.0,
y: 0.25,
z: -1.05,
};
function vec3Mult(a, b) {
return {
x: a.x * b.x,
y: a.y * b.y,
z: a.z * b.z,
};
}
function randomFloat(low, high) {
if (high === undefined) {
high = low;
low = 0;
}
return low + Math.random() * (high - low);
};
var machineProperties = Entities.getEntityProperties(machineID, ["dimensions", "position", "rotation"]);
var pitchFromPositionBase = machineProperties.position;
var pitchFromOffset = vec3Mult(machineProperties.dimensions, PITCHING_MACHINE_OUTPUT_OFFSET_PCT);
pitchFromOffset = Vec3.multiplyQbyV(machineProperties.rotation, pitchFromOffset);
var pitchFromPosition = Vec3.sum(pitchFromPositionBase, pitchFromOffset);
var pitchDirection = Quat.getFront(machineProperties.rotation);
var ballScale = machineProperties.dimensions.x;
var velocity = Vec3.multiply(7.0, pitchDirection);
var BALL_PROPERTIES = {
type: "Model",
shapeType: "sphere",
modelURL: "http://mpassets.highfidelity.com/a250c5bc-1ee1-4c34-a1d0-7a6acbcaa989-v1/ball.fbx",
dynamic: true,
dimensions: {
"x": 0.12999999523162842,
"y": 0.12999999523162842,
"z": 0.12999999523162842
},
gravity: {
"x": 0,
"y": -5.0,
"z": 0
},
density: 100,
restitution: 0.9900000095367432,
lifetime: 5
}
function shallowCopy(obj) {
var copy = {}
for (var key in obj) {
copy[key] = obj[key];
}
return copy;
}
var thisBallProperties = shallowCopy(BALL_PROPERTIES);
thisBallProperties.position = pitchFromPosition;
thisBallProperties.velocity = velocity;
Entities.addEntity(thisBallProperties);
}
var launchTimer;
this.toggleLaunching = function () {
if (!launchTimer) {
this.launchBall();
launchTimer = Script.setInterval(this.launchBall, 1000);
} else {
Script.clearInterval(launchTimer);
launchTimer = null;
}
}
this.clickDownOnEntity = function () {
this.toggleLaunching();
}
this.startNearTrigger = function () {
this.toggleLaunching();
}
this.startFarTrigger = function () {
this.toggleLaunching();
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment