Search in sources :

Example 6 with Prefab

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

the class AnimationScreen method initialise.

@Override
public void initialise() {
    spawnEntityIdButton = find("spawnEntityIdButton", UIButton.class);
    entityDropdown = find("entityDropdown", UIDropdownScrollable.class);
    logger.info("Number of available skeletal meshes: " + assetManager.getAvailableAssets(SkeletalMesh.class).size());
    ArrayList skeletalMesh = new ArrayList(assetManager.getAvailableAssets(SkeletalMesh.class));
    if (entityDropdown != null) {
        entityDropdown.setOptions(skeletalMesh);
    }
    animationSpeedSlider = find("entityAnimationSpeedSlider", UISlider.class);
    if (animationSpeedSlider != null) {
        animationSpeedSlider.setMinimum(-0.0f);
        animationSpeedSlider.setIncrement(0.1f);
        animationSpeedSlider.setRange(10.0f);
        animationSpeedSlider.setPrecision(1);
    }
    spawnEntityIdButton.subscribe(widget -> {
        Vector3f localPlayerPosition = localPlayer.getPosition();
        Quat4f localPlayerRotation = localPlayer.getRotation();
        Vector3f offset = localPlayer.getViewDirection();
        offset.scale(2.0f);
        offset.y = 0;
        localPlayerPosition.add(offset);
        Optional<Prefab> prefab = assetManager.getAsset(entityDropdown.getSelection(), Prefab.class);
        if (prefab.isPresent() && prefab.get().getComponent(LocationComponent.class) != null) {
            entityRef = entityManager.create(prefab.get(), localPlayerPosition, localPlayerRotation);
            SkeletalMeshComponent skeletalMeshComponent = entityRef.getComponent(SkeletalMeshComponent.class);
            skeletalMeshComponent.animationRate = animationSpeedSlider.getValue();
            entityRef.saveComponent(skeletalMeshComponent);
            CharacterMovementComponent movementComponent = entityRef.getComponent(CharacterMovementComponent.class);
            movementComponent.speedMultiplier = animationSpeedSlider.getValue();
            entityRef.saveComponent(movementComponent);
        }
    });
}
Also used : SkeletalMeshComponent(org.terasology.rendering.logic.SkeletalMeshComponent) UISlider(org.terasology.rendering.nui.widgets.UISlider) UIButton(org.terasology.rendering.nui.widgets.UIButton) UIDropdownScrollable(org.terasology.rendering.nui.widgets.UIDropdownScrollable) Vector3f(org.terasology.math.geom.Vector3f) ArrayList(java.util.ArrayList) CharacterMovementComponent(org.terasology.logic.characters.CharacterMovementComponent) SkeletalMesh(org.terasology.rendering.assets.skeletalmesh.SkeletalMesh) Prefab(org.terasology.entitySystem.prefab.Prefab) Quat4f(org.terasology.math.geom.Quat4f)

Example 7 with Prefab

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

the class HealthCommands method damageWithType.

@Command(shortDescription = "Reduce the player's health by an amount by a specific type of damage", runOnServer = true, requiredPermission = PermissionManager.CHEAT_PERMISSION)
public String damageWithType(@Sender EntityRef client, @CommandParam("damageType") String damageType, @CommandParam("amount") int amount) {
    ClientComponent clientComp = client.getComponent(ClientComponent.class);
    Prefab damageTypePrefab = prefabManager.getPrefab(damageType);
    if (damageTypePrefab != null) {
        clientComp.character.send(new DoDamageEvent(amount, damageTypePrefab, clientComp.character));
        return "Inflicted " + amount + " damage of type " + damageType;
    } else {
        return "Specified damage type does not exist.";
    }
}
Also used : ClientComponent(org.terasology.network.ClientComponent) Prefab(org.terasology.entitySystem.prefab.Prefab) Command(org.terasology.logic.console.commandSystem.annotations.Command)

Example 8 with Prefab

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

the class BlockTypeEntityGenerator method generateBlockTypeEntity.

private void generateBlockTypeEntity(Block block) {
    EntityBuilder builder = entityManager.newBuilder(blockTypePrefab);
    builder.getComponent(BlockTypeComponent.class).block = block;
    // TODO: Copy across settings as necessary
    Optional<Prefab> prefab = block.getPrefab();
    if (prefab.isPresent()) {
        for (Component comp : prefab.get().iterateComponents()) {
            if (!(comp instanceof NetworkComponent)) {
                builder.addComponent(entityManager.getComponentLibrary().copy(comp));
            }
        }
    }
    block.setEntity(builder.build());
}
Also used : NetworkComponent(org.terasology.network.NetworkComponent) EntityBuilder(org.terasology.entitySystem.entity.EntityBuilder) NetworkComponent(org.terasology.network.NetworkComponent) Component(org.terasology.entitySystem.Component) Prefab(org.terasology.entitySystem.prefab.Prefab)

Example 9 with Prefab

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

the class EntityAwareWorldProvider method cleanUpTemporaryEntity.

