use of org.terasology.entitySystem.Component in project Terasology by MovingBlocks.
the class NetworkEntitySerializer method deserializeOnto.
public void deserializeOnto(MutableComponentContainer entity, EntityData.PackedEntity entityData, FieldSerializeCheck<Component> fieldCheck) {
int fieldPos = 0;
for (int componentIndex = 0; componentIndex < entityData.getComponentIdCount(); ++componentIndex) {
Integer componentId = entityData.getComponentId(componentIndex);
Class<? extends Component> componentClass = idTable.inverse().get(componentId);
ComponentMetadata<?> metadata = componentLibrary.getMetadata(componentClass);
if (metadata == null) {
logger.warn("Skipping unknown component {}", componentId);
fieldPos += UnsignedBytes.toInt(entityData.getComponentFieldCounts().byteAt(componentIndex));
continue;
}
if (!componentSerializeCheck.serialize(metadata)) {
fieldPos += UnsignedBytes.toInt(entityData.getComponentFieldCounts().byteAt(componentIndex));
continue;
}
Component component = entity.getComponent(metadata.getType());
boolean createdNewComponent = false;
if (component == null) {
createdNewComponent = true;
component = metadata.newInstance();
}
Serializer serializer = typeSerializationLibrary.getSerializerFor(metadata);
for (int fieldIndex = 0; fieldIndex < UnsignedBytes.toInt(entityData.getComponentFieldCounts().byteAt(componentIndex)); ++fieldIndex) {
byte fieldId = entityData.getFieldIds().byteAt(fieldPos);
ReplicatedFieldMetadata fieldMetadata = metadata.getField(fieldId);
if (fieldMetadata != null && fieldCheck.shouldDeserialize(metadata, fieldMetadata)) {
logger.trace("Deserializing field {} of component {} as value {}", fieldMetadata, metadata, entityData.getFieldValue(fieldPos));
serializer.deserializeOnto(component, fieldMetadata, new ProtobufPersistedData(entityData.getFieldValue(fieldPos)), deserializationContext);
}
fieldPos++;
}
if (createdNewComponent) {
entity.addComponent(component);
} else {
entity.saveComponent(component);
}
}
for (int componentId : entityData.getRemovedComponentList()) {
Class<? extends Component> componentClass = idTable.inverse().get(componentId);
ComponentMetadata<?> metadata = componentLibrary.getMetadata(componentClass);
if (componentSerializeCheck.serialize(metadata)) {
entity.removeComponent(metadata.getType());
}
}
}
use of org.terasology.entitySystem.Component in project Terasology by MovingBlocks.
the class NetworkEntitySerializer method serialize.
public EntityData.PackedEntity serialize(EntityRef entityRef, Set<Class<? extends Component>> added, Set<Class<? extends Component>> changed, Set<Class<? extends Component>> removed, FieldSerializeCheck<Component> fieldCheck) {
EntityData.PackedEntity.Builder entity = EntityData.PackedEntity.newBuilder();
ByteString.Output fieldIds = ByteString.newOutput();
ByteString.Output componentFieldCounts = ByteString.newOutput();
for (Class<? extends Component> componentType : added) {
Component component = entityRef.getComponent(componentType);
if (component == null) {
logger.error("Non-existent component marked as added: {}", componentType);
}
serializeComponentFull(entityRef.getComponent(componentType), false, fieldCheck, entity, fieldIds, componentFieldCounts, true);
}
for (Class<? extends Component> componentType : changed) {
Component comp = entityRef.getComponent(componentType);
if (comp != null) {
serializeComponentFull(comp, true, fieldCheck, entity, fieldIds, componentFieldCounts, false);
} else {
logger.error("Non-existent component marked as changed: {}", componentType);
}
}
for (Class<? extends Component> componentType : removed) {
entity.addRemovedComponent(idTable.get(componentType));
}
entity.setFieldIds(fieldIds.toByteString());
entity.setComponentFieldCounts(componentFieldCounts.toByteString());
if (entity.getFieldIds().isEmpty() && entity.getRemovedComponentCount() == 0) {
return null;
} else {
return entity.build();
}
}
use of org.terasology.entitySystem.Component in project Terasology by MovingBlocks.
the class PrefabSerializer method serialize.
/**
* @param prefab
* @return The serialized prefab
*/
public EntityData.Prefab serialize(Prefab prefab) {
EntityData.Prefab.Builder prefabData = EntityData.Prefab.newBuilder();
prefabData.setName(prefab.getName());
if (prefab.getParent() != null) {
prefabData.setParentName(prefab.getParent().getName());
}
prefabData.setAlwaysRelevant(prefab.isAlwaysRelevant());
prefabData.setPersisted(prefab.isPersisted());
// Delta off the parent
for (Component component : prefab.iterateComponents()) {
if (prefab.getParent() != null && prefab.getParent().hasComponent(component.getClass())) {
EntityData.Component serializedComponent = componentSerializer.serialize(prefab.getParent().getComponent(component.getClass()), component);
if (serializedComponent != null) {
prefabData.addComponent(serializedComponent);
}
} else {
prefabData.addComponent(componentSerializer.serialize(component));
}
}
if (prefab.getParent() != null) {
for (Component parentComp : prefab.getParent().iterateComponents()) {
if (!prefab.hasComponent(parentComp.getClass())) {
prefabData.addRemovedComponent(componentLibrary.getMetadata(parentComp).getUri().toString());
}
}
}
return prefabData.build();
}
use of org.terasology.entitySystem.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.entitySystem.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 Quat4f(TeraMath.DEG_TO_RAD * heldItemTransformComponent.rotateDegrees.y, TeraMath.DEG_TO_RAD * heldItemTransformComponent.rotateDegrees.x, TeraMath.DEG_TO_RAD * heldItemTransformComponent.rotateDegrees.z), heldItemTransformComponent.scale);
}
}
}
Aggregations