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;
}
}
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);
}
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();
}
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()));
}
}
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);
}
}
}
Aggregations