Search in sources :

Example 61 with LocationComponent

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

the class BulletPhysics method newTrigger.

// *******************Private helper methods**************************\\
/**
 * Creates a new trigger.
 *
 * @param entity the entity to create a trigger for.
 */
private boolean newTrigger(EntityRef entity) {
    LocationComponent location = entity.getComponent(LocationComponent.class);
    TriggerComponent trigger = entity.getComponent(TriggerComponent.class);
    btCollisionShape shape = getShapeFor(entity);
    if (shape != null && location != null && trigger != null) {
        float scale = location.getWorldScale();
        shape.setLocalScaling(new Vector3f(scale, scale, scale));
        List<CollisionGroup> detectGroups = Lists.newArrayList(trigger.detectGroups);
        CollisionGroup collisionGroup = trigger.collisionGroup;
        btPairCachingGhostObject triggerObj = createCollider(location.getWorldPosition(new Vector3f()), shape, collisionGroup.getFlag(), combineGroups(detectGroups), btCollisionObject.CollisionFlags.CF_NO_CONTACT_RESPONSE);
        triggerObj.userData = entity;
        btPairCachingGhostObject oldTrigger = entityTriggers.put(entity, triggerObj);
        if (oldTrigger != null) {
            logger.warn("Creating a trigger for an entity that already has a trigger. " + "Multiple trigger pre entity are not supported. Removing old one. Entity: {}", entity);
            removeCollider(oldTrigger);
            return false;
        } else {
            return true;
        }
    } else {
        logger.warn("Trying to create trigger for entity without ShapeComponent or without LocationComponent " + "or without TriggerComponent. Entity: {}", entity);
        return false;
    }
}
Also used : StandardCollisionGroup(org.terasology.engine.physics.StandardCollisionGroup) CollisionGroup(org.terasology.engine.physics.CollisionGroup) Vector3f(org.joml.Vector3f) LocationComponent(org.terasology.engine.logic.location.LocationComponent) com.badlogic.gdx.physics.bullet.collision.btPairCachingGhostObject(com.badlogic.gdx.physics.bullet.collision.btPairCachingGhostObject) TriggerComponent(org.terasology.engine.physics.components.TriggerComponent) com.badlogic.gdx.physics.bullet.collision.btCollisionShape(com.badlogic.gdx.physics.bullet.collision.btCollisionShape)

Example 62 with LocationComponent

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

the class EntityMotionState method setWorldTransform.

@Override
public void setWorldTransform(Matrix4f transform) {
    LocationComponent loc = entity.getComponent(LocationComponent.class);
    if (loc != null) {
        rot.setFromNormalized(transform);
        rot.normalize();
        transform.getTranslation(position);
        loc.setWorldRotation(rot);
        loc.setWorldPosition(position);
    }
}
Also used : LocationComponent(org.terasology.engine.logic.location.LocationComponent)

Example 63 with LocationComponent

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

the class FloatingTextRenderer method render.

