Search in sources :

Example 31 with Vector3f

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());
}
Also used : CharacterTeleportEvent(org.terasology.logic.characters.CharacterTeleportEvent) Vector3f(org.terasology.math.geom.Vector3f) Vector3i(org.terasology.math.geom.Vector3i) Client(org.terasology.network.Client) ClientComponent(org.terasology.network.ClientComponent) EntityRef(org.terasology.entitySystem.entity.EntityRef) LocationComponent(org.terasology.logic.location.LocationComponent) OnPlayerRespawnedEvent(org.terasology.logic.players.event.OnPlayerRespawnedEvent) AliveCharacterComponent(org.terasology.logic.characters.AliveCharacterComponent)

Example 32 with Vector3f

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);
    }
}
Also used : Vector3f(org.terasology.math.geom.Vector3f) CharacterComponent(org.terasology.logic.characters.CharacterComponent) AliveCharacterComponent(org.terasology.logic.characters.AliveCharacterComponent) Client(org.terasology.network.Client) ClientComponent(org.terasology.network.ClientComponent) AliveCharacterComponent(org.terasology.logic.characters.AliveCharacterComponent) Quat4f(org.terasology.math.geom.Quat4f)

Example 33 with Vector3f

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());
}
Also used : SurfaceHeightFacet(org.terasology.world.generation.facets.SurfaceHeightFacet) Vector3f(org.terasology.math.geom.Vector3f) Vector3i(org.terasology.math.geom.Vector3i) BaseVector2i(org.terasology.math.geom.BaseVector2i) Region(org.terasology.world.generation.Region) SeaLevelFacet(org.terasology.world.generation.facets.SeaLevelFacet) SpiralIterable(org.terasology.math.geom.SpiralIterable) Region3i(org.terasology.math.Region3i)

Example 34 with Vector3f

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;
}
Also used : Vector3f(org.terasology.math.geom.Vector3f)

Example 35 with Vector3f

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;
}
Also used : Vector3f(org.terasology.math.geom.Vector3f)

Aggregations

Vector3f (org.terasology.math.geom.Vector3f)194 LocationComponent (org.terasology.logic.location.LocationComponent)45 EntityRef (org.terasology.entitySystem.entity.EntityRef)44 Quat4f (org.terasology.math.geom.Quat4f)33 ReceiveEvent (org.terasology.entitySystem.event.ReceiveEvent)26 Vector3i (org.terasology.math.geom.Vector3i)21 Test (org.junit.Test)20 ClientComponent (org.terasology.network.ClientComponent)15 Command (org.terasology.logic.console.commandSystem.annotations.Command)14 Matrix4f (org.terasology.math.geom.Matrix4f)13 BaseVector3f (org.terasology.math.geom.BaseVector3f)9 Block (org.terasology.world.block.Block)9 Vector2f (org.terasology.math.geom.Vector2f)8 HitResult (org.terasology.physics.HitResult)8 CharacterTeleportEvent (org.terasology.logic.characters.CharacterTeleportEvent)7 IOException (java.io.IOException)6 FloatBuffer (java.nio.FloatBuffer)6 EntityBuilder (org.terasology.entitySystem.entity.EntityBuilder)5 AABB (org.terasology.math.AABB)5 ChunkMesh (org.terasology.rendering.primitives.ChunkMesh)5