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;
}
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);
}
}
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);
}
}
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);
}
}
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);
}
Aggregations