MC中的爆炸其实挺科学的,在空中爆炸冲击波传得远,破坏范围比较大;在地面爆炸破坏范围小但是破坏深度较深
不科学的地方是它的爆炸范围是正方体,只不过一般范围小到看不出是正方体。至于为什么MOD里的核弹爆炸范围是球体,那是别人重新实现了爆炸函数把范围改掉了…
类名net.minecraft.world.World
/**
* Creates an explosion. Args: entity, x, y, z, strength
*/
public Explosion createExplosion(Entity entityIn, double x, double y, double z, float strength, boolean isSmoking)
{
return this.newExplosion(entityIn, x, y, z, strength, false, isSmoking);
}
/**
* returns a new explosion. Does initiation (at time of writing Explosion is not finished)
*/
// isFlaming影响是否着火,isSmoking影响是否生成粒子和破坏方块
public Explosion newExplosion(Entity entityIn, double x, double y, double z, float strength, boolean isFlaming, boolean isSmoking)
{
Explosion explosion = new Explosion(this, entityIn, x, y, z, strength, isFlaming, isSmoking);
if (net.minecraftforge.event.ForgeEventFactory.onExplosionStart(this, explosion)) return explosion;
// 第一阶段,计算被破坏的方块,伤害实体
explosion.doExplosionA();
// 第二阶段,破坏方块,生成粒子效果和掉落物
explosion.doExplosionB(true);
return explosion;
}
类名net.minecraft.world.Explosion
public Explosion(World worldIn, Entity p_i45754_2_, double p_i45754_3_, double p_i45754_5_, double p_i45754_7_, float size, boolean p_i45754_10_, boolean p_i45754_11_)
{
this.explosionRNG = new Random();
this.affectedBlockPositions = Lists.<BlockPos>newArrayList();
this.playerKnockbackMap = Maps.<EntityPlayer, Vec3>newHashMap();
this.worldObj = worldIn;
this.exploder = p_i45754_2_;
// 就是上面的strength
this.explosionSize = size;
this.explosionX = p_i45754_3_;
this.explosionY = p_i45754_5_;
this.explosionZ = p_i45754_7_;
this.isFlaming = p_i45754_10_;
this.isSmoking = p_i45754_11_;
this.position = new Vec3(explosionX, explosionY, explosionZ);
}
/**
* Does the first part of the explosion (destroy blocks)
*/
public void doExplosionA()
{
Set<BlockPos> blocksToDestroy = Sets.<BlockPos>newHashSet();
// 遍历16*16*16正方体边界上各点,从中心向边界连线,计算是否破坏连线上的方块
// 由于这里正方体尺寸写死了,如果爆炸范围太大边界就会有些方块没有遍历到
for (int x = 0; x < 16; ++x)
{
for (int y = 0; y < 16; ++y)
{
for (int z = 0; z < 16; ++z)
{
// 在边界
if (x == 0 || x == 15 || y == 0 || y == 15 || z == 0 || z == 15)
{
// 这里只有-1或1两个取值
double xStep = (double