content/hifi-content/Content/Scripted/RocketApp/testRocketScript.js
2022-02-13 22:31:32 +01:00

225 lines
No EOL
6.3 KiB
JavaScript

/*
Tablet Button Stuff
*/
var TABLET_BUTTON_NAME = "ROCKET";
var BUTTON_ACTIVATED = false;
var APP_URL = "https://s3-us-west-1.amazonaws.com/hifi-content/Content/Scripted/RocketApp/rocketApp.html?" + Date.now();
var ICONS = {
icon: "https://s3-us-west-1.amazonaws.com/hifi-content/Content/Scripted/RocketApp/rocket-a.svg",
activeIcon: "https://s3-us-west-1.amazonaws.com/hifi-content/Content/Scripted/RocketApp/rocket-i.svg"
};
function onTabletAppClicked() {
if(!BUTTON_ACTIVATED) {
BUTTON_ACTIVATED = true;
button.editProperties({isActive: true});
tablet.gotoWebScreen(APP_URL);
return;
}
BUTTON_ACTIVATED = false;
button.editProperties({isActive: false});
}
var tablet = Tablet.getTablet("com.highfidelity.interface.tablet.system");
var button = tablet.addButton({
icon: ICONS.icon,
activeIcon: ICONS.activeIcon,
text: TABLET_BUTTON_NAME,
// sortOrder: 1
});
button.clicked.connect(onTabletAppClicked);
/* End Tablet Button Stuff */
var ROCKET_POSITION_OFFSET_Y = 10;
var ROCKET_VELOCITY = 20;
var ROCKET_LIFETIME = 60; // 60 = 1 minute
var rocketModel = {
"type": "Model",
"position": {
"x": MyAvatar.position.x,
"y": MyAvatar.position.y + ROCKET_POSITION_OFFSET_Y,
"z": MyAvatar.position.z,
},
"dimensions": {
"x": 12.183699607849121,
"y": 7.080900192260742,
"z": 22.80660057067871,
},
"rotation": {
"x": 0.0025177001953125,
"y": 0.7070878744125366,
"z": 0.7070878744125366,
"w": 0.0034027099609375,
},
"userData": "{\"grabbableKey\":{\"grabbable\":false}}",
"modelURL": "https://s3-us-west-1.amazonaws.com/hifi-content/Content/Scripted/RocketApp/1393+Rocket+Ship.obj",
"shapeType": "simple-compound",
"dynamic": 1,
"collisionless": 1,
};
var rocketParticle = {
"type": "ParticleEffect",
"position": {
"x": MyAvatar.position.x,
"y": MyAvatar.position.y,
"z": MyAvatar.position.z,
},
"dimensions": {
"x": 121,
"y": 121,
"z": 121,
},
"rotation": {
"x": -0.0000152587890625,
"y": 0.9738765954971313,
"z": -0.0000152587890625,
"w": 0.22700846195220947,
},
"name": "RocketParticles",
"color": {
"red": 201,
"green": 141,
"blue": 28
},
"isEmitting": 1,
"maxParticles": 10000,
"lifespan": 10,
"emitRate": 700,
"emitSpeed": 0,
"emitOrientation": {
"x": -0.7071220278739929,
"y": -0.000015258869098033756,
"z": -0.000015258869098033756,
"w": 0.7070915699005127,
},
"emitRadiusStart": 1,
"emitAcceleration": {
"x": 0,
"y": -2.5,
"z": 0,
},
"accelerationSpread": {
"x": 0.5,
"y": 1,
"z": 0.5,
},
"particleRadius": 1.149999976158142,
"radiusSpread": 0,
"radiusStart": 0.7799999713897705,
"radiusFinish": 0.10000000149011612,
"colorStart": {
"red":200,
"green":200,
"blue":200,
},
"alpha": 0.12999999523162842,
"alphaSpread": 0,
"alphaStart": 1,
"alphaFinish": 0,
"emitterShouldTrail": 1,
"shapeType": "none",
"parentID": "{}",
"textures": "https://content.highfidelity.com/DomainContent/production/Particles/wispy-smoke.png",
};
var rocketEntities = [];
function loadUpRocket() {
var center = Vec3.sum(MyAvatar.position, Vec3.multiply(Quat.getFront(MyAvatar.orientation), 20));
center.y = MyAvatar.position.y + ROCKET_POSITION_OFFSET_Y;
rocketModel.position = center;
var rocketEntity = Entities.addEntity(rocketModel);
rocketParticle.parentID = rocketEntity;
center.y = MyAvatar.position.y;
rocketParticle.position = center;
var rocketParticles = Entities.addEntity(rocketParticle);
rocketEntities.push(rocketEntity);
rocketEntities.push(rocketParticles);
}
function cleanUpRockets() {
for (var i = 0; i < rocketEntities.length; i++) {
Entities.deleteEntity(rocketEntities[i]);
}
rocketEntities = [];
}
function cleanUpRocketScript() {
cleanUpRockets();
button.clicked.disconnect(onTabletAppClicked);
tablet.removeButton(button);
}
function launchRocket(lifetime, velocity) {
var theRocket = rocketEntities[0];
var rocketAccelerationValues = {
"gravity": {
"x": 0,
"y": velocity,
"z": 0,
},
"acceleration": {
"x": 0,
"y": velocity,
"z": 0,
},
"lifetime": lifetime,
};
print("Launching rocket with " + lifetime + " seconds lifetime, " + velocity + " acceleration into the sky.");
Entities.editEntity(theRocket, rocketAccelerationValues);
}
// loadUpRocket();
var shown = false;
function onScreenChanged(type, url) {
if (type === 'Web' && url === APP_URL) {
button.editProperties({ isActive: true });
if (!shown) {
tablet.webEventReceived.connect(onWebEventReceived);
}
BUTTON_ACTIVATED = true;
shown = true;
} else {
button.editProperties({ isActive: false });
if (shown) {
tablet.webEventReceived.disconnect(onWebEventReceived);
}
BUTTON_ACTIVATED = false;
shown = false;
}
}
tablet.screenChanged.connect(onScreenChanged);
function onWebEventReceived(event) {
var results = (JSON.parse(event));
if(results.data == "spawnRocket") {
if(rocketEntities[0]) {
Window.alert("Please cleanup any old rockets before spawning a new one.");
return;
}
cleanUpRockets();
loadUpRocket();
return;
}
if(results.data == "launchRocket") {
launchRocket(results.airTime, results.rocketSpeed);
var rocketSound = SoundCache.getSound("https://s3-us-west-1.amazonaws.com/hifi-content/Content/Scripted/RocketApp/rocket.wav");
Audio.playSound(rocketSound, "{\"volume\": \"0.3\", \"loop\": true}");
return;
}
if(results.data == "destroyRockets") {
cleanUpRockets();
return;
}
}
Script.scriptEnding.connect(cleanUpRocketScript);