Search in sources :

Example 1 with CharacterHeldItemComponent

use of org.terasology.engine.logic.characters.CharacterHeldItemComponent in project Terasology by MovingBlocks.

the class ThirdPersonRemoteClientSystem method update.

/**
 * Modifies the remote players' held item mount points to show and move their held items at their location. Clean up
 * no longer needed held item entities.
 * <p>
 * TODO: Also responsible for catching characters without current held item entities and then create them. Should be
 * moved elsewhere
 */
@Override
public void update(float delta) {
    // Make a set of all held items that exist so we can review them and later toss any no longer needed
    Set<EntityRef> heldItemsForReview = Sets.newHashSet(entityManager.getEntitiesWith(ItemIsRemotelyHeldComponent.class));
    // critters may also want held items
    for (EntityRef remotePlayer : entityManager.getEntitiesWith(CharacterComponent.class, PlayerCharacterComponent.class)) {
        if (relatesToLocalPlayer(remotePlayer)) {
            continue;
        }
        // Find the associated held item entity for this player, if one exists
        EntityRef currentHeldItem = EntityRef.NULL;
        Iterator<EntityRef> heldItermsIterator = heldItemsForReview.iterator();
        while (heldItermsIterator.hasNext()) {
            EntityRef heldItemCandidate = heldItermsIterator.next();
            ItemIsRemotelyHeldComponent itemIsRemotelyHeldComponent = heldItemCandidate.getComponent(ItemIsRemotelyHeldComponent.class);
            if (itemIsRemotelyHeldComponent.remotePlayer.equals(remotePlayer)) {
                currentHeldItem = heldItemCandidate;
                heldItermsIterator.remove();
                break;
            }
        }
        // selected
        if (currentHeldItem == EntityRef.NULL) {
            if (remotePlayer.hasComponent(CharacterHeldItemComponent.class)) {
                CharacterHeldItemComponent characterHeldItemComponent = remotePlayer.getComponent(CharacterHeldItemComponent.class);
                if (characterHeldItemComponent != null && !characterHeldItemComponent.selectedItem.equals(EntityRef.NULL)) {
                    linkHeldItemLocationForRemotePlayer(remotePlayer.getComponent(CharacterHeldItemComponent.class).selectedItem, remotePlayer);
                }
            }
        }
        // get the remote person mount point
        CharacterHeldItemComponent characterHeldItemComponent = remotePlayer.getComponent(CharacterHeldItemComponent.class);
        RemotePersonHeldItemMountPointComponent mountPointComponent = remotePlayer.getComponent(RemotePersonHeldItemMountPointComponent.class);
        if (characterHeldItemComponent == null || mountPointComponent == null) {
            continue;
        }
        LocationComponent locationComponent = mountPointComponent.mountPointEntity.getComponent(LocationComponent.class);
        if (locationComponent == null) {
            continue;
        }
        long timeElapsedSinceLastUsed = time.getGameTimeInMs() - characterHeldItemComponent.lastItemUsedTime;
        float animateAmount = 0f;
        if (timeElapsedSinceLastUsed < USEANIMATIONLENGTH) {
            // TODO add other easing functions into utilities and use here?
            // half way through the animation will be the maximum extent of rotation and translation
            animateAmount = 1f - Math.abs(((float) timeElapsedSinceLastUsed / (float) USEANIMATIONLENGTH) - 0.5f);
        }
        float addPitch = 15f * animateAmount;
        float addYaw = 10f * animateAmount;
        locationComponent.setLocalRotation(new Quaternionf().rotationYXZ(Math.toRadians(mountPointComponent.rotateDegrees.y + addYaw), Math.toRadians(mountPointComponent.rotateDegrees.x + addPitch), Math.toRadians(mountPointComponent.rotateDegrees.z)));
        Vector3f offset = new Vector3f(0.05f * animateAmount, -0.24f * animateAmount, 0f);
        offset.add(mountPointComponent.translate);
        locationComponent.setLocalPosition(offset);
        mountPointComponent.mountPointEntity.saveComponent(locationComponent);
    }
    heldItemsForReview.forEach(remainingHeldItem -> {
        if (remainingHeldItem.exists()) {
            remainingHeldItem.destroy();
        }
    });
}
Also used : Vector3f(org.joml.Vector3f) Quaternionf(org.joml.Quaternionf) CharacterHeldItemComponent(org.terasology.engine.logic.characters.CharacterHeldItemComponent) EntityRef(org.terasology.engine.entitySystem.entity.EntityRef) LocationComponent(org.terasology.engine.logic.location.LocationComponent)

Example 2 with CharacterHeldItemComponent

use of org.terasology.engine.logic.characters.CharacterHeldItemComponent in project Terasology by MovingBlocks.

the class FirstPersonClientSystem method update.

/**
 * modifies the held item mount point to move the held item in first person view
 */
@Override
public void update(float delta) {
    // ensure empty hand is shown if no item is hold at the moment
    if (!currentHeldItem.exists() && currentHeldItem != getHandEntity()) {
        linkHeldItemLocationForLocalPlayer(getHandEntity());
    }
    // ensure that there are no lingering items that are marked as still held. This situation happens with client side predicted items
    for (EntityRef entityRef : entityManager.getEntitiesWith(ItemIsHeldComponent.class)) {
        if (!entityRef.equals(currentHeldItem) && !entityRef.equals(handEntity)) {
            entityRef.destroy();
        }
    }
    // get the first person mount point and rotate it away from the camera
    CharacterHeldItemComponent characterHeldItemComponent = localPlayer.getCharacterEntity().getComponent(CharacterHeldItemComponent.class);
    FirstPersonHeldItemMountPointComponent mountPointComponent = localPlayer.getCameraEntity().getComponent(FirstPersonHeldItemMountPointComponent.class);
    if (characterHeldItemComponent == null || mountPointComponent == null) {
        return;
    }
    LocationComponent locationComponent = mountPointComponent.mountPointEntity.getComponent(LocationComponent.class);
    if (locationComponent == null) {
        return;
    }
    long timeElapsedSinceLastUsed = time.getGameTimeInMs() - characterHeldItemComponent.lastItemUsedTime;
    float animateAmount = 0f;
    if (timeElapsedSinceLastUsed < USEANIMATIONLENGTH) {
        // half way through the animation will be the maximum extent of rotation and translation
        animateAmount = 1f - Math.abs(((float) timeElapsedSinceLastUsed / (float) USEANIMATIONLENGTH) - 0.5f);
    }
    float addPitch = 15f * animateAmount;
    float addYaw = 10f * animateAmount;
    locationComponent.setLocalRotation(new Quaternionf().rotationYXZ(TeraMath.DEG_TO_RAD * (mountPointComponent.rotateDegrees.y + addYaw), TeraMath.DEG_TO_RAD * (mountPointComponent.rotateDegrees.x + addPitch), TeraMath.DEG_TO_RAD * mountPointComponent.rotateDegrees.z));
    Vector3f offset = new Vector3f(0.25f * animateAmount, -0.12f * animateAmount, 0f);
    offset.add(mountPointComponent.translate);
    locationComponent.setLocalPosition(offset);
    mountPointComponent.mountPointEntity.saveComponent(locationComponent);
}
Also used : Vector3f(org.joml.Vector3f) Quaternionf(org.joml.Quaternionf) CharacterHeldItemComponent(org.terasology.engine.logic.characters.CharacterHeldItemComponent) EntityRef(org.terasology.engine.entitySystem.entity.EntityRef) LocationComponent(org.terasology.engine.logic.location.LocationComponent)

Example 3 with CharacterHeldItemComponent

use of org.terasology.engine.logic.characters.CharacterHeldItemComponent in project Terasology by MovingBlocks.

the class ThirdPersonRemoteClientSystem method ensureHeldItemIsMountedOnLoad.

@ReceiveEvent
public void ensureHeldItemIsMountedOnLoad(OnChangedComponent event, EntityRef clientEntity, ClientComponent clientComponent) {
    if (relatesToLocalPlayer(clientEntity)) {
        logger.debug("ensureHeldItemIsMountedOnLoad found its given clientEntity to relate to the local player, " + "ignoring: {}", clientEntity);
        return;
    }
    if (clientEntity.exists() && clientComponent.character != EntityRef.NULL) {
        logger.debug("ensureHeldItemIsMountedOnLoad says a given clientEntity exists, has a character, and isn't " + "related to the local player: {}", clientEntity);
        CharacterHeldItemComponent characterHeldItemComponent = clientComponent.character.getComponent(CharacterHeldItemComponent.class);
        if (characterHeldItemComponent != null && !(clientComponent.character.equals(localPlayer.getCharacterEntity()))) {
            linkHeldItemLocationForRemotePlayer(characterHeldItemComponent.selectedItem, clientComponent.character);
        }
    } else {
        logger.debug("ensureHeldItemIsMountedOnLoad given a remote client, but one that didn't properly exist?");
    }
}
Also used : CharacterHeldItemComponent(org.terasology.engine.logic.characters.CharacterHeldItemComponent) ReceiveEvent(org.terasology.engine.entitySystem.event.ReceiveEvent)

Aggregations

CharacterHeldItemComponent (org.terasology.engine.logic.characters.CharacterHeldItemComponent)3 Quaternionf (org.joml.Quaternionf)2 Vector3f (org.joml.Vector3f)2 EntityRef (org.terasology.engine.entitySystem.entity.EntityRef)2 LocationComponent (org.terasology.engine.logic.location.LocationComponent)2 ReceiveEvent (org.terasology.engine.entitySystem.event.ReceiveEvent)1