Skip to content

Instantly share code, notes, and snippets.

@cozza13
Last active August 29, 2015 14:18
Show Gist options
  • Save cozza13/f48c5bbf2f7380eb3f4a to your computer and use it in GitHub Desktop.
Save cozza13/f48c5bbf2f7380eb3f4a to your computer and use it in GitHub Desktop.
//
// groupVehicle.js
// examples
//
// Created by Eric Levin on April 9, 2015
// Copyright 2015 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
//
Script.include("https://hifi-public.s3.amazonaws.com/scripts/libraries/displayMessage.js");
Script.include("https://hifi-public.s3.amazonaws.com/scripts/libraries/tween.js")
var isAssignmentScript = false;
var startingPosition;
//************************************************************************************************
// If you are running this as an assignment script, set the starting spawn point for the vehicle here
// Otherwise, leave this commented out and the vehicle will spawn in front of your avatar
isAssignmentScript = true
startingPosition = {
x: 1483.08,
y: 1108.32,
z: 2091.03
};
// //************************************************************************************************
if (!isAssignmentScript) {
startingPosition = MyAvatar.position;
MyAvatar.orientation = Quat.fromPitchYawRollDegrees(0, -90, 0);
new Message({
description: "Click on the ship to take a ride! \n Invite your friends to hop on as well!",
displayTime: 5000
});
}
var shipModelURL = "https://s3.amazonaws.com/hifi-public/ozan/Polyworld/Sets/ship/ship.fbx";
var seatManagerScriptURL = "https://hifi-public.s3.amazonaws.com/scripts/entityScripts/seatManager.js";
var shipSound = SoundCache.getSound("https://hifi-public.s3.amazonaws.com/eric/sounds/spaceship.wav");
var shipDimensions = {
x: 10.8,
y: 4.04,
z: 10.8
};
var waypoints = [];
var numPoints = 5;
var currentWaypointIndex = 0;
var ship;
var radius = 50;
//Modify these variables to change the pace of your vehicle tour
var pointToPointTime = 5000;
var waitTimeBetweenLoops = 20000;
var waitTimeBetweenStops = 5000;
function init() {
createWaypoints();
ship = Entities.addEntity({
type: "Model",
modelURL: shipModelURL,
position: waypoints[0],
dimensions: shipDimensions,
script: seatManagerScriptURL
});
currentWaypointIndex++;
Script.setTimeout(function() {
followWaypoints();
}, waitTimeBetweenLoops);
}
function createWaypoints() {
waypoints.push({
x: 1483.08,
y: 1108.32,
z: 2091.03
},{
x: 960.7,
y: 1081.19,
z: 1812.38
},
{
x: 2051.1,
y: 1086.87,
z: 1660.37
},{
x: 890.679,
y: 1033.48,
z: 896.706
},{
x: 980.052,
y: 1071.27,
z: 1039.82
},{
x: 966.398,
y: 1069.86,
z: 1106.63
});
}
function followWaypoints() {
startingPosition = Entities.getEntityProperties(ship).position;
var currentProps = {
x: startingPosition.x,
y: startingPosition.y,
z: startingPosition.z,
}
var endProps = {
x: waypoints[
currentWaypointIndex].x,
y: waypoints[currentWaypointIndex].y,
z: waypoints[currentWaypointIndex].z,
}
var moveTween = new TWEEN.Tween(currentProps).
to(endProps, pointToPointTime).
easing(TWEEN.Easing.Cubic.InOut).
onUpdate(function() {
Entities.editEntity(ship, {
position: {
x: currentProps.x,
y: currentProps.y,
z: currentProps.z
}
});
}).start();
Audio.playSound(shipSound, {
position: startingPosition
});
moveTween.onComplete(function() {
currentWaypointIndex++;
if (currentWaypointIndex === waypoints.length) {
currentWaypointIndex = 0;
//If we've finished loop, then wait specified time to start over again
Script.setTimeout(function() {
followWaypoints();
}, waitTimeBetweenLoops)
} else {
Script.setTimeout(function() {
followWaypoints();
}, waitTimeBetweenStops)
}
});
}
function update() {
TWEEN.update();
}
Script.setTimeout(function() {
init()
}, 500);
Script.update.connect(update);
Script.scriptEnding.connect(destroy);
function destroy() {
Entities.deleteEntity(ship);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment