Search in sources :

Example 21 with EntityRef

use of org.terasology.entitySystem.entity.EntityRef in project Terasology by MovingBlocks.

the class SaveTransaction method createChunkPosToUnsavedOwnerLessEntitiesMap.

private Map<Vector3i, Collection<EntityRef>> createChunkPosToUnsavedOwnerLessEntitiesMap() {
    Map<Vector3i, Collection<EntityRef>> chunkPosToEntitiesMap = Maps.newHashMap();
    for (EntityRef entity : privateEntityManager.getEntitiesWith(LocationComponent.class)) {
        /*
             * Note: Entities with owners get saved with the owner. Entities that are always relevant don't get stored
             * in chunk as the chunk is not always loaded
             */
        if (entity.isPersistent() && !entity.getOwner().exists() && !entity.hasComponent(ClientComponent.class) && !entity.isAlwaysRelevant()) {
            LocationComponent locationComponent = entity.getComponent(LocationComponent.class);
            if (locationComponent != null) {
                Vector3f loc = locationComponent.getWorldPosition();
                Vector3i chunkPos = ChunkMath.calcChunkPos((int) loc.x, (int) loc.y, (int) loc.z);
                Collection<EntityRef> collection = chunkPosToEntitiesMap.get(chunkPos);
                if (collection == null) {
                    collection = Lists.newArrayList();
                    chunkPosToEntitiesMap.put(chunkPos, collection);
                }
                collection.add(entity);
            }
        }
    }
    return chunkPosToEntitiesMap;
}
Also used : Vector3f(org.terasology.math.geom.Vector3f) Vector3i(org.terasology.math.geom.Vector3i) Collection(java.util.Collection) EntityRef(org.terasology.entitySystem.entity.EntityRef) ClientComponent(org.terasology.network.ClientComponent) LocationComponent(org.terasology.logic.location.LocationComponent)

Example 22 with EntityRef

use of org.terasology.entitySystem.entity.EntityRef in project Terasology by MovingBlocks.

the class EntitySerializer method serializeEntityFull.

private EntityData.Entity serializeEntityFull(EntityRef entityRef, FieldSerializeCheck<Component> fieldCheck) {
    EntityData.Entity.Builder entity = EntityData.Entity.newBuilder();
    if (!ignoringEntityId) {
        entity.setId(entityRef.getId());
    }
    entity.setAlwaysRelevant(entityRef.isAlwaysRelevant());
    EntityRef owner = entityRef.getOwner();
    if (owner.exists()) {
        entity.setOwner(owner.getId());
    }
    EntityScope scope = entityRef.getScope();
    if (scope != null) {
        switch(scope) {
            case GLOBAL:
                entity.setScope(GLOBAL);
                break;
            case SECTOR:
                entity.setScope(SECTOR);
                break;
            case CHUNK:
                entity.setScope(CHUNK);
                break;
        }
    }
    for (Component component : entityRef.iterateComponents()) {
        if (!componentSerializeCheck.serialize(componentLibrary.getMetadata(component.getClass()))) {
            continue;
        }
        EntityData.Component componentData = componentSerializer.serialize(component, fieldCheck);
        if (componentData != null) {
            entity.addComponent(componentData);
        }
    }
    return entity.build();
}
Also used : EntityData(org.terasology.protobuf.EntityData) EntityInfoComponent(org.terasology.entitySystem.entity.internal.EntityInfoComponent) Component(org.terasology.entitySystem.Component) EntityRef(org.terasology.entitySystem.entity.EntityRef) EntityScope(org.terasology.entitySystem.entity.internal.EntityScope)

Example 23 with EntityRef

use of org.terasology.entitySystem.entity.EntityRef in project Terasology by MovingBlocks.

the class EntitySerializer method serializeEntityDelta.

