Search in sources :

Example 1 with Component

use of org.terasology.entitySystem.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() {

            @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.world.generator.WorldConfigurator) PropertyLayout(org.terasology.rendering.nui.layouts.PropertyLayout) PropertyProvider(org.terasology.rendering.nui.properties.PropertyProvider) ComponentLibrary(org.terasology.entitySystem.metadata.ComponentLibrary) Component(org.terasology.entitySystem.Component) Property(org.terasology.rendering.nui.properties.Property)

Example 2 with Component

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

the class PreviewWorldScreen method resetEnvironment.

private void resetEnvironment() {
    CoreRegistry.setContext(context);
    if (environment != null) {
        EnvironmentSwitchHandler environmentSwitchHandler = context.get(EnvironmentSwitchHandler.class);
        environmentSwitchHandler.handleSwitchBackFromPreviewEnvironment(subContext);
        environment.close();
        environment = null;
    }
    previewGen.close();
    WorldConfigurator worldConfig = worldGenerator.getConfigurator();
    Map<String, Component> params = worldConfig.getProperties();
    if (params != null) {
        config.setModuleConfigs(worldGenerator.getUri(), params);
    }
}
Also used : WorldConfigurator(org.terasology.world.generator.WorldConfigurator) Component(org.terasology.entitySystem.Component) EnvironmentSwitchHandler(org.terasology.engine.bootstrap.EnvironmentSwitchHandler)

Example 3 with Component

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

the class EntitySerializer method serializeEntityFull.

private EntityData.Entity serializeEntityFull(EntityRef entityRef, FieldSerializeCheck<Component> fieldCheck) {
    EntityData.Entity.Builder entity = EntityData.Entity.newBuilder();
    if (!ignoringEntityId) {
        entity.setId(entityRef.getId());
    }
    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;
        }
    }
    for (Component component : entityRef.iterateComponents()) {
        if (!componentSerializeCheck.serialize(componentLibrary.getMetadata(component.getClass()))) {
            continue;
        }
        EntityData.Component componentData = componentSerializer.serialize(component, fieldCheck);
        if (componentData != null) {
            entity.addComponent(componentData);
        }
    }
    return entity.build();
}
Also used : EntityData(org.terasology.protobuf.EntityData) EntityInfoComponent(org.terasology.entitySystem.entity.internal.EntityInfoComponent) Component(org.terasology.entitySystem.Component) EntityRef(org.terasology.entitySystem.entity.EntityRef) EntityScope(org.terasology.entitySystem.entity.internal.EntityScope)

Example 4 with Component

use of org.terasology.entitySystem.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.getUri().toString());
        }
    }
    return entity.build();
}
Also used : EntityData(org.terasology.protobuf.EntityData) EntityInfoComponent(org.terasology.entitySystem.entity.internal.EntityInfoComponent) Component(org.terasology.entitySystem.Component) EntityRef(org.terasology.entitySystem.entity.EntityRef) EntityScope(org.terasology.entitySystem.entity.internal.EntityScope)

Example 5 with Component

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

the class NetworkEntitySerializer method serializeEntityFull.

private EntityData.PackedEntity.Builder serializeEntityFull(EntityRef entityRef, FieldSerializeCheck<Component> fieldCheck) {
    EntityData.PackedEntity.Builder entity = EntityData.PackedEntity.newBuilder();
    ByteString.Output fieldIds = ByteString.newOutput();
    ByteString.Output componentFieldCounts = ByteString.newOutput();
    for (Component component : entityRef.iterateComponents()) {
        if (!componentSerializeCheck.serialize(componentLibrary.getMetadata(component.getClass()))) {
            continue;
        }
        serializeComponentFull(component, false, fieldCheck, entity, fieldIds, componentFieldCounts, true);
    }
    entity.setFieldIds(fieldIds.toByteString());
    entity.setComponentFieldCounts(componentFieldCounts.toByteString());
    return entity;
}
Also used : ByteString(com.google.protobuf.ByteString) Component(org.terasology.entitySystem.Component)

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