Search in sources :

Example 1 with Prefab

use of org.terasology.entitySystem.prefab.Prefab in project Terasology by MovingBlocks.

the class PrefabSerializer method serialize.

/**
 * @param prefab
 * @return The serialized prefab
 */
public EntityData.Prefab serialize(Prefab prefab) {
    EntityData.Prefab.Builder prefabData = EntityData.Prefab.newBuilder();
    prefabData.setName(prefab.getName());
    if (prefab.getParent() != null) {
        prefabData.setParentName(prefab.getParent().getName());
    }
    prefabData.setAlwaysRelevant(prefab.isAlwaysRelevant());
    prefabData.setPersisted(prefab.isPersisted());
    // Delta off the parent
    for (Component component : prefab.iterateComponents()) {
        if (prefab.getParent() != null && prefab.getParent().hasComponent(component.getClass())) {
            EntityData.Component serializedComponent = componentSerializer.serialize(prefab.getParent().getComponent(component.getClass()), component);
            if (serializedComponent != null) {
                prefabData.addComponent(serializedComponent);
            }
        } else {
            prefabData.addComponent(componentSerializer.serialize(component));
        }
    }
    if (prefab.getParent() != null) {
        for (Component parentComp : prefab.getParent().iterateComponents()) {
            if (!prefab.hasComponent(parentComp.getClass())) {
                prefabData.addRemovedComponent(componentLibrary.getMetadata(parentComp).getUri().toString());
            }
        }
    }
    return prefabData.build();
}
Also used : EntityData(org.terasology.protobuf.EntityData) Component(org.terasology.entitySystem.Component) Prefab(org.terasology.entitySystem.prefab.Prefab)

Example 2 with Prefab

use of org.terasology.entitySystem.prefab.Prefab 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 3 with Prefab

use of org.terasology.entitySystem.prefab.Prefab in project Terasology by MovingBlocks.

the class CharacterSystem method getInstigatorName.

/**
 * Extracts the name from an entity.
 * If the entity is a character, then the display name from the {@link ClientComponent#clientInfo} is used.
 * Otherwise the entity itself is checked for a {@link DisplayNameComponent}.
 * In the last case, the prefab name of the entity is used, e.g. "engine:player" will be parsed to "Player".
 * @param instigator The entity for which an instigator name is needed.
 * @return The instigator name.
 */
public String getInstigatorName(EntityRef instigator) {
    if (instigator.hasComponent(CharacterComponent.class)) {
        EntityRef instigatorClient = instigator.getComponent(CharacterComponent.class).controller;
        EntityRef instigatorClientInfo = instigatorClient.getComponent(ClientComponent.class).clientInfo;
        DisplayNameComponent displayNameComponent = instigatorClientInfo.getComponent(DisplayNameComponent.class);
        return displayNameComponent.name;
    } else if (instigator.getParentPrefab() != null) {
        // A DisplayName can be specified in the entity prefab
        // Otherwise, the game will attempt to generate one from the name of that prefab
        Prefab parentPrefab = instigator.getParentPrefab();
        if (parentPrefab.hasComponent(DisplayNameComponent.class)) {
            DisplayNameComponent displayNameComponent = parentPrefab.getComponent(DisplayNameComponent.class);
            return displayNameComponent.name;
        } else {
            String instigatorName = parentPrefab.getName();
            // getParentPrefab.getName() returns a ResourceUrn String such as "engine:player"
            // The following calls change the damage type to be more readable
            // For instance, "engine:player" becomes "Player"
            instigatorName = instigatorName.replaceAll(".*:(.*)", "$1");
            instigatorName = Character.toUpperCase(instigatorName.charAt(0)) + instigatorName.substring(1);
            return instigatorName;
        }
    } else {
        return null;
    }
}
Also used : DisplayNameComponent(org.terasology.logic.common.DisplayNameComponent) PlayerCharacterComponent(org.terasology.logic.players.PlayerCharacterComponent) EntityRef(org.terasology.entitySystem.entity.EntityRef) ClientComponent(org.terasology.network.ClientComponent) Prefab(org.terasology.entitySystem.prefab.Prefab)

Example 4 with Prefab

use of org.terasology.entitySystem.prefab.Prefab in project Terasology by MovingBlocks.

the class VisualCharacterSystem method onCreateDefaultVisualCharacter.

@ReceiveEvent(priority = EventPriority.PRIORITY_TRIVIAL)
public void onCreateDefaultVisualCharacter(CreateVisualCharacterEvent event, EntityRef characterEntity) {
    Prefab prefab = assetManager.getAsset("engine:defaultVisualCharacter", Prefab.class).get();
    EntityBuilder entityBuilder = event.getVisualCharacterBuilder();
    entityBuilder.addPrefab(prefab);
    event.consume();
}
Also used : EntityBuilder(org.terasology.entitySystem.entity.EntityBuilder) Prefab(org.terasology.entitySystem.prefab.Prefab) ReceiveEvent(org.terasology.entitySystem.event.ReceiveEvent)

Example 5 with Prefab

use of org.terasology.entitySystem.prefab.Prefab in project Terasology by MovingBlocks.

the class ModuleConfigSystem method preBegin.

@Override
public void preBegin() {
    for (Prefab prefab : prefabManager.listPrefabs(ModuleConfigComponent.class)) {
        ModuleConfigComponent moduleConfig = prefab.getComponent(ModuleConfigComponent.class);
        String moduleName = moduleConfig.moduleName;
        Map<String, String> properties;
        if (propertiesPerModule.containsKey(moduleName)) {
            logger.error("Encountered more than one Module Config for module - " + moduleName + ", this is not recommended, as the property values visible are not going to be well defined.");
            properties = propertiesPerModule.get(moduleName);
        } else {
            properties = new HashMap<>();
            propertiesPerModule.put(moduleName, properties);
        }
        properties.putAll(moduleConfig.properties);
    }
}
Also used : Prefab(org.terasology.entitySystem.prefab.Prefab)

Aggregations

Prefab (org.terasology.entitySystem.prefab.Prefab)49 PojoPrefab (org.terasology.entitySystem.prefab.internal.PojoPrefab)20 PrefabData (org.terasology.entitySystem.prefab.PrefabData)13 Test (org.junit.Test)11 ResourceUrn (org.terasology.assets.ResourceUrn)9 Component (org.terasology.entitySystem.Component)9 EntityRef (org.terasology.entitySystem.entity.EntityRef)9 ModuleAwareAssetTypeManager (org.terasology.assets.module.ModuleAwareAssetTypeManager)7 ContextImpl (org.terasology.context.internal.ContextImpl)6 ModuleManager (org.terasology.engine.module.ModuleManager)6 StringComponent (org.terasology.entitySystem.stubs.StringComponent)6 Command (org.terasology.logic.console.commandSystem.annotations.Command)6 ClientComponent (org.terasology.network.ClientComponent)6 Before (org.junit.Before)5 EntityBuilder (org.terasology.entitySystem.entity.EntityBuilder)5 NetworkComponent (org.terasology.network.NetworkComponent)5 BeforeClass (org.junit.BeforeClass)4 NetworkSystem (org.terasology.network.NetworkSystem)4 EngineEntityManager (org.terasology.entitySystem.entity.internal.EngineEntityManager)3 AssetManager (org.terasology.assets.management.AssetManager)2