|
| 1 | +package it.feargames.tileculling; |
| 2 | + |
| 3 | +import it.feargames.tileculling.adapter.IAdapter; |
| 4 | +import it.feargames.tileculling.occlusionculling.AxisAlignedBB; |
| 5 | +import it.feargames.tileculling.occlusionculling.OcclusionCulling; |
| 6 | +import org.bukkit.Chunk; |
| 7 | +import org.bukkit.Location; |
| 8 | +import org.bukkit.Material; |
| 9 | +import org.bukkit.World; |
| 10 | +import org.bukkit.block.BlockState; |
| 11 | +import org.bukkit.entity.Player; |
| 12 | +import org.bukkit.util.Vector; |
| 13 | + |
| 14 | +import java.util.List; |
| 15 | + |
| 16 | +public class ChunkTileVisibilityManager { |
| 17 | + |
| 18 | + public static final int CHUNK_RANGE = 6; // TODO: makes no sense to have a chunk range, not this way at least |
| 19 | + public static final AxisAlignedBB BLOCK_AABB = new AxisAlignedBB(0d, 0d, 0d, 1d, 1d, 1d); |
| 20 | + |
| 21 | + private final IAdapter adapter; |
| 22 | + private final PlayerChunkTracker playerTracker; |
| 23 | + private final VisibilityCache visibilityCache; |
| 24 | + private final ChunkCache chunkCache; |
| 25 | + |
| 26 | + private final OcclusionCulling culling; |
| 27 | + |
| 28 | + public ChunkTileVisibilityManager(IAdapter adapter, PlayerChunkTracker playerTracker, VisibilityCache visibilityCache, ChunkCache chunkCache) { |
| 29 | + this.adapter = adapter; |
| 30 | + this.playerTracker = playerTracker; |
| 31 | + this.visibilityCache = visibilityCache; |
| 32 | + this.chunkCache = chunkCache; |
| 33 | + |
| 34 | + this.culling = new OcclusionCulling(chunkCache); |
| 35 | + } |
| 36 | + |
| 37 | + public void updateVisibility(Player player) { |
| 38 | + World world = player.getWorld(); |
| 39 | + Location playerLocation = player.getLocation(); |
| 40 | + |
| 41 | + Vector playerEyeLocation = player.getEyeLocation().toVector(); |
| 42 | + |
| 43 | + int playerChunkX = playerLocation.getBlockX() >> 4; |
| 44 | + int playerChunkZ = playerLocation.getBlockZ() >> 4; |
| 45 | + |
| 46 | + culling.resetCache(); |
| 47 | + |
| 48 | + for (int x = -CHUNK_RANGE; x <= CHUNK_RANGE; x++) { |
| 49 | + for (int z = -CHUNK_RANGE; z <= CHUNK_RANGE; z++) { |
| 50 | + int chunkX = playerChunkX + x; |
| 51 | + int chunkZ = playerChunkZ + z; |
| 52 | + long chunkKey = Chunk.getChunkKey(chunkX, chunkZ); |
| 53 | + |
| 54 | + if (!playerTracker.isChunkTracked(player, chunkKey)) { |
| 55 | + continue; |
| 56 | + } |
| 57 | + |
| 58 | + List<BlockState> tiles = chunkCache.getChunkTiles(world, chunkKey); |
| 59 | + if (tiles == null) { |
| 60 | + continue; |
| 61 | + } |
| 62 | + for (BlockState block : tiles) { |
| 63 | + Location bloc = block.getLocation(); |
| 64 | + boolean canSee = culling.isAABBVisible(player, playerEyeLocation, bloc, BLOCK_AABB); |
| 65 | + boolean hidden = visibilityCache.isHidden(player, block.getLocation()); |
| 66 | + if (hidden && canSee) { |
| 67 | + visibilityCache.setHidden(player, block.getLocation(), false); |
| 68 | + adapter.updateBlockState(player, block.getLocation(), block.getBlockData()); |
| 69 | + } else if (!hidden && !canSee) { |
| 70 | + visibilityCache.setHidden(player, block.getLocation(), true); |
| 71 | + adapter.updateBlockState(player, block.getLocation(), Material.AIR, (byte) 0); |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + public void seedChunk(Player player, long chunkKey) { |
| 79 | + World world = player.getWorld(); |
| 80 | + Vector playerEyeLocation = player.getEyeLocation().toVector(); |
| 81 | + |
| 82 | + List<BlockState> tiles = chunkCache.getChunkTiles(world, chunkKey); |
| 83 | + if (tiles == null) { |
| 84 | + // Should never happen, just in case |
| 85 | + return; |
| 86 | + } |
| 87 | + |
| 88 | + for (BlockState block : tiles) { |
| 89 | + Location bloc = block.getLocation(); |
| 90 | + boolean canSee = culling.isAABBVisible(player, playerEyeLocation, bloc, BLOCK_AABB); |
| 91 | + if (canSee) { |
| 92 | + visibilityCache.setHidden(player, block.getLocation(), false); |
| 93 | + player.sendBlockChange(block.getLocation(), block.getBlockData()); |
| 94 | + } else { |
| 95 | + visibilityCache.setHidden(player, block.getLocation(), true); |
| 96 | + } |
| 97 | + } |
| 98 | + } |
| 99 | +} |
0 commit comments