Skip to content

Commit

Permalink
feat: Basic entity move logic
Browse files Browse the repository at this point in the history
  • Loading branch information
smartcmd committed Aug 6, 2023
1 parent a6426d5 commit f3e4a3c
Show file tree
Hide file tree
Showing 10 changed files with 360 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ protected static AABBd parseAABBStr(String str) {
return new AABBd(numbers.get(0), numbers.get(1), numbers.get(2), numbers.get(3), numbers.get(4), numbers.get(5));
}
@Builder.Default
protected AABBdc aabb = new AABBd(0, 0, 0, 1, 1, 1);
protected AABBdc aabbCollision = new AABBd(0, 0, 0, 1, 1, 1);
@Builder.Default
protected boolean hasCollision = true;
@Builder.Default
protected boolean blocksPrecipitation = true;
@Builder.Default
Expand Down Expand Up @@ -118,7 +120,7 @@ public static BlockAttributes fromNBT(NbtMap nbt) {
);
return BlockAttributes
.builder()
.aabb(parseAABBStr(nbt.getString("aabb")))
.aabbCollision(parseAABBStr(nbt.getString("aabbCollision")))
.blocksPrecipitation(nbt.getBoolean("blocksPrecipitation"))
.canBeMovingBlock(nbt.getBoolean("canBeMovingBlock"))
.breaksFallingBlocks(nbt.getBoolean("breaksFallingBlocks"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.cloudburstmc.protocol.bedrock.packet.BedrockPacket;
import org.cloudburstmc.protocol.bedrock.packet.MoveEntityDeltaPacket;
import org.jetbrains.annotations.UnmodifiableView;
import org.joml.Vector3d;
import org.joml.Vector3dc;
import org.joml.primitives.AABBd;
import org.joml.primitives.AABBdc;
Expand Down Expand Up @@ -54,16 +55,20 @@ public interface EntityBaseComponent {
Map<Long, Client> getViewers();

@Inject
Vector3dc getSpeed();
Vector3dc getMotion();

@Inject
void setSpeed(Vector3dc speed);
void setMotion(Vector3dc motion);

default void addMotion(Vector3dc add) {
setMotion(getMotion().add(add, new Vector3d()));
}

@Inject
Vector3dc getMotion();
boolean isOnGround();

@Inject
void setMotion(Vector3dc motion);
void setOnGround(boolean onGround);

@Inject
void spawnTo(Client client);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ public class EntityBaseComponentImpl<T extends Entity> implements EntityBaseComp
protected Function<T, AABBdc> aabbGetter;
protected boolean hasCollision = true;
protected Map<Long, Client> viewers = new Long2ObjectOpenHashMap<>();
protected Vector3d speed = new Vector3d();
protected Vector3d motion = new Vector3d();
protected boolean onGround = true;

public EntityBaseComponentImpl(EntityInitInfo<T> info, Function<T, AABBdc> aabbGetter) {
if (info.location().world() == null)
Expand Down Expand Up @@ -199,26 +199,24 @@ public void setHasCollision(boolean hasCollision) {

@Override
@Impl
public Vector3dc getSpeed() {
return speed;
public Vector3dc getMotion() {
return motion;
}

@Override
@Impl
public void setSpeed(Vector3dc speed) {
this.speed = new Vector3d(speed);
public void setMotion(Vector3dc motion) {
this.motion = new Vector3d(motion);
}

@Override
@Impl
public Vector3dc getMotion() {
return motion;
public boolean isOnGround() {
return onGround;
}

@Override
@Impl
public void setMotion(Vector3dc motion) {
this.motion = new Vector3d(motion);
public void setOnGround(boolean onGround) {
this.onGround = onGround;
}

@Override
Expand Down
7 changes: 7 additions & 0 deletions Allay-API/src/main/java/cn/allay/api/math/Location3d.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@ public class Location3d extends Position3d implements Location3dc {
public double yaw;
public double headYaw;

public Location3d(Location3dc l) {
super(l);
this.pitch = l.pitch();
this.yaw = l.yaw();
this.headYaw = l.headYaw();
}

public Location3d(double x, double y, double z, World world) {
super(x, y, z, world);
}
Expand Down
5 changes: 5 additions & 0 deletions Allay-API/src/main/java/cn/allay/api/math/Position3d.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@
public class Position3d extends Vector3d implements Position3dc {
public World world;

public Position3d(Position3dc p) {
super(p);
this.world = p.world();
}

public Position3d(World world) {
super();
this.world = world;
Expand Down
99 changes: 99 additions & 0 deletions Allay-API/src/main/java/cn/allay/api/world/World.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package cn.allay.api.world;

import cn.allay.api.block.type.BlockState;
import cn.allay.api.entity.Entity;
import cn.allay.api.math.Position3ic;
import cn.allay.api.client.Client;
Expand All @@ -11,9 +12,12 @@
import cn.allay.api.world.generator.WorldGenerator;
import org.cloudburstmc.protocol.bedrock.data.GameType;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.Range;
import org.jetbrains.annotations.UnmodifiableView;
import org.joml.primitives.AABBdc;

import java.util.Collection;
import java.util.List;

/**
* Describe a world
Expand Down Expand Up @@ -91,4 +95,99 @@ default int minChunkX() {
default int minChunkZ() {
return Integer.MIN_VALUE;
}

default void setBlockState(int x, int y, int z, BlockState state) {
setBlockState(x, y, z, state, false);
}

default void setBlockState(int x, int y, int z, BlockState state, boolean layer) {
var chunk = getChunk(x >> 4, z >> 4);
if (chunk == null) return;
chunk.setBlockState(x & 15, y, z & 15, layer, state);
}

@Nullable
default BlockState getBlockState(int x, int y, int z) {
return getBlockState(x, y, z, false);
}

@Nullable
default BlockState getBlockState(int x, int y, int z, boolean layer) {
var chunk = getChunk(x >> 4, z >> 4);
if (chunk == null) return null;
return chunk.getBlockState(x & 15, y, z & 15, layer);
}

default BlockState[][][] getBlockStates(int x, int y, int z, @Range(from = 1, to = Integer.MAX_VALUE) int sizeX, @Range(from = 1, to = Integer.MAX_VALUE) int sizeY, @Range(from = 1, to = Integer.MAX_VALUE) int sizeZ, boolean layer) {
BlockState[][][] blockStates = new BlockState[sizeX][sizeY][sizeZ];

int startX = x >> 4;
int endX = (x + sizeX - 1) >> 4;
int startZ = z >> 4;
int endZ = (z + sizeZ - 1) >> 4;

for (int chunkX = startX; chunkX <= endX; chunkX++) {
for (int chunkZ = startZ; chunkZ <= endZ; chunkZ++) {
final int cX = chunkX << 4;
final int cZ = chunkZ << 4;
int localStartX = Math.max(x - cX, 0);
int localStartZ = Math.max(z - cZ, 0);
int localEndX = Math.min(x + sizeX - cX, 16);
int localEndZ = Math.min(z + sizeZ - cZ, 16);

var chunk = getChunk(chunkX, chunkZ);
if (chunk != null) {
chunk.batchProcess(
sectionOperate -> {
for (int localX = localStartX; localX < localEndX; localX++) {
for (int globalY = y; globalY < y + sizeY; globalY++) {
for (int localZ = localStartZ; localZ < localEndZ; localZ++) {
int globalX = cX + localX;
int globalZ = cZ + localZ;
blockStates[globalX - x][globalY - y][globalZ - z] =
sectionOperate.getBlockState(localX, globalY, localZ, layer);
}
}
}
},
null,
null
);
}
}
}

return blockStates;
}

default BlockState[][][] getCollidingBlocks(AABBdc aabb) {
return getCollidingBlocks(aabb, false);
}

default BlockState[][][] getCollidingBlocks(AABBdc aabb, boolean layer) {
return getCollidingBlocks(aabb, layer, false);
}

default BlockState[][][] getCollidingBlocks(AABBdc aabb, boolean layer, boolean ignoreCollision) {
int maxX = (int) Math.ceil(aabb.maxX());
int maxY = (int) Math.ceil(aabb.maxY());
int maxZ = (int) Math.ceil(aabb.maxZ());
int minX = (int) Math.floor(aabb.minX());
int minY = (int) Math.floor(aabb.minY());
int minZ = (int) Math.floor(aabb.minZ());
var blockStates = getBlockStates(minX, minY, minZ, maxX - minX, maxY - minY, maxZ - minZ, layer);
if (!ignoreCollision) {
//过滤掉没有碰撞的方块
for (int x = 0; x < blockStates.length; x++) {
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()) {
blockStates[x][y][z] = null;
}
}
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ public interface EntityPhysicsService {

boolean containEntity(Entity entity);

default void offerScheduledMove(Entity entity, Location3dc newLoc) {
offerScheduledMove(entity, newLoc, true);
}

void offerScheduledMove(Entity entity, Location3dc newLoc);

default List<Entity> computeCollidingEntities(Entity entity) {
Expand Down
Loading

0 comments on commit f3e4a3c

Please sign in to comment.