private void cleanUpTemporaryEntity(EntityRef entity) {
    Prefab prefab = entity.getParentPrefab();
    for (Component comp : entity.iterateComponents()) {
        if (!COMMON_BLOCK_COMPONENTS.contains(comp.getClass()) && (prefab == null || !prefab.hasComponent(comp.getClass()))) {
            entity.removeComponent(comp.getClass());
        }
    }
    entity.removeComponent(NetworkComponent.class);
    if (prefab != null) {
        for (Component comp : prefab.iterateComponents()) {
            Component currentComp = entity.getComponent(comp.getClass());
            if (currentComp == null) {
                entity.addComponent(entityManager.getComponentLibrary().copy(comp));
            } else {
                ComponentMetadata<?> metadata = entityManager.getComponentLibrary().getMetadata(comp.getClass());
                boolean changed = false;
                for (FieldMetadata field : metadata.getFields()) {
                    Object expected = field.getValue(comp);
                    if (!Objects.equal(expected, field.getValue(currentComp))) {
                        field.setValue(currentComp, expected);
                        changed = true;
                    }
                }
                if (changed) {
                    entity.saveComponent(currentComp);
                }
            }
        }
    }
    entityManager.destroyEntityWithoutEvents(entity);
}
Also used : FieldMetadata(org.terasology.reflection.metadata.FieldMetadata) OnActivatedComponent(org.terasology.entitySystem.entity.lifecycleEvents.OnActivatedComponent) OnChangedComponent(org.terasology.entitySystem.entity.lifecycleEvents.OnChangedComponent) NetworkComponent(org.terasology.network.NetworkComponent) BlockComponent(org.terasology.world.block.BlockComponent) BeforeDeactivateComponent(org.terasology.entitySystem.entity.lifecycleEvents.BeforeDeactivateComponent) LocationComponent(org.terasology.logic.location.LocationComponent) Component(org.terasology.entitySystem.Component) BlockRegionComponent(org.terasology.world.block.regions.BlockRegionComponent) Prefab(org.terasology.entitySystem.prefab.Prefab)

Example 10 with Prefab

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

the class EntityAwareWorldProvider method updateBlockEntityComponents.

/**
 * Transforms a block entity with the change of block type. This is driven from the delta between the old and new
 * block type prefabs, but takes into account changes made to the block entity.
 *
 * @param blockEntity The entity to update
 * @param oldType     The previous type of the block
 * @param type        The new type of the block
 */
private void updateBlockEntityComponents(EntityRef blockEntity, Block oldType, Block type, Set<Class<? extends Component>> retainComponents) {
    BlockComponent blockComponent = blockEntity.getComponent(BlockComponent.class);
    Optional<Prefab> oldPrefab = oldType.getPrefab();
    EntityBuilder oldEntityBuilder = entityManager.newBuilder(oldPrefab.orElse(null));
    oldEntityBuilder.addComponent(new BlockComponent(oldType, new Vector3i(blockComponent.getPosition())));
    BeforeEntityCreated oldEntityEvent = new BeforeEntityCreated(oldPrefab.orElse(null), oldEntityBuilder.iterateComponents());
    blockEntity.send(oldEntityEvent);
    for (Component comp : oldEntityEvent.getResultComponents()) {
        oldEntityBuilder.addComponent(comp);
    }
    Optional<Prefab> newPrefab = type.getPrefab();
    EntityBuilder newEntityBuilder = entityManager.newBuilder(newPrefab.orElse(null));
    newEntityBuilder.addComponent(new BlockComponent(type, new Vector3i(blockComponent.getPosition())));
    BeforeEntityCreated newEntityEvent = new BeforeEntityCreated(newPrefab.orElse(null), newEntityBuilder.iterateComponents());
    blockEntity.send(newEntityEvent);
    for (Component comp : newEntityEvent.getResultComponents()) {
        newEntityBuilder.addComponent(comp);
    }
    for (Component component : blockEntity.iterateComponents()) {
        if (!COMMON_BLOCK_COMPONENTS.contains(component.getClass()) && !entityManager.getComponentLibrary().getMetadata(component.getClass()).isRetainUnalteredOnBlockChange() && !newEntityBuilder.hasComponent(component.getClass()) && !retainComponents.contains(component.getClass())) {
            blockEntity.removeComponent(component.getClass());
        }
    }
    blockComponent.setBlock(type);
    blockEntity.saveComponent(blockComponent);
    for (Component comp : newEntityBuilder.iterateComponents()) {
        copyIntoPrefab(blockEntity, comp, retainComponents);
    }
}
Also used : BlockComponent(org.terasology.world.block.BlockComponent) Vector3i(org.terasology.math.geom.Vector3i) BeforeEntityCreated(org.terasology.entitySystem.entity.lifecycleEvents.BeforeEntityCreated) EntityBuilder(org.terasology.entitySystem.entity.EntityBuilder) OnActivatedComponent(org.terasology.entitySystem.entity.lifecycleEvents.OnActivatedComponent) OnChangedComponent(org.terasology.entitySystem.entity.lifecycleEvents.OnChangedComponent) NetworkComponent(org.terasology.network.NetworkComponent) BlockComponent(org.terasology.world.block.BlockComponent) BeforeDeactivateComponent(org.terasology.entitySystem.entity.lifecycleEvents.BeforeDeactivateComponent) LocationComponent(org.terasology.logic.location.LocationComponent) Component(org.terasology.entitySystem.Component) BlockRegionComponent(org.terasology.world.block.regions.BlockRegionComponent) 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