use of org.terasology.gestalt.entitysystem.component.Component in project Terasology by MovingBlocks.
the class PreviewWorldScreen method resetEnvironment.
private void resetEnvironment() {
CoreRegistry.setContext(context);
if (environment != null) {
EnvironmentSwitchHandler environmentSwitchHandler = context.get(EnvironmentSwitchHandler.class);
environmentSwitchHandler.handleSwitchBackFromPreviewEnvironment(subContext);
environment.close();
environment = null;
}
previewGen.close();
WorldConfigurator worldConfig = worldGenerator.getConfigurator();
Map<String, Component> params = worldConfig.getProperties();
if (params != null) {
config.setModuleConfigs(worldGenerator.getUri(), params);
}
}
use of org.terasology.gestalt.entitysystem.component.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.gestalt.entitysystem.component.Component in project Terasology by MovingBlocks.
the class PojoEntityManager method deactivateForStorage.
@Override
public void deactivateForStorage(EntityRef entity) {
if (!entity.exists()) {
return;
}
long entityId = entity.getId();
if (eventSystem != null) {
eventSystem.send(entity, BeforeDeactivateComponent.newInstance());
}
List<Component> components = Collections.unmodifiableList(getPool(entityId).map(pool -> pool.getComponentStore().getComponentsInNewList(entityId)).orElse(Collections.emptyList()));
notifyBeforeDeactivation(entity, components);
for (Component component : components) {
getPool(entityId).ifPresent(pool -> pool.getComponentStore().remove(entityId, component.getClass()));
}
loadedIds.remove(entityId);
}
use of org.terasology.gestalt.entitysystem.component.Component in project Terasology by MovingBlocks.
the class EntityBuilder method build.
/**
* Produces an entity with the components contained in this entity builder
*
* @return The built entity.
*/
public EntityRef build() {
if (id.isPresent() && !entityManager.registerId(id.get())) {
return EntityRef.NULL;
}
long finalId = id.orElse(entityManager.createEntity());
components.values().forEach(c -> entityManager.getComponentStore().put(finalId, c));
entityManager.assignToPool(finalId, pool);
EntityRef entity = entityManager.getEntity(finalId);
if (sendLifecycleEvents && entityManager.getEventSystem() != null) {
// TODO: don't send OnAddedComponent when the entity is being re-loaded from storage
entity.send(OnAddedComponent.newInstance());
entity.send(OnActivatedComponent.newInstance());
}
// Retrieve the components again in case they were modified by the previous events
for (Component component : entityManager.iterateComponents(entity.getId())) {
entityManager.notifyComponentAdded(entity, component.getClass());
}
entity.setScope(scope.orElse(getEntityInfo().scope));
return entity;
}
use of org.terasology.gestalt.entitysystem.component.Component in project Terasology by MovingBlocks.
the class ItemPickupAuthoritySystem method onDropItemEvent.
@ReceiveEvent
public void onDropItemEvent(DropItemEvent event, EntityRef itemEntity, ItemComponent itemComponent) {
for (Component component : itemComponent.pickupPrefab.iterateComponents()) {
Component componentCopy = library.getComponentLibrary().copy(component);
if (componentCopy instanceof LocationComponent) {
((LocationComponent) componentCopy).setWorldPosition(event.getPosition());
}
itemEntity.addOrSaveComponent(componentCopy);
}
if (!itemEntity.hasComponent(LocationComponent.class)) {
itemEntity.addComponent(new LocationComponent(event.getPosition()));
}
}
Aggregations