mirror of
https://github.com/JulianGro/overte.git
synced 2025-04-30 13:43:01 +02:00
removed _lastedEdited changed to editedAgo
This commit is contained in:
parent
c68a226094
commit
920ef65ccb
5 changed files with 49 additions and 41 deletions
|
@ -42,7 +42,7 @@ void Particle::init(glm::vec3 position, float radius, rgbColor color, glm::vec3
|
|||
_id = id;
|
||||
}
|
||||
uint64_t now = usecTimestampNow();
|
||||
_lastEdited = now;
|
||||
_edited = now;
|
||||
_lastSimulated = now;
|
||||
_created = now; // will get updated as appropriate in setLifetime()
|
||||
|
||||
|
@ -67,7 +67,7 @@ bool Particle::appendParticleData(OctreePacketData* packetData) const {
|
|||
success = packetData->appendValue(getLifetime());
|
||||
}
|
||||
if (success) {
|
||||
success = packetData->appendValue(getLastEdited());
|
||||
success = packetData->appendValue(getEditedAgo());
|
||||
}
|
||||
if (success) {
|
||||
success = packetData->appendValue(getRadius());
|
||||
|
@ -103,7 +103,7 @@ bool Particle::appendParticleData(OctreePacketData* packetData) const {
|
|||
int Particle::expectedBytes() {
|
||||
int expectedBytes = sizeof(uint32_t) // id
|
||||
+ sizeof(float) // lifetime
|
||||
+ sizeof(uint64_t) // lastedited
|
||||
+ sizeof(float) // edited ago
|
||||
+ sizeof(float) // radius
|
||||
+ sizeof(glm::vec3) // position
|
||||
+ sizeof(rgbColor) // color
|
||||
|
@ -132,10 +132,12 @@ int Particle::readParticleDataFromBuffer(const unsigned char* data, int bytesLef
|
|||
bytesRead += sizeof(lifetime);
|
||||
setLifetime(lifetime);
|
||||
|
||||
// _lastEdited
|
||||
memcpy(&_lastEdited, dataAt, sizeof(_lastEdited));
|
||||
dataAt += sizeof(_lastEdited);
|
||||
bytesRead += sizeof(_lastEdited);
|
||||
// edited ago
|
||||
float editedAgo;
|
||||
memcpy(&editedAgo, dataAt, sizeof(editedAgo));
|
||||
dataAt += sizeof(editedAgo);
|
||||
bytesRead += sizeof(editedAgo);
|
||||
setEditedAgo(editedAgo);
|
||||
|
||||
// radius
|
||||
memcpy(&_radius, dataAt, sizeof(_radius));
|
||||
|
@ -225,11 +227,9 @@ Particle Particle::fromEditPacket(unsigned char* data, int length, int& processe
|
|||
newParticle._newlyCreated = false;
|
||||
}
|
||||
|
||||
// lastEdited
|
||||
memcpy(&newParticle._lastEdited, dataAt, sizeof(newParticle._lastEdited));
|
||||
dataAt += sizeof(newParticle._lastEdited);
|
||||
processedBytes += sizeof(newParticle._lastEdited);
|
||||
|
||||
// clearly we just edited it
|
||||
newParticle.setEditedAgo(0);
|
||||
|
||||
// radius
|
||||
memcpy(&newParticle._radius, dataAt, sizeof(newParticle._radius));
|
||||
dataAt += sizeof(newParticle._radius);
|
||||
|
@ -288,7 +288,7 @@ Particle Particle::fromEditPacket(unsigned char* data, int length, int& processe
|
|||
void Particle::debugDump() const {
|
||||
printf("Particle id :%u\n", _id);
|
||||
printf(" lifetime:%f\n", getLifetime());
|
||||
printf(" last edited:%llu\n", _lastEdited);
|
||||
printf(" edited ago:%f\n", getEditedAgo());
|
||||
printf(" position:%f,%f,%f\n", _position.x, _position.y, _position.z);
|
||||
printf(" velocity:%f,%f,%f\n", _velocity.x, _velocity.y, _velocity.z);
|
||||
printf(" gravity:%f,%f,%f\n", _gravity.x, _gravity.y, _gravity.z);
|
||||
|
@ -334,11 +334,6 @@ bool Particle::encodeParticleEditMessageDetails(PACKET_TYPE command, int count,
|
|||
sizeOut += sizeof(details[i].creatorTokenID);
|
||||
}
|
||||
|
||||
// lastEdited
|
||||
memcpy(copyAt, &details[i].lastEdited, sizeof(details[i].lastEdited));
|
||||
copyAt += sizeof(details[i].lastEdited);
|
||||
sizeOut += sizeof(details[i].lastEdited);
|
||||
|
||||
// radius
|
||||
memcpy(copyAt, &details[i].radius, sizeof(details[i].radius));
|
||||
copyAt += sizeof(details[i].radius);
|
||||
|
@ -399,20 +394,25 @@ bool Particle::encodeParticleEditMessageDetails(PACKET_TYPE command, int count,
|
|||
|
||||
|
||||
void Particle::update() {
|
||||
|
||||
uint64_t now = usecTimestampNow();
|
||||
uint64_t elapsed = now - _lastSimulated;
|
||||
uint64_t USECS_PER_SECOND = 1000 * 1000;
|
||||
float timeElapsed = (float)((float)elapsed/(float)USECS_PER_SECOND);
|
||||
|
||||
|
||||
// calculate our default shouldDie state... then allow script to change it if it wants...
|
||||
float velocityScalar = glm::length(getVelocity());
|
||||
const float STILL_MOVING = 0.05 / TREE_SCALE;
|
||||
bool isStillMoving = (velocityScalar > STILL_MOVING);
|
||||
const uint64_t REALLY_OLD = 30 * 1000 * 1000;
|
||||
const float REALLY_OLD = 300.0f; // 300 seconds
|
||||
bool isReallyOld = (getLifetime() > REALLY_OLD);
|
||||
bool isInHand = getInHand();
|
||||
bool shouldDie = !isInHand && !isStillMoving && isReallyOld;
|
||||
setShouldDie(shouldDie);
|
||||
|
||||
printf("Particle::update()... timeElapsed: %f lifeTime:%f editedAgo:%f isInHand:%s isStillMoveing:%s isReallyOld:%s shouldDie:%s\n",
|
||||
timeElapsed, getLifetime(), getEditedAgo(), debug::valueOf(isInHand), debug::valueOf(isStillMoving),
|
||||
debug::valueOf(isReallyOld), debug::valueOf(shouldDie));
|
||||
|
||||
runScript(); // allow the javascript to alter our state
|
||||
|
||||
|
@ -470,3 +470,14 @@ void Particle::setLifetime(float lifetime) {
|
|||
uint64_t lifetimeInUsecs = lifetime * USECS_PER_SECOND;
|
||||
_created = usecTimestampNow() - lifetimeInUsecs;
|
||||
}
|
||||
|
||||
void Particle::setEditedAgo(float editedAgo) {
|
||||
uint64_t editedAgoInUsecs = editedAgo * USECS_PER_SECOND;
|
||||
_edited = usecTimestampNow() - editedAgoInUsecs;
|
||||
}
|
||||
|
||||
void Particle::copyChangedProperties(const Particle& other) {
|
||||
float lifetime = getLifetime();
|
||||
*this = other;
|
||||
setLifetime(lifetime);
|
||||
}
|
||||
|
|
|
@ -25,7 +25,6 @@ const uint32_t UNKNOWN_TOKEN = 0xFFFFFFFF;
|
|||
class ParticleDetail {
|
||||
public:
|
||||
uint32_t id;
|
||||
uint64_t lastEdited;
|
||||
glm::vec3 position;
|
||||
float radius;
|
||||
rgbColor color;
|
||||
|
@ -70,8 +69,8 @@ public:
|
|||
|
||||
/// lifetime of the particle in seconds
|
||||
float getLifetime() const { return (float)(usecTimestampNow() - _created) / (float)USECS_PER_SECOND; }
|
||||
|
||||
uint64_t getLastEdited() const { return _lastEdited; }
|
||||
/// seconds since last edited
|
||||
float getEditedAgo() const { return (float)(usecTimestampNow() - _edited) / (float)USECS_PER_SECOND; }
|
||||
uint32_t getID() const { return _id; }
|
||||
bool getShouldDie() const { return _shouldDie; }
|
||||
QString getUpdateScript() const { return _updateScript; }
|
||||
|
@ -93,7 +92,6 @@ public:
|
|||
void setShouldDie(bool shouldDie) { _shouldDie = shouldDie; }
|
||||
void setUpdateScript(QString updateScript) { _updateScript = updateScript; }
|
||||
void setCreatorTokenID(uint32_t creatorTokenID) { _creatorTokenID = creatorTokenID; }
|
||||
void setLifetime(float lifetime);
|
||||
|
||||
bool appendParticleData(OctreePacketData* packetData) const;
|
||||
int readParticleDataFromBuffer(const unsigned char* data, int bytesLeftToRead, ReadBitstreamToTreeParams& args);
|
||||
|
@ -105,19 +103,24 @@ public:
|
|||
void update();
|
||||
|
||||
void debugDump() const;
|
||||
|
||||
// similar to an assignment, but it handles no breaking lifetime and editedAgo
|
||||
void copyChangedProperties(const Particle& other);
|
||||
|
||||
protected:
|
||||
void runScript();
|
||||
static QScriptValue vec3toScriptValue(QScriptEngine *engine, const glm::vec3 &vec3);
|
||||
static void vec3FromScriptValue(const QScriptValue &object, glm::vec3 &vec3);
|
||||
static QScriptValue xColorToScriptValue(QScriptEngine *engine, const xColor& color);
|
||||
static void xColorFromScriptValue(const QScriptValue &object, xColor& color);
|
||||
|
||||
void setLifetime(float lifetime);
|
||||
void setEditedAgo(float editedAgo);
|
||||
|
||||
glm::vec3 _position;
|
||||
rgbColor _color;
|
||||
float _radius;
|
||||
glm::vec3 _velocity;
|
||||
uint64_t _created;
|
||||
uint64_t _lastEdited;
|
||||
uint32_t _id;
|
||||
static uint32_t _nextID;
|
||||
bool _shouldDie;
|
||||
|
@ -128,8 +131,11 @@ protected:
|
|||
|
||||
uint32_t _creatorTokenID;
|
||||
bool _newlyCreated;
|
||||
|
||||
|
||||
// these are never included in wire time
|
||||
uint64_t _lastSimulated;
|
||||
uint64_t _created;
|
||||
uint64_t _edited;
|
||||
|
||||
};
|
||||
|
||||
|
|
|
@ -44,8 +44,7 @@ void ParticleEditHandle::createParticle(glm::vec3 position, float radius, xColor
|
|||
glm::vec3 gravity, float damping, bool inHand, QString updateScript) {
|
||||
|
||||
// setup a ParticleDetail struct with the data
|
||||
uint64_t now = usecTimestampNow();
|
||||
ParticleDetail addParticleDetail = { NEW_PARTICLE, now,
|
||||
ParticleDetail addParticleDetail = { NEW_PARTICLE,
|
||||
position, radius, {color.red, color.green, color.blue },
|
||||
velocity, gravity, damping, inHand, updateScript, _creatorTokenID };
|
||||
|
||||
|
@ -70,8 +69,7 @@ bool ParticleEditHandle::updateParticle(glm::vec3 position, float radius, xColor
|
|||
}
|
||||
|
||||
// setup a ParticleDetail struct with the data
|
||||
uint64_t now = usecTimestampNow();
|
||||
ParticleDetail newParticleDetail = { _id, now,
|
||||
ParticleDetail newParticleDetail = { _id,
|
||||
position, radius, {color.red, color.green, color.blue },
|
||||
velocity, gravity, damping, inHand, updateScript, _creatorTokenID };
|
||||
|
||||
|
|
|
@ -22,8 +22,7 @@ unsigned int ParticleScriptingInterface::queueParticleAdd(glm::vec3 position, fl
|
|||
_nextCreatorTokenID++;
|
||||
|
||||
// setup a ParticleDetail struct with the data
|
||||
uint64_t now = usecTimestampNow();
|
||||
ParticleDetail addParticleDetail = { NEW_PARTICLE, now,
|
||||
ParticleDetail addParticleDetail = { NEW_PARTICLE,
|
||||
position, radius, {color.red, color.green, color.blue }, velocity,
|
||||
gravity, damping, inHand, updateScript, creatorTokenID };
|
||||
|
||||
|
|
|
@ -119,20 +119,14 @@ bool ParticleTreeElement::updateParticle(const Particle& particle) {
|
|||
uint16_t numberOfParticles = _particles.size();
|
||||
for (uint16_t i = 0; i < numberOfParticles; i++) {
|
||||
if (_particles[i].getID() == particle.getID()) {
|
||||
bool changedOnServer = _particles[i].getLastEdited() < particle.getLastEdited();
|
||||
bool changedOnServer = _particles[i].getEditedAgo() > particle.getEditedAgo();
|
||||
if (changedOnServer) {
|
||||
if (wantDebug) {
|
||||
printf("local particle [id:%d] %s, particle.isNewlyCreated()=%s\n",
|
||||
particle.getID(), (changedOnServer ? "CHANGED" : "same"),
|
||||
debug::valueOf(particle.isNewlyCreated()) );
|
||||
}
|
||||
|
||||
float actualLifetime = particle.getLifetime();
|
||||
if (!particle.isNewlyCreated()) {
|
||||
actualLifetime = _particles[i].getLifetime();
|
||||
}
|
||||
_particles[i] = particle;
|
||||
_particles[i].setLifetime(actualLifetime);
|
||||
_particles[i].copyChangedProperties(particle);
|
||||
} else {
|
||||
if (wantDebug) {
|
||||
printf(">>> NO CHANGE <<< -- local particle [id:%d] %s particle.isNewlyCreated()=%s\n",
|
||||
|
|
Loading…
Reference in a new issue