Search in sources :

Example 21 with EntityRef

use of org.terasology.engine.entitySystem.entity.EntityRef in project Terasology by MovingBlocks.

the class BehaviorSystem method update.

@Override
public void update(float delta) {
    Iterable<EntityRef> entities = entityManager.getEntitiesWith(BehaviorComponent.class);
    for (EntityRef entity : entities) {
        BehaviorComponent behaviorComponent = entity.getComponent(BehaviorComponent.class);
        if (behaviorComponent.interpreter == null) {
            // NPE observed in the past, suspected to be about loss of behavior state. Hopefully one skip is OK then
            // resume next tick?
            // TODO: Highlight this log entry to the telemetry system to gather better data over time
            logger.warn("Found a null interpreter during tick updates, skipping for entity {}", entity);
            continue;
        }
        behaviorComponent.interpreter.tick(delta);
    }
}
Also used : EntityRef(org.terasology.engine.entitySystem.entity.EntityRef)

Example 22 with EntityRef

use of org.terasology.engine.entitySystem.entity.EntityRef in project Terasology by MovingBlocks.

the class AfkClientSystem method disable.

private boolean disable() {
    EntityRef clientEntity = localPlayer.getClientEntity();
    AfkComponent component = clientEntity.getComponent(AfkComponent.class);
    if (component != null && component.afk) {
        component.afk = false;
        clientEntity.addOrSaveComponent(component);
        AfkRequest request = new AfkRequest(clientEntity, false);
        clientEntity.send(request);
        return true;
    }
    return false;
}
Also used : EntityRef(org.terasology.engine.entitySystem.entity.EntityRef)

Example 23 with EntityRef

use of org.terasology.engine.entitySystem.entity.EntityRef in project Terasology by MovingBlocks.

the class BulletPhysics method rayTrace.

@Override
public HitResult rayTrace(Vector3f from, Vector3f direction, float distance, Set<EntityRef> excludedEntities, CollisionGroup... collisionGroups) {
    if (excludedEntities == null || excludedEntities.size() == 0) {
        return rayTrace(from, direction, distance, collisionGroups);
    }
    Vector3f to = new Vector3f(direction);
    to.normalize();
    to.mul(distance);
    to.add(from);
    short filter = combineGroups(collisionGroups);
    // lookup all the collision item ids for these entities
    Set<Integer> excludedCollisionIds = Sets.newHashSet();
    for (EntityRef excludedEntity : excludedEntities) {
        if (entityRigidBodies.containsKey(excludedEntity)) {
            excludedCollisionIds.add(entityRigidBodies.get(excludedEntity).rb.getBroadphaseHandle().getUid());
        }
        if (entityColliders.containsKey(excludedEntity)) {
            excludedCollisionIds.add(entityColliders.get(excludedEntity).collider.getBroadphaseHandle().getUid());
        }
        if (entityTriggers.containsKey(excludedEntity)) {
            excludedCollisionIds.add(entityTriggers.get(excludedEntity).getBroadphaseHandle().getUid());
        }
    }
    AllHitsRayResultCallback callback = new AllHitsRayResultCallback(from, to);
    callback.setCollisionFilterGroup(StandardCollisionGroup.ALL.getFlag());
    callback.setCollisionFilterMask(filter);
    discreteDynamicsWorld.rayTest(from, to, callback);
    if (callback.hasHit()) {
        btCollisionObjectConstArray collisionObjects = callback.getCollisionObjects();
        for (int x = 0; x < collisionObjects.size(); x++) {
            btCollisionObject collisionObject = collisionObjects.atConst(x);
            if (!excludedCollisionIds.contains(collisionObject.getBroadphaseHandle().getUid())) {
                Vector3f hitPointWorld = callback.getHitPointWorld().at(x);
                Vector3f hitNormalWorld = callback.getHitNormalWorld().at(x);
                callback.dispose();
                if (collisionObject.userData instanceof EntityRef) {
                    // we hit an other entity
                    return new HitResult((EntityRef) collisionObject.userData, hitPointWorld, hitNormalWorld);
                } else if ((collisionObject.getCollisionFlags() & btCollisionObject.CollisionFlags.CF_VOXEL_OBJECT) > 0) {
                    btVector3i pos = new btVector3i();
                    collisionObject.getVoxelPosition(pos);
                    Vector3i voxelPosition = new Vector3i(pos.getX(), pos.getY(), pos.getZ());
                    final EntityRef entityAt = blockEntityRegistry.getEntityAt(voxelPosition);
                    return new HitResult(entityAt, hitPointWorld, hitNormalWorld, voxelPosition);
                } else {
                    // we hit something we don't understand, assume its nothing and log a warning
                    logger.warn("Unidentified object was hit in the physics engine: {}", collisionObject.userData);
                }
            }
        }
    } else {
        callback.dispose();
    }
    return new HitResult();
}
Also used : HitResult(org.terasology.engine.physics.HitResult) com.badlogic.gdx.physics.bullet.collision.btCollisionObjectConstArray(com.badlogic.gdx.physics.bullet.collision.btCollisionObjectConstArray) Vector3f(org.joml.Vector3f) Vector3i(org.joml.Vector3i) com.badlogic.gdx.physics.bullet.collision.btVector3i(com.badlogic.gdx.physics.bullet.collision.btVector3i) com.badlogic.gdx.physics.bullet.collision.btVector3i(com.badlogic.gdx.physics.bullet.collision.btVector3i) EntityRef(org.terasology.engine.entitySystem.entity.EntityRef) AllHitsRayResultCallback(com.badlogic.gdx.physics.bullet.collision.AllHitsRayResultCallback) com.badlogic.gdx.physics.bullet.collision.btManifoldPoint(com.badlogic.gdx.physics.bullet.collision.btManifoldPoint) com.badlogic.gdx.physics.bullet.collision.btCollisionObject(com.badlogic.gdx.physics.bullet.collision.btCollisionObject)

