use of org.terasology.entitySystem.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.entitySystem.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.entitySystem.Component in project Terasology by MovingBlocks.
the class PojoEntityManager method addComponent.
/**
* Adds (or replaces) a component to an entity
*
* @param entityId
* @param component
* @param <T>
* @return The added component
*/
@Override
public <T extends Component> T addComponent(long entityId, T component) {
Preconditions.checkNotNull(component);
Optional<Component> oldComponent = getPool(entityId).map(pool -> pool.getComponentStore().put(entityId, component));
if (!oldComponent.isPresent()) {
notifyComponentAdded(getEntity(entityId), component.getClass());
} else {
logger.error("Adding a component ({}) over an existing component for entity {}", component.getClass(), entityId);
notifyComponentChanged(getEntity(entityId), component.getClass());
}
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);
}
}
return component;
}
use of org.terasology.entitySystem.Component in project Terasology by MovingBlocks.
the class NetworkSystemImpl method updateOwnership.
public void updateOwnership(EntityRef entity) {
NetworkComponent netComponent = entity.getComponent(NetworkComponent.class);
if (netComponent == null || netComponent.getNetworkId() == NULL_NET_ID) {
return;
}
EntityRef lastOwnerEntity = ownerLookup.get(entity);
if (lastOwnerEntity == null) {
lastOwnerEntity = EntityRef.NULL;
}
EntityRef newOwnerEntity = entity.getOwner();
if (!Objects.equal(lastOwnerEntity, newOwnerEntity)) {
NetClient lastOwner = getNetOwner(lastOwnerEntity);
NetClient newOwner = getNetOwner(newOwnerEntity);
if (!Objects.equal(lastOwner, newOwner)) {
recursiveUpdateOwnership(entity, lastOwner, newOwner);
if (newOwner != null) {
int id = netComponent.getNetworkId();
for (Component component : entity.iterateComponents()) {
if (componentLibrary.getMetadata(component.getClass()).isReplicated()) {
newOwner.setComponentDirty(id, component.getClass());
}
}
}
}
if (lastOwnerEntity.exists()) {
ownedLookup.remove(lastOwnerEntity, entity);
}
if (newOwnerEntity.exists()) {
ownerLookup.put(entity, newOwnerEntity);
ownedLookup.put(newOwnerEntity, entity);
} else {
ownerLookup.remove(entity);
}
}
}
use of org.terasology.entitySystem.Component in project Terasology by MovingBlocks.
the class ServerImpl method sendEntities.
private void sendEntities(NetData.NetMessage.Builder message) {
TIntIterator dirtyIterator = netDirty.iterator();
while (dirtyIterator.hasNext()) {
int netId = dirtyIterator.next();
EntityRef entity = networkSystem.getEntity(netId);
if (isOwned(entity)) {
Set<Class<? extends Component>> emptyComponentClassSet = Collections.emptySet();
EntityData.PackedEntity entityData = entitySerializer.serialize(entity, emptyComponentClassSet, changedComponents.get(netId), emptyComponentClassSet, new ClientComponentFieldCheck());
if (entityData != null) {
message.addUpdateEntity(NetData.UpdateEntityMessage.newBuilder().setEntity(entityData).setNetId(netId));
}
}
}
netDirty.clear();
}
Aggregations