use of org.terasology.engine.rendering.logic.VisualComponent in project Terasology by MovingBlocks.
the class ThirdPersonRemoteClientSystem method linkHeldItemLocationForRemotePlayer.
/**
* Changes held item entity.
* <p>
* Detaches old held item and removes its components. Adds components to new held item and attaches it to the mount
* point entity.
*/
private void linkHeldItemLocationForRemotePlayer(EntityRef newItem, EntityRef player) {
if (relatesToLocalPlayer(player)) {
logger.debug("linkHeldItemLocationForRemotePlayer called with an entity that relates to the local player," + " ignoring{}", player);
return;
}
// Find out if there is a current held item that maps to this player
EntityRef currentHeldItem = EntityRef.NULL;
for (EntityRef heldItemCandidate : entityManager.getEntitiesWith(ItemIsRemotelyHeldComponent.class)) {
EntityRef remotePlayerCandidate = heldItemCandidate.getComponent(ItemIsRemotelyHeldComponent.class).remotePlayer;
logger.debug("For held item candidate {} got its player candidate as {}", heldItemCandidate, remotePlayerCandidate);
if (remotePlayerCandidate.equals(player)) {
logger.debug("Thinking we found a match with player {} so counting this held item as relevant for " + "processing", player);
currentHeldItem = heldItemCandidate;
// need to remove the old item
if (newItem.equals(EntityRef.NULL)) {
logger.debug("Found an existing held item but the new request was to no longer hold anything so " + "destroying {}", currentHeldItem);
currentHeldItem.destroy();
return;
}
break;
}
}
// In the case of an actual change of item other than an empty hand we need to hook up a new held item entity
if (newItem != null && !newItem.equals(EntityRef.NULL) && !newItem.equals(currentHeldItem)) {
RemotePersonHeldItemMountPointComponent mountPointComponent = player.getComponent(RemotePersonHeldItemMountPointComponent.class);
if (mountPointComponent != null) {
// currentHeldItem is at this point the old item
if (currentHeldItem != EntityRef.NULL) {
currentHeldItem.destroy();
}
currentHeldItem = entityManager.create();
logger.debug("linkHeldItemLocationForRemotePlayer is now creating a new held item {}", currentHeldItem);
// add the visually relevant components
for (Component component : newItem.iterateComponents()) {
if (component instanceof VisualComponent && !(component instanceof FirstPersonHeldItemTransformComponent)) {
currentHeldItem.addComponent(component);
}
}
// ensure world location is set
currentHeldItem.addComponent(new LocationComponent());
// Map this held item to the player it is held by
ItemIsRemotelyHeldComponent itemIsRemotelyHeldComponent = new ItemIsRemotelyHeldComponent();
itemIsRemotelyHeldComponent.remotePlayer = player;
currentHeldItem.addComponent(itemIsRemotelyHeldComponent);
RemotePersonHeldItemTransformComponent heldItemTransformComponent = currentHeldItem.getComponent(RemotePersonHeldItemTransformComponent.class);
if (heldItemTransformComponent == null) {
heldItemTransformComponent = new RemotePersonHeldItemTransformComponent();
currentHeldItem.addComponent(heldItemTransformComponent);
}
Location.attachChild(mountPointComponent.mountPointEntity, currentHeldItem, heldItemTransformComponent.translate, new Quaternionf().rotationYXZ(Math.toRadians(heldItemTransformComponent.rotateDegrees.y), Math.toRadians(heldItemTransformComponent.rotateDegrees.x), Math.toRadians(heldItemTransformComponent.rotateDegrees.z)), heldItemTransformComponent.scale);
}
} else {
logger.info("Somehow ended up in the else during linkHeldItemLocationForRemotePlayer - current item was " + "{} and new item {}", currentHeldItem, newItem);
}
}
use of org.terasology.engine.rendering.logic.VisualComponent in project Terasology by MovingBlocks.
the class FirstPersonClientSystem method linkHeldItemLocationForLocalPlayer.
/**
* Changes held item entity.
*
* <p>Detaches old held item and removes it's components. Adds components to new held item and
* attaches it to the mount point entity.</p>
*/
private void linkHeldItemLocationForLocalPlayer(EntityRef newItem) {
if (!newItem.equals(currentHeldItem)) {
EntityRef camera = localPlayer.getCameraEntity();
FirstPersonHeldItemMountPointComponent mountPointComponent = camera.getComponent(FirstPersonHeldItemMountPointComponent.class);
if (mountPointComponent != null) {
// currentHeldItem is at this point the old item
if (currentHeldItem != EntityRef.NULL) {
currentHeldItem.destroy();
}
// use the hand if there is no new item
EntityRef newHeldItem;
if (newItem == EntityRef.NULL) {
newHeldItem = getHandEntity();
} else {
newHeldItem = newItem;
}
// create client side held item entity
currentHeldItem = entityManager.create();
// add the visually relevant components
for (Component component : newHeldItem.iterateComponents()) {
if (component instanceof VisualComponent) {
currentHeldItem.addComponent(component);
}
}
// ensure world location is set
currentHeldItem.addComponent(new LocationComponent());
currentHeldItem.addComponent(new ItemIsHeldComponent());
FirstPersonHeldItemTransformComponent heldItemTransformComponent = currentHeldItem.getComponent(FirstPersonHeldItemTransformComponent.class);
if (heldItemTransformComponent == null) {
heldItemTransformComponent = new FirstPersonHeldItemTransformComponent();
currentHeldItem.addComponent(heldItemTransformComponent);
}
Location.attachChild(mountPointComponent.mountPointEntity, currentHeldItem, heldItemTransformComponent.translate, new Quaternionf().rotationYXZ(TeraMath.DEG_TO_RAD * heldItemTransformComponent.rotateDegrees.y, TeraMath.DEG_TO_RAD * heldItemTransformComponent.rotateDegrees.x, TeraMath.DEG_TO_RAD * heldItemTransformComponent.rotateDegrees.z), heldItemTransformComponent.scale);
}
}
}
Aggregations