Example 24 with EntityRef

use of org.terasology.engine.entitySystem.entity.EntityRef in project Terasology by MovingBlocks.

the class BulletPhysics method rayTrace.

@Override
public HitResult rayTrace(Vector3f from, Vector3f direction, float distance, CollisionGroup... collisionGroups) {
    Vector3f to = new Vector3f(direction);
    to.normalize();
    to.mul(distance);
    to.add(from);
    short filter = combineGroups(collisionGroups);
    ClosestRayResultCallback callback = new ClosestRayResultCallback(from, to);
    callback.setCollisionFilterGroup(StandardCollisionGroup.ALL.getFlag());
    callback.setCollisionFilterMask(filter);
    discreteDynamicsWorld.rayTest(from, to, callback);
    if (callback.hasHit()) {
        btCollisionObject collisionObject = callback.getCollisionObject();
        Vector3f hitPointWorld = new Vector3f();
        callback.getHitPointWorld(hitPointWorld);
        Vector3f hitNormalWorld = new Vector3f();
        callback.getHitNormalWorld(hitNormalWorld);
        if (callback.hasHit()) {
            callback.dispose();
            if (collisionObject.userData instanceof EntityRef) {
                // we hit an other entity
                return new HitResult((EntityRef) collisionObject.userData, hitPointWorld, hitNormalWorld);
            } else if ((collisionObject.getCollisionFlags() & btCollisionObject.CollisionFlags.CF_VOXEL_OBJECT) > 0) {
                btVector3i pos = new btVector3i();
                collisionObject.getVoxelPosition(pos);
                Vector3i voxelPosition = new Vector3i(pos.getX(), pos.getY(), pos.getZ());
                final EntityRef entityAt = blockEntityRegistry.getEntityAt(voxelPosition);
                return new HitResult(entityAt, hitPointWorld, hitNormalWorld, voxelPosition);
            } else {
                // we hit something we don't understand, assume its nothing and log a warning
                logger.warn("Unidentified object was hit in the physics engine: {}", collisionObject.userData);
            }
        }
    } else {
        callback.dispose();
    }
    return new HitResult();
}
Also used : HitResult(org.terasology.engine.physics.HitResult) Vector3f(org.joml.Vector3f) Vector3i(org.joml.Vector3i) com.badlogic.gdx.physics.bullet.collision.btVector3i(com.badlogic.gdx.physics.bullet.collision.btVector3i) com.badlogic.gdx.physics.bullet.collision.btVector3i(com.badlogic.gdx.physics.bullet.collision.btVector3i) EntityRef(org.terasology.engine.entitySystem.entity.EntityRef) com.badlogic.gdx.physics.bullet.collision.btCollisionObject(com.badlogic.gdx.physics.bullet.collision.btCollisionObject) ClosestRayResultCallback(com.badlogic.gdx.physics.bullet.collision.ClosestRayResultCallback)

Example 25 with EntityRef

use of org.terasology.engine.entitySystem.entity.EntityRef in project Terasology by MovingBlocks.

the class ReadWriteStorageManager method deactivateChunk.

@Override
public void deactivateChunk(Chunk chunk) {
    Collection<EntityRef> entitiesOfChunk = getEntitiesOfChunk(chunk);
    // storage manager only works with ChunkImpl
    ChunkImpl chunkImpl = (ChunkImpl) chunk;
    unloadedAndUnsavedChunkMap.put(chunk.getPosition(new Vector3i()), new CompressedChunkBuilder(getEntityManager(), chunkImpl, entitiesOfChunk, true));
    entitiesOfChunk.forEach(this::deactivateOrDestroyEntityRecursive);
}
Also used : ChunkImpl(org.terasology.engine.world.chunks.internal.ChunkImpl) Vector3i(org.joml.Vector3i) EntityRef(org.terasology.engine.entitySystem.entity.EntityRef)

Aggregations

EntityRef (org.terasology.engine.entitySystem.entity.EntityRef)298 Test (org.junit.jupiter.api.Test)88 ClientComponent (org.terasology.engine.network.ClientComponent)55 Vector3f (org.joml.Vector3f)51 LocationComponent (org.terasology.engine.logic.location.LocationComponent)44 Vector3i (org.joml.Vector3i)36 Command (org.terasology.engine.logic.console.commandSystem.annotations.Command)34 ReceiveEvent (org.terasology.engine.entitySystem.event.ReceiveEvent)29 StringComponent (org.terasology.unittest.stubs.StringComponent)26 NetworkComponent (org.terasology.engine.network.NetworkComponent)23 EntityData (org.terasology.protobuf.EntityData)23 Quaternionf (org.joml.Quaternionf)19 DisplayNameComponent (org.terasology.engine.logic.common.DisplayNameComponent)19 Component (org.terasology.gestalt.entitysystem.component.Component)19 CharacterComponent (org.terasology.engine.logic.characters.CharacterComponent)15 Map (java.util.Map)14 EntityBuilder (org.terasology.engine.entitySystem.entity.EntityBuilder)13 BlockComponent (org.terasology.engine.world.block.BlockComponent)13 Block (org.terasology.engine.world.block.Block)11 Prefab (org.terasology.engine.entitySystem.prefab.Prefab)10