private void render(Iterable<EntityRef> floatingTextEntities) {
    Vector3fc cameraPosition = camera.getPosition();
    Matrix4f model = new Matrix4f();
    Matrix4f modelView = new Matrix4f();
    Vector3f worldPos = new Vector3f();
    for (EntityRef entity : floatingTextEntities) {
        FloatingTextComponent floatingText = entity.getComponent(FloatingTextComponent.class);
        LocationComponent location = entity.getComponent(LocationComponent.class);
        if (location == null) {
            logger.warn("location component is not defined can't render text: {}", floatingText.text);
            continue;
        }
        location.getWorldPosition(worldPos);
        if (!worldProvider.isBlockRelevant(worldPos) || !worldPos.isFinite()) {
            continue;
        }
        String[] linesOfText = floatingText.text.split("\n");
        Color baseColor = floatingText.textColor;
        Color shadowColor = floatingText.textShadowColor;
        boolean underline = false;
        int textWidth = 0;
        for (String singleLine : linesOfText) {
            if (font.getWidth(singleLine) > textWidth) {
                textWidth = font.getWidth(singleLine);
            }
        }
        FontMeshBuilder meshBuilder = new FontMeshBuilder(underlineMaterial);
        Map<Material, Mesh> meshMap = entityMeshCache.get(entity);
        if (meshMap == null) {
            meshMap = meshBuilder.createTextMesh(font, Arrays.asList(linesOfText), textWidth, HorizontalAlign.CENTER, baseColor, shadowColor, underline);
            entityMeshCache.put(entity, meshMap);
        }
        if (floatingText.isOverlay) {
            glDisable(GL_DEPTH_TEST);
        }
        float scale = METER_PER_PIXEL * floatingText.scale;
        model.setTranslation(worldPos.sub(cameraPosition));
        modelView.set(camera.getViewMatrix()).mul(model).m00(1.0f).m10(0.0f).m20(0.0f).m01(0.0f).m11(1.0f).m21(0.0f).m02(0.0f).m12(0.0f).m22(1.0f);
        modelView.scale(scale, -scale, scale);
        modelView.translate(-textWidth / 2.0f, 0.0f, 0.0f);
        for (Map.Entry<Material, Mesh> meshMapEntry : meshMap.entrySet()) {
            Mesh mesh = meshMapEntry.getValue();
            Material material = meshMapEntry.getKey();
            material.enable();
            material.bindTextures();
            material.setFloat4("croppingBoundaries", Float.MIN_VALUE, Float.MAX_VALUE, Float.MIN_VALUE, Float.MAX_VALUE);
            material.setMatrix4("modelViewMatrix", modelView);
            material.setMatrix4("projectionMatrix", camera.getProjectionMatrix());
            material.setFloat2("offset", 0.0f, 0.0f);
            material.setFloat("alpha", 1.0f);
            mesh.render();
        }
        // Revert to default state
        if (floatingText.isOverlay) {
            glEnable(GL_DEPTH_TEST);
        }
    }
}
Also used : Color(org.terasology.nui.Color) Mesh(org.terasology.engine.rendering.assets.mesh.Mesh) Material(org.terasology.engine.rendering.assets.material.Material) LocationComponent(org.terasology.engine.logic.location.LocationComponent) Vector3fc(org.joml.Vector3fc) Matrix4f(org.joml.Matrix4f) FontMeshBuilder(org.terasology.engine.rendering.assets.font.FontMeshBuilder) Vector3f(org.joml.Vector3f) EntityRef(org.terasology.engine.entitySystem.entity.EntityRef) Map(java.util.Map)

Example 64 with LocationComponent

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

the class SkeletonRenderer method newSkeleton.

@ReceiveEvent(components = { SkeletalMeshComponent.class, LocationComponent.class })
public void newSkeleton(OnActivatedComponent event, EntityRef entity) {
    SkeletalMeshComponent skeleton = entity.getComponent(SkeletalMeshComponent.class);
    if (skeleton.mesh == null) {
        return;
    }
    if (skeleton.boneEntities == null) {
        skeleton.boneEntities = Maps.newHashMap();
        for (Bone bone : skeleton.mesh.getBones()) {
            LocationComponent loc = new LocationComponent();
            EntityRef boneEntity = entityManager.create(loc);
            skeleton.boneEntities.put(bone.getName(), boneEntity);
        }
    }
    for (Bone bone : skeleton.mesh.getBones()) {
        EntityRef boneEntity = skeleton.boneEntities.get(bone.getName());
        EntityRef parent = (bone.getParent() != null) ? skeleton.boneEntities.get(bone.getParent().getName()) : entity;
        Location.attachChild(parent, boneEntity);
    }
    for (Bone bone : skeleton.mesh.getBones()) {
        EntityRef boneEntity = skeleton.boneEntities.get(bone.getName());
        LocationComponent loc = boneEntity.getComponent(LocationComponent.class);
        loc.setLocalPosition(bone.getLocalPosition());
        loc.setLocalRotation(bone.getLocalRotation());
        loc.setLocalScale(bone.getLocalScale().x);
        boneEntity.saveComponent(loc);
        if (bone.getParent() == null) {
            skeleton.rootBone = boneEntity;
        }
    }
    entity.saveComponent(skeleton);
}
Also used : Bone(org.terasology.engine.rendering.assets.skeletalmesh.Bone) LocationComponent(org.terasology.engine.logic.location.LocationComponent) EntityRef(org.terasology.engine.entitySystem.entity.EntityRef) ReceiveEvent(org.terasology.engine.entitySystem.event.ReceiveEvent)

Example 65 with LocationComponent

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

the class EntityBasedRenderableChunk method hasMesh.

@Override
public boolean hasMesh() {
    ChunkMeshComponent mesh = entity.getComponent(ChunkMeshComponent.class);
    LocationComponent location = entity.getComponent(LocationComponent.class);
    return mesh != null && location != null && mesh.mesh != null;
}
Also used : 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