用户:
双鱼冬寒查看:0 回复:0 评论:0 创建时间:2022-11-03T21:36:09
const MeshScale = [0.0625,0.0625,0.0625] function spawn() {//把生成的代码封装成函数 const e = world.createEntity({ mesh:'mesh/.vb',//.vb前面是模型名称 meshScale:MeshScale, collides:true, // 开启碰撞 gravity:true, // 开启重力 friction:0, // 关闭摩擦力 maxHp:100, hp:100, position:[ 喵+(Math.random()-0.5) * 10, 24 + Math.random() * 10, 喵+(Math.random()-0.5) * 10, ], }); e.addTag('');//模型名称 e.enableDamage = false; e.onVoxelContact(({x,y,z,voxel})=>{ e.velocity.y = 0.2+Math.random()*0.5//碰到砖块就跳一下 }) e.onEntityContact(({other}) => { if(other.isPlayer){//撞到玩家 other.hurt(0)//对玩家造成伤害 } }) e.onDie(()=>{ e.destroy()//喵掉,实体消失 for (let i = 0; i < 1; i++) {//生成 spawn() } }) e.enableInteract = true;//开启实体交互 e.interactRadius = 2;//实体交互半径 e.interactHint = '';//实体交互名称 e.interactColor.set(1, 0, 1);//实体交互名称颜色,这里为蓝色 e.onInteract(async ({ entity }) => { const halloween = await entity.player.dialog({ type: Box3DialogType.SELECT, title: '',//标题 content: ``,//说什么 options: [ '退出'], })
if (j) { if (j.value == '退出') { textDialog(entity, `欢迎下次再来!`, '') } } }) }
for (let i = 0; i < 1; i++) {//开局生成一个 spawn() }
const Quat = new Box3Quaternion(0,0,0,1)// box引擎默认的旋转朝向 let allPlayers = []//所有玩家 let allZombies = []//所有实体 world.onTick(async ({tick}) => {//每秒16个tick if(tick%16===0){//每16个tick运行一次, 而不是每个tick都运行,节省性能 allPlayers = world.querySelectorAll('player') allZombies = world.querySelectorAll('.')//.后面加实体名称 } allZombies.forEach(async (e) => { let zomPos = e.position if(tick%10===0){//每10个tick运行一次, 而不是每个tick都运行,节省性能 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.destroyed){//如果要追杀的玩家还没有离开地图 var direction = e.target.position.sub(zomPos); //往玩家的方向矢量 var dist = direction.mag() //矢量的长度 var speed = 0.4+Math.random()*0.6 //速度0.2~0.5随机 e.velocity.x = direction.x*speed/dist e.velocity.z = direction.z*speed/dist //让实体面向自己的前进方向 var orientation = Quat.rotateY(Math.atan2(e.velocity.z, e.velocity.x)) e.meshOrientation.copy(orientation) } }) }) function textDialog(entity, content, title) { return entity.player.dialog({ //弹出对话框 type: Box3DialogType.TEXT, //对话框类型为纯文本 content, //文本内容 title, //对话框左上角标题内容 hasArrow: true, }) }