Search in sources :

Example 1 with WorldComponent

use of org.terasology.world.WorldComponent in project Terasology by MovingBlocks.

the class CreateWorldEntity method step.

@Override
public boolean step() {
    EntityManager entityManager = context.get(EntityManager.class);
    ChunkProvider chunkProvider = context.get(ChunkProvider.class);
    Iterator<EntityRef> worldEntityIterator = entityManager.getEntitiesWith(WorldComponent.class).iterator();
    if (worldEntityIterator.hasNext()) {
        EntityRef worldEntity = worldEntityIterator.next();
        chunkProvider.setWorldEntity(worldEntity);
        // get the world generator config from the world entity
        // replace the world generator values from the components in the world entity
        WorldGenerator worldGenerator = context.get(WorldGenerator.class);
        WorldConfigurator worldConfigurator = worldGenerator.getConfigurator();
        Map<String, Component> params = worldConfigurator.getProperties();
        for (Map.Entry<String, Component> entry : params.entrySet()) {
            Class<? extends Component> clazz = entry.getValue().getClass();
            Component comp = worldEntity.getComponent(clazz);
            if (comp != null) {
                worldConfigurator.setProperty(entry.getKey(), comp);
            }
        }
    } else {
        EntityRef worldEntity = entityManager.create();
        worldEntity.addComponent(new WorldComponent());
        NetworkComponent networkComponent = new NetworkComponent();
        networkComponent.replicateMode = NetworkComponent.ReplicateMode.ALWAYS;
        worldEntity.addComponent(networkComponent);
        chunkProvider.setWorldEntity(worldEntity);
        // transfer all world generation parameters from Config to WorldEntity
        WorldGenerator worldGenerator = context.get(WorldGenerator.class);
        SimpleUri generatorUri = worldGenerator.getUri();
        Config config = context.get(Config.class);
        // get the map of properties from the world generator.
        // Replace its values with values from the config set by the UI.
        // Also set all the components to the world entity.
        WorldConfigurator worldConfigurator = worldGenerator.getConfigurator();
        Map<String, Component> params = worldConfigurator.getProperties();
        for (Map.Entry<String, Component> entry : params.entrySet()) {
            Class<? extends Component> clazz = entry.getValue().getClass();
            Component comp = config.getModuleConfig(generatorUri, entry.getKey(), clazz);
            if (comp != null) {
                worldEntity.addComponent(comp);
                worldConfigurator.setProperty(entry.getKey(), comp);
            } else {
                worldEntity.addComponent(entry.getValue());
            }
        }
    }
    return true;
}
Also used : WorldGenerator(org.terasology.world.generator.WorldGenerator) WorldConfigurator(org.terasology.world.generator.WorldConfigurator) Config(org.terasology.config.Config) SimpleUri(org.terasology.engine.SimpleUri) EntityManager(org.terasology.entitySystem.entity.EntityManager) NetworkComponent(org.terasology.network.NetworkComponent) ChunkProvider(org.terasology.world.chunks.ChunkProvider) WorldComponent(org.terasology.world.WorldComponent) NetworkComponent(org.terasology.network.NetworkComponent) WorldComponent(org.terasology.world.WorldComponent) Component(org.terasology.entitySystem.Component) EntityRef(org.terasology.entitySystem.entity.EntityRef) Map(java.util.Map)

Example 2 with WorldComponent

use of org.terasology.world.WorldComponent in project Terasology by MovingBlocks.

the class CreateRemoteWorldEntity method step.

@Override
public boolean step() {
    EntityRef worldEntity = entityManager.create();
    worldEntity.addComponent(new WorldComponent());
    chunkProvider.setWorldEntity(worldEntity);
    return true;
}
Also used : WorldComponent(org.terasology.world.WorldComponent) EntityRef(org.terasology.entitySystem.entity.EntityRef)

Aggregations

EntityRef (org.terasology.entitySystem.entity.EntityRef)2 WorldComponent (org.terasology.world.WorldComponent)2 Map (java.util.Map)1 Config (org.terasology.config.Config)1 SimpleUri (org.terasology.engine.SimpleUri)1 Component (org.terasology.entitySystem.Component)1 EntityManager (org.terasology.entitySystem.entity.EntityManager)1 NetworkComponent (org.terasology.network.NetworkComponent)1 ChunkProvider (org.terasology.world.chunks.ChunkProvider)1 WorldConfigurator (org.terasology.world.generator.WorldConfigurator)1 WorldGenerator (org.terasology.world.generator.WorldGenerator)1