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