
Lv.1
一个不知名的大佬
在 [求助] 如何制作怪物追人? 中回复
async function createBoss(level/*等级*/) {
var position = new Box3Vector3(50, 20, 93);
var scale = 1/16;
var boss = world.createEntity({
mesh: "mesh/蜘蛛boss.vb",
position,
meshScale: new Box3Vector3(scale, scale, scale),
collides: true,
fixed: false,
gravity: true,
id: 'boss',
})
boss.level = 1
boss.addTag("boss");
boss.enableDamage = true;
boss.maxHp = 100 + level * 50;
boss.onEntityContact(({ other }) => {
if (!other.isPlayer) { return };
other.hurt(boss.level * (Math.random() * gjxl), { attacker: boss });
})
boss.onDie(async ({ attacker }) => {
if (attacker) { attacker.exp += 20 }
boss.destroy()
await sleep(2000)
createBoss(boss.level + 1)
})
const Quat1 = new Box3Quaternion(0.000, 0.707, 0.000, 0.707)//初始方向
allPlayers = []
allZombies = []
world.onTick(async ({ tick }) => {//如果时间变化
if (tick % 16 == 0) {//如果tick值对16取模得0
allPlayers = world.querySelectorAll('player')//添加所有玩家到玩家列表
allZombies = [boss]//添加所有boss到boss列表
}
allZombies.forEach(async (e) => {//遍历整个boss列表
let zomPos = e.position//boss位置
if (tick % 11 == 0) {//如果tick值对11取模得0
let target = allPlayers.sort((a, b) => {//遍历整个玩家实体
return a.position.distance(zomPos) - b.position.distance(zomPos)
})[0]//选择其中一个玩家
if (target) {//如果获取到了玩家
e.target = target
}
}
if (e.target && e.target.hp != 0) {//判断是否获取到了玩家并且这个玩
var direction = e.target.position.sub(zomPos); //方向
var dist = direction.mag() //距离
var speed = Math.random() * 0.5 //移动速度
e.velocity.x = direction.x * speed / dist //x速度
e.velocity.z = direction.z * speed / dist //z速度
var orientation = Quat1.rotateY(Math.atan2(e.velocity.z, e.velocity.x))//方向
e.meshOrientation.copy(orientation)//方向
}
})
})
}
for (i = 0; i < 1; i++) {
createBoss(10)
}2022-05-01T07:41:14 点赞:0