Skip to content

Commit

Permalink
Implement entity collision
Browse files Browse the repository at this point in the history
  • Loading branch information
Creeperface01 committed Oct 13, 2019
1 parent 6318c0e commit 55e12a1
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 12 deletions.
5 changes: 5 additions & 0 deletions src/main/java/cn/nukkit/block/BlockMoving.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,9 @@ public boolean canBePushed() {
public boolean isBreakable(Item item) {
return false;
}

@Override
public boolean canPassThrough() {
return true;
}
}
15 changes: 14 additions & 1 deletion src/main/java/cn/nukkit/blockentity/BlockEntityMovingBlock.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

import cn.nukkit.block.Block;
import cn.nukkit.block.BlockID;
import cn.nukkit.entity.Entity;
import cn.nukkit.level.format.FullChunk;
import cn.nukkit.math.AxisAlignedBB;
import cn.nukkit.math.BlockFace;
import cn.nukkit.math.BlockVector3;
import cn.nukkit.nbt.tag.CompoundTag;

Expand Down Expand Up @@ -56,8 +59,18 @@ public String getMovingBlockString() {
return this.blockString;
}

public void moveCollidedEntities(BlockEntityPistonArm piston) {
public void moveCollidedEntities(BlockEntityPistonArm piston, BlockFace moveDirection) {
AxisAlignedBB bb = block.getBoundingBox().getOffsetBoundingBox(

This comment has been minimized.

Copy link
@wode490390

wode490390 Oct 22, 2019

Contributor

Potential NPE here

This comment has been minimized.

Copy link
@Creeperface01

Creeperface01 Oct 22, 2019

Author Contributor

right, thanks :D

this.x + (piston.progress * moveDirection.getXOffset()) - moveDirection.getXOffset(),
this.y + (piston.progress * moveDirection.getYOffset()) - moveDirection.getYOffset(),
this.z + (piston.progress * moveDirection.getZOffset()) - moveDirection.getZOffset()
);

Entity[] entities = this.level.getCollidingEntities(bb);

for (Entity entity : entities) {
piston.moveEntity(entity, moveDirection);
}
}

@Override
Expand Down
47 changes: 36 additions & 11 deletions src/main/java/cn/nukkit/blockentity/BlockEntityPistonArm.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
package cn.nukkit.blockentity;

import cn.nukkit.Player;
import cn.nukkit.block.Block;
import cn.nukkit.block.BlockAir;
import cn.nukkit.block.BlockID;
import cn.nukkit.entity.Entity;
import cn.nukkit.level.Level;
import cn.nukkit.level.format.FullChunk;
import cn.nukkit.math.AxisAlignedBB;
import cn.nukkit.math.BlockFace;
import cn.nukkit.math.BlockVector3;
import cn.nukkit.math.SimpleAxisAlignedBB;
import cn.nukkit.nbt.tag.CompoundTag;
import cn.nukkit.nbt.tag.IntTag;
import cn.nukkit.nbt.tag.ListTag;
Expand Down Expand Up @@ -82,24 +86,45 @@ protected void initBlockEntity() {
}

private void moveCollidedEntities() {
// float lastProgress = this.getExtendedProgress(this.lastProgress);
// double x = (double) (lastProgress * (float) this.facing.getXOffset());
// double y = (double) (lastProgress * (float) this.facing.getYOffset());
// double z = (double) (lastProgress * (float) this.facing.getZOffset());
// AxisAlignedBB bb = new SimpleAxisAlignedBB(x, y, z, x + 1.0D, y + 1.0D, z + 1.0D);
// Entity[] entities = this.level.getCollidingEntities(bb);
// if (entities.length != 0) {
//
// }

BlockFace pushDir = this.extending ? facing : facing.getOpposite();
for (BlockVector3 pos : this.attachedBlocks) {
BlockEntity blockEntity = this.level.getBlockEntity(pos.getSide(pushDir));

if (blockEntity instanceof BlockEntityMovingBlock) {
((BlockEntityMovingBlock) blockEntity).moveCollidedEntities(this);
((BlockEntityMovingBlock) blockEntity).moveCollidedEntities(this, pushDir);
}
}

AxisAlignedBB bb = new SimpleAxisAlignedBB(0, 0, 0, 1, 1, 1).getOffsetBoundingBox(
this.x + (pushDir.getXOffset() * progress),
this.y + (pushDir.getYOffset() * progress),
this.z + (pushDir.getZOffset() * progress)
);

Entity[] entities = this.level.getCollidingEntities(bb);

for (Entity entity : entities) {
moveEntity(entity, pushDir);
}
}

void moveEntity(Entity entity, BlockFace moveDirection) {
if (!entity.canBePushed()) {
return;
}

//TODO: event

if (entity instanceof Player) {
return;
}

float diff = this.progress - this.lastProgress;
entity.move(
diff * moveDirection.getXOffset(),
diff * moveDirection.getYOffset(),
diff * moveDirection.getZOffset()
);
}

public void move(boolean extending, List<BlockVector3> attachedBlocks) {
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/cn/nukkit/entity/Entity.java
Original file line number Diff line number Diff line change
Expand Up @@ -1494,6 +1494,10 @@ public void setAbsorption(float absorption) {
}
}

public boolean canBePushed() {
return true;
}

public BlockFace getDirection() {
double rotation = this.yaw % 360;
if (rotation < 0) {
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/cn/nukkit/math/AxisAlignedBB.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,10 @@ default AxisAlignedBB setBB(AxisAlignedBB bb) {
return this;
}

default AxisAlignedBB getOffsetBoundingBox(BlockFace face, double x, double y, double z) {
return getOffsetBoundingBox(face.getXOffset() * x, face.getYOffset() * y, face.getZOffset() * z);
}

default AxisAlignedBB getOffsetBoundingBox(double x, double y, double z) {
return new SimpleAxisAlignedBB(this.getMinX() + x, this.getMinY() + y, this.getMinZ() + z, this.getMaxX() + x, this.getMaxY() + y, this.getMaxZ() + z);
}
Expand Down

0 comments on commit 55e12a1

Please sign in to comment.