Search in sources :

Example 6 with CharacterComponent

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

the class InventoryCell method moveItemSmartly.

private void moveItemSmartly() {
    EntityRef fromEntity = getTargetInventory();
    int fromSlot = getTargetSlot();
    EntityRef playerEntity = CoreRegistry.get(LocalPlayer.class).getCharacterEntity();
    InventoryComponent playerInventory = playerEntity.getComponent(InventoryComponent.class);
    if (playerInventory == null) {
        return;
    }
    CharacterComponent characterComponent = playerEntity.getComponent(CharacterComponent.class);
    if (characterComponent == null) {
        logger.error("Character entity of player had no character component");
        return;
    }
    int totalSlotCount = playerInventory.itemSlots.size();
    EntityRef interactionTarget = characterComponent.predictedInteractionTarget;
    InventoryComponent interactionTargetInventory = interactionTarget.getComponent(InventoryComponent.class);
    EntityRef targetEntity;
    List<Integer> toSlots = new ArrayList<>(totalSlotCount);
    if (fromEntity.equals(playerEntity)) {
        if (interactionTarget.exists() && interactionTargetInventory != null) {
            targetEntity = interactionTarget;
            toSlots = numbersBetween(0, interactionTargetInventory.itemSlots.size());
        } else {
            targetEntity = playerEntity;
            // TODO use a constant once there is one
            int hudSlotCount = 10;
            boolean fromHud = (fromSlot < hudSlotCount);
            boolean toHud = !fromHud;
            if (toHud) {
                toSlots = numbersBetween(0, hudSlotCount);
            } else {
                toSlots = numbersBetween(hudSlotCount, totalSlotCount);
            }
        }
    } else {
        targetEntity = playerEntity;
        toSlots = numbersBetween(0, totalSlotCount);
    }
    CoreRegistry.get(InventoryManager.class).moveItemToSlots(getTransferEntity(), fromEntity, fromSlot, targetEntity, toSlots);
}
Also used : InventoryManager(org.terasology.logic.inventory.InventoryManager) InventoryComponent(org.terasology.logic.inventory.InventoryComponent) LocalPlayer(org.terasology.logic.players.LocalPlayer) ArrayList(java.util.ArrayList) CharacterComponent(org.terasology.logic.characters.CharacterComponent) EntityRef(org.terasology.entitySystem.entity.EntityRef)

Example 7 with CharacterComponent

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

the class PlayerTargetSystem method update.

@Override
public void update(float delta) {
    EntityRef charEntity = player.getCharacterEntity();
    if (charEntity.exists()) {
        Vector3f cameraPos = player.getViewPosition();
        CharacterComponent charComp = charEntity.getComponent(CharacterComponent.class);
        if (charComp != null) {
            Vector3f dir = player.getViewDirection();
            float maxDist = charComp.interactionRange;
            FirstPersonHeldItemMountPointComponent heldItemMountPoint = player.getCameraEntity().getComponent(FirstPersonHeldItemMountPointComponent.class);
            if (heldItemMountPoint != null && heldItemMountPoint.isTracked()) {
                maxDist = heldItemMountPoint.translate.length() + 0.25f;
                dir = heldItemMountPoint.translate.normalize();
            }
            if (targetSystem.updateTarget(cameraPos, dir, maxDist)) {
                EntityRef oldTarget = targetSystem.getPreviousTarget();
                EntityRef newTarget = targetSystem.getTarget();
                charEntity.send(new PlayerTargetChangedEvent(oldTarget, newTarget));
            }
        }
    }
}
Also used : PlayerTargetChangedEvent(org.terasology.logic.players.PlayerTargetChangedEvent) Vector3f(org.terasology.math.geom.Vector3f) CharacterComponent(org.terasology.logic.characters.CharacterComponent) EntityRef(org.terasology.entitySystem.entity.EntityRef) FirstPersonHeldItemMountPointComponent(org.terasology.logic.players.FirstPersonHeldItemMountPointComponent)

Example 8 with CharacterComponent

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

the class InteractionSystem method onActivationPredicted.

@ReceiveEvent(components = { InteractionTargetComponent.class })
public void onActivationPredicted(ActivationPredicted event, EntityRef target) {
    EntityRef character = event.getInstigator();
    CharacterComponent characterComponent = character.getComponent(CharacterComponent.class);
    if (characterComponent == null) {
        return;
    }
    if (characterComponent.predictedInteractionTarget.exists()) {
        InteractionUtil.cancelInteractionAsClient(character);
    }
    if (target.exists()) {
        characterComponent.predictedInteractionTarget = target;
        characterComponent.predictedInteractionId = event.getActivationId();
        character.saveComponent(characterComponent);
        target.send(new InteractionStartPredicted(character));
    }
}
Also used : CharacterComponent(org.terasology.logic.characters.CharacterComponent) EntityRef(org.terasology.entitySystem.entity.EntityRef) ReceiveEvent(org.terasology.entitySystem.event.ReceiveEvent)

Example 9 with CharacterComponent

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

the class InteractionSystem method onActivate.

@ReceiveEvent(components = { InteractionTargetComponent.class }, netFilter = RegisterMode.AUTHORITY)
public void onActivate(ActivateEvent event, EntityRef target) {
    EntityRef instigator = event.getInstigator();
    CharacterComponent characterComponent = instigator.getComponent(CharacterComponent.class);
    if (characterComponent == null) {
        logger.error("Interaction start request instigator has no character component");
        return;
    }
    if (characterComponent.authorizedInteractionTarget.exists()) {
        logger.error("Interaction wasn't finished at start of next interaction");
        instigator.send(new InteractionEndEvent(characterComponent.authorizedInteractionId));
    }
    characterComponent.authorizedInteractionTarget = target;
    characterComponent.authorizedInteractionId = event.getActivationId();
    instigator.saveComponent(characterComponent);
}
Also used : CharacterComponent(org.terasology.logic.characters.CharacterComponent) EntityRef(org.terasology.entitySystem.entity.EntityRef) ReceiveEvent(org.terasology.entitySystem.event.ReceiveEvent)

Example 10 with CharacterComponent

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

the class InteractionUtil method cancelInteractionAsClient.

static void cancelInteractionAsClient(EntityRef character, boolean notifyServer) {
    CharacterComponent characterComponent = character.getComponent(CharacterComponent.class);
    if (characterComponent == null) {
        logger.error("Interaction instigator has no character component");
        return;
    }
    EntityRef oldTarget = characterComponent.predictedInteractionTarget;
    if (oldTarget.exists()) {
        characterComponent.predictedInteractionTarget = EntityRef.NULL;
        character.saveComponent(characterComponent);
        oldTarget.send(new InteractionEndPredicted(character));
        if (notifyServer) {
            character.send(new InteractionEndRequest());
        }
    }
}
Also used : CharacterComponent(org.terasology.logic.characters.CharacterComponent) EntityRef(org.terasology.entitySystem.entity.EntityRef)

Aggregations

CharacterComponent (org.terasology.logic.characters.CharacterComponent)12 EntityRef (org.terasology.entitySystem.entity.EntityRef)11 Vector3f (org.terasology.math.geom.Vector3f)4 ReceiveEvent (org.terasology.entitySystem.event.ReceiveEvent)3 Quat4f (org.terasology.math.geom.Quat4f)2 ClientComponent (org.terasology.network.ClientComponent)2 ArrayList (java.util.ArrayList)1 ResourceUrn (org.terasology.assets.ResourceUrn)1 EntityBuilder (org.terasology.entitySystem.entity.EntityBuilder)1 AliveCharacterComponent (org.terasology.logic.characters.AliveCharacterComponent)1 ActivationPredicted (org.terasology.logic.characters.events.ActivationPredicted)1 ActivationRequest (org.terasology.logic.characters.events.ActivationRequest)1 InventoryComponent (org.terasology.logic.inventory.InventoryComponent)1 InventoryManager (org.terasology.logic.inventory.InventoryManager)1 LocationComponent (org.terasology.logic.location.LocationComponent)1 FirstPersonHeldItemMountPointComponent (org.terasology.logic.players.FirstPersonHeldItemMountPointComponent)1 LocalPlayer (org.terasology.logic.players.LocalPlayer)1 PlayerTargetChangedEvent (org.terasology.logic.players.PlayerTargetChangedEvent)1 Client (org.terasology.network.Client)1 HitResult (org.terasology.physics.HitResult)1