Reuse fields

This commit is contained in:
TheMode 2021-07-13 15:16:18 +02:00
parent fe28ba6f04
commit 86472003f8

View File

@ -479,9 +479,10 @@ public class Entity implements Viewable, Tickable, EventHandler<EntityEvent>, Da
} }
// Velocity // Velocity
final boolean noGravity = hasNoGravity();
boolean applyVelocity; boolean applyVelocity;
// Non-player entities with either velocity or gravity enabled // Non-player entities with either velocity or gravity enabled
applyVelocity = !isNettyClient && (hasVelocity() || !hasNoGravity()); applyVelocity = !isNettyClient && (hasVelocity() || !noGravity);
// Players with a velocity applied (client is responsible for gravity) // Players with a velocity applied (client is responsible for gravity)
applyVelocity |= isNettyClient && hasVelocity(); applyVelocity |= isNettyClient && hasVelocity();
@ -490,13 +491,11 @@ public class Entity implements Viewable, Tickable, EventHandler<EntityEvent>, Da
final Pos newPosition; final Pos newPosition;
final Vec newVelocity; final Vec newVelocity;
// Gravity force final Vec currentVelocity = getVelocity();
final double gravityY = hasNoGravity() ? 0 : gravityAcceleration;
final Vec deltaPos = new Vec( final Vec deltaPos = new Vec(
getVelocity().x() / tps, currentVelocity.x() / tps,
getVelocity().y() / tps - gravityY, currentVelocity.y() / tps - (noGravity ? 0 : gravityAcceleration),
getVelocity().z() / tps currentVelocity.z() / tps
); );
if (this.hasPhysics) { if (this.hasPhysics) {
@ -527,21 +526,21 @@ public class Entity implements Viewable, Tickable, EventHandler<EntityEvent>, Da
} }
// Update velocity // Update velocity
if (hasVelocity() || !newVelocity.equals(Vec.ZERO)) { if (hasVelocity() || !newVelocity.isZero()) {
if (onGround && isNettyClient) { if (onGround && isNettyClient) {
// Stop player velocity // Stop player velocity
velocity = Vec.ZERO; this.velocity = Vec.ZERO;
} else { } else {
final Block block = finalChunk.getBlock(position); final Block block = finalChunk.getBlock(position);
final double drag = block.registry().friction(); final double drag = block.registry().friction();
velocity = newVelocity this.velocity = newVelocity
// Convert from blocks/tick to blocks/sec // Convert from block/tick to block/sec
.mul(tps) .mul(tps)
// Apply drag // Apply drag
.apply((x, y, z) -> new Vec( .apply((x, y, z) -> new Vec(
x * drag, x * drag,
!hasNoGravity() ? y * (1 - gravityDragPerTick) : y, !noGravity ? y * (1 - gravityDragPerTick) : y,
z * drag z * drag
)) ))
// Prevent infinitely decreasing velocity // Prevent infinitely decreasing velocity