Search in sources :

Example 1 with BlockComponent

use of org.terasology.world.block.BlockComponent in project Terasology by MovingBlocks.

the class LocalPlayerSystem method onTargetChanged.

@ReceiveEvent
public void onTargetChanged(PlayerTargetChangedEvent event, EntityRef entity) {
    EntityRef target = event.getNewTarget();
    if (target.exists()) {
        LocationComponent location = target.getComponent(LocationComponent.class);
        if (location != null) {
            BlockComponent blockComp = target.getComponent(BlockComponent.class);
            BlockRegionComponent blockRegion = target.getComponent(BlockRegionComponent.class);
            if (blockComp != null || blockRegion != null) {
                Vector3f blockPos = location.getWorldPosition();
                Block block = worldProvider.getBlock(blockPos);
                aabb = block.getBounds(blockPos);
            } else {
                MeshComponent mesh = target.getComponent(MeshComponent.class);
                if (mesh != null && mesh.mesh != null) {
                    aabb = mesh.mesh.getAABB();
                    aabb = aabb.transform(location.getWorldRotation(), location.getWorldPosition(), location.getWorldScale());
                }
            }
        }
    } else {
        aabb = null;
    }
}
Also used : BlockComponent(org.terasology.world.block.BlockComponent) MeshComponent(org.terasology.rendering.logic.MeshComponent) BlockRegionComponent(org.terasology.world.block.regions.BlockRegionComponent) Vector3f(org.terasology.math.geom.Vector3f) Block(org.terasology.world.block.Block) EntityRef(org.terasology.entitySystem.entity.EntityRef) LocationComponent(org.terasology.logic.location.LocationComponent) ReceiveEvent(org.terasology.entitySystem.event.ReceiveEvent)

Example 2 with BlockComponent

use of org.terasology.world.block.BlockComponent in project Terasology by MovingBlocks.

the class NetEntityRefTypeHandler method serializeCollection.

@Override
public PersistedData serializeCollection(Collection<EntityRef> value, SerializationContext context) {
    List<PersistedData> items = Lists.newArrayList();
    for (EntityRef ref : value) {
        BlockComponent blockComponent = ref.getComponent(BlockComponent.class);
        if (blockComponent != null) {
            Vector3i blockPos = blockComponent.getPosition();
            items.add(context.create(blockPos.x, blockPos.y, blockPos.z));
        } else {
            NetworkComponent netComponent = ref.getComponent(NetworkComponent.class);
            if (netComponent != null) {
                items.add(context.create(netComponent.getNetworkId()));
            } else {
                items.add(context.createNull());
            }
        }
    }
    return context.create(items);
}
Also used : BlockComponent(org.terasology.world.block.BlockComponent) NetworkComponent(org.terasology.network.NetworkComponent) Vector3i(org.terasology.math.geom.Vector3i) PersistedData(org.terasology.persistence.typeHandling.PersistedData) EntityRef(org.terasology.entitySystem.entity.EntityRef)

Example 3 with BlockComponent

use of org.terasology.world.block.BlockComponent in project Terasology by MovingBlocks.

the class NetEntityRefTypeHandler method serialize.

@Override
public PersistedData serialize(EntityRef value, SerializationContext context) {
    BlockComponent blockComponent = value.getComponent(BlockComponent.class);
    if (blockComponent != null) {
        Vector3i pos = blockComponent.getPosition();
        return context.create(pos.x, pos.y, pos.z);
    }
    NetworkComponent netComponent = value.getComponent(NetworkComponent.class);
    if (netComponent != null) {
        return context.create(netComponent.getNetworkId());
    }
    return context.createNull();
}
Also used : BlockComponent(org.terasology.world.block.BlockComponent) NetworkComponent(org.terasology.network.NetworkComponent) Vector3i(org.terasology.math.geom.Vector3i)

Example 4 with BlockComponent

use of org.terasology.world.block.BlockComponent in project Terasology by MovingBlocks.

the class DoorSystem method placeDoor.

@ReceiveEvent(components = { DoorComponent.class, ItemComponent.class })
public void placeDoor(ActivateEvent event, EntityRef entity) {
    DoorComponent door = entity.getComponent(DoorComponent.class);
    BlockComponent targetBlockComp = event.getTarget().getComponent(BlockComponent.class);
    if (targetBlockComp == null) {
        event.consume();
        return;
    }
    Vector3f horizDir = new Vector3f(event.getDirection());
    horizDir.y = 0;
    Side facingDir = Side.inDirection(horizDir);
    if (!facingDir.isHorizontal()) {
        event.consume();
        return;
    }
    Vector3f offset = new Vector3f(event.getHitPosition());
    offset.sub(targetBlockComp.getPosition().toVector3f());
    Side offsetDir = Side.inDirection(offset);
    Vector3i primePos = new Vector3i(targetBlockComp.getPosition());
    primePos.add(offsetDir.getVector3i());
    Block primeBlock = worldProvider.getBlock(primePos);
    if (!primeBlock.isReplacementAllowed()) {
        event.consume();
        return;
    }
    Block belowBlock = worldProvider.getBlock(primePos.x, primePos.y - 1, primePos.z);
    Block aboveBlock = worldProvider.getBlock(primePos.x, primePos.y + 1, primePos.z);
    // Determine top and bottom blocks
    Vector3i bottomBlockPos;
    Vector3i topBlockPos;
    if (belowBlock.isReplacementAllowed()) {
        bottomBlockPos = new Vector3i(primePos.x, primePos.y - 1, primePos.z);
        topBlockPos = primePos;
    } else if (aboveBlock.isReplacementAllowed()) {
        bottomBlockPos = primePos;
        topBlockPos = new Vector3i(primePos.x, primePos.y + 1, primePos.z);
    } else {
        event.consume();
        return;
    }
    Side attachSide = determineAttachSide(facingDir, offsetDir, bottomBlockPos, topBlockPos);
    if (attachSide == null) {
        event.consume();
        return;
    }
    Side closedSide = facingDir.reverse();
    if (closedSide == attachSide || closedSide.reverse() == attachSide) {
        closedSide = attachSide.yawClockwise(1);
    }
    Block newBottomBlock = door.bottomBlockFamily.getBlockForPlacement(worldProvider, blockEntityRegistry, bottomBlockPos, closedSide, Side.TOP);
    Block newTopBlock = door.topBlockFamily.getBlockForPlacement(worldProvider, blockEntityRegistry, bottomBlockPos, closedSide, Side.TOP);
    Map<Vector3i, Block> blockMap = new HashMap<>();
    blockMap.put(bottomBlockPos, newBottomBlock);
    blockMap.put(topBlockPos, newTopBlock);
    PlaceBlocks blockEvent = new PlaceBlocks(blockMap, event.getInstigator());
    worldProvider.getWorldEntity().send(blockEvent);
    if (!blockEvent.isConsumed()) {
        EntityRef newDoor = entityManager.create(door.doorRegionPrefab);
        entity.removeComponent(MeshComponent.class);
        newDoor.addComponent(new BlockRegionComponent(Region3i.createBounded(bottomBlockPos, topBlockPos)));
        Vector3f doorCenter = bottomBlockPos.toVector3f();
        doorCenter.y += 0.5f;
        newDoor.addComponent(new LocationComponent(doorCenter));
        DoorComponent newDoorComp = newDoor.getComponent(DoorComponent.class);
        newDoorComp.closedSide = closedSide;
        newDoorComp.openSide = attachSide.reverse();
        newDoorComp.isOpen = false;
        newDoor.saveComponent(newDoorComp);
        newDoor.send(new PlaySoundEvent(Assets.getSound("engine:PlaceBlock").get(), 0.5f));
        logger.info("Closed Side: {}", newDoorComp.closedSide);
        logger.info("Open Side: {}", newDoorComp.openSide);
        newDoor.send(new DoorPlacedEvent(event.getInstigator()));
    }
}
Also used : HashMap(java.util.HashMap) BlockRegionComponent(org.terasology.world.block.regions.BlockRegionComponent) PlaySoundEvent(org.terasology.audio.events.PlaySoundEvent) LocationComponent(org.terasology.logic.location.LocationComponent) BlockComponent(org.terasology.world.block.BlockComponent) Side(org.terasology.math.Side) Vector3f(org.terasology.math.geom.Vector3f) Vector3i(org.terasology.math.geom.Vector3i) Block(org.terasology.world.block.Block) PlaceBlocks(org.terasology.world.block.entity.placement.PlaceBlocks) EntityRef(org.terasology.entitySystem.entity.EntityRef) ReceiveEvent(org.terasology.entitySystem.event.ReceiveEvent)

Example 5 with BlockComponent

use of org.terasology.world.block.BlockComponent in project Terasology by MovingBlocks.

the class ExplosionAuthoritySystem method onDelayedExplosion.

@ReceiveEvent
public void onDelayedExplosion(DelayedActionTriggeredEvent event, EntityRef entityRef, ExplosionActionComponent explosionActionComponent) {
    if (event.getActionId().equals(DELAYED_EXPLOSION_ACTION_ID)) {
        // check if the exploding entity is a block or not
        if (entityRef.hasComponent(BlockComponent.class)) {
            BlockComponent blockComponent = entityRef.getComponent(BlockComponent.class);
            // always destroy the block that caused the explosion
            worldProvider.setBlock(blockComponent.getPosition(), blockManager.getBlock(BlockManager.AIR_ID));
            // create the explosion from the block's location
            doExplosion(explosionActionComponent, blockComponent.getPosition().toVector3f(), entityRef);
        } else if (entityRef.hasComponent(LocationComponent.class)) {
            // get the position of the non-block entity to make it explode from there
            Vector3f position = entityRef.getComponent(LocationComponent.class).getWorldPosition();
            // destroy the non-block entity
            entityRef.destroy();
            // create the explosion from the non-block entity location
            doExplosion(explosionActionComponent, position, EntityRef.NULL);
        }
    }
}
Also used : BlockComponent(org.terasology.world.block.BlockComponent) Vector3f(org.terasology.math.geom.Vector3f) LocationComponent(org.terasology.logic.location.LocationComponent) ReceiveEvent(org.terasology.entitySystem.event.ReceiveEvent)

Aggregations

BlockComponent (org.terasology.world.block.BlockComponent)20 EntityRef (org.terasology.entitySystem.entity.EntityRef)11 Vector3i (org.terasology.math.geom.Vector3i)10 LocationComponent (org.terasology.logic.location.LocationComponent)9 NetworkComponent (org.terasology.network.NetworkComponent)7 ReceiveEvent (org.terasology.entitySystem.event.ReceiveEvent)6 Component (org.terasology.entitySystem.Component)4 Vector3f (org.terasology.math.geom.Vector3f)4 MeshComponent (org.terasology.rendering.logic.MeshComponent)4 Block (org.terasology.world.block.Block)4 BlockRegionComponent (org.terasology.world.block.regions.BlockRegionComponent)4 FastRandom (org.terasology.utilities.random.FastRandom)3 PlaySoundEvent (org.terasology.audio.events.PlaySoundEvent)2 EntityBuilder (org.terasology.entitySystem.entity.EntityBuilder)2 PojoEntityManager (org.terasology.entitySystem.entity.internal.PojoEntityManager)2 Side (org.terasology.math.Side)2 Client (org.terasology.network.Client)2 PlaceBlocks (org.terasology.world.block.entity.placement.PlaceBlocks)2 BlockFamily (org.terasology.world.block.family.BlockFamily)2 HashMap (java.util.HashMap)1