use of org.terasology.world.block.Block in project Terasology by MovingBlocks.
the class ExplosionAuthoritySystem method doExplosion.
void doExplosion(ExplosionActionComponent explosionComp, Vector3f origin, EntityRef instigatingBlockEntity) {
EntityBuilder builder = entityManager.newBuilder("core:smokeExplosion");
builder.getComponent(LocationComponent.class).setWorldPosition(origin);
EntityRef smokeEntity = builder.build();
smokeEntity.send(new PlaySoundEvent(getRandomExplosionSound(), 1f));
Vector3i blockPos = new Vector3i();
for (int i = 0; i < explosionComp.maxRange; i++) {
Vector3f direction = random.nextVector3f(1.0f);
for (int j = 0; j < 4; j++) {
Vector3f target = new Vector3f(origin);
target.x += direction.x * j;
target.y += direction.y * j;
target.z += direction.z * j;
blockPos.set((int) target.x, (int) target.y, (int) target.z);
Block currentBlock = worldProvider.getBlock(blockPos);
/* PHYSICS */
if (currentBlock.isDestructible()) {
EntityRef blockEntity = blockEntityRegistry.getEntityAt(blockPos);
// allow explosions to chain together, but do not chain on the instigating block
if (!blockEntity.equals(instigatingBlockEntity) && blockEntity.hasComponent(ExplosionActionComponent.class)) {
doExplosion(blockEntity.getComponent(ExplosionActionComponent.class), blockPos.toVector3f(), blockEntity);
} else {
blockEntity.send(new DoDamageEvent(explosionComp.damageAmount, explosionComp.damageType));
}
}
}
}
}
use of org.terasology.world.block.Block in project Terasology by MovingBlocks.
the class TunnelAction method onActivate.
@ReceiveEvent
public void onActivate(ActivateEvent event, EntityRef entity, TunnelActionComponent tunnelActionComponent) {
Vector3f dir = new Vector3f(event.getDirection());
dir.scale(4.0f);
Vector3f origin = new Vector3f(event.getOrigin());
origin.add(dir);
Vector3i blockPos = new Vector3i();
int particleEffects = 0;
int blockCounter = tunnelActionComponent.maxDestroyedBlocks;
for (int s = 0; s <= tunnelActionComponent.maxTunnelDepth; s++) {
origin.add(dir);
if (!worldProvider.isBlockRelevant(origin)) {
break;
}
for (int i = 0; i < tunnelActionComponent.maxRaysCast; i++) {
Vector3f direction = random.nextVector3f();
Vector3f impulse = new Vector3f(direction);
impulse.scale(tunnelActionComponent.explosiveForce);
for (int j = 0; j < 3; j++) {
Vector3f target = new Vector3f(origin);
target.x += direction.x * j;
target.y += direction.y * j;
target.z += direction.z * j;
blockPos.set((int) target.x, (int) target.y, (int) target.z);
Block currentBlock = worldProvider.getBlock(blockPos);
if (currentBlock.isDestructible()) {
if (particleEffects < tunnelActionComponent.maxParticalEffects) {
EntityBuilder builder = entityManager.newBuilder("Core:smokeExplosion");
builder.getComponent(LocationComponent.class).setWorldPosition(target);
builder.build();
particleEffects++;
}
if (random.nextFloat() < tunnelActionComponent.thoroughness) {
EntityRef blockEntity = blockEntityRegistry.getEntityAt(blockPos);
blockEntity.send(new DoDamageEvent(tunnelActionComponent.damageAmount, tunnelActionComponent.damageType));
}
blockCounter--;
}
if (blockCounter <= 0) {
return;
}
}
}
}
// No blocks were destroyed, so cancel the event
if (blockCounter == tunnelActionComponent.maxDestroyedBlocks) {
event.consume();
}
}
use of org.terasology.world.block.Block in project Terasology by MovingBlocks.
the class EntityAwareWorldProvider method setBlockRetainComponent.
@Override
@SafeVarargs
public final Block setBlockRetainComponent(Vector3i pos, Block type, Class<? extends Component>... components) {
if (GameThread.isCurrentThread()) {
EntityRef blockEntity = getBlockEntityAt(pos);
Block oldType = super.setBlock(pos, type);
if (oldType != null) {
updateBlockEntity(blockEntity, pos, oldType, type, false, Sets.newHashSet(components));
}
return oldType;
}
return null;
}
use of org.terasology.world.block.Block in project Terasology by MovingBlocks.
the class WorldProviderCore method setBlocks.
/**
* Places all given blocks of specific types at their corresponding positions
* </p>
* Chunks are
*
* @param blocks A mapping from world position to change to the type of block to set
* @return A mapping from world position to previous block type.
* The value of a map entry is Null if the change failed (because the necessary chunk was not loaded)
*/
default Map<Vector3i, Block> setBlocks(Map<Vector3i, Block> blocks) {
Map<Vector3i, Block> resultMap = Maps.newHashMap();
for (Map.Entry<Vector3i, Block> entry : blocks.entrySet()) {
Block oldBlock = setBlock(entry.getKey(), entry.getValue());
resultMap.put(entry.getKey(), oldBlock);
}
return resultMap;
}
use of org.terasology.world.block.Block in project Terasology by MovingBlocks.
the class WorldProviderCoreImpl method setBlock.
@Override
public Block setBlock(Vector3i worldPos, Block type) {
/*
* Hint: This method has a benchmark available in the BenchmarkScreen, The screen can be opened ingame via the
* command "showSCreen BenchmarkScreen".
*/
Vector3i chunkPos = ChunkMath.calcChunkPos(worldPos);
CoreChunk chunk = chunkProvider.getChunk(chunkPos);
if (chunk != null) {
Vector3i blockPos = ChunkMath.calcBlockPos(worldPos);
Block oldBlockType = chunk.setBlock(blockPos, type);
if (oldBlockType != type) {
BlockChange oldChange = blockChanges.get(worldPos);
if (oldChange == null) {
blockChanges.put(worldPos, new BlockChange(worldPos, oldBlockType, type));
} else {
oldChange.setTo(type);
}
for (Vector3i pos : ChunkMath.getChunkRegionAroundWorldPos(worldPos, 1)) {
RenderableChunk dirtiedChunk = chunkProvider.getChunk(pos);
if (dirtiedChunk != null) {
dirtiedChunk.setDirty(true);
}
}
notifyBlockChanged(worldPos, type, oldBlockType);
}
return oldBlockType;
}
return null;
}
Aggregations