Search in sources :

Example 16 with Component

use of org.terasology.entitySystem.Component 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 17 with Component

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

the class PojoEntityManager method saveComponent.

/**
 * Saves a component to an entity
 *
 * @param entityId
 * @param component
 */
@Override
public void saveComponent(long entityId, Component component) {
    Optional<Component> oldComponent = getPool(entityId).map(pool -> pool.getComponentStore().put(entityId, component));
    if (!oldComponent.isPresent()) {
        logger.error("Saving a component ({}) that doesn't belong to this entity {}", component.getClass(), entityId);
    }
    if (eventSystem != null) {
        EntityRef entityRef = getEntity(entityId);
        if (!oldComponent.isPresent()) {
            eventSystem.send(entityRef, OnAddedComponent.newInstance(), component);
            eventSystem.send(entityRef, OnActivatedComponent.newInstance(), component);
        } else {
            eventSystem.send(entityRef, OnChangedComponent.newInstance(), component);
        }
    }
    if (!oldComponent.isPresent()) {
        notifyComponentAdded(getEntity(entityId), component.getClass());
    } else {
        notifyComponentChanged(getEntity(entityId), component.getClass());
    }
}
Also used : BeforeRemoveComponent(org.terasology.entitySystem.entity.lifecycleEvents.BeforeRemoveComponent) OnActivatedComponent(org.terasology.entitySystem.entity.lifecycleEvents.OnActivatedComponent) SectorSimulationComponent(org.terasology.entitySystem.sectors.SectorSimulationComponent) OnChangedComponent(org.terasology.entitySystem.entity.lifecycleEvents.OnChangedComponent) BeforeDeactivateComponent(org.terasology.entitySystem.entity.lifecycleEvents.BeforeDeactivateComponent) OnAddedComponent(org.terasology.entitySystem.entity.lifecycleEvents.OnAddedComponent) Component(org.terasology.entitySystem.Component) EntityRef(org.terasology.entitySystem.entity.EntityRef)

Example 18 with Component

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

the class IterateMultipleComponentBenchmark method setup.

@Override
public void setup() {
    FastRandom rand = new FastRandom(0L);
    rawEntityData = Lists.newArrayList();
    for (int i = 0; i < 1000; ++i) {
        List<Component> entityData = Lists.newArrayList();
        if (rand.nextFloat() < 0.75f) {
            entityData.add(new LocationComponent());
        }
        if (rand.nextFloat() < 0.5f) {
            entityData.add(new MeshComponent());
        }
        if (rand.nextFloat() < 0.25f) {
            entityData.add(new BlockComponent());
        }
        rawEntityData.add(entityData);
    }
    entityManager = new PojoEntityManager();
    for (List<Component> rawEntity : rawEntityData) {
        entityManager.create(rawEntity);
    }
}
Also used : BlockComponent(org.terasology.world.block.BlockComponent) MeshComponent(org.terasology.rendering.logic.MeshComponent) PojoEntityManager(org.terasology.entitySystem.entity.internal.PojoEntityManager) FastRandom(org.terasology.utilities.random.FastRandom) MeshComponent(org.terasology.rendering.logic.MeshComponent) BlockComponent(org.terasology.world.block.BlockComponent) Component(org.terasology.entitySystem.Component) LocationComponent(org.terasology.logic.location.LocationComponent) LocationComponent(org.terasology.logic.location.LocationComponent)

Example 19 with Component

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

the class EventSystemImpl method registerEventHandler.

@Override
public void registerEventHandler(ComponentSystem handler) {
    Class handlerClass = handler.getClass();
    if (!Modifier.isPublic(handlerClass.getModifiers())) {
        logger.error("Cannot register handler {}, must be public", handler.getClass().getName());
        return;
    }
    logger.debug("Registering event handler " + handlerClass.getName());
    for (Method method : handlerClass.getMethods()) {
        ReceiveEvent receiveEventAnnotation = method.getAnnotation(ReceiveEvent.class);
        if (receiveEventAnnotation != null) {
            if (!receiveEventAnnotation.netFilter().isValidFor(networkSystem.getMode(), false)) {
                continue;
            }
            Set<Class<? extends Component>> requiredComponents = Sets.newLinkedHashSet();
            method.setAccessible(true);
            Class<?>[] types = method.getParameterTypes();
            logger.debug("Found method: " + method.toString());
            if (!Event.class.isAssignableFrom(types[0]) || !EntityRef.class.isAssignableFrom(types[1])) {
                logger.error("Invalid event handler method: {}", method.getName());
                return;
            }
            requiredComponents.addAll(Arrays.asList(receiveEventAnnotation.components()));
            List<Class<? extends Component>> componentParams = Lists.newArrayList();
            for (int i = 2; i < types.length; ++i) {
                if (!Component.class.isAssignableFrom(types[i])) {
                    logger.error("Invalid event handler method: {} - {} is not a component class", method.getName(), types[i]);
                    return;
                }
                requiredComponents.add((Class<? extends Component>) types[i]);
                componentParams.add((Class<? extends Component>) types[i]);
            }
            ByteCodeEventHandlerInfo handlerInfo = new ByteCodeEventHandlerInfo(handler, method, receiveEventAnnotation.priority(), receiveEventAnnotation.activity(), requiredComponents, componentParams);
            addEventHandler((Class<? extends Event>) types[0], handlerInfo, requiredComponents);
        }
    }
}
Also used : ReceiveEvent(org.terasology.entitySystem.event.ReceiveEvent) Method(java.lang.reflect.Method) NetworkComponent(org.terasology.network.NetworkComponent) BlockComponent(org.terasology.world.block.BlockComponent) Component(org.terasology.entitySystem.Component)

Example 20 with Component

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

the class Actor method getComponentField.

public Object getComponentField(ComponentFieldUri uri) {
    ComponentLibrary componentLibrary = CoreRegistry.get(EntitySystemLibrary.class).getComponentLibrary();
    ComponentMetadata<? extends Component> metadata = componentLibrary.getMetadata(uri.getComponentUri());
    if (metadata == null) {
        return null;
    }
    Component component = entity.getComponent(metadata.getType());
    if (component == null) {
        return null;
    }
    FieldMetadata<?, ?> fieldMetadata = metadata.getField(uri.getFieldName());
    if (fieldMetadata == null) {
        return null;
    }
    Field field = fieldMetadata.getField();
    try {
        return field.get(component);
    } catch (IllegalAccessException e) {
        throw new RuntimeException(e);
    }
}
Also used : Field(java.lang.reflect.Field) EntitySystemLibrary(org.terasology.entitySystem.metadata.EntitySystemLibrary) ComponentLibrary(org.terasology.entitySystem.metadata.ComponentLibrary) 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