| 1 | package net.minecraft.world.entity;
|
| 2 |
|
| 3 | import net.minecraft.core.PositionAndRotation;
|
| 4 | import net.minecraft.world.phys.Vec3;
|
| 5 | import org.jspecify.annotations.Nullable;
|
| 6 |
|
| 7 | public abstract class AbstractInterpolationHandler implements InterpolationHandler {
|
| 8 | protected final Entity entity;
|
| 9 | protected int interpolationSteps;
|
| 10 | protected final PositionAndRotation.Mutable lastPositionAndRotation = new PositionAndRotation.Mutable();
|
| 11 |
|
| 12 | public AbstractInterpolationHandler(final Entity entity, final int interpolationSteps) {
|
| 13 | this.entity = entity;
|
| 14 | this.interpolationSteps = interpolationSteps;
|
| 15 | }
|
| 16 |
|
| 17 | protected abstract PositionAndRotation.Mutable interpolationData();
|
| 18 |
|
| 19 | protected abstract void startInterpolating(PositionPath position, float yRot, float xRot);
|
| 20 |
|
| 21 | protected abstract void doInterpolate();
|
| 22 |
|
| 23 | @Override
|
| 24 | public @Nullable PositionAndRotation target() {
|
| 25 | return this.hasActiveInterpolation() ? this.interpolationData().immutable() : null;
|
| 26 | }
|
| 27 |
|
| 28 | @Override
|
| 29 | public boolean interpolateTo(final @Nullable PositionPath position, final float yRot, final float xRot, final boolean hasRotation) {
|
| 30 | PositionAndRotation current = this.hasActiveInterpolation() ? this.interpolationData() : this.entity.storePositionAndRotation();
|
| 31 | this.interpolateTo(
|
| 32 | position != null ? position : PositionPath.of(current.position()), hasRotation ? yRot : current.yRot(), hasRotation ? xRot : current.xRot()
|
| 33 | );
|
| 34 | return true;
|
| 35 | }
|
| 36 |
|
| 37 | protected void interpolateTo(final PositionPath position, final float yRot, final float xRot) {
|
| 38 | if (this.interpolationSteps == 0) {
|
| 39 | this.entity.snapTo(position.endPosition(), yRot, xRot);
|
| 40 | this.cancel();
|
| 41 | } else if (!this.hasActiveInterpolation() || !this.interpolationData().is(position.endPosition(), yRot, xRot)) {
|
| 42 | this.startInterpolating(position, yRot, xRot);
|
| 43 | this.setLastPositionAndRotation();
|
| 44 | this.entity.onInterpolationStart(this);
|
| 45 | }
|
| 46 | }
|
| 47 |
|
| 48 | @Override
|
| 49 | public void applyPredictedMovement(final Vec3 delta) {
|
| 50 | this.adjustInterpolationPosition(delta);
|
| 51 | this.lastPositionAndRotation.addDelta(delta);
|
| 52 | }
|
| 53 |
|
| 54 | @Override
|
| 55 | public void interpolate() {
|
| 56 | if (!this.hasActiveInterpolation()) {
|
| 57 | this.cancel();
|
| 58 | } else {
|
| 59 | this.adjustInterpolationTargetFromDeltas();
|
| 60 | this.doInterpolate();
|
| 61 | this.setLastPositionAndRotation();
|
| 62 | }
|
| 63 | }
|
| 64 |
|
| 65 | private void adjustInterpolationPosition(final Vec3 delta) {
|
| 66 | Vec3 adjustedPosition = this.interpolationData().position().add(delta);
|
| 67 | if (this.entity.level().noCollision(this.entity, this.entity.makeBoundingBox(adjustedPosition))) {
|
| 68 | this.interpolationData().addDelta(delta);
|
| 69 | }
|
| 70 | }
|
| 71 |
|
| 72 | private void adjustInterpolationTargetFromDeltas() {
|
| 73 | Vec3 deltaSinceLastInterpolation = this.entity.position().subtract(this.lastPositionAndRotation.position());
|
| 74 | if (deltaSinceLastInterpolation.lengthSqr() > 1.0E-5F) {
|
| 75 | this.adjustInterpolationPosition(deltaSinceLastInterpolation);
|
| 76 | }
|
| 77 |
|
| 78 | float deltaYRotSinceLastInterpolation = this.entity.getYRot() - this.lastPositionAndRotation.yRot();
|
| 79 | float deltaXRotSinceLastInterpolation = this.entity.getXRot() - this.lastPositionAndRotation.xRot();
|
| 80 | this.interpolationData().addRotation(deltaYRotSinceLastInterpolation, deltaXRotSinceLastInterpolation);
|
| 81 | }
|
| 82 |
|
| 83 | private void setLastPositionAndRotation() {
|
| 84 | this.lastPositionAndRotation.set(this.entity.position(), this.entity.getYRot(), this.entity.getXRot());
|
| 85 | }
|
| 86 | }
|
| 87 |
|