mirror of
https://github.com/Minestom/Minestom.git
synced 2025-01-02 14:38:26 +01:00
Move block collision code into its own method
This commit is contained in:
parent
2dbf795d5c
commit
ef58d770b4
@ -124,7 +124,6 @@ public class Entity implements Viewable, Tickable, TagHandler, PermissionHandler
|
|||||||
|
|
||||||
// Tick related
|
// Tick related
|
||||||
private long ticks;
|
private long ticks;
|
||||||
private final EntityTickEvent tickEvent = new EntityTickEvent(this);
|
|
||||||
|
|
||||||
private final Acquirable<Entity> acquirable = Acquirable.of(this);
|
private final Acquirable<Entity> acquirable = Acquirable.of(this);
|
||||||
|
|
||||||
@ -438,33 +437,7 @@ public class Entity implements Viewable, Tickable, TagHandler, PermissionHandler
|
|||||||
velocityTick();
|
velocityTick();
|
||||||
|
|
||||||
// handle block contacts
|
// handle block contacts
|
||||||
// TODO do not call every tick (it is pretty expensive)
|
touchTick();
|
||||||
final int minX = (int) Math.floor(boundingBox.getMinX());
|
|
||||||
final int maxX = (int) Math.ceil(boundingBox.getMaxX());
|
|
||||||
final int minY = (int) Math.floor(boundingBox.getMinY());
|
|
||||||
final int maxY = (int) Math.ceil(boundingBox.getMaxY());
|
|
||||||
final int minZ = (int) Math.floor(boundingBox.getMinZ());
|
|
||||||
final int maxZ = (int) Math.ceil(boundingBox.getMaxZ());
|
|
||||||
for (int y = minY; y <= maxY; y++) {
|
|
||||||
for (int x = minX; x <= maxX; x++) {
|
|
||||||
for (int z = minZ; z <= maxZ; z++) {
|
|
||||||
final Chunk chunk = ChunkUtils.retrieve(instance, currentChunk, x, z);
|
|
||||||
if (!ChunkUtils.isLoaded(chunk))
|
|
||||||
continue;
|
|
||||||
final Block block = chunk.getBlock(x, y, z, BlockGetter.Condition.CACHED);
|
|
||||||
if (block == null)
|
|
||||||
continue;
|
|
||||||
final BlockHandler handler = block.handler();
|
|
||||||
if (handler != null) {
|
|
||||||
// checks that we are actually in the block, and not just here because of a rounding error
|
|
||||||
if (boundingBox.intersectWithBlock(x, y, z)) {
|
|
||||||
// TODO: replace with check with custom block bounding box
|
|
||||||
handler.onTouch(new BlockHandler.Touch(block, instance, new Vec(x, y, z), this));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
handleVoid();
|
handleVoid();
|
||||||
|
|
||||||
@ -472,7 +445,7 @@ public class Entity implements Viewable, Tickable, TagHandler, PermissionHandler
|
|||||||
update(time);
|
update(time);
|
||||||
|
|
||||||
ticks++;
|
ticks++;
|
||||||
EventDispatcher.call(tickEvent); // reuse tickEvent to avoid recreating it each tick
|
EventDispatcher.call(new EntityTickEvent(this));
|
||||||
|
|
||||||
// remove expired effects
|
// remove expired effects
|
||||||
if (!effects.isEmpty()) {
|
if (!effects.isEmpty()) {
|
||||||
@ -578,6 +551,36 @@ public class Entity implements Viewable, Tickable, TagHandler, PermissionHandler
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void touchTick() {
|
||||||
|
// TODO do not call every tick (it is pretty expensive)
|
||||||
|
final int minX = (int) Math.floor(boundingBox.getMinX());
|
||||||
|
final int maxX = (int) Math.ceil(boundingBox.getMaxX());
|
||||||
|
final int minY = (int) Math.floor(boundingBox.getMinY());
|
||||||
|
final int maxY = (int) Math.ceil(boundingBox.getMaxY());
|
||||||
|
final int minZ = (int) Math.floor(boundingBox.getMinZ());
|
||||||
|
final int maxZ = (int) Math.ceil(boundingBox.getMaxZ());
|
||||||
|
for (int y = minY; y <= maxY; y++) {
|
||||||
|
for (int x = minX; x <= maxX; x++) {
|
||||||
|
for (int z = minZ; z <= maxZ; z++) {
|
||||||
|
final Chunk chunk = ChunkUtils.retrieve(instance, currentChunk, x, z);
|
||||||
|
if (!ChunkUtils.isLoaded(chunk))
|
||||||
|
continue;
|
||||||
|
final Block block = chunk.getBlock(x, y, z, BlockGetter.Condition.CACHED);
|
||||||
|
if (block == null)
|
||||||
|
continue;
|
||||||
|
final BlockHandler handler = block.handler();
|
||||||
|
if (handler != null) {
|
||||||
|
// checks that we are actually in the block, and not just here because of a rounding error
|
||||||
|
if (boundingBox.intersectWithBlock(x, y, z)) {
|
||||||
|
// TODO: replace with check with custom block bounding box
|
||||||
|
handler.onTouch(new BlockHandler.Touch(block, instance, new Vec(x, y, z), this));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the number of ticks this entity has been active for.
|
* Gets the number of ticks this entity has been active for.
|
||||||
*
|
*
|
||||||
|
Loading…
Reference in New Issue
Block a user