store action lifetime as an expires integer

This commit is contained in:
Seth Alves 2015-09-30 13:46:23 -07:00
parent 06f7f92951
commit b34ea6ded4
3 changed files with 44 additions and 15 deletions

View file

@ -15,6 +15,33 @@ var ZERO_VEC3 = {x: 0, y: 0, z: 0};
var IDENTITY_QUAT = {x: 0, y: 0, z: 0, w: 0};
if (typeof String.prototype.startsWith != 'function') {
// see below for better implementation!
String.prototype.startsWith = function (str){
return this.indexOf(str) === 0;
};
}
function getTag() {
return "grab-" + MyAvatar.sessionUUID;
}
function entityIsGrabbedByOther(entityID) {
var actionIDs = Entities.getActionIDs(entityID);
for (var actionIndex = 0; actionIndex < actionIDs.length; actionIndex++) {
var actionID = actionIDs[actionIndex];
var actionArguments = Entities.getActionArguments(entityID, actionID);
var tag = actionArguments["tag"];
if (tag == getTag()) {
continue;
}
if (tag.startsWith("grab-")) {
return true;
}
}
return false;
}
// helper function
function mouseIntersectionWithPlane(pointOnPlane, planeNormal, event, maxDistance) {
var cameraPosition = Camera.getPosition();
@ -288,7 +315,7 @@ Grabber.prototype.moveEvent = function(event) {
}
this.currentPosition = entityProperties.position;
var actionArgs = {tag: "grab", lifetime: 5};
var actionArgs = {tag: getTag(), lifetime: 5};
if (this.mode === "rotate") {
var drag = mouse.getDrag();
@ -303,7 +330,7 @@ Grabber.prototype.moveEvent = function(event) {
// var qZero = entityProperties.rotation;
//var qZero = this.lastRotation;
this.lastRotation = Quat.multiply(deltaQ, this.lastRotation);
actionArgs = {targetRotation: this.lastRotation, angularTimeScale: 0.1, tag: "grab", lifetime: 5};
actionArgs = {targetRotation: this.lastRotation, angularTimeScale: 0.1, tag: getTag(), lifetime: 5};
} else {
var newPointOnPlane;
if (this.mode === "verticalCylinder") {
@ -327,13 +354,15 @@ Grabber.prototype.moveEvent = function(event) {
}
}
this.targetPosition = Vec3.subtract(newPointOnPlane, this.offset);
actionArgs = {targetPosition: this.targetPosition, linearTimeScale: 0.1, tag: "grab", lifetime: 5};
actionArgs = {targetPosition: this.targetPosition, linearTimeScale: 0.1, tag: getTag(), lifetime: 5};
beacon.updatePosition(this.targetPosition);
}
if (!this.actionID) {
this.actionID = Entities.addAction("spring", this.entityID, actionArgs);
if (!entityIsGrabbedByOther(this.entityID)) {
this.actionID = Entities.addAction("spring", this.entityID, actionArgs);
}
} else {
Entities.updateAction(this.entityID, this.actionID, actionArgs);
}

View file

@ -31,8 +31,8 @@ void ObjectAction::updateAction(btCollisionWorld* collisionWorld, btScalar delta
return;
}
if (_expires > 0.0f) {
float now = (float)usecTimestampNow() / USECS_PER_SECOND;
if (_expires > 0) {
quint64 now = usecTimestampNow();
if (now > _expires) {
EntityItemPointer ownerEntity = _ownerEntity.lock();
_active = false;
@ -53,10 +53,10 @@ bool ObjectAction::updateArguments(QVariantMap arguments) {
bool lifetimeSet = true;
float lifetime = EntityActionInterface::extractFloatArgument("action", arguments, "lifetime", lifetimeSet, false);
if (lifetimeSet) {
float now = (float)usecTimestampNow() / USECS_PER_SECOND;
_expires = now + lifetime;
quint64 now = usecTimestampNow();
_expires = now + (quint64)(lifetime * USECS_PER_SECOND);
} else {
_expires = 0.0f;
_expires = 0;
}
bool tagSet = true;
@ -72,11 +72,11 @@ bool ObjectAction::updateArguments(QVariantMap arguments) {
QVariantMap ObjectAction::getArguments() {
QVariantMap arguments;
float now = (float)usecTimestampNow() / USECS_PER_SECOND;
if (_expires == 0.0f) {
if (_expires == 0) {
arguments["lifetime"] = 0.0f;
} else {
arguments["lifetime"] = _expires - now;
quint64 now = usecTimestampNow();
arguments["lifetime"] = (float)(_expires - now) / (float)USECS_PER_SECOND;
}
arguments["tag"] = _tag;
return arguments;
@ -183,11 +183,11 @@ void ObjectAction::activateBody() {
}
bool ObjectAction::lifetimeIsOver() {
if (_expires == 0.0f) {
if (_expires == 0) {
return false;
}
float now = (float)usecTimestampNow() / USECS_PER_SECOND;
quint64 now = usecTimestampNow();
if (now >= _expires) {
return true;
}

View file

@ -64,7 +64,7 @@ protected:
bool _active;
EntityItemWeakPointer _ownerEntity;
float _expires; // in seconds since epoch
quint64 _expires; // in seconds since epoch
QString _tag;
};