use of org.terasology.math.geom.Vector3f 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.Vector3f in project Terasology by MovingBlocks.
the class PlayerSystem method restoreCharacter.
private void restoreCharacter(EntityRef entity, EntityRef character) {
Client clientListener = networkSystem.getOwner(entity);
updateRelevanceEntity(entity, clientListener.getViewDistance().getChunkDistance());
ClientComponent client = entity.getComponent(ClientComponent.class);
client.character = character;
entity.saveComponent(client);
CharacterComponent characterComp = character.getComponent(CharacterComponent.class);
if (characterComp != null) {
characterComp.controller = entity;
character.saveComponent(characterComp);
character.setOwner(entity);
if (!character.hasComponent(AliveCharacterComponent.class)) {
character.addComponent(new AliveCharacterComponent());
}
Location.attachChild(character, entity, new Vector3f(), new Quat4f(0, 0, 0, 1));
} else {
character.destroy();
spawnPlayer(entity);
}
}
use of org.terasology.math.geom.Vector3f 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.Vector3f in project Terasology by MovingBlocks.
the class AABB method getFirstHitPlane.
public Vector3f getFirstHitPlane(Vector3f direction, Vector3f pos, Vector3f dimensions, boolean testX, boolean testY, boolean testZ) {
Vector3f hitNormal = new Vector3f();
float dist = Float.POSITIVE_INFINITY;
if (testX) {
float distX;
if (direction.x > 0) {
distX = (min.x - pos.x - dimensions.x) / direction.x;
} else {
distX = (max.x - pos.x + dimensions.x) / direction.x;
}
if (distX >= 0 && distX < dist) {
hitNormal.set(Math.copySign(1, direction.x), 0, 0);
}
}
if (testY) {
float distY;
if (direction.y > 0) {
distY = (min.y - pos.y - dimensions.y) / direction.y;
} else {
distY = (max.y - pos.y + dimensions.y) / direction.y;
}
if (distY >= 0 && distY < dist) {
hitNormal.set(0, Math.copySign(1, direction.y), 0);
}
}
if (testZ) {
float distZ;
if (direction.z > 0) {
distZ = (min.z - pos.z - dimensions.z) / direction.z;
} else {
distZ = (max.z - pos.z + dimensions.z) / direction.z;
}
if (distZ >= 0 && distZ < dist) {
hitNormal.set(0, 0, Math.copySign(1, direction.z));
}
}
return hitNormal;
}
use of org.terasology.math.geom.Vector3f in project Terasology by MovingBlocks.
the class AABB method getCenter.
public Vector3f getCenter() {
Vector3f dimensions = new Vector3f(max);
dimensions.add(min);
dimensions.scale(HALVING_FACTOR);
return dimensions;
}
Aggregations