mirror of
https://github.com/overte-org/overte.git
synced 2025-08-09 22:28:37 +02:00
removed extra actions, creating all entities in create script.
This commit is contained in:
parent
5ce70decfc
commit
2257af42be
2 changed files with 75 additions and 156 deletions
|
@ -21,6 +21,9 @@ var BALL_DAMPING = 0.5;
|
||||||
var BALL_ANGULAR_DAMPING = 0.5;
|
var BALL_ANGULAR_DAMPING = 0.5;
|
||||||
var BALL_RESTITUTION = 0.4;
|
var BALL_RESTITUTION = 0.4;
|
||||||
var BALL_DENSITY = 1000;
|
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_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 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";
|
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 = {
|
var STICK_PROPERTIES = {
|
||||||
type: 'Model',
|
type: 'Model',
|
||||||
name: "TetherballStick Stick",
|
name: "TetherballStick Stick",
|
||||||
|
@ -115,7 +142,10 @@ var STICK_PROPERTIES = {
|
||||||
},
|
},
|
||||||
ownerID: MyAvatar.sessionUUID,
|
ownerID: MyAvatar.sessionUUID,
|
||||||
ballID: ballID,
|
ballID: ballID,
|
||||||
lifetime: LIFETIME
|
lineID: lineID,
|
||||||
|
actionID: actionID,
|
||||||
|
lifetime: LIFETIME,
|
||||||
|
maxDistanceBetweenBallAndStick: ACTION_DISTANCE * MAX_DISTANCE_MULTIPLIER
|
||||||
})
|
})
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -17,29 +17,21 @@
|
||||||
(function() {
|
(function() {
|
||||||
var _this;
|
var _this;
|
||||||
|
|
||||||
var NULL_UUID = "{00000000-0000-0000-0000-000000000000}";
|
|
||||||
var LINE_WIDTH = 0.02;
|
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 COLLISION_SOUND_URL = "http://public.highfidelity.io/sounds/Footsteps/FootstepW3Left-12db.wav";
|
||||||
var TIP_OFFSET = 0.26;
|
var TIP_OFFSET = 0.26;
|
||||||
var LIFETIME = 3600;
|
|
||||||
|
|
||||||
tetherballStick = function() {
|
tetherballStick = function() {
|
||||||
_this = this;
|
_this = this;
|
||||||
};
|
};
|
||||||
|
|
||||||
tetherballStick.prototype = {
|
tetherballStick.prototype = {
|
||||||
entityID: NULL_UUID,
|
|
||||||
ballID: NULL_UUID,
|
|
||||||
lineID: NULL_UUID,
|
|
||||||
|
|
||||||
getUserData: function(key) {
|
getUserData: function() {
|
||||||
try {
|
try {
|
||||||
var stickProps = Entities.getEntityProperties(this.entityID);
|
var stickProps = Entities.getEntityProperties(this.entityID);
|
||||||
var userData = JSON.parse(stickProps.userData);
|
var userData = JSON.parse(stickProps.userData);
|
||||||
return userData[key];
|
return userData;
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
print("Error parsing Tetherball Stick UserData in file " +
|
print("Error parsing Tetherball Stick UserData in file " +
|
||||||
e.fileName + " on line " + e.lineNumber);
|
e.fileName + " on line " + e.lineNumber);
|
||||||
|
@ -48,51 +40,36 @@
|
||||||
|
|
||||||
preload: function(entityID) {
|
preload: function(entityID) {
|
||||||
this.entityID = 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) {
|
update: function(dt) {
|
||||||
_this.drawLine();
|
_this.drawLine();
|
||||||
},
|
},
|
||||||
|
|
||||||
startEquip: function(id, params) {
|
startEquip: function() {
|
||||||
this.removeActions();
|
Script.update.disconnect(this.update);
|
||||||
this.createLine();
|
|
||||||
|
|
||||||
Script.update.connect(this.update);
|
|
||||||
},
|
},
|
||||||
|
|
||||||
continueEquip: function(id, params) {
|
continueEquip: function(id, params) {
|
||||||
var stickProps = Entities.getEntityProperties(this.entityID);
|
this.updateOffsetAction();
|
||||||
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.capBallDistance();
|
this.capBallDistance();
|
||||||
|
this.drawLine();
|
||||||
},
|
},
|
||||||
|
|
||||||
releaseEquip: function(id, params) {
|
releaseEquip: function() {
|
||||||
this.deleteLine();
|
Script.update.connect(this.update);
|
||||||
this.createSpringAction();
|
|
||||||
|
|
||||||
Script.update.disconnect(this.update);
|
|
||||||
},
|
},
|
||||||
|
|
||||||
getTipPosition: function() {
|
getTipPosition: function() {
|
||||||
|
@ -104,13 +81,10 @@
|
||||||
return tipPosition;
|
return tipPosition;
|
||||||
},
|
},
|
||||||
|
|
||||||
getStickFrontPosition: function() {
|
updateOffsetAction: function() {
|
||||||
var stickProps = Entities.getEntityProperties(this.entityID);
|
Entities.updateAction(this.ballID, this.actionID, {
|
||||||
var stickFront = Quat.getFront(stickProps.rotation);
|
pointToOffsetFrom: this.getTipPosition()
|
||||||
var tipPosition = this.getTipPosition();
|
});
|
||||||
var frontPostion = Vec3.sum(tipPosition, Vec3.multiply(TIP_OFFSET * 0.4, stickFront));
|
|
||||||
|
|
||||||
return frontPostion;
|
|
||||||
},
|
},
|
||||||
|
|
||||||
capBallDistance: function() {
|
capBallDistance: function() {
|
||||||
|
@ -118,7 +92,7 @@
|
||||||
var ballProps = Entities.getEntityProperties(this.ballID);
|
var ballProps = Entities.getEntityProperties(this.ballID);
|
||||||
var tipPosition = this.getTipPosition();
|
var tipPosition = this.getTipPosition();
|
||||||
var distance = Vec3.distance(tipPosition, ballProps.position);
|
var distance = Vec3.distance(tipPosition, ballProps.position);
|
||||||
var maxDistance = ACTION_DISTANCE * MAX_DISTANCE_MULTIPLIER;
|
var maxDistance = this.maxDistanceBetweenBallAndStick;
|
||||||
|
|
||||||
if(distance > maxDistance) {
|
if(distance > maxDistance) {
|
||||||
var direction = Vec3.normalize(Vec3.subtract(ballProps.position, tipPosition));
|
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() {
|
drawLine: function() {
|
||||||
if(this.hasLine()) {
|
var stickProps = Entities.getEntityProperties(this.entityID);
|
||||||
var stickProps = Entities.getEntityProperties(this.entityID);
|
var tipPosition = this.getTipPosition();
|
||||||
var tipPosition = this.getTipPosition();
|
var ballProps = Entities.getEntityProperties(this.ballID);
|
||||||
var ballProps = Entities.getEntityProperties(this.ballID);
|
var cameraQuat = Vec3.multiplyQbyV(Camera.getOrientation(), Vec3.UNIT_NEG_Z);
|
||||||
var cameraQuat = Vec3.multiplyQbyV(Camera.getOrientation(), Vec3.UNIT_NEG_Z);
|
var linePoints = [];
|
||||||
var linePoints = [];
|
var normals = [];
|
||||||
var normals = [];
|
var strokeWidths = [];
|
||||||
var strokeWidths = [];
|
linePoints.push(Vec3.ZERO);
|
||||||
linePoints.push(Vec3.ZERO);
|
normals.push(cameraQuat);
|
||||||
normals.push(cameraQuat);
|
strokeWidths.push(LINE_WIDTH);
|
||||||
strokeWidths.push(LINE_WIDTH);
|
linePoints.push(Vec3.subtract(ballProps.position, tipPosition));
|
||||||
linePoints.push(Vec3.subtract(ballProps.position, tipPosition));
|
normals.push(cameraQuat);
|
||||||
normals.push(cameraQuat);
|
strokeWidths.push(LINE_WIDTH);
|
||||||
strokeWidths.push(LINE_WIDTH);
|
|
||||||
|
|
||||||
var lineProps = Entities.getEntityProperties(this.lineID);
|
var lineProps = Entities.getEntityProperties(this.lineID);
|
||||||
Entities.editEntity(this.lineID, {
|
Entities.editEntity(this.lineID, {
|
||||||
linePoints: linePoints,
|
linePoints: linePoints,
|
||||||
normals: normals,
|
normals: normals,
|
||||||
strokeWidths: strokeWidths,
|
strokeWidths: strokeWidths,
|
||||||
position: tipPosition,
|
position: tipPosition,
|
||||||
});
|
|
||||||
}
|
|
||||||
},
|
|
||||||
|
|
||||||
createOffsetAction: function() {
|
|
||||||
this.removeActions();
|
|
||||||
|
|
||||||
Entities.addAction("offset", this.ballID, {
|
|
||||||
pointToOffsetFrom: this.getTipPosition(),
|
|
||||||
linearDistance: ACTION_DISTANCE,
|
|
||||||
linearTimeScale: ACTION_TIMESCALE
|
|
||||||
});
|
});
|
||||||
},
|
|
||||||
|
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue