用户:
liuwentao查看:0 回复:2 评论:0 创建时间:2022-08-22T18:04:06
async function weaponBoom(tnt, attacker) {
boom(~~(tnt.position.x), ~~(tnt.position.y), ~~(tnt.position.z), 5)
const size = 12
killBox(tnt.position, size, (e) => {
const dist = tnt.position.distance(e.position)
const ratio = (size - dist) / size
makeDamage(e, 50 * ratio, attacker)
hit_fly(tnt.position, Math.max(1, 0.1 * ratio), e)
})
fire(tnt, [new Box3RGBColor(9, 9, 1), new Box3RGBColor(5, 0.25, 0.1), new Box3RGBColor(3, 0.05, 0.05), new Box3RGBColor(0, 0, 0), new Box3RGBColor(0, 0, 0)], 1000)
}
async function killBox(pos, size, fn, tag = null) {
const entities = world.searchBox({
lo: [pos.x - size, pos.y - size, pos.z - size],
hi: [pos.x + size, tag === null ? pos.y + size : pos.y + 80, pos.z + size],
})
for (const e of entities) {
fn(e)
}
world.removeZone(entities)
}
async function hit_fly(center, power, entity) {
if (!entity.hasTag('不可击飞')) {
entity.velocity = new Box3Vector3(
(entity.position.x - center.x) * power,
(entity.position.y - center.y) * power,
(entity.position.z - center.z) * power
)
}
}
async function boom(x, y, z, size) {
let voxelIds = []
for (let i = -size; i < size + 1; i++) {
for (let l = -size; l < size + 1; l++) {
for (let k = -size; k < size + 1; k++) {
let id = await voxels.getVoxelId(x + i, y + l, z + k)
if (id !== 0 && !(id == 291 || id == 281)) {
voxelIds.push({ "id": id, "position": [x + i, y + l, z + k] })
voxels.setVoxelId(x + i, y + l, z + k, 0)
}
}
}
}
setTimeout(function () {
voxelIds.forEach((v) => {
voxels.setVoxelId(v.position[0], v.position[1], v.position[2], v.id)
})
}, 5000)
}
async function fire(entity, color, time) {
const fireball = world.createEntity({
bounds: [0, 0, 0],
collides: false,
gravity: false,
position: entity.position,
})
fireball.addTag('不可击飞')
fireball.velocity.y = 0.1
particleSpread(fireball, color, 1)
await sleep(time)
fireball.destroy()
}
async function makeDamage(e, dmg, entity) {
if (e.enableDamage === true) {
e.hurt(Math.max(1, dmg), { attacker: entity })
}
}
function particleSpread(entity, colorList, scale) {
entity.particleRate = 300
entity.particleLifetime = 3
entity.particleColor = colorList
entity.particleSize = [15 * scale, 10 * scale, 5 * scale, 3 * scale, 3 * scale]
entity.particleSizeSpread = 2 * scale
entity.particleVelocitySpread = [30 * scale, 30 * scale, 30 * scale]
entity.particleAcceleration = [0, -10 * scale, 0]
}
使用时只需调用weaponBoom就行了