use of org.terasology.logic.health.DoDamageEvent 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.logic.health.DoDamageEvent 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.logic.health.DoDamageEvent 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();
}
}
Aggregations