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