Search in sources :

Example 51 with Component

use of org.terasology.gestalt.entitysystem.component.Component in project Terasology by MovingBlocks.

the class EntitySerializer method serializeEntityDelta.

private EntityData.Entity serializeEntityDelta(EntityRef entityRef, Prefab prefab, FieldSerializeCheck<Component> fieldCheck) {
    EntityData.Entity.Builder entity = EntityData.Entity.newBuilder();
    if (!ignoringEntityId) {
        entity.setId(entityRef.getId());
    }
    entity.setParentPrefab(prefab.getName());
    if (entityRef.isAlwaysRelevant() != prefab.isAlwaysRelevant()) {
        entity.setAlwaysRelevant(entityRef.isAlwaysRelevant());
    }
    EntityRef owner = entityRef.getOwner();
    if (owner.exists()) {
        entity.setOwner(owner.getId());
    }
    EntityScope scope = entityRef.getScope();
    if (scope != null) {
        switch(scope) {
            case GLOBAL:
                entity.setScope(GLOBAL);
                break;
            case SECTOR:
                entity.setScope(SECTOR);
                break;
            case CHUNK:
                entity.setScope(CHUNK);
                break;
        }
    }
    Set<Class<? extends Component>> presentClasses = Sets.newHashSet();
    for (Component component : entityRef.iterateComponents()) {
        if (!componentSerializeCheck.serialize(componentLibrary.getMetadata(component.getClass()))) {
            continue;
        }
        presentClasses.add(component.getClass());
        Component prefabComponent = prefab.getComponent(component.getClass());
        EntityData.Component componentData;
        if (prefabComponent == null) {
            componentData = componentSerializer.serialize(component, fieldCheck);
        } else {
            componentData = componentSerializer.serialize(prefabComponent, component, fieldCheck);
        }
        if (componentData != null) {
            entity.addComponent(componentData);
        }
    }
    for (Component prefabComponent : prefab.iterateComponents()) {
        ComponentMetadata<?> metadata = componentLibrary.getMetadata(prefabComponent.getClass());
        if (!presentClasses.contains(prefabComponent.getClass()) && componentSerializeCheck.serialize(metadata)) {
            // TODO: Use component ids here
            entity.addRemovedComponent(metadata.getId().toString());
        }
    }
    return entity.build();
}
Also used : EntityData(org.terasology.protobuf.EntityData) EntityInfoComponent(org.terasology.engine.entitySystem.entity.internal.EntityInfoComponent) Component(org.terasology.gestalt.entitysystem.component.Component) EntityRef(org.terasology.engine.entitySystem.entity.EntityRef) EntityScope(org.terasology.engine.entitySystem.entity.internal.EntityScope)

Example 52 with Component

use of org.terasology.gestalt.entitysystem.component.Component in project Terasology by MovingBlocks.

the class PreviewWorldScreen method configureProperties.

private void configureProperties() {
    PropertyLayout propLayout = find("properties", PropertyLayout.class);
    propLayout.setOrdering(PropertyOrdering.byLabel());
    propLayout.clear();
    WorldConfigurator worldConfig = worldGenerator.getConfigurator();
    Map<String, Component> params = worldConfig.getProperties();
    for (String key : params.keySet()) {
        Class<? extends Component> clazz = params.get(key).getClass();
        Component comp = config.getModuleConfig(worldGenerator.getUri(), key, clazz);
        if (comp != null) {
            // use the data from the config instead of defaults
            worldConfig.setProperty(key, comp);
        }
    }
    ComponentLibrary compLib = subContext.get(ComponentLibrary.class);
    for (String label : params.keySet()) {
        PropertyProvider provider = new PropertyProvider(context.get(ReflectFactory.class), context.get(OneOfProviderFactory.class)) {

            @Override
            protected <T> Binding<T> createTextBinding(Object target, FieldMetadata<Object, T> fieldMetadata) {
                return new WorldConfigBinding<>(worldConfig, label, compLib, fieldMetadata);
            }

            @Override
            protected Binding<Float> createFloatBinding(Object target, FieldMetadata<Object, ?> fieldMetadata) {
                return new WorldConfigNumberBinding(worldConfig, label, compLib, fieldMetadata);
            }
        };
        Component target = params.get(label);
        List<Property<?, ?>> properties = provider.createProperties(target);
        propLayout.addProperties(label, properties);
    }
}
Also used : FieldMetadata(org.terasology.reflection.metadata.FieldMetadata) WorldConfigurator(org.terasology.engine.world.generator.WorldConfigurator) ReflectFactory(org.terasology.reflection.reflect.ReflectFactory) OneOfProviderFactory(org.terasology.nui.properties.OneOfProviderFactory) PropertyLayout(org.terasology.nui.layouts.PropertyLayout) PropertyProvider(org.terasology.nui.properties.PropertyProvider) ComponentLibrary(org.terasology.engine.entitySystem.metadata.ComponentLibrary) Component(org.terasology.gestalt.entitysystem.component.Component) Property(org.terasology.nui.properties.Property)

Example 53 with Component

use of org.terasology.gestalt.entitysystem.component.Component in project Terasology by MovingBlocks.

