Search in sources :

Example 41 with Component

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

the class ConditionAction method condition.

protected boolean condition(Actor actor) throws ClassNotFoundException, NoSuchFieldException, IllegalAccessException {
    boolean passing = true;
    if (componentAbsent != null && actor.hasComponent(componentLibrary.resolve(componentAbsent).getType())) {
        passing = false;
    }
    if (componentPresent != null) {
        Component component = actor.getComponent(componentLibrary.resolve(componentPresent).getType());
        if (component == null) {
            passing = false;
        } else {
            // Check values
            if (values != null) {
                for (String value : values) {
                    String[] tokens = value.split(" ");
                    Object fieldValue = component.getClass().getDeclaredField(tokens[1]).get(component);
                    String secondValue;
                    switch(tokens[0]) {
                        // Read second value from the definition
                        case "V":
                            secondValue = tokens[3];
                            break;
                        // Read second value from a field of the component
                        case "F":
                            secondValue = component.getClass().getDeclaredField(tokens[3]).get(component).toString();
                            break;
                        // No second value needed.
                        case "N":
                            secondValue = "";
                            break;
                        default:
                            logger.error("Unsupported guard value type: {}", tokens[0]);
                            secondValue = "";
                    }
                    // Can't use a switch for this :(
                    if (fieldValue instanceof Boolean) {
                        switch(tokens[2]) {
                            case "=":
                            case "==":
                                passing = (Boolean) fieldValue == Boolean.parseBoolean(secondValue);
                                break;
                            case "!":
                            case "!=":
                                passing = (Boolean) fieldValue != Boolean.parseBoolean(secondValue);
                                break;
                            default:
                                logger.error("Unsupported operation for boolean values: {}", tokens[2]);
                        }
                    } else if (fieldValue instanceof Number) {
                        switch(tokens[2]) {
                            case "=":
                            case "==":
                                passing = (Double) fieldValue == Double.parseDouble(secondValue);
                                break;
                            case "!":
                            case "!=":
                                passing = (Double) fieldValue == Double.parseDouble(secondValue);
                                break;
                            case "<=":
                                passing = ((Number) fieldValue).doubleValue() <= Double.parseDouble(secondValue);
                                break;
                            case ">=":
                                passing = ((Number) fieldValue).doubleValue() >= Double.parseDouble(secondValue);
                                break;
                            case ">":
                                passing = ((Number) fieldValue).doubleValue() > Double.parseDouble(secondValue);
                                break;
                            case "<":
                                passing = ((Number) fieldValue).doubleValue() < Double.parseDouble(secondValue);
                                break;
                            default:
                                logger.error("Unsupported operation for numeric values: {}", tokens[2]);
                        }
                    } else if (fieldValue instanceof String) {
                        switch(tokens[2]) {
                            case "=":
                            case "==":
                                passing = fieldValue.equals(secondValue);
                                break;
                            case "!":
                            case "!=":
                                passing = !fieldValue.equals(secondValue);
                                break;
                            default:
                                logger.error("Unsupported operation for strings: {}", tokens[2]);
                        }
                    } else {
                        if (fieldValue == null) {
                            if (!tokens[2].equals("null")) {
                                // If a more complex check is requested and the field is null, fail
                                passing = false;
                            }
                        } else {
                            switch(tokens[2]) {
                                // Null check
                                case "exists":
                                    if (fieldValue instanceof EntityRef && fieldValue == EntityRef.NULL) {
                                        passing = false;
                                    }
                                    break;
                                // Collection checks
                                case "empty":
                                    if (fieldValue instanceof Collection) {
                                        passing = ((Collection<?>) fieldValue).isEmpty();
                                    }
                                    break;
                                case "nonEmpty":
                                    if (fieldValue instanceof Collection) {
                                        passing = !((Collection<?>) fieldValue).isEmpty();
                                    }
                                    break;
                                default:
                                    logger.error("Unknown field type or operation: {} {}", fieldValue.getClass(), tokens[2]);
                            }
                        }
                    }
                }
            }
        }
    }
    return passing;
}
Also used : Collection(java.util.Collection) Component(org.terasology.entitySystem.Component) EntityRef(org.terasology.entitySystem.entity.EntityRef)

Example 42 with Component

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

the class BehaviorEditorScreen method onEntitySelected.

private void onEntitySelected(Interpreter value, PropertyProvider provider) {
    if (selectedInterpreter != null) {
        selectedInterpreter.setCallback(null);
    }
    selectedInterpreter = value;
    if (selectedInterpreter != null) {
        EntityRef entity = value.actor().getEntity();
        onTreeSelected(selectedInterpreter.getTree());
        entityProperties.clear();
        for (Component component : entity.iterateComponents()) {
            String name = component.getClass().getSimpleName().replace("Component", "");
            List<Property<?, ?>> componentProperties = provider.createProperties(component);
            entityProperties.addProperties(name, componentProperties);
        }
        selectedInterpreter.setCallback(behaviorEditor);
    }
}
Also used : BehaviorComponent(org.terasology.logic.behavior.BehaviorComponent) Component(org.terasology.entitySystem.Component) EntityRef(org.terasology.entitySystem.entity.EntityRef) Property(org.terasology.rendering.nui.properties.Property)

Example 43 with Component

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

the class EntityCreateBenchmark 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);
    }
}
Also used : BlockComponent(org.terasology.world.block.BlockComponent) MeshComponent(org.terasology.rendering.logic.MeshComponent) 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 44 with Component

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

the class IterateSingleComponentBenchmark 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 45 with Component

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

the class Config method setModuleConfigs.

/**
 * @param generatorUri the generator Uri
 * @param configs      the new config params for the world generator
 */
public void setModuleConfigs(SimpleUri generatorUri, Map<String, Component> configs) {
    Gson gson = createGsonForModules();
    Map<String, JsonElement> map = Maps.newHashMap();
    for (Map.Entry<String, Component> entry : configs.entrySet()) {
        JsonElement json = gson.toJsonTree(entry.getValue());
        map.put(entry.getKey(), json);
    }
    config.getModuleConfigs().put(generatorUri, map);
}
Also used : JsonElement(com.google.gson.JsonElement) Gson(com.google.gson.Gson) Component(org.terasology.entitySystem.Component) Map(java.util.Map)

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