use of org.terasology.math.geom.Vector3i in project Terasology by MovingBlocks.
the class NetEntityRefTypeHandler method serialize.
@Override
public PersistedData serialize(EntityRef value, SerializationContext context) {
BlockComponent blockComponent = value.getComponent(BlockComponent.class);
if (blockComponent != null) {
Vector3i pos = blockComponent.getPosition();
return context.create(pos.x, pos.y, pos.z);
}
NetworkComponent netComponent = value.getComponent(NetworkComponent.class);
if (netComponent != null) {
return context.create(netComponent.getNetworkId());
}
return context.createNull();
}
use of org.terasology.math.geom.Vector3i in project Terasology by MovingBlocks.
the class AbstractStorageManager method loadChunkZip.
protected byte[] loadChunkZip(Vector3i chunkPos) {
byte[] chunkData = null;
Vector3i chunkZipPos = storagePathProvider.getChunkZipPosition(chunkPos);
Path chunkPath = storagePathProvider.getChunkZipPath(chunkZipPos);
if (Files.isRegularFile(chunkPath)) {
try (FileSystem chunkZip = FileSystems.newFileSystem(chunkPath, null)) {
Path targetChunk = chunkZip.getPath(storagePathProvider.getChunkFilename(chunkPos));
if (Files.isRegularFile(targetChunk)) {
chunkData = Files.readAllBytes(targetChunk);
}
} catch (IOException e) {
logger.error("Failed to load chunk zip {}", chunkPath, e);
}
}
return chunkData;
}
use of org.terasology.math.geom.Vector3i in project Terasology by MovingBlocks.
the class BenchmarkScreen method getChunkRegionAbove.
static Region3i getChunkRegionAbove(Vector3f location) {
Vector3i charecterPos = new Vector3i(location);
Vector3i chunkAboveCharacter = ChunkMath.calcChunkPos(charecterPos);
chunkAboveCharacter.addY(1);
Vector3i chunkRelativePos = ChunkMath.calcBlockPos(charecterPos);
Vector3i characterChunkOriginPos = new Vector3i(charecterPos);
characterChunkOriginPos.sub(chunkRelativePos);
Vector3i chunkAboveOrigin = new Vector3i(characterChunkOriginPos);
chunkAboveOrigin.addY(ChunkConstants.CHUNK_SIZE.getY());
return ChunkConstants.CHUNK_REGION.move(chunkAboveOrigin);
}
use of org.terasology.math.geom.Vector3i in project Terasology by MovingBlocks.
the class DoorSystem method placeDoor.
@ReceiveEvent(components = { DoorComponent.class, ItemComponent.class })
public void placeDoor(ActivateEvent event, EntityRef entity) {
DoorComponent door = entity.getComponent(DoorComponent.class);
BlockComponent targetBlockComp = event.getTarget().getComponent(BlockComponent.class);
if (targetBlockComp == null) {
event.consume();
return;
}
Vector3f horizDir = new Vector3f(event.getDirection());
horizDir.y = 0;
Side facingDir = Side.inDirection(horizDir);
if (!facingDir.isHorizontal()) {
event.consume();
return;
}
Vector3f offset = new Vector3f(event.getHitPosition());
offset.sub(targetBlockComp.getPosition().toVector3f());
Side offsetDir = Side.inDirection(offset);
Vector3i primePos = new Vector3i(targetBlockComp.getPosition());
primePos.add(offsetDir.getVector3i());
Block primeBlock = worldProvider.getBlock(primePos);
if (!primeBlock.isReplacementAllowed()) {
event.consume();
return;
}
Block belowBlock = worldProvider.getBlock(primePos.x, primePos.y - 1, primePos.z);
Block aboveBlock = worldProvider.getBlock(primePos.x, primePos.y + 1, primePos.z);
// Determine top and bottom blocks
Vector3i bottomBlockPos;
Vector3i topBlockPos;
if (belowBlock.isReplacementAllowed()) {
bottomBlockPos = new Vector3i(primePos.x, primePos.y - 1, primePos.z);
topBlockPos = primePos;
} else if (aboveBlock.isReplacementAllowed()) {
bottomBlockPos = primePos;
topBlockPos = new Vector3i(primePos.x, primePos.y + 1, primePos.z);
} else {
event.consume();
return;
}
Side attachSide = determineAttachSide(facingDir, offsetDir, bottomBlockPos, topBlockPos);
if (attachSide == null) {
event.consume();
return;
}
Side closedSide = facingDir.reverse();
if (closedSide == attachSide || closedSide.reverse() == attachSide) {
closedSide = attachSide.yawClockwise(1);
}
Block newBottomBlock = door.bottomBlockFamily.getBlockForPlacement(worldProvider, blockEntityRegistry, bottomBlockPos, closedSide, Side.TOP);
Block newTopBlock = door.topBlockFamily.getBlockForPlacement(worldProvider, blockEntityRegistry, bottomBlockPos, closedSide, Side.TOP);
Map<Vector3i, Block> blockMap = new HashMap<>();
blockMap.put(bottomBlockPos, newBottomBlock);
blockMap.put(topBlockPos, newTopBlock);
PlaceBlocks blockEvent = new PlaceBlocks(blockMap, event.getInstigator());
worldProvider.getWorldEntity().send(blockEvent);
if (!blockEvent.isConsumed()) {
EntityRef newDoor = entityManager.create(door.doorRegionPrefab);
entity.removeComponent(MeshComponent.class);
newDoor.addComponent(new BlockRegionComponent(Region3i.createBounded(bottomBlockPos, topBlockPos)));
Vector3f doorCenter = bottomBlockPos.toVector3f();
doorCenter.y += 0.5f;
newDoor.addComponent(new LocationComponent(doorCenter));
DoorComponent newDoorComp = newDoor.getComponent(DoorComponent.class);
newDoorComp.closedSide = closedSide;
newDoorComp.openSide = attachSide.reverse();
newDoorComp.isOpen = false;
newDoor.saveComponent(newDoorComp);
newDoor.send(new PlaySoundEvent(Assets.getSound("engine:PlaceBlock").get(), 0.5f));
logger.info("Closed Side: {}", newDoorComp.closedSide);
logger.info("Open Side: {}", newDoorComp.openSide);
newDoor.send(new DoorPlacedEvent(event.getInstigator()));
}
}
use of org.terasology.math.geom.Vector3i in project Terasology by MovingBlocks.
the class SolidRasterizer method generateChunk.
@Override
public void generateChunk(CoreChunk chunk, Region chunkRegion) {
LiquidData waterLiquid = new LiquidData(LiquidType.WATER, LiquidData.MAX_LIQUID_DEPTH);
DensityFacet solidityFacet = chunkRegion.getFacet(DensityFacet.class);
SurfaceHeightFacet surfaceFacet = chunkRegion.getFacet(SurfaceHeightFacet.class);
SurfaceDepthFacet surfaceDepthFacet = chunkRegion.getFacet(SurfaceDepthFacet.class);
BiomeFacet biomeFacet = chunkRegion.getFacet(BiomeFacet.class);
SeaLevelFacet seaLevelFacet = chunkRegion.getFacet(SeaLevelFacet.class);
int seaLevel = seaLevelFacet.getSeaLevel();
Vector2i pos2d = new Vector2i();
for (Vector3i pos : ChunkConstants.CHUNK_REGION) {
pos2d.set(pos.x, pos.z);
int posY = pos.y + chunk.getChunkWorldOffsetY();
// Check for an optional depth for this layer - if defined stop generating below that level
if (surfaceDepthFacet != null && posY < surfaceDepthFacet.get(pos2d)) {
continue;
}
Biome biome = biomeFacet.get(pos2d);
chunk.setBiome(pos.x, pos.y, pos.z, biome);
float density = solidityFacet.get(pos);
if (density >= 32) {
chunk.setBlock(pos, stone);
} else if (density >= 0) {
int depth = TeraMath.floorToInt(surfaceFacet.get(pos2d)) - posY;
Block block = getSurfaceBlock(depth, posY, biome, seaLevel);
chunk.setBlock(pos, block);
} else {
// fill up terrain up to sealevel height with water or ice
if (posY == seaLevel && CoreBiome.SNOW == biome) {
chunk.setBlock(pos, ice);
} else if (posY <= seaLevel) {
// either OCEAN or SNOW
chunk.setBlock(pos, water);
chunk.setLiquid(pos, waterLiquid);
}
}
}
}
Aggregations