the class WorldSetupScreen method configureProperties.

/**
 * Assigns a {@link WorldConfigurator} for every world if it doesn't exist. Using
 * the WorldConfigurator it gets the properties associated with that world.
 */
private void configureProperties() {
    PropertyLayout propLayout = find("properties", PropertyLayout.class);
    propLayout.setOrdering(PropertyOrdering.byLabel());
    propLayout.clear();
    WorldConfigurator worldConfig;
    if (world.getWorldConfigurator() != null) {
        worldConfig = world.getWorldConfigurator();
    } else {
        worldConfig = worldGenerator.getConfigurator();
        world.setWorldConfigurator(worldConfig);
    }
    oldWorldConfig = worldConfig;
    Map<String, Component> params = worldConfig.getProperties();
    for (String key : params.keySet()) {
        Class<? extends Component> clazz = params.get(key).getClass();
        Component comp = config.getModuleConfig(worldGenerator.getUri(), key, clazz);
        if (comp != null) {
            // use the data from the config instead of defaults
            worldConfig.setProperty(key, comp);
        }
    }
    ComponentLibrary compLib = context.get(ComponentLibrary.class);
    for (String label : params.keySet()) {
        PropertyProvider provider = new PropertyProvider(context.get(ReflectFactory.class), context.get(OneOfProviderFactory.class)) {

            @Override
            protected <T> Binding<T> createTextBinding(Object target, FieldMetadata<Object, T> fieldMetadata) {
                return new WorldSetupScreen.WorldConfigBinding<>(worldConfig, label, compLib, fieldMetadata);
            }

            @Override
            protected Binding<Float> createFloatBinding(Object target, FieldMetadata<Object, ?> fieldMetadata) {
                return new WorldSetupScreen.WorldConfigNumberBinding(worldConfig, label, compLib, fieldMetadata);
            }
        };
        Component target = params.get(label);
        List<Property<?, ?>> properties = provider.createProperties(target);
        propLayout.addProperties(label, properties);
    }
}
Also used : FieldMetadata(org.terasology.reflection.metadata.FieldMetadata) WorldConfigurator(org.terasology.engine.world.generator.WorldConfigurator) ReflectFactory(org.terasology.reflection.reflect.ReflectFactory) OneOfProviderFactory(org.terasology.nui.properties.OneOfProviderFactory) PropertyLayout(org.terasology.nui.layouts.PropertyLayout) PropertyProvider(org.terasology.nui.properties.PropertyProvider) ComponentLibrary(org.terasology.engine.entitySystem.metadata.ComponentLibrary) Component(org.terasology.gestalt.entitysystem.component.Component) Property(org.terasology.nui.properties.Property)

Example 54 with Component

use of org.terasology.gestalt.entitysystem.component.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.gestalt.entitysystem.component.Component) Prefab(org.terasology.engine.entitySystem.prefab.Prefab)

Example 55 with Component

use of org.terasology.gestalt.entitysystem.component.Component in project Terasology by MovingBlocks.

the class LocalChunkProvider method generateQueuedEntities.

private void generateQueuedEntities(EntityStore store) {
    Prefab prefab = store.getPrefab();
    EntityRef entity;
    if (prefab != null) {
        entity = entityManager.create(prefab);
    } else {
        entity = entityManager.create();
    }
    for (Component component : store.iterateComponents()) {
        entity.addComponent(component);
    }
}
Also used : Component(org.terasology.gestalt.entitysystem.component.Component) Prefab(org.terasology.engine.entitySystem.prefab.Prefab) EntityRef(org.terasology.engine.entitySystem.entity.EntityRef)

Aggregations

Component (org.terasology.gestalt.entitysystem.component.Component)57 EntityRef (org.terasology.engine.entitySystem.entity.EntityRef)20 Prefab (org.terasology.engine.entitySystem.prefab.Prefab)12 LocationComponent (org.terasology.engine.logic.location.LocationComponent)12 OnActivatedComponent (org.terasology.engine.entitySystem.entity.lifecycleEvents.OnActivatedComponent)11 NetworkComponent (org.terasology.engine.network.NetworkComponent)10 OnChangedComponent (org.terasology.engine.entitySystem.entity.lifecycleEvents.OnChangedComponent)9 EntityData (org.terasology.protobuf.EntityData)8 BeforeDeactivateComponent (org.terasology.engine.entitySystem.entity.lifecycleEvents.BeforeDeactivateComponent)7 OnAddedComponent (org.terasology.engine.entitySystem.entity.lifecycleEvents.OnAddedComponent)7 BlockComponent (org.terasology.engine.world.block.BlockComponent)7 EntityInfoComponent (org.terasology.engine.entitySystem.entity.internal.EntityInfoComponent)6 ReceiveEvent (org.terasology.engine.entitySystem.event.ReceiveEvent)6 EntityManager (org.terasology.engine.entitySystem.entity.EntityManager)5 ClientComponent (org.terasology.engine.network.ClientComponent)5 List (java.util.List)4 Map (java.util.Map)4 Optional (java.util.Optional)4 FieldMetadata (org.terasology.reflection.metadata.FieldMetadata)4 ByteString (com.google.protobuf.ByteString)3