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());
}
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());
}
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);
}
}
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);
}
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
}
Aggregations