Search in sources :

Example 31 with Component

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;
}
Also used : EntityInfoComponent(org.terasology.entitySystem.entity.internal.EntityInfoComponent) OnActivatedComponent(org.terasology.entitySystem.entity.lifecycleEvents.OnActivatedComponent) OnAddedComponent(org.terasology.entitySystem.entity.lifecycleEvents.OnAddedComponent) Component(org.terasology.entitySystem.Component)

Example 32 with Component

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);
}
Also used : BeforeRemoveComponent(org.terasology.entitySystem.entity.lifecycleEvents.BeforeRemoveComponent) OnActivatedComponent(org.terasology.entitySystem.entity.lifecycleEvents.OnActivatedComponent) SectorSimulationComponent(org.terasology.entitySystem.sectors.SectorSimulationComponent) OnChangedComponent(org.terasology.entitySystem.entity.lifecycleEvents.OnChangedComponent) BeforeDeactivateComponent(org.terasology.entitySystem.entity.lifecycleEvents.BeforeDeactivateComponent) OnAddedComponent(org.terasology.entitySystem.entity.lifecycleEvents.OnAddedComponent) Component(org.terasology.entitySystem.Component)

Example 33 with Component

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;
}
Also used : BeforeRemoveComponent(org.terasology.entitySystem.entity.lifecycleEvents.BeforeRemoveComponent) OnActivatedComponent(org.terasology.entitySystem.entity.lifecycleEvents.OnActivatedComponent) SectorSimulationComponent(org.terasology.entitySystem.sectors.SectorSimulationComponent) OnChangedComponent(org.terasology.entitySystem.entity.lifecycleEvents.OnChangedComponent) BeforeDeactivateComponent(org.terasology.entitySystem.entity.lifecycleEvents.BeforeDeactivateComponent) OnAddedComponent(org.terasology.entitySystem.entity.lifecycleEvents.OnAddedComponent) Component(org.terasology.entitySystem.Component) EntityRef(org.terasology.entitySystem.entity.EntityRef)

Example 34 with 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);
        }
    }
}
Also used : NetworkComponent(org.terasology.network.NetworkComponent) NetworkComponent(org.terasology.network.NetworkComponent) Component(org.terasology.entitySystem.Component) EntityRef(org.terasology.entitySystem.entity.EntityRef)

Example 35 with Component

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();
}
Also used : TIntIterator(gnu.trove.iterator.TIntIterator) EntityData(org.terasology.protobuf.EntityData) BlockComponent(org.terasology.world.block.BlockComponent) NetworkComponent(org.terasology.network.NetworkComponent) Component(org.terasology.entitySystem.Component) EntityRef(org.terasology.entitySystem.entity.EntityRef) ClientComponentFieldCheck(org.terasology.network.serialization.ClientComponentFieldCheck)

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