private EntityData.Entity serializeEntityDelta(EntityRef entityRef, Prefab prefab, FieldSerializeCheck<Component> fieldCheck) {
    EntityData.Entity.Builder entity = EntityData.Entity.newBuilder();
    if (!ignoringEntityId) {
        entity.setId(entityRef.getId());
    }
    entity.setParentPrefab(prefab.getName());
    if (entityRef.isAlwaysRelevant() != prefab.isAlwaysRelevant()) {
        entity.setAlwaysRelevant(entityRef.isAlwaysRelevant());
    }
    EntityRef owner = entityRef.getOwner();
    if (owner.exists()) {
        entity.setOwner(owner.getId());
    }
    EntityScope scope = entityRef.getScope();
    if (scope != null) {
        switch(scope) {
            case GLOBAL:
                entity.setScope(GLOBAL);
                break;
            case SECTOR:
                entity.setScope(SECTOR);
                break;
            case CHUNK:
                entity.setScope(CHUNK);
                break;
        }
    }
    Set<Class<? extends Component>> presentClasses = Sets.newHashSet();
    for (Component component : entityRef.iterateComponents()) {
        if (!componentSerializeCheck.serialize(componentLibrary.getMetadata(component.getClass()))) {
            continue;
        }
        presentClasses.add(component.getClass());
        Component prefabComponent = prefab.getComponent(component.getClass());
        EntityData.Component componentData;
        if (prefabComponent == null) {
            componentData = componentSerializer.serialize(component, fieldCheck);
        } else {
            componentData = componentSerializer.serialize(prefabComponent, component, fieldCheck);
        }
        if (componentData != null) {
            entity.addComponent(componentData);
        }
    }
    for (Component prefabComponent : prefab.iterateComponents()) {
        ComponentMetadata<?> metadata = componentLibrary.getMetadata(prefabComponent.getClass());
        if (!presentClasses.contains(prefabComponent.getClass()) && componentSerializeCheck.serialize(metadata)) {
            // TODO: Use component ids here
            entity.addRemovedComponent(metadata.getUri().toString());
        }
    }
    return entity.build();
}
Also used : EntityData(org.terasology.protobuf.EntityData) EntityInfoComponent(org.terasology.entitySystem.entity.internal.EntityInfoComponent) Component(org.terasology.entitySystem.Component) EntityRef(org.terasology.entitySystem.entity.EntityRef) EntityScope(org.terasology.entitySystem.entity.internal.EntityScope)

Example 24 with EntityRef

use of org.terasology.entitySystem.entity.EntityRef in project Terasology by MovingBlocks.

the class WorldSerializerImpl method serializeWorld.

@Override
public EntityData.GlobalStore serializeWorld(boolean verbose) {
    final EntityData.GlobalStore.Builder world = EntityData.GlobalStore.newBuilder();
    if (!verbose) {
        writeComponentTypeTable(world);
    }
    for (Prefab prefab : prefabManager.listPrefabs()) {
        world.addPrefab(prefabSerializer.serialize(prefab));
    }
    for (EntityRef entity : entityManager.getAllEntities()) {
        if (verbose || entity.isPersistent()) {
            world.addEntity(entitySerializer.serialize(entity));
        }
    }
    writeIdInfo(world);
    entitySerializer.removeComponentIdMapping();
    prefabSerializer.removeComponentIdMapping();
    return world.build();
}
Also used : Prefab(org.terasology.entitySystem.prefab.Prefab) EntityRef(org.terasology.entitySystem.entity.EntityRef)

Example 25 with EntityRef

use of org.terasology.entitySystem.entity.EntityRef in project Terasology by MovingBlocks.

the class EntityRefTypeHandler method deserializeCollection.

@Override
public List<EntityRef> deserializeCollection(PersistedData data, DeserializationContext context) {
    PersistedDataArray array = data.getAsArray();
    List<EntityRef> result = Lists.newArrayListWithCapacity(array.size());
    addEntitiesFromLongArray(result, array);
    return result;
}
Also used : PersistedDataArray(org.terasology.persistence.typeHandling.PersistedDataArray) EntityRef(org.terasology.entitySystem.entity.EntityRef)

Aggregations

EntityRef (org.terasology.entitySystem.entity.EntityRef)337 Test (org.junit.Test)106 ClientComponent (org.terasology.network.ClientComponent)49 LocationComponent (org.terasology.logic.location.LocationComponent)45 Vector3f (org.terasology.math.geom.Vector3f)44 ReceiveEvent (org.terasology.entitySystem.event.ReceiveEvent)36 Vector3i (org.terasology.math.geom.Vector3i)34 Command (org.terasology.logic.console.commandSystem.annotations.Command)28 StringComponent (org.terasology.entitySystem.stubs.StringComponent)26 NetworkComponent (org.terasology.network.NetworkComponent)21 EntityData (org.terasology.protobuf.EntityData)21 DisplayNameComponent (org.terasology.logic.common.DisplayNameComponent)17 Block (org.terasology.world.block.Block)16 Component (org.terasology.entitySystem.Component)15 EntityBuilder (org.terasology.entitySystem.entity.EntityBuilder)15 CharacterComponent (org.terasology.logic.characters.CharacterComponent)14 Quat4f (org.terasology.math.geom.Quat4f)14 BlockComponent (org.terasology.world.block.BlockComponent)13 Map (java.util.Map)11 LocalPlayer (org.terasology.logic.players.LocalPlayer)11