Search in sources :

Example 21 with EntityBuilder

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

the class VisualCharacterSystemTest method setup.

@Before
public void setup() throws Exception {
    this.system = new VisualCharacterSystem();
    Context context = new ContextImpl();
    this.localPlayer = Mockito.mock(LocalPlayer.class);
    context.put(LocalPlayer.class, localPlayer);
    Mockito.doAnswer(new Answer() {

        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            return clientEntityReturnedByLocalPlayer;
        }
    }).when(localPlayer).getClientEntity();
    this.entityManager = Mockito.mock(EntityManager.class);
    Mockito.doReturn(Mockito.mock(EntityBuilder.class)).when(entityManager).newBuilder();
    context.put(EntityManager.class, this.entityManager);
    InjectionHelper.inject(system, context);
    system.setCreateAndAttachVisualEntityStrategy((entityBuilder, characterEntity) -> Mockito.mock(EntityRef.class));
}
Also used : Context(org.terasology.context.Context) Answer(org.mockito.stubbing.Answer) EntityManager(org.terasology.entitySystem.entity.EntityManager) LocalPlayer(org.terasology.logic.players.LocalPlayer) InvocationOnMock(org.mockito.invocation.InvocationOnMock) EntityBuilder(org.terasology.entitySystem.entity.EntityBuilder) ContextImpl(org.terasology.context.internal.ContextImpl) EntityRef(org.terasology.entitySystem.entity.EntityRef) Before(org.junit.Before)

Example 22 with EntityBuilder

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

the class BlockItemFactory method newBuilder.

/**
 * Use this method instead of {@link #newInstance(BlockFamily)} to modify entity properties like the persistence
 * flag before it gets created.
 *
 * @param blockFamily must not be null
 */
public EntityBuilder newBuilder(BlockFamily blockFamily, int quantity) {
    EntityBuilder builder = entityManager.newBuilder("engine:blockItemBase");
    if (blockFamily.getArchetypeBlock().getLuminance() > 0) {
        builder.addComponent(new LightComponent());
    }
    // Copy the components from block prefab into the block item
    Optional<Prefab> prefab = blockFamily.getArchetypeBlock().getPrefab();
    if (prefab.isPresent()) {
        for (Component component : prefab.get().iterateComponents()) {
            if (component.getClass().getAnnotation(AddToBlockBasedItem.class) != null) {
                builder.addComponent(entityManager.getComponentLibrary().copy(component));
            }
        }
    }
    DisplayNameComponent displayNameComponent = builder.getComponent(DisplayNameComponent.class);
    displayNameComponent.name = blockFamily.getDisplayName();
    ItemComponent item = builder.getComponent(ItemComponent.class);
    if (blockFamily.getArchetypeBlock().isStackable()) {
        item.stackId = "block:" + blockFamily.getURI().toString();
        item.stackCount = (byte) quantity;
    }
    BlockItemComponent blockItem = builder.getComponent(BlockItemComponent.class);
    blockItem.blockFamily = blockFamily;
    return builder;
}
Also used : DisplayNameComponent(org.terasology.logic.common.DisplayNameComponent) ItemComponent(org.terasology.logic.inventory.ItemComponent) LightComponent(org.terasology.rendering.logic.LightComponent) EntityBuilder(org.terasology.entitySystem.entity.EntityBuilder) LightComponent(org.terasology.rendering.logic.LightComponent) ItemComponent(org.terasology.logic.inventory.ItemComponent) Component(org.terasology.entitySystem.Component) DisplayNameComponent(org.terasology.logic.common.DisplayNameComponent) Prefab(org.terasology.entitySystem.prefab.Prefab)

Example 23 with EntityBuilder

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

the class PojoEntityManager method createEntityWithId.

@Override
public EntityRef createEntityWithId(long id, Iterable<Component> components) {
    EntityBuilder builder = newBuilder();
    builder.setId(id);
    builder.addComponents(components);
    return builder.build();
}
Also used : EntityBuilder(org.terasology.entitySystem.entity.EntityBuilder)

Example 24 with EntityBuilder

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

the class PojoEntityPool method createEntityWithId.

@Override
public EntityRef createEntityWithId(long id, Iterable<Component> components) {
    EntityBuilder builder = newBuilder();
    builder.setId(id);
    builder.addComponents(components);
    return builder.build();
}
Also used : EntityBuilder(org.terasology.entitySystem.entity.EntityBuilder)

Example 25 with EntityBuilder

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

the class PojoEntityPool method create.

private EntityRef create(Prefab prefab, Vector3f position, Quat4f rotation, boolean sendLifecycleEvents) {
    EntityBuilder builder = newBuilder(prefab);
    builder.setSendLifecycleEvents(sendLifecycleEvents);
    LocationComponent loc = builder.getComponent(LocationComponent.class);
    if (loc == null && (position != null || rotation != null)) {
        loc = new LocationComponent();
        builder.addComponent(loc);
    }
    if (position != null) {
        loc.setWorldPosition(position);
    }
    if (rotation != null) {
        loc.setWorldRotation(rotation);
    }
    return builder.build();
}
Also used : EntityBuilder(org.terasology.entitySystem.entity.EntityBuilder) LocationComponent(org.terasology.logic.location.LocationComponent)

Aggregations

EntityBuilder (org.terasology.entitySystem.entity.EntityBuilder)34 EntityRef (org.terasology.entitySystem.entity.EntityRef)16 LocationComponent (org.terasology.logic.location.LocationComponent)10 NetworkComponent (org.terasology.network.NetworkComponent)7 Test (org.junit.Test)5 ReceiveEvent (org.terasology.entitySystem.event.ReceiveEvent)5 Prefab (org.terasology.entitySystem.prefab.Prefab)5 Vector3f (org.terasology.math.geom.Vector3f)5 Vector3i (org.terasology.math.geom.Vector3i)5 Component (org.terasology.entitySystem.Component)4 DoDamageEvent (org.terasology.logic.health.DoDamageEvent)3 ItemComponent (org.terasology.logic.inventory.ItemComponent)3 Quat4f (org.terasology.math.geom.Quat4f)3 Block (org.terasology.world.block.Block)3 PlaySoundEvent (org.terasology.audio.events.PlaySoundEvent)2 DisplayNameComponent (org.terasology.logic.common.DisplayNameComponent)2 LightComponent (org.terasology.rendering.logic.LightComponent)2 BlockComponent (org.terasology.world.block.BlockComponent)2 Before (org.junit.Before)1 InvocationOnMock (org.mockito.invocation.InvocationOnMock)1