removed extra actions, creating all entities in create script.

This commit is contained in:
Rob Kayson 2017-04-12 17:07:59 -07:00
parent 5ce70decfc
commit 2257af42be
2 changed files with 75 additions and 156 deletions

View file

@ -21,6 +21,9 @@ var BALL_DAMPING = 0.5;
var BALL_ANGULAR_DAMPING = 0.5;
var BALL_RESTITUTION = 0.4;
var BALL_DENSITY = 1000;
var ACTION_DISTANCE = 0.35;
var ACTION_TIMESCALE = 0.035;
var MAX_DISTANCE_MULTIPLIER = 4;
var STICK_SCRIPT_URL = Script.resolvePath("./entity_scripts/tetherballStick.js?v=" + Date.now());
var STICK_MODEL_URL = "http://hifi-content.s3.amazonaws.com/caitlyn/production/raveStick/newRaveStick2.fbx";
var COLLISION_SOUND_URL = "http://public.highfidelity.io/sounds/Footsteps/FootstepW3Left-12db.wav";
@ -64,6 +67,30 @@ var ballID = Entities.addEntity({
})
});
var lineID = Entities.addEntity({
type: "PolyLine",
name: "TetherballStick Line",
color: {
red: 0,
green: 120,
blue: 250
},
textures: "https://hifi-public.s3.amazonaws.com/alan/Particles/Particle-Sprite-Smoke-1.png",
position: ballStartPosition,
dimensions: {
x: 10,
y: 10,
z: 10
},
lifetime: LIFETIME
});
var actionID = Entities.addAction("offset", ballID, {
pointToOffsetFrom: stickStartPosition,
linearDistance: ACTION_DISTANCE,
linearTimeScale: ACTION_TIMESCALE
});
var STICK_PROPERTIES = {
type: 'Model',
name: "TetherballStick Stick",
@ -115,7 +142,10 @@ var STICK_PROPERTIES = {
},
ownerID: MyAvatar.sessionUUID,
ballID: ballID,
lifetime: LIFETIME
lineID: lineID,
actionID: actionID,
lifetime: LIFETIME,
maxDistanceBetweenBallAndStick: ACTION_DISTANCE * MAX_DISTANCE_MULTIPLIER
})
};

View file

@ -17,29 +17,21 @@
(function() {
var _this;
var NULL_UUID = "{00000000-0000-0000-0000-000000000000}";
var LINE_WIDTH = 0.02;
var MAX_DISTANCE_MULTIPLIER = 4;
var ACTION_DISTANCE = 0.35;
var ACTION_TIMESCALE = 0.035;
var COLLISION_SOUND_URL = "http://public.highfidelity.io/sounds/Footsteps/FootstepW3Left-12db.wav";
var TIP_OFFSET = 0.26;
var LIFETIME = 3600;
tetherballStick = function() {
_this = this;
};
tetherballStick.prototype = {
entityID: NULL_UUID,
ballID: NULL_UUID,
lineID: NULL_UUID,
getUserData: function(key) {
getUserData: function() {
try {
var stickProps = Entities.getEntityProperties(this.entityID);
var userData = JSON.parse(stickProps.userData);
return userData[key];
return userData;
} catch (e) {
print("Error parsing Tetherball Stick UserData in file " +
e.fileName + " on line " + e.lineNumber);
@ -48,51 +40,36 @@
preload: function(entityID) {
this.entityID = entityID;
this.ballID = this.getUserData("ballID");
var userData = this.getUserData();
this.ballID = userData.ballID;
this.lineID = userData.lineID;
this.actionID = userData.actionID;
this.maxDistanceBetweenBallAndStick = userData.maxDistanceBetweenBallAndStick;
// Script.update.connect(this.update);
},
unload: function() {
// Script.update.disconnect(this.update);
},
update: function(dt) {
_this.drawLine();
},
startEquip: function(id, params) {
this.removeActions();
this.createLine();
Script.update.connect(this.update);
startEquip: function() {
Script.update.disconnect(this.update);
},
continueEquip: function(id, params) {
var stickProps = Entities.getEntityProperties(this.entityID);
var ballProps = Entities.getEntityProperties(this.ballID);
var tipPosition = this.getTipPosition();
var distance = Vec3.distance(tipPosition, ballProps.position);
var maxDistance = ACTION_DISTANCE;
var dVel = Vec3.subtract(ballProps.velocity, stickProps.velocity);
var dPos = Vec3.subtract(ballProps.position, stickProps.position);
var ballWithinMaxDistance = distance <= maxDistance;
var ballMovingCloserToStick = Vec3.dot(dVel, dPos) < 0;
var ballAboveStick = ballProps.position.y > tipPosition.y;
if(this.hasAction()) {
if(ballWithinMaxDistance && (ballMovingCloserToStick || ballAboveStick)) {
this.removeActions();
} else {
this.updateOffsetAction();
}
} else if(!ballWithinMaxDistance && !ballMovingCloserToStick){
this.createOffsetAction();
}
this.updateOffsetAction();
this.capBallDistance();
this.drawLine();
},
releaseEquip: function(id, params) {
this.deleteLine();
this.createSpringAction();
Script.update.disconnect(this.update);
releaseEquip: function() {
Script.update.connect(this.update);
},
getTipPosition: function() {
@ -104,13 +81,10 @@
return tipPosition;
},
getStickFrontPosition: function() {
var stickProps = Entities.getEntityProperties(this.entityID);
var stickFront = Quat.getFront(stickProps.rotation);
var tipPosition = this.getTipPosition();
var frontPostion = Vec3.sum(tipPosition, Vec3.multiply(TIP_OFFSET * 0.4, stickFront));
return frontPostion;
updateOffsetAction: function() {
Entities.updateAction(this.ballID, this.actionID, {
pointToOffsetFrom: this.getTipPosition()
});
},
capBallDistance: function() {
@ -118,7 +92,7 @@
var ballProps = Entities.getEntityProperties(this.ballID);
var tipPosition = this.getTipPosition();
var distance = Vec3.distance(tipPosition, ballProps.position);
var maxDistance = ACTION_DISTANCE * MAX_DISTANCE_MULTIPLIER;
var maxDistance = this.maxDistanceBetweenBallAndStick;
if(distance > maxDistance) {
var direction = Vec3.normalize(Vec3.subtract(ballProps.position, tipPosition));
@ -129,113 +103,28 @@
}
},
createLine: function() {
if(!this.hasLine()) {
this.lineID = Entities.addEntity({
type: "PolyLine",
name: "TetherballStick Line",
color: {
red: 0,
green: 120,
blue: 250
},
textures: "https://hifi-public.s3.amazonaws.com/alan/Particles/Particle-Sprite-Smoke-1.png",
position: this.getTipPosition(),
dimensions: {
x: 10,
y: 10,
z: 10
},
lifetime: this.getUserData("lifetime")
});
}
},
deleteLine: function() {
Entities.deleteEntity(this.lineID);
this.lineID = NULL_UUID;
},
hasLine: function() {
return this.lineID != NULL_UUID;
},
drawLine: function() {
if(this.hasLine()) {
var stickProps = Entities.getEntityProperties(this.entityID);
var tipPosition = this.getTipPosition();
var ballProps = Entities.getEntityProperties(this.ballID);
var cameraQuat = Vec3.multiplyQbyV(Camera.getOrientation(), Vec3.UNIT_NEG_Z);
var linePoints = [];
var normals = [];
var strokeWidths = [];
linePoints.push(Vec3.ZERO);
normals.push(cameraQuat);
strokeWidths.push(LINE_WIDTH);
linePoints.push(Vec3.subtract(ballProps.position, tipPosition));
normals.push(cameraQuat);
strokeWidths.push(LINE_WIDTH);
var stickProps = Entities.getEntityProperties(this.entityID);
var tipPosition = this.getTipPosition();
var ballProps = Entities.getEntityProperties(this.ballID);
var cameraQuat = Vec3.multiplyQbyV(Camera.getOrientation(), Vec3.UNIT_NEG_Z);
var linePoints = [];
var normals = [];
var strokeWidths = [];
linePoints.push(Vec3.ZERO);
normals.push(cameraQuat);
strokeWidths.push(LINE_WIDTH);
linePoints.push(Vec3.subtract(ballProps.position, tipPosition));
normals.push(cameraQuat);
strokeWidths.push(LINE_WIDTH);
var lineProps = Entities.getEntityProperties(this.lineID);
Entities.editEntity(this.lineID, {
linePoints: linePoints,
normals: normals,
strokeWidths: strokeWidths,
position: tipPosition,
});
}
},
createOffsetAction: function() {
this.removeActions();
Entities.addAction("offset", this.ballID, {
pointToOffsetFrom: this.getTipPosition(),
linearDistance: ACTION_DISTANCE,
linearTimeScale: ACTION_TIMESCALE
var lineProps = Entities.getEntityProperties(this.lineID);
Entities.editEntity(this.lineID, {
linePoints: linePoints,
normals: normals,
strokeWidths: strokeWidths,
position: tipPosition,
});
},
createSpringAction: function() {
this.removeActions();
Entities.addAction("spring", this.ballID, {
targetPosition: this.getTipPosition(),
linearTimeScale: ACTION_TIMESCALE
});
},
updateOffsetAction: function() {
var actionIDs = Entities.getActionIDs(this.ballID);
var actionID;
// Sometimes two offset actions are applied to the ball simultaneously.
// Here we ensure that only the most recent action is updated
// and the rest are deleted.
while(actionIDs.length > 1) {
actionID = actionIDs.shift();
Entities.deleteAction(this.ballID, actionID);
}
actionID = actionIDs.shift();
if(actionID) {
Entities.updateAction(this.ballID, actionID, {
pointToOffsetFrom: this.getTipPosition()
});
}
},
removeActions: function() {
// Ball should only ever have one action, but sometimes sneaky little actions attach themselves
// So we remove all possible actions in this call.
var actionIDs = Entities.getActionIDs(this.ballID);
for(var i = 0; i < actionIDs.length; i++) {
Entities.deleteAction(this.ballID, actionIDs[i]);
}
},
hasAction: function() {
return Entities.getActionIDs(this.ballID).length > 0;
}
};