Search in sources :

Example 31 with EntityBuilder

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

the class FirstPersonClientSystem method ensureClientSideEntityOnHeldItemMountPoint.

// ensures held item mount point entity exists, attaches it to the camera and sets its transform
@ReceiveEvent
public void ensureClientSideEntityOnHeldItemMountPoint(OnActivatedComponent event, EntityRef camera, FirstPersonHeldItemMountPointComponent firstPersonHeldItemMountPointComponent) {
    if (!firstPersonHeldItemMountPointComponent.mountPointEntity.exists()) {
        EntityBuilder builder = entityManager.newBuilder("engine:FirstPersonHeldItemMountPoint");
        builder.setPersistent(false);
        firstPersonHeldItemMountPointComponent.mountPointEntity = builder.build();
        camera.saveComponent(firstPersonHeldItemMountPointComponent);
    }
    // link the mount point entity to the camera
    Location.removeChild(camera, firstPersonHeldItemMountPointComponent.mountPointEntity);
    Location.attachChild(camera, firstPersonHeldItemMountPointComponent.mountPointEntity, firstPersonHeldItemMountPointComponent.translate, new Quat4f(TeraMath.DEG_TO_RAD * firstPersonHeldItemMountPointComponent.rotateDegrees.y, TeraMath.DEG_TO_RAD * firstPersonHeldItemMountPointComponent.rotateDegrees.x, TeraMath.DEG_TO_RAD * firstPersonHeldItemMountPointComponent.rotateDegrees.z), firstPersonHeldItemMountPointComponent.scale);
}
Also used : EntityBuilder(org.terasology.entitySystem.entity.EntityBuilder) Quat4f(org.terasology.math.geom.Quat4f) ReceiveEvent(org.terasology.entitySystem.event.ReceiveEvent)

Example 32 with EntityBuilder

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

the class PlayerFactory method newInstance.

/**
 * Creates a new player character entity. The desired spawning location is derived from
 * the {@link LocationComponent} of the controller.
 * @param controller the controlling client entity
 * @return a new player character entity
 */
public EntityRef newInstance(EntityRef controller) {
    EntityBuilder builder = entityManager.newBuilder("engine:player");
    LocationComponent location = controller.getComponent(LocationComponent.class);
    Vector3f spawnPosition = findSpawnPositionFromLocationComponent(location);
    location.setWorldPosition(spawnPosition);
    controller.saveComponent(location);
    logger.debug("Spawing player at: {}", spawnPosition);
    builder.getComponent(LocationComponent.class).setWorldPosition(spawnPosition);
    builder.setOwner(controller);
    CharacterComponent playerComponent = builder.getComponent(CharacterComponent.class);
    playerComponent.controller = controller;
    EntityRef player = builder.build();
    Location.attachChild(player, controller, new Vector3f(), new Quat4f(0, 0, 0, 1));
    return player;
}
Also used : Vector3f(org.terasology.math.geom.Vector3f) CharacterComponent(org.terasology.logic.characters.CharacterComponent) EntityBuilder(org.terasology.entitySystem.entity.EntityBuilder) LocationComponent(org.terasology.logic.location.LocationComponent) EntityRef(org.terasology.entitySystem.entity.EntityRef) Quat4f(org.terasology.math.geom.Quat4f)

Example 33 with EntityBuilder

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

the class BlockDropGrammarSystem method onDestroyed.

@ReceiveEvent
public void onDestroyed(DoDestroyEvent event, EntityRef entity, BlockDropGrammarComponent blockDrop, LocationComponent locationComp) {
    BlockDamageModifierComponent blockDamageModifierComponent = event.getDamageType().getComponent(BlockDamageModifierComponent.class);
    float chanceOfBlockDrop = 1;
    if (blockDamageModifierComponent != null) {
        chanceOfBlockDrop = 1 - blockDamageModifierComponent.blockAnnihilationChance;
    }
    if (random.nextFloat() < chanceOfBlockDrop) {
        List<String> blockDrops = blockDrop.blockDrops;
        List<String> itemDrops = blockDrop.itemDrops;
        if (blockDamageModifierComponent != null && blockDrop.droppedWithTool != null) {
            for (String toolType : blockDamageModifierComponent.materialDamageMultiplier.keySet()) {
                if (blockDrop.droppedWithTool.containsKey(toolType)) {
                    BlockDropGrammarComponent.DropDefinition dropDefinition = blockDrop.droppedWithTool.get(toolType);
                    blockDrops = dropDefinition.blockDrops;
                    itemDrops = dropDefinition.itemDrops;
                    break;
                }
            }
        }
        if (blockDrops != null) {
            for (String drop : blockDrops) {
                String dropResult = drop;
                boolean dropping = true;
                int pipeIndex = dropResult.indexOf('|');
                if (pipeIndex > -1) {
                    float chance = Float.parseFloat(dropResult.substring(0, pipeIndex));
                    if (random.nextFloat() >= chance) {
                        dropping = false;
                    }
                    dropResult = dropResult.substring(pipeIndex + 1);
                }
                if (dropping) {
                    DropParser dropParser = new DropParser(random, dropResult).invoke();
                    EntityRef dropItem = blockItemFactory.newInstance(blockManager.getBlockFamily(dropParser.getDrop()), dropParser.getCount());
                    if (shouldDropToWorld(event, blockDamageModifierComponent, dropItem)) {
                        createDrop(dropItem, locationComp.getWorldPosition(), true);
                    }
                }
            }
        }
        if (itemDrops != null) {
            for (String drop : itemDrops) {
                String dropResult = drop;
                boolean dropping = true;
                int pipeIndex = dropResult.indexOf('|');
                if (pipeIndex > -1) {
                    float chance = Float.parseFloat(dropResult.substring(0, pipeIndex));
                    if (random.nextFloat() >= chance) {
                        dropping = false;
                    }
                    dropResult = dropResult.substring(pipeIndex + 1);
                }
                if (dropping) {
                    DropParser dropParser = new DropParser(random, dropResult).invoke();
                    EntityBuilder dropEntity = entityManager.newBuilder(dropParser.getDrop());
                    if (dropParser.getCount() > 1) {
                        ItemComponent itemComponent = dropEntity.getComponent(ItemComponent.class);
                        itemComponent.stackCount = (byte) dropParser.getCount();
                    }
                    EntityRef dropItem = dropEntity.build();
                    if (shouldDropToWorld(event, blockDamageModifierComponent, dropItem)) {
                        createDrop(dropItem, locationComp.getWorldPosition(), false);
                    }
                }
            }
        }
    }
}
Also used : ItemComponent(org.terasology.logic.inventory.ItemComponent) BlockDamageModifierComponent(org.terasology.world.block.entity.damage.BlockDamageModifierComponent) EntityBuilder(org.terasology.entitySystem.entity.EntityBuilder) EntityRef(org.terasology.entitySystem.entity.EntityRef) ReceiveEvent(org.terasology.entitySystem.event.ReceiveEvent)

Example 34 with EntityBuilder

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

the class ArrowAction method onActivate.

@ReceiveEvent
public void onActivate(ActivateEvent event, EntityRef entity, ArrowActionComponent arrowActionComponent) {
    if (time.getGameTime() > lastTime + 1.0f / arrowActionComponent.arrowsPerSecond) {
        Vector3f target = event.getHitNormal();
        Vector3i blockPos = new Vector3i(target);
        Vector3f position = new Vector3f(event.getOrigin());
        Vector3f dir = new Vector3f(event.getDirection());
        HitResult result;
        result = physicsRenderer.rayTrace(position, dir, arrowActionComponent.maxDistance, filter);
        Block currentBlock = worldProvider.getBlock(blockPos);
        if (currentBlock.isDestructible()) {
            EntityBuilder builder = entityManager.newBuilder("Core:defaultBlockParticles");
            builder.getComponent(LocationComponent.class).setWorldPosition(target);
            builder.build();
        }
        EntityRef blockEntity = result.getEntity();
        blockEntity.send(new DoDamageEvent(arrowActionComponent.damageAmount, arrowActionComponent.damageType));
        lastTime = time.getGameTime();
    }
}
Also used : HitResult(org.terasology.physics.HitResult) Vector3f(org.terasology.math.geom.Vector3f) Vector3i(org.terasology.math.geom.Vector3i) Block(org.terasology.world.block.Block) EntityBuilder(org.terasology.entitySystem.entity.EntityBuilder) DoDamageEvent(org.terasology.logic.health.DoDamageEvent) LocationComponent(org.terasology.logic.location.LocationComponent) EntityRef(org.terasology.entitySystem.entity.EntityRef) ReceiveEvent(org.terasology.entitySystem.event.ReceiveEvent)

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