Search in sources :

Example 21 with Component

use of org.terasology.entitySystem.Component in project Terasology by MovingBlocks.

the class ComponentSerializerTest method testComponentTypeIdUsedWhenLookupTableEnabled.

@Test
public void testComponentTypeIdUsedWhenLookupTableEnabled() throws Exception {
    componentSerializer.setIdMapping(ImmutableMap.<Class<? extends Component>, Integer>builder().put(StringComponent.class, 1).build());
    Component stringComponent = new StringComponent("Test");
    EntityData.Component compData = componentSerializer.serialize(stringComponent);
    assertEquals(1, compData.getTypeIndex());
    assertFalse(compData.hasType());
}
Also used : StringComponent(org.terasology.entitySystem.stubs.StringComponent) EntityData(org.terasology.protobuf.EntityData) GetterSetterComponent(org.terasology.entitySystem.stubs.GetterSetterComponent) IntegerComponent(org.terasology.entitySystem.stubs.IntegerComponent) Component(org.terasology.entitySystem.Component) StringComponent(org.terasology.entitySystem.stubs.StringComponent) Test(org.junit.Test)

Example 22 with Component

use of org.terasology.entitySystem.Component in project Terasology by MovingBlocks.

the class ComponentSerializerTest method testComponentTypeIdDeserializes.

@Test
public void testComponentTypeIdDeserializes() throws Exception {
    componentSerializer.setIdMapping(ImmutableMap.<Class<? extends Component>, Integer>builder().put(StringComponent.class, 1).build());
    EntityData.Component compData = EntityData.Component.newBuilder().setTypeIndex(1).addField(EntityData.NameValue.newBuilder().setName("value").setValue(EntityData.Value.newBuilder().addString("item"))).build();
    Component comp = componentSerializer.deserialize(compData);
    assertTrue(comp instanceof StringComponent);
    assertEquals("item", ((StringComponent) comp).value);
}
Also used : StringComponent(org.terasology.entitySystem.stubs.StringComponent) EntityData(org.terasology.protobuf.EntityData) GetterSetterComponent(org.terasology.entitySystem.stubs.GetterSetterComponent) IntegerComponent(org.terasology.entitySystem.stubs.IntegerComponent) Component(org.terasology.entitySystem.Component) StringComponent(org.terasology.entitySystem.stubs.StringComponent) Test(org.junit.Test)

Example 23 with Component

use of org.terasology.entitySystem.Component in project Terasology by MovingBlocks.

the class BlockPrefabManager method updateBlock.

private void updateBlock(Block block) {
    Optional<Prefab> prefab = block.getPrefab();
    boolean keepActive = block.isKeepActive();
    boolean requiresLifecycleEvents = false;
    if (prefab.isPresent()) {
        for (Component comp : prefab.get().iterateComponents()) {
            ComponentMetadata<?> metadata = entityManager.getComponentLibrary().getMetadata(comp.getClass());
            if (metadata.isForceBlockActive()) {
                keepActive = true;
                break;
            }
            if (metadata.isBlockLifecycleEventsRequired()) {
                requiresLifecycleEvents = true;
            }
        }
    }
    block.setKeepActive(keepActive);
    block.setLifecycleEventsRequired(requiresLifecycleEvents && !keepActive);
}
Also used : Component(org.terasology.entitySystem.Component) Prefab(org.terasology.entitySystem.prefab.Prefab)

Example 24 with Component

use of org.terasology.entitySystem.Component in project Terasology by MovingBlocks.

the class BlockItemFactory method newBuilder.

/**
 * Use this method instead of {@link #newInstance(BlockFamily)} to modify entity properties like the persistence
 * flag before it gets created.
 *
 * @param blockFamily must not be null
 */
public EntityBuilder newBuilder(BlockFamily blockFamily, int quantity) {
    EntityBuilder builder = entityManager.newBuilder("engine:blockItemBase");
    if (blockFamily.getArchetypeBlock().getLuminance() > 0) {
        builder.addComponent(new LightComponent());
    }
    // Copy the components from block prefab into the block item
    Optional<Prefab> prefab = blockFamily.getArchetypeBlock().getPrefab();
    if (prefab.isPresent()) {
        for (Component component : prefab.get().iterateComponents()) {
            if (component.getClass().getAnnotation(AddToBlockBasedItem.class) != null) {
                builder.addComponent(entityManager.getComponentLibrary().copy(component));
            }
        }
    }
    DisplayNameComponent displayNameComponent = builder.getComponent(DisplayNameComponent.class);
    displayNameComponent.name = blockFamily.getDisplayName();
    ItemComponent item = builder.getComponent(ItemComponent.class);
    if (blockFamily.getArchetypeBlock().isStackable()) {
        item.stackId = "block:" + blockFamily.getURI().toString();
        item.stackCount = (byte) quantity;
    }
    BlockItemComponent blockItem = builder.getComponent(BlockItemComponent.class);
    blockItem.blockFamily = blockFamily;
    return builder;
}
Also used : DisplayNameComponent(org.terasology.logic.common.DisplayNameComponent) ItemComponent(org.terasology.logic.inventory.ItemComponent) LightComponent(org.terasology.rendering.logic.LightComponent) EntityBuilder(org.terasology.entitySystem.entity.EntityBuilder) LightComponent(org.terasology.rendering.logic.LightComponent) ItemComponent(org.terasology.logic.inventory.ItemComponent) Component(org.terasology.entitySystem.Component) DisplayNameComponent(org.terasology.logic.common.DisplayNameComponent) Prefab(org.terasology.entitySystem.prefab.Prefab)

Example 25 with Component

use of org.terasology.entitySystem.Component in project Terasology by MovingBlocks.

the class ComponentSerializer method serialize.

/**
 * Serializes the differences between two components.
 *
 * @param base  The base component to compare against.
 * @param delta The component whose differences will be serialized
 * @param check A check to use to see if each field should be serialized.
 * @return The serialized component, or null if it could not be serialized
 */
public EntityData.Component serialize(Component base, Component delta, FieldSerializeCheck<Component> check) {
    ComponentMetadata<?> componentMetadata = componentLibrary.getMetadata(base.getClass());
    if (componentMetadata == null) {
        logger.error("Unregistered component type: {}", base.getClass());
        return null;
    }
    EntityData.Component.Builder componentMessage = EntityData.Component.newBuilder();
    serializeComponentType(componentMetadata, componentMessage);
    Serializer serializer = typeSerializationLibrary.getSerializerFor(componentMetadata);
    boolean changed = false;
    for (ReplicatedFieldMetadata field : componentMetadata.getFields()) {
        if (check.shouldSerializeField(field, delta) && serializer.getHandlerFor(field) != null) {
            Object origValue = field.getValue(base);
            Object deltaValue = field.getValue(delta);
            if (!Objects.equal(origValue, deltaValue)) {
                PersistedData value = serializer.serializeValue(field, deltaValue, serializationContext);
                if (!value.isNull()) {
                    EntityData.Value dataValue = ((ProtobufPersistedData) value).getValue();
                    if (usingFieldIds) {
                        componentMessage.addField(EntityData.NameValue.newBuilder().setNameIndex(field.getId()).setValue(dataValue).build());
                    } else {
                        componentMessage.addField(EntityData.NameValue.newBuilder().setName(field.getName()).setValue(dataValue).build());
                    }
                    changed = true;
                }
            }
        }
    }
    if (changed) {
        return componentMessage.build();
    }
    return null;
}
Also used : ProtobufPersistedData(org.terasology.persistence.typeHandling.protobuf.ProtobufPersistedData) EntityData(org.terasology.protobuf.EntityData) ReplicatedFieldMetadata(org.terasology.entitySystem.metadata.ReplicatedFieldMetadata) PersistedData(org.terasology.persistence.typeHandling.PersistedData) ProtobufPersistedData(org.terasology.persistence.typeHandling.protobuf.ProtobufPersistedData) Component(org.terasology.entitySystem.Component) Serializer(org.terasology.persistence.typeHandling.Serializer)

Aggregations

Component (org.terasology.entitySystem.Component)49 EntityRef (org.terasology.entitySystem.entity.EntityRef)15 Prefab (org.terasology.entitySystem.prefab.Prefab)9 LocationComponent (org.terasology.logic.location.LocationComponent)9 EntityData (org.terasology.protobuf.EntityData)8 OnActivatedComponent (org.terasology.entitySystem.entity.lifecycleEvents.OnActivatedComponent)7 NetworkComponent (org.terasology.network.NetworkComponent)7 BlockComponent (org.terasology.world.block.BlockComponent)7 OnAddedComponent (org.terasology.entitySystem.entity.lifecycleEvents.OnAddedComponent)6 OnChangedComponent (org.terasology.entitySystem.entity.lifecycleEvents.OnChangedComponent)6 EntityInfoComponent (org.terasology.entitySystem.entity.internal.EntityInfoComponent)5 BeforeDeactivateComponent (org.terasology.entitySystem.entity.lifecycleEvents.BeforeDeactivateComponent)5 EntityBuilder (org.terasology.entitySystem.entity.EntityBuilder)4 ByteString (com.google.protobuf.ByteString)3 Test (org.junit.Test)3 BeforeRemoveComponent (org.terasology.entitySystem.entity.lifecycleEvents.BeforeRemoveComponent)3 ReceiveEvent (org.terasology.entitySystem.event.ReceiveEvent)3 SectorSimulationComponent (org.terasology.entitySystem.sectors.SectorSimulationComponent)3 ParticleEmitterComponent (org.terasology.particles.components.ParticleEmitterComponent)3 WorldConfigurator (org.terasology.world.generator.WorldConfigurator)3