use of org.terasology.math.geom.Vector3i in project Terasology by MovingBlocks.
the class PlayerSystem method respawnPlayer.
private void respawnPlayer(EntityRef clientEntity) {
ClientComponent client = clientEntity.getComponent(ClientComponent.class);
EntityRef playerCharacter = client.character;
LocationComponent location = clientEntity.getComponent(LocationComponent.class);
PlayerFactory playerFactory = new PlayerFactory(entityManager, worldProvider);
Vector3f spawnPosition = playerFactory.findSpawnPositionFromLocationComponent(location);
location.setWorldPosition(spawnPosition);
clientEntity.saveComponent(location);
playerCharacter.addComponent(new AliveCharacterComponent());
playerCharacter.send(new CharacterTeleportEvent(spawnPosition));
logger.debug("Re-spawing player at: {}", spawnPosition);
Client clientListener = networkSystem.getOwner(clientEntity);
Vector3i distance = clientListener.getViewDistance().getChunkDistance();
updateRelevanceEntity(clientEntity, distance);
playerCharacter.send(new OnPlayerRespawnedEvent());
}
use of org.terasology.math.geom.Vector3i in project Terasology by MovingBlocks.
the class AbstractSpawner method findSpawnPosition.
/**
* Tries to find a suitable spawning point based on {@link SurfaceHeightFacet} and {@link SeaLevelFacet}.
* @param searchRadius the radius within a suitable spawning point will be searched
* @param world the facet-based world
* @param pos the desired 2D position in that world
* @return a 3D position above the surface and sea level or <code>null</code> if none was found
* @throws IllegalStateException if no SurfaceHeightFacet can be created.
*/
protected Vector3f findSpawnPosition(World world, Vector2i pos, int searchRadius) {
Vector3i ext = new Vector3i(searchRadius, 1, searchRadius);
Vector3i desiredPos = new Vector3i(pos.getX(), 1, pos.getY());
// try and find somewhere in this region a spot to land
Region3i spawnArea = Region3i.createFromCenterExtents(desiredPos, ext);
Region worldRegion = world.getWorldData(spawnArea);
// check if generation uses sea level and surface height facets
SurfaceHeightFacet surfaceHeightFacet = worldRegion.getFacet(SurfaceHeightFacet.class);
if (surfaceHeightFacet == null) {
throw new IllegalStateException("surface height facet not found");
}
SeaLevelFacet seaLevelFacet = worldRegion.getFacet(SeaLevelFacet.class);
int seaLevel = (seaLevelFacet != null) ? seaLevelFacet.getSeaLevel() : 0;
int spiralRad = searchRadius / 2 - 1;
SpiralIterable spiral = SpiralIterable.clockwise(pos).maxRadius(spiralRad).scale(2).build();
for (BaseVector2i test : spiral) {
float val = surfaceHeightFacet.getWorld(test.getX(), test.getY());
int height = TeraMath.floorToInt(val);
if (height >= seaLevel) {
return new Vector3f(test.getX(), height, test.getY());
}
}
// nothing above sea level found
float y = surfaceHeightFacet.getWorld(pos.getX(), pos.getY());
return new Vector3f(pos.getX(), y, pos.getY());
}
use of org.terasology.math.geom.Vector3i in project Terasology by MovingBlocks.
the class ChunkMath method calcChunkPos.
public static Vector3i[] calcChunkPos(Region3i region, Vector3i chunkPower) {
int minX = calcChunkPosX(region.minX(), chunkPower.x);
int minY = calcChunkPosY(region.minY(), chunkPower.y);
int minZ = calcChunkPosZ(region.minZ(), chunkPower.z);
int maxX = calcChunkPosX(region.maxX(), chunkPower.x);
int maxY = calcChunkPosY(region.maxY(), chunkPower.y);
int maxZ = calcChunkPosZ(region.maxZ(), chunkPower.z);
int size = (maxX - minX + 1) * (maxY - minY + 1) * (maxZ - minZ + 1);
Vector3i[] result = new Vector3i[size];
int index = 0;
for (int x = minX; x <= maxX; x++) {
for (int y = minY; y <= maxY; y++) {
for (int z = minZ; z <= maxZ; z++) {
result[index++] = new Vector3i(x, y, z);
}
}
}
return result;
}
use of org.terasology.math.geom.Vector3i in project Terasology by MovingBlocks.
the class ChunkMath method getEdgeRegion.
/**
* Produces a region containing the region touching the side of the given region, both in and outside the region.
*
* @param region
* @param side
* @return
*/
public static Region3i getEdgeRegion(Region3i region, Side side) {
Vector3i sideDir = side.getVector3i();
Vector3i min = region.min();
Vector3i max = region.max();
Vector3i edgeMin = new Vector3i(min);
Vector3i edgeMax = new Vector3i(max);
if (sideDir.x < 0) {
edgeMin.x = min.x;
edgeMax.x = min.x;
} else if (sideDir.x > 0) {
edgeMin.x = max.x;
edgeMax.x = max.x;
} else if (sideDir.y < 0) {
edgeMin.y = min.y;
edgeMax.y = min.y;
} else if (sideDir.y > 0) {
edgeMin.y = max.y;
edgeMax.y = max.y;
} else if (sideDir.z < 0) {
edgeMin.z = min.z;
edgeMax.z = min.z;
} else if (sideDir.z > 0) {
edgeMin.z = max.z;
edgeMax.z = max.z;
}
return Region3i.createFromMinMax(edgeMin, edgeMax);
}
use of org.terasology.math.geom.Vector3i in project Terasology by MovingBlocks.
the class Region3i method createEncompassing.
public static Region3i createEncompassing(Region3i a, Region3i b) {
if (a.isEmpty()) {
return b;
}
if (b.isEmpty()) {
return a;
}
Vector3i min = a.min();
min.min(b.min());
Vector3i max = a.max();
max.max(b.max());
return createFromMinMax(min, max);
}
Aggregations