Skip to content

Commit

Permalink
fix: 步行辅助现在正常工作!
Browse files Browse the repository at this point in the history
  • Loading branch information
smartcmd committed Aug 11, 2023
1 parent 9e3f853 commit 78b704c
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ public class BlockAttributeComponentImpl implements BlockAttributeComponent, Blo
@ComponentIdentifier
public static final Identifier IDENTIFIER = new Identifier("minecraft:block_attribute_component");

@Dependency
protected BlockBaseComponent baseComponent;
protected Function<BlockState, BlockAttributes> attributeAccessor;

protected BlockAttributeComponentImpl(Function<BlockState, BlockAttributes> attributeAccessor) {
Expand Down
7 changes: 5 additions & 2 deletions Allay-API/src/main/java/cn/allay/api/world/World.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.Range;
import org.jetbrains.annotations.UnmodifiableView;
import org.joml.primitives.AABBd;
import org.joml.primitives.AABBdc;
import org.slf4j.Logger;

Expand Down Expand Up @@ -217,9 +218,11 @@ default BlockState[][][] getCollidingBlocks(AABBdc aabb, boolean layer, boolean
for (int y = 0; y < blockStates[x].length; y++) {
for (int z = 0; z < blockStates[x][y].length; z++) {
var blockState = blockStates[x][y][z];
if (blockState != null && !blockState.blockType().getBlockBehavior().getBlockAttributes(blockState).hasCollision()) {
if (blockState == null) continue;
var attributes = blockState.blockType().getBlockBehavior().getBlockAttributes(blockState);
if (!attributes.hasCollision() || !aabb.intersectsAABB(attributes.aabbCollision().translate(minX + x, minY + y, minZ + z, new AABBd()))) {
blockStates[x][y][z] = null;
} else if (blockState != null) {
} else {
notEmpty = true;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public class AllayEntityPhysicsService implements EntityPhysicsService {
public static final double DIFF_ROTATION_THRESHOLD = 0.1;

public static final double MOTION_THRESHOLD = 0.003;
public static final double STEPPING_OFFSET = 0.00001;
public static final double STEPPING_OFFSET = 0.1;

protected World world;
protected Map<Long, Entity> entities = new Long2ObjectNonBlockingMap<>();
Expand Down Expand Up @@ -168,10 +168,13 @@ protected double applyMotionZ(double stepHeight, Location3d pos, double mz, AABB
if (mz != 0) {
var resultAABB = new AABBd(aabb);
var resultPos = new Vector3d(pos);
//第一次直接移动
var zResult = moveAlongZAxisAndStopWhenCollision(resultAABB, mz, resultPos);
if (zResult.right()) {
//有碰撞,尝试跨步
if (tryStepping(aabb, stepHeight, mz > 0, false)) {
//计算剩余速度
mz = mz - (resultPos.z - pos.z);
if (tryStepping(resultPos, resultAABB, stepHeight, mz > 0, false)) {
//跨步成功
zResult = moveAlongZAxisAndStopWhenCollision(resultAABB, mz, resultPos);
}
Expand All @@ -187,10 +190,13 @@ protected double applyMotionX(double stepHeight, Location3d pos, double mx, AABB
if (mx != 0) {
var resultAABB = new AABBd(aabb);
var resultPos = new Vector3d(pos);
//第一次直接移动
var xResult = moveAlongXAxisAndStopWhenCollision(resultAABB, mx, resultPos);
if (xResult.right()) {
//有碰撞,尝试跨步
if (tryStepping(aabb, stepHeight, mx > 0, true)) {
//计算剩余速度
mx = mx - (resultPos.x - pos.x);
if (tryStepping(resultPos, resultAABB, stepHeight, mx > 0, true)) {
//跨步成功
xResult = moveAlongXAxisAndStopWhenCollision(resultAABB, mx, resultPos);
}
Expand Down Expand Up @@ -248,16 +254,17 @@ protected Pair<Double, Boolean> moveAlongXAxisAndStopWhenCollision(AABBd aabb, d
return new DoubleBooleanImmutablePair(mx, collision);
}

protected boolean tryStepping(AABBd aabb, double stepHeight, boolean positive, boolean xAxis) {
protected boolean tryStepping(Vector3d pos, AABBd aabb, double stepHeight, boolean positive, boolean xAxis) {
var offset = positive ? STEPPING_OFFSET : -STEPPING_OFFSET;
var offsetAABB = aabb.translate(xAxis ? offset : 0, 0, xAxis ? 0 : offset, new AABBd());
var recorder = new Vector3d();
moveAlongYAxisAndStopWhenCollision(offsetAABB, stepHeight, recorder);
moveAlongYAxisAndStopWhenCollision(offsetAABB, -stepHeight, recorder);
if (world.getCollidingBlocks(offsetAABB) != null) {
if (recorder.y == 0 || world.getCollidingBlocks(offsetAABB) != null) {
return false;
} else {
aabb.set(offsetAABB.translate(xAxis ? -offset : 0, 0, xAxis ? 0 : -offset));
pos.add(recorder);
return true;
}
}
Expand Down Expand Up @@ -438,7 +445,7 @@ public void addEntity(Entity entity) {
@Override
public void removeEntity(Entity entity) {
if (!entities.containsKey(entity.getUniqueId()))
throw new IllegalArgumentException("Entity " + entity.getUniqueId() + " is not added!");
return;
entityUpdateOperationQueue.offer(new EntityUpdateOperation(entity, EntityUpdateType.REMOVE));
}

Expand Down

0 comments on commit 78b704c

Please sign in to comment.