content/hifi-content/caitlyn/production/minigolf course/GolfBallLauncher_14.js
2022-02-13 22:19:19 +01:00

95 lines
No EOL
3.3 KiB
JavaScript

/*
Created by Timo Kandra September, 2016
Modified by Piper.Peppercorn October 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.
The box is set to be invisible and is on the small brown mat under the windmill sails.
Select the invisible box in edit mode and copy it's ID.
Use it to make a JSON object, it should look like this example. {detectionBox: "0d1dc288-892b-4e6f-818f-4abcbb3ab38e"}
Paste it into the userData of the windmill sails. ( windmillWheel.fbx )
In code view mode it will look like this...
{
"detectionBox": "0d1dc288-892b-4e6f-818f-4abcbb3ab38e"
}
Note the quotes in Code view.
*/
(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 BALL_HIT_VOLUME = 1.0;
const ONE_SECOND = 1000; // In milliseconds
const BALL_LAUNCH_VELOCITY = {velocity:{x: 0.0, y: 9.43, z: -6.5}};
const TAU = 2.0 * Math.PI;
const SAIL_COUNT = 4;
const WINDMILL_ROTATION = {
angularVelocity: {x: TAU / 3.0, y: 0.0, z: 0.0},
angularDamping: 0.0,
rotation:{x: 0.0, y: 0.0, z: 0.0, w: 1.0}
};
var detectionBoxID;
var boxData;
var boxSize;
var pos;
var boxCorner;
var hitBall = function () {
if (detectionBoxID == undefined) {
setupDetectionBox(id);
};
var entitiesInBox = Entities.findEntitiesInBox (boxCorner, boxSize);
for (var i in entitiesInBox) {
var entity = Entities.getEntityProperties (entitiesInBox[i]);
if (entity.type == "Sphere" && entity.dynamic == true) {
// Launch the ball
Entities.editEntity (entity.id, BALL_LAUNCH_VELOCITY);
playBallHitSoundAtBallPosition (entity.position);
}
}
};
var setupDetectionBox = function (objectID) {
var windmillUserData = JSON.parse (Entities.getEntityProperties (objectID).userData);
if (windmillUserData.detectionBox != undefined) {
detectionBoxID = windmillUserData.detectionBox;
} else {
detectionBoxID = "0d1dc288-892b-4e6f-818f-4abcbb3ab38e";
}
boxData = Entities.getEntityProperties (detectionBoxID);
boxSize = boxData.dimensions;
pos = boxData.position;
boxCorner = {
x: (pos.x - boxSize.x / 2.0),
y: (pos.y - boxSize.y / 2.0),
z: (pos.z - boxSize.z / 2.0)
};
};
var playBallHitSoundAtBallPosition = function (ballPosition) {
var ballHitSoundProps = {
volume: BALL_HIT_VOLUME,
position: ballPosition
};
Audio.playSound (_this.BALL_HIT_SOUND, ballHitSoundProps);
};
_this.preload = function (uuID){
_this.BALL_HIT_SOUND = SoundCache.getSound (BALL_HIT_SOUND_URL);
id = uuID;
Entities.editEntity (id, WINDMILL_ROTATION);
timer = Script.setInterval (hitBall, ONE_SECOND * (TAU / WINDMILL_ROTATION.angularVelocity.x) / SAIL_COUNT);
};
})
// https://dl.dropboxusercontent.com/u/96759331/WindmillBallLauncher_013.js