@Override public ChunkViewCore getSubviewAroundChunk(Vector3i chunkPos) { Region3i region = Region3i.createFromCenterExtents(chunkPos, ChunkConstants.LOCAL_REGION_EXTENTS); if (getChunk(chunkPos) != null) { return createWorldView(region, new Vector3i(-region.min().x, -region.min().y, -region.min().z)); } return null; }
@Override public ChunkViewCore getSubviewAroundChunk(Vector3i chunkPos) { Region3i region = Region3i.createFromCenterExtents(chunkPos, ChunkConstants.LOCAL_REGION_EXTENTS); if (getChunk(chunkPos) != null) { return createWorldView(region, new Vector3i(-region.min().x, -region.min().y, -region.min().z)); } return null; }
/** * @param offset * @return A copy of the region offset by the given value */ public Region3i move(BaseVector3i offset) { Vector3i newMin = min(); newMin.add(offset); return Region3i.createFromMinAndSize(newMin, size); }
@Override public PersistedData serialize(Region3i value, SerializationContext context) { if (value == null) { return context.createNull(); } else { Map<String, PersistedData> map = Maps.newLinkedHashMap(); map.put(MIN_FIELD, context.create(value.min(), Vector3i.class)); map.put(SIZE_FIELD, context.create(value.size(), Vector3i.class)); return context.create(map); } }
/** * @param other * @return The region that is encompassed by both this and other. If they * do not overlap then the empty region is returned */ public Region3i intersect(Region3i other) { Vector3i intersectMin = min(); intersectMin.max(other.min()); Vector3i intersectMax = max(); intersectMax.min(other.max()); return createFromMinMax(intersectMin, intersectMax); }
/** * Creates a new region that is the same as this region but expanded in all directions by the given amount * * @param amount * @return A new region */ public Region3i expand(int amount) { Vector3i expandedMin = min(); expandedMin.sub(amount, amount, amount); Vector3i expandedMax = max(); expandedMax.add(amount, amount, amount); return createFromMinMax(expandedMin, expandedMax); }
public Region3i expand(BaseVector3i amount) { Vector3i expandedMin = min(); expandedMin.sub(amount); Vector3i expandedMax = max(); expandedMax.add(amount); return createFromMinMax(expandedMin, expandedMax); }
public Region3i expandToContain(BaseVector3i adjPos) { Vector3i expandedMin = min(); expandedMin.min(adjPos); Vector3i expandedMax = max(); expandedMax.max(adjPos); return createFromMinMax(expandedMin, expandedMax); }
private ChunkViewCore createWorldView(Region3i region, Vector3i offset) { Chunk[] chunks = new Chunk[region.size().x * region.size().y * region.size().z]; for (Vector3i chunkPos : region) { Chunk chunk = chunkCache.get(chunkPos); if (chunk == null || !chunk.isReady()) { return null; } chunkPos.sub(region.min()); int index = TeraMath.calculate3DArrayIndex(chunkPos, region.size()); chunks[index] = chunk; } return new ChunkViewCoreImpl(chunks, region, offset, blockManager.getBlock(BlockManager.AIR_ID)); }
@ReceiveEvent public void openDoor(OpenDoorEvent event, EntityRef player) { EntityRef entity = event.getDoorEntity(); DoorComponent door = entity.getComponent(DoorComponent.class); Side newSide = door.openSide; BlockRegionComponent regionComp = entity.getComponent(BlockRegionComponent.class); Block bottomBlock = door.bottomBlockFamily.getBlockForPlacement(regionComp.region.min(), newSide, Side.TOP); worldProvider.setBlock(regionComp.region.min(), bottomBlock); Block topBlock = door.topBlockFamily.getBlockForPlacement(regionComp.region.max(), newSide, Side.TOP); worldProvider.setBlock(regionComp.region.max(), topBlock); if (door.openSound != null) { entity.send(new PlaySoundEvent(door.openSound, 1f)); } door.isOpen = true; } }
@ReceiveEvent public void closeDoor(CloseDoorEvent event, EntityRef player) { EntityRef entity = event.getDoorEntity(); DoorComponent door = entity.getComponent(DoorComponent.class); Side newSide = door.closedSide; BlockRegionComponent regionComp = entity.getComponent(BlockRegionComponent.class); Block bottomBlock = door.bottomBlockFamily.getBlockForPlacement(regionComp.region.min(), newSide, Side.TOP); worldProvider.setBlock(regionComp.region.min(), bottomBlock); Block topBlock = door.topBlockFamily.getBlockForPlacement(regionComp.region.max(), newSide, Side.TOP); worldProvider.setBlock(regionComp.region.max(), topBlock); if (door.closeSound != null) { entity.send(new PlaySoundEvent(door.closeSound, 1f)); } door.isOpen = false; entity.saveComponent(door); }
@Test public void testCreateRegionWithMinMax() { List<Vector3i> mins = Arrays.asList(new Vector3i(), new Vector3i(1, 1, 1), new Vector3i(3, 4, 5)); List<Vector3i> expectedSize = Arrays.asList(new Vector3i(1, 1, 1), new Vector3i(3, 3, 3), new Vector3i(8, 5, 2)); List<Vector3i> max = Arrays.asList(new Vector3i(), new Vector3i(3, 3, 3), new Vector3i(10, 8, 6)); for (int i = 0; i < mins.size(); ++i) { Region3i region = Region3i.createFromMinMax(mins.get(i), max.get(i)); assertEquals(mins.get(i), region.min()); assertEquals(max.get(i), region.max()); assertEquals(expectedSize.get(i), region.size()); assertFalse(region.isEmpty()); } }
@Override public void setDirtyAround(Region3i region) { Vector3i minPos = new Vector3i(region.min()); minPos.sub(1, 1, 1); Vector3i maxPos = new Vector3i(region.max()); maxPos.add(1, 1, 1); Vector3i minChunk = ChunkMath.calcChunkPos(minPos, chunkPower); Vector3i maxChunk = ChunkMath.calcChunkPos(maxPos, chunkPower); for (Vector3i pos : Region3i.createFromMinMax(minChunk, maxChunk)) { chunks[pos.x + offset.x + chunkRegion.size().x * (pos.z + offset.z)].setDirty(true); } }
@Test public void testCreateRegionWithMinAndSize() { List<Vector3i> mins = Arrays.asList(new Vector3i(), new Vector3i(1, 1, 1), new Vector3i(3, 4, 5)); List<Vector3i> size = Arrays.asList(new Vector3i(1, 1, 1), new Vector3i(3, 3, 3), new Vector3i(8, 5, 2)); List<Vector3i> expectedMax = Arrays.asList(new Vector3i(), new Vector3i(3, 3, 3), new Vector3i(10, 8, 6)); for (int i = 0; i < mins.size(); ++i) { Region3i region = Region3i.createFromMinAndSize(mins.get(i), size.get(i)); assertEquals(mins.get(i), region.min()); assertEquals(size.get(i), region.size()); assertEquals(expectedMax.get(i), region.max()); assertFalse(region.isEmpty()); } }
private void renderOverlayForOneBlockSelection(BlockSelectionComponent blockSelectionComponent, BlockSelectionRenderer selectionRenderer) { selectionRenderer.beginRenderOverlay(); if (blockSelectionComponent.currentSelection == null) { if (blockSelectionComponent.startPosition != null) { selectionRenderer.renderMark(blockSelectionComponent.startPosition); } } else { Vector3i size = blockSelectionComponent.currentSelection.size(); Vector3i block = new Vector3i(); for (int z = 0; z < size.z; z++) { for (int y = 0; y < size.y; y++) { for (int x = 0; x < size.x; x++) { block.set(x, y, z); block.add(blockSelectionComponent.currentSelection.min()); selectionRenderer.renderMark(block); } } } } selectionRenderer.endRenderOverlay(); }