mirror of
https://github.com/Minestom/Minestom.git
synced 2024-06-26 06:24:54 +02:00
Optimise entity velocity (to do not send the packet every tick)
This commit is contained in:
parent
2f96a47270
commit
38dc50bb1a
|
@ -83,13 +83,11 @@ public abstract class Entity implements Viewable, EventHandler, DataContainer, P
|
||||||
|
|
||||||
// Velocity
|
// Velocity
|
||||||
protected Vector velocity = new Vector(); // Movement in block per second
|
protected Vector velocity = new Vector(); // Movement in block per second
|
||||||
protected long lastVelocityUpdateTime; // Reset velocity to 0 after countdown
|
|
||||||
private long velocityUpdatePeriod;
|
|
||||||
|
|
||||||
protected float gravityDragPerTick;
|
protected float gravityDragPerTick;
|
||||||
protected float gravityAcceleration;
|
protected float gravityAcceleration;
|
||||||
protected float gravityTerminalVelocity;
|
protected float gravityTerminalVelocity;
|
||||||
protected float gravityTickCount; // Number of tick where gravity tick was applied
|
protected int gravityTickCount; // Number of tick where gravity tick was applied
|
||||||
protected float eyeHeight;
|
protected float eyeHeight;
|
||||||
|
|
||||||
private boolean autoViewable;
|
private boolean autoViewable;
|
||||||
|
@ -148,7 +146,6 @@ public abstract class Entity implements Viewable, EventHandler, DataContainer, P
|
||||||
setAutoViewable(true);
|
setAutoViewable(true);
|
||||||
|
|
||||||
entityById.put(id, this);
|
entityById.put(id, this);
|
||||||
setVelocityUpdatePeriod(5);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public Entity(@NotNull EntityType entityType) {
|
public Entity(@NotNull EntityType entityType) {
|
||||||
|
@ -205,34 +202,6 @@ public abstract class Entity implements Viewable, EventHandler, DataContainer, P
|
||||||
return onGround || EntityUtils.isOnGround(this) /* backup for levitating entities */;
|
return onGround || EntityUtils.isOnGround(this) /* backup for levitating entities */;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Checks if now is a good time to send a velocity update packet.
|
|
||||||
*
|
|
||||||
* @param time the current time in milliseconds
|
|
||||||
* @return true if the velocity update packet should be send
|
|
||||||
*/
|
|
||||||
protected boolean shouldSendVelocityUpdate(long time) {
|
|
||||||
return (time - lastVelocityUpdateTime) >= velocityUpdatePeriod;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Gets the period, in ms, between two velocity update packets.
|
|
||||||
*
|
|
||||||
* @return period, in ms, between two velocity update packets
|
|
||||||
*/
|
|
||||||
public long getVelocityUpdatePeriod() {
|
|
||||||
return velocityUpdatePeriod;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Sets the period, in ms, between two velocity update packets.
|
|
||||||
*
|
|
||||||
* @param velocityUpdatePeriod period, in ms, between two velocity update packets
|
|
||||||
*/
|
|
||||||
public void setVelocityUpdatePeriod(long velocityUpdatePeriod) {
|
|
||||||
this.velocityUpdatePeriod = velocityUpdatePeriod;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Teleports the entity only if the chunk at {@code position} is loaded or if
|
* Teleports the entity only if the chunk at {@code position} is loaded or if
|
||||||
* {@link Instance#hasEnabledAutoChunkLoad()} returns true.
|
* {@link Instance#hasEnabledAutoChunkLoad()} returns true.
|
||||||
|
@ -487,12 +456,12 @@ public abstract class Entity implements Viewable, EventHandler, DataContainer, P
|
||||||
|
|
||||||
// Compute the gravity change (drag per tick + acceleration)
|
// Compute the gravity change (drag per tick + acceleration)
|
||||||
final float gravityY = Math.min(
|
final float gravityY = Math.min(
|
||||||
-gravityDragPerTick - (gravityAcceleration * gravityTickCount),
|
gravityDragPerTick + (gravityAcceleration * (float) gravityTickCount),
|
||||||
gravityTerminalVelocity);
|
gravityTerminalVelocity);
|
||||||
|
|
||||||
// Change velocity to apply gravity
|
// Change velocity to apply gravity
|
||||||
if (!noGravity) {
|
if (!noGravity) {
|
||||||
velocity.setY(velocity.getY() + gravityY);
|
velocity.setY(velocity.getY() - gravityY);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -550,9 +519,9 @@ public abstract class Entity implements Viewable, EventHandler, DataContainer, P
|
||||||
|
|
||||||
// Synchronization and packets...
|
// Synchronization and packets...
|
||||||
sendSynchronization();
|
sendSynchronization();
|
||||||
if (shouldSendVelocityUpdate(time)) {
|
// Verify if velocity packet has to be sent
|
||||||
|
if (hasVelocity() || gravityTickCount > 0) {
|
||||||
sendVelocityPacket();
|
sendVelocityPacket();
|
||||||
lastVelocityUpdateTime = time;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -821,7 +790,7 @@ public abstract class Entity implements Viewable, EventHandler, DataContainer, P
|
||||||
*
|
*
|
||||||
* @return the number of tick of which gravity has been consequently applied
|
* @return the number of tick of which gravity has been consequently applied
|
||||||
*/
|
*/
|
||||||
public float getGravityTickCount() {
|
public int getGravityTickCount() {
|
||||||
return gravityTickCount;
|
return gravityTickCount;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user