use of org.terasology.gestalt.entitysystem.component.Component 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);
}
}
}
use of org.terasology.gestalt.entitysystem.component.Component in project Terasology by MovingBlocks.
the class ItemPickupAuthoritySystem method onBumpGiveItemToEntity.
@ReceiveEvent
public void onBumpGiveItemToEntity(CollideEvent event, EntityRef entity, PickupComponent pickupComponent) {
if (pickupComponent.timeDropped + pickupComponent.timeToPickUp < time.getGameTimeInMs()) {
GiveItemEvent giveItemEvent = new GiveItemEvent(event.getOtherEntity());
entity.send(giveItemEvent);
if (giveItemEvent.isHandled()) {
// remove all the components added from the pickup prefab
ItemComponent itemComponent = entity.getComponent(ItemComponent.class);
if (itemComponent != null) {
for (Component component : itemComponent.pickupPrefab.iterateComponents()) {
entity.removeComponent(component.getClass());
}
}
}
}
}
use of org.terasology.gestalt.entitysystem.component.Component in project Terasology by MovingBlocks.
the class GlobalStoreBuilder method build.
public EntityData.GlobalStore build(EngineEntityManager entityManager, Iterable<EntityRef> entities) {
EntityData.GlobalStore.Builder store = EntityData.GlobalStore.newBuilder();
Map<Class<? extends Component>, Integer> componentIdTable = Maps.newHashMap();
for (ComponentMetadata<?> componentMetadata : entityManager.getComponentLibrary().iterateComponentMetadata()) {
store.addComponentClass(componentMetadata.getId().toString());
componentIdTable.put(componentMetadata.getType(), componentIdTable.size());
}
prefabSerializer.setComponentIdMapping(componentIdTable);
/*
* The prefabs can't be obtained from entityManager.getPrefabManager().listPrefabs() as that might not
* be thread save.
*/
Set<Prefab> prefabsRequiredForEntityStorage = new HashSet<>();
for (EntityRef entityRef : entityManager.getAllEntities()) {
Prefab prefab = entityRef.getParentPrefab();
if (prefab != null) {
prefabsRequiredForEntityStorage.add(prefab);
}
}
for (Prefab prefab : prefabsRequiredForEntityStorage) {
store.addPrefab(prefabSerializer.serialize(prefab));
}
EntitySerializer entitySerializer = new EntitySerializer(entityManager);
entitySerializer.setComponentSerializeCheck(new PersistenceComponentSerializeCheck());
entitySerializer.setComponentIdMapping(componentIdTable);
for (EntityRef entity : entities) {
if (entity.isPersistent()) {
store.addEntity(entitySerializer.serialize(entity));
}
}
store.setNextEntityId(nextEntityId);
return store.build();
}
use of org.terasology.gestalt.entitysystem.component.Component in project Terasology by MovingBlocks.
the class ServerImpl method sendEntities.
private void sendEntities(NetData.NetMessage.Builder message) {
TIntIterator dirtyIterator = netDirty.iterator();
while (dirtyIterator.hasNext()) {
int netId = dirtyIterator.next();
EntityRef entity = networkSystem.getEntity(netId);
if (isOwned(entity)) {
Set<Class<? extends Component>> emptyComponentClassSet = Collections.emptySet();
EntityData.PackedEntity entityData = entitySerializer.serialize(entity, emptyComponentClassSet, changedComponents.get(netId), emptyComponentClassSet, new ClientComponentFieldCheck());
if (entityData != null) {
message.addUpdateEntity(NetData.UpdateEntityMessage.newBuilder().setEntity(entityData).setNetId(netId));
}
}
}
netDirty.clear();
}
use of org.terasology.gestalt.entitysystem.component.Component in project Terasology by MovingBlocks.
the class NetworkSystemImpl method updateOwnership.
public void updateOwnership(EntityRef entity) {
NetworkComponent netComponent = entity.getComponent(NetworkComponent.class);
if (netComponent == null || netComponent.getNetworkId() == NULL_NET_ID) {
return;
}
EntityRef lastOwnerEntity = ownerLookup.get(entity);
if (lastOwnerEntity == null) {
lastOwnerEntity = EntityRef.NULL;
}
EntityRef newOwnerEntity = entity.getOwner();
if (!Objects.equal(lastOwnerEntity, newOwnerEntity)) {
NetClient lastOwner = getNetOwner(lastOwnerEntity);
NetClient newOwner = getNetOwner(newOwnerEntity);
if (!Objects.equal(lastOwner, newOwner)) {
recursiveUpdateOwnership(entity, lastOwner, newOwner);
if (newOwner != null) {
int id = netComponent.getNetworkId();
for (Component component : entity.iterateComponents()) {
if (componentLibrary.getMetadata(component.getClass()).isReplicated()) {
newOwner.setComponentDirty(id, component.getClass());
}
}
}
}
if (lastOwnerEntity.exists()) {
ownedLookup.remove(lastOwnerEntity, entity);
}
if (newOwnerEntity.exists()) {
ownerLookup.put(entity, newOwnerEntity);
ownedLookup.put(newOwnerEntity, entity);
} else {
ownerLookup.remove(entity);
}
}
}
Aggregations