overte/script-archive/flowArts/arcBall/arcBallEntityScript.js

155 lines
5.1 KiB
JavaScript
Raw Permalink Normal View History

2015-12-17 13:31:27 -08:00
// arcBallEntityScript.js
//
// Script Type: Entity
// Created by Eric Levin on 12/17/15.
// Copyright 2015 High Fidelity, Inc.
//
// This entity script handles the logic for the arcBall rave toy
// Distributed under the Apache License, Version 2.0.
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
//
(function() {
Script.include("../../libraries/utils.js");
var _this;
var ArcBall = function() {
_this = this;
2015-12-21 11:00:10 -08:00
this.colorPalette = [{
red: 25,
green: 20,
blue: 162
}, {
red: 200,
green: 10,
blue: 10
}];
2015-12-21 15:20:36 -08:00
this.searchRadius = 10;
2015-12-17 13:31:27 -08:00
};
ArcBall.prototype = {
isGrabbed: false,
2015-12-21 13:59:00 -08:00
startDistantGrab: function() {
this.searchForNearbyArcBalls();
},
2015-12-21 13:59:00 -08:00
startNearGrab: function() {
this.searchForNearbyArcBalls();
},
searchForNearbyArcBalls: function() {
2015-12-17 13:31:27 -08:00
//Search for nearby balls and create an arc to it if one is found
var position = Entities.getEntityProperties(this.entityID, "position").position
2015-12-21 15:20:36 -08:00
var entities = Entities.findEntities(position, this.searchRadius);
2015-12-17 13:31:27 -08:00
entities.forEach(function(entity) {
var props = Entities.getEntityProperties(entity, ["position", "name"]);
if (props.name === "Arc Ball" && JSON.stringify(_this.entityID) !== JSON.stringify(entity)) {
2015-12-21 10:43:59 -08:00
_this.target = entity;
2015-12-17 15:02:25 -08:00
_this.createBeam(position, props.position);
2015-12-17 13:31:27 -08:00
}
2015-12-17 15:02:25 -08:00
});
2015-12-17 13:31:27 -08:00
},
2015-12-17 15:02:25 -08:00
createBeam: function(startPosition, endPosition) {
2015-12-21 10:43:59 -08:00
// Creates particle arc from start position to end position
2015-12-18 15:38:01 -08:00
var rotation = Entities.getEntityProperties(this.entityID, "rotation").rotation;
2015-12-17 15:02:25 -08:00
var sourceToTargetVec = Vec3.subtract(endPosition, startPosition);
var emitOrientation = Quat.rotationBetween(Vec3.UNIT_Z, sourceToTargetVec);
2015-12-18 15:38:01 -08:00
emitOrientation = Quat.multiply(Quat.inverse(rotation), emitOrientation);
2015-12-17 15:02:25 -08:00
2015-12-21 11:00:10 -08:00
var color = this.colorPalette[randInt(0, this.colorPalette.length)];
var props = {
type: "ParticleEffect",
name: "Particle Arc",
parentID: this.entityID,
parentJointIndex: -1,
// position: startPosition,
isEmitting: true,
colorStart: color,
color: {
red: 200,
green: 200,
blue: 255
},
colorFinish: color,
maxParticles: 100000,
2015-12-21 13:59:00 -08:00
lifespan: 1,
emitRate: 1000,
emitOrientation: emitOrientation,
2015-12-21 13:59:00 -08:00
emitSpeed: 1,
speedSpread: 0.02,
emitDimensions: {
x: .01,
y: .01,
z: .01
},
polarStart: 0,
2015-12-21 15:20:36 -08:00
polarFinish: 0,
azimuthStart: 0.02,
azimuthFinish: .01,
emitAcceleration: {
x: 0,
y: 0,
z: 0
},
accelerationSpread: {
2015-12-21 15:20:36 -08:00
x: 0,
y: 0,
z: 0
},
radiusStart: 0.01,
radiusFinish: 0.005,
2015-12-21 15:20:36 -08:00
radiusSpread: 0.005,
alpha: 0.5,
2015-12-21 15:20:36 -08:00
alphaSpread: 0.1,
alphaStart: 0.5,
2015-12-21 13:59:00 -08:00
alphaFinish: 0.5,
textures: "https://s3.amazonaws.com/hifi-public/eric/textures/particleSprites/beamParticle.png",
emitterShouldTrail: true
}
2015-12-17 15:02:25 -08:00
this.particleArc = Entities.addEntity(props);
2015-12-17 13:31:27 -08:00
},
updateBeam: function() {
if(!this.target) {
return;
}
var startPosition = Entities.getEntityProperties(this.entityID, "position").position;
2015-12-21 10:43:59 -08:00
var targetPosition = Entities.getEntityProperties(this.target, "position").position;
var rotation = Entities.getEntityProperties(this.entityID, "rotation").rotation;
var sourceToTargetVec = Vec3.subtract(targetPosition, startPosition);
var emitOrientation = Quat.rotationBetween(Vec3.UNIT_Z, sourceToTargetVec);
Entities.editEntity(this.particleArc, {
emitOrientation: emitOrientation
});
},
2015-12-17 15:02:25 -08:00
2015-12-21 10:43:59 -08:00
continueNearGrab: function() {
this.updateBeam();
},
2015-12-21 13:59:00 -08:00
continueDistantGrab: function() {
this.updateBeam();
2015-12-21 10:43:59 -08:00
},
releaseGrab: function() {
Entities.editEntity(this.particleArc, {
isEmitting: false
});
this.target = null;
2015-12-21 10:43:59 -08:00
},
2015-12-17 15:02:25 -08:00
unload: function() {
2015-12-21 10:43:59 -08:00
if (this.particleArc) {
Entities.deleteEntity(this.particleArc);
}
2015-12-17 13:31:27 -08:00
},
preload: function(entityID) {
this.entityID = entityID;
},
};
return new ArcBall();
2015-12-17 15:02:25 -08:00
});