Search in sources :

Example 1 with LocationComponent

use of org.terasology.engine.logic.location.LocationComponent in project Terasology by MovingBlocks.

the class StorageManagerTest method testPlayerRelevanceLocationSurvivesStorage.

@Test
public void testPlayerRelevanceLocationSurvivesStorage() {
    Vector3f loc = new Vector3f(1, 2, 3);
    character.addComponent(new LocationComponent(loc));
    esm.waitForCompletionOfPreviousSaveAndStartSaving();
    esm.finishSavingAndShutdown();
    PlayerStore restored = esm.loadPlayerStore(PLAYER_ID);
    assertEquals(loc, restored.getRelevanceLocation());
}
Also used : PlayerStore(org.terasology.engine.persistence.PlayerStore) Vector3f(org.joml.Vector3f) LocationComponent(org.terasology.engine.logic.location.LocationComponent) Test(org.junit.jupiter.api.Test)

Example 2 with LocationComponent

use of org.terasology.engine.logic.location.LocationComponent in project Terasology by MovingBlocks.

the class StorageManagerTest method testEntitySurvivesStorageInChunkStore.

@Test
public void testEntitySurvivesStorageInChunkStore() throws Exception {
    Chunk chunk = new ChunkImpl(CHUNK_POS, blockManager, extraDataManager);
    chunk.setBlock(0, 0, 0, testBlock);
    chunk.markReady();
    ChunkProvider chunkProvider = mock(ChunkProvider.class);
    when(chunkProvider.getAllChunks()).thenReturn(Arrays.asList(chunk));
    CoreRegistry.put(ChunkProvider.class, chunkProvider);
    EntityRef entity = entityManager.create();
    long id = entity.getId();
    LocationComponent locationComponent = new LocationComponent();
    AABBfc aabb = chunk.getAABB();
    Vector3f positionInChunk = new Vector3f(aabb.minX(), aabb.minY(), aabb.minZ());
    positionInChunk.x += 1;
    positionInChunk.y += 1;
    positionInChunk.z += 1;
    locationComponent.setWorldPosition(positionInChunk);
    entity.addComponent(locationComponent);
    esm.waitForCompletionOfPreviousSaveAndStartSaving();
    esm.finishSavingAndShutdown();
    EntitySystemSetupUtil.addReflectionBasedLibraries(context);
    EntitySystemSetupUtil.addEntityManagementRelatedClasses(context);
    EngineEntityManager newEntityManager = context.get(EngineEntityManager.class);
    StorageManager newSM = new ReadWriteStorageManager(savePath, moduleEnvironment, newEntityManager, blockManager, extraDataManager, false, recordAndReplaySerializer, recordAndReplayUtils, recordAndReplayCurrentStatus);
    newSM.loadGlobalStore();
    ChunkStore restored = newSM.loadChunkStore(CHUNK_POS);
    restored.restoreEntities();
    EntityRef ref = newEntityManager.getEntity(id);
    assertTrue(ref.exists());
    assertTrue(ref.isActive());
}
Also used : AABBfc(org.terasology.joml.geom.AABBfc) EngineEntityManager(org.terasology.engine.entitySystem.entity.internal.EngineEntityManager) ChunkImpl(org.terasology.engine.world.chunks.internal.ChunkImpl) Vector3f(org.joml.Vector3f) StorageManager(org.terasology.engine.persistence.StorageManager) Chunk(org.terasology.engine.world.chunks.Chunk) ChunkProvider(org.terasology.engine.world.chunks.ChunkProvider) EntityRef(org.terasology.engine.entitySystem.entity.EntityRef) LocationComponent(org.terasology.engine.logic.location.LocationComponent) ChunkStore(org.terasology.engine.persistence.ChunkStore) Test(org.junit.jupiter.api.Test)

Example 3 with LocationComponent

use of org.terasology.engine.logic.location.LocationComponent in project Terasology by MovingBlocks.

the class BulletPhysics method newRigidBody.

private RigidBody newRigidBody(EntityRef entity) {
    LocationComponent location = entity.getComponent(LocationComponent.class);
    RigidBodyComponent rigidBody = entity.getComponent(RigidBodyComponent.class);
    btCollisionShape shape = getShapeFor(entity);
    if (location != null && rigidBody != null && shape != null) {
        float scale = location.getWorldScale();
        shape.setLocalScaling(new Vector3f(scale, scale, scale));
        if (rigidBody.mass < 1) {
            logger.warn("RigidBodyComponent.mass is set to less than 1.0, this can lead to strange behaviour, " + "such as the objects moving through walls. " + "Entity: {}", entity);
        }
        Vector3f inertia = new Vector3f();
        shape.calculateLocalInertia(rigidBody.mass, inertia);
        btRigidBody.btRigidBodyConstructionInfo info = new btRigidBody.btRigidBodyConstructionInfo(rigidBody.mass, new EntityMotionState(entity), shape, inertia);
        BulletRigidBody collider = new BulletRigidBody(info);
        collider.rb.userData = entity;
        collider.rb.setAngularFactor(rigidBody.angularFactor);
        collider.rb.setLinearFactor(rigidBody.linearFactor);
        collider.rb.setFriction(rigidBody.friction);
        collider.collidesWith = combineGroups(rigidBody.collidesWith);
        collider.setVelocity(rigidBody.velocity, rigidBody.angularVelocity);
        collider.setTransform(location.getWorldPosition(new Vector3f()), location.getWorldRotation(new Quaternionf()));
        updateKinematicSettings(rigidBody, collider);
        BulletRigidBody oldBody = entityRigidBodies.put(entity, collider);
        addRigidBody(collider, Lists.newArrayList(rigidBody.collisionGroup), rigidBody.collidesWith);
        if (oldBody != null) {
            removeRigidBody(oldBody);
        }
        return collider;
    } else {
        throw new IllegalArgumentException("Can only create a new rigid body for entities with a " + "LocationComponent," + " RigidBodyComponent and ShapeComponent, this entity misses at least one: " + entity);
    }
}
Also used : com.badlogic.gdx.physics.bullet.dynamics.btRigidBody(com.badlogic.gdx.physics.bullet.dynamics.btRigidBody) RigidBodyComponent(org.terasology.engine.physics.components.RigidBodyComponent) Vector3f(org.joml.Vector3f) Quaternionf(org.joml.Quaternionf) LocationComponent(org.terasology.engine.logic.location.LocationComponent) com.badlogic.gdx.physics.bullet.collision.btCollisionShape(com.badlogic.gdx.physics.bullet.collision.btCollisionShape)

Example 4 with LocationComponent

use of org.terasology.engine.logic.location.LocationComponent in project Terasology by MovingBlocks.

the class BulletPhysics method createCharacterCollider.

private CharacterCollider createCharacterCollider(EntityRef owner) {
    LocationComponent locComp = owner.getComponent(LocationComponent.class);
    CharacterMovementComponent movementComp = owner.getComponent(CharacterMovementComponent.class);
    if (locComp == null || movementComp == null) {
        throw new IllegalArgumentException("Expected an entity with a Location component and " + "CharacterMovementComponent.");
    }
    Vector3f pos = locComp.getWorldPosition(new Vector3f());
    final float worldScale = locComp.getWorldScale();
    final float height = (movementComp.height - 2 * movementComp.radius) * worldScale;
    final float width = movementComp.radius * worldScale;
    btConvexShape shape = new btCapsuleShape(width, height);
    shapes.add(shape);
    shape.setMargin(0.1f);
    return createCustomCollider(pos, shape, movementComp.collisionGroup.getFlag(), combineGroups(movementComp.collidesWith), btCollisionObject.CollisionFlags.CF_CHARACTER_OBJECT, owner);
}
Also used : com.badlogic.gdx.physics.bullet.collision.btConvexShape(com.badlogic.gdx.physics.bullet.collision.btConvexShape) Vector3f(org.joml.Vector3f) com.badlogic.gdx.physics.bullet.collision.btCapsuleShape(com.badlogic.gdx.physics.bullet.collision.btCapsuleShape) CharacterMovementComponent(org.terasology.engine.logic.characters.CharacterMovementComponent) LocationComponent(org.terasology.engine.logic.location.LocationComponent)

Example 5 with LocationComponent

use of org.terasology.engine.logic.location.LocationComponent in project Terasology by MovingBlocks.

the class BulletPhysics method updateRigidBody.

@Override
public boolean updateRigidBody(EntityRef entity) {
    LocationComponent location = entity.getComponent(LocationComponent.class);
    RigidBodyComponent rb = entity.getComponent(RigidBodyComponent.class);
    BulletRigidBody rigidBody = entityRigidBodies.get(entity);
    if (location == null) {
        logger.warn("Updating rigid body of entity that has no " + "LocationComponent?! Nothing is done, except log this" + " warning instead. Entity: {}", entity);
        return false;
    } else if (rigidBody != null) {
        float scale = location.getWorldScale();
        if (Math.abs(rigidBody.rb.getCollisionShape().getLocalScaling().x - scale) > this.getEpsilon() || rigidBody.collidesWith != combineGroups(rb.collidesWith)) {
            removeRigidBody(rigidBody);
            newRigidBody(entity);
        } else {
            rigidBody.rb.setAngularFactor(rb.angularFactor);
            rigidBody.rb.setLinearFactor(rb.linearFactor);
            rigidBody.rb.setFriction(rb.friction);
        }
        return true;
    } else {
        /*
             * During the destruction of the entity it can happen that the rigged body is already destroyed while
             * the location component changes.
             * e.g. because another component that was attached via the LocationComponent gets removed.
             *
             * In such a situation it would be wrong to recreate the rigid body as it can't be updated properly after
             * the destruction of the entity.
             *
             */
        return false;
    }
// TODO: update if mass or collision groups change
}
Also used : RigidBodyComponent(org.terasology.engine.physics.components.RigidBodyComponent) LocationComponent(org.terasology.engine.logic.location.LocationComponent)

Aggregations

LocationComponent (org.terasology.engine.logic.location.LocationComponent)65 Vector3f (org.joml.Vector3f)46 EntityRef (org.terasology.engine.entitySystem.entity.EntityRef)40 Quaternionf (org.joml.Quaternionf)18 ClientComponent (org.terasology.engine.network.ClientComponent)17 ReceiveEvent (org.terasology.engine.entitySystem.event.ReceiveEvent)9 Command (org.terasology.engine.logic.console.commandSystem.annotations.Command)9 Matrix4f (org.joml.Matrix4f)7 Vector3i (org.joml.Vector3i)6 CharacterTeleportEvent (org.terasology.engine.logic.characters.CharacterTeleportEvent)5 BlockFamily (org.terasology.engine.world.block.family.BlockFamily)5 Vector3fc (org.joml.Vector3fc)4 EntityBuilder (org.terasology.engine.entitySystem.entity.EntityBuilder)4 CharacterHeldItemComponent (org.terasology.engine.logic.characters.CharacterHeldItemComponent)4 DisplayNameComponent (org.terasology.engine.logic.common.DisplayNameComponent)4 ConsoleCommand (org.terasology.engine.logic.console.commandSystem.ConsoleCommand)4 DropItemEvent (org.terasology.engine.logic.inventory.events.DropItemEvent)4 Bone (org.terasology.engine.rendering.assets.skeletalmesh.Bone)4 BlockItemFactory (org.terasology.engine.world.block.items.BlockItemFactory)4 Component (org.terasology.gestalt.entitysystem.component.Component)4