for(let x=0;x<256;x++){; //x坐标的初始与结尾 for(let y=3;y<9;y++){; //y坐标的初始与结尾 for(let z=0;z<256;z++){; //z坐标的初始与结尾 let num = voxels.getVoxel(x,y,z); //获取当前方块id if (num == 0){; //判断当前方块是否为air voxels.setVoxel(x,y,z,"grass"); //将当前方块转换为water }; }; }; };
async function sphere(vox, cx, cy, cz, radius){//参数分别是方块、x,y,z和圆的半径 let xend = cx+radius let yend = cy+radius let zend = cz+radius for(let x=cx-radius;x<=xend;x++){ for(let y=cy-radius;y<=yend;y++){ for(let z=cz-radius;z<=zend;z++){ let dx = x-cx; let dy = y-cy; let dz = z-cz; if(Math.round(Math.sqrt(dx*dx+dy*dy+dz*dz)) <= radius){ voxels.setVoxel(x,y,z,vox) } }}} } world.onPress(async({ button, entity, raycast }) => { //事件 if(button ==='action0'){ const ball =world.createEntity({//放个模型 mesh:"mesh/tnt.vb", collides:true, meshScale:[0.06,0.06,0.06], position:[entity.position.x+raycast.direction.x, entity.position.y+raycast.direction.y, entity.position.z+raycast.direction.z], }) ball.onVoxelContact(({ x, y, z, voxel }) => {//TNT碰到了方块 sphere('air',ball.position.x,ball.position.y,ball.position.z,128)//造一个空气球 ball.destroy()//删除TNT }); ball.velocity.x += raycast.direction.x*2; ball.velocity.y += raycast.direction.y*2; ball.velocity.z += raycast.direction.z*2; } })
world.onPress(async({entity,button,raycast})=>{ entity.player.enable3DCursor=true; if(button==Box3ButtonType.CROUCH&&entity.player.moveState!=Box喵layerMoveState.FLYING){ const p=await entity.player.dialog({ type:Box3DialogType.SELECT, options:['搜索方块'].concat(voxels.VoxelTypes), }); if(!p||p==null)return; if(p.index==0){ const x=await entity.player.dialog({ type:Box3DialogType.INPUT, confirmText:'搜索', placeholder:'在此输入关键词' }); if(!x||x==null)return; var a=voxels.VoxelTypes.includes(x)?[x]:[]; for(const i of voxels.VoxelTypes){ if(i!=x&&i.includes(x)){ a.push(i); } } const u=await entity.player.dialog({ type:Box3DialogType.SELECT, content:'以下是为您检索到的方块', options:a }) if(!u||u==null)return; entity.block=u.value; }else entity.block=p.value; }else if(button==Box3ButtonType.ACTION0&&entity.block!=undefined){ const pos=raycast.voxelIndex.add(raycast.normal); voxels.setVoxel(pos.x,pos.y,pos.z,entity.block) }else if(button==Box3ButtonType.ACTION1){ voxels.setVoxel(raycast.voxelIndex.x,raycast.voxelIndex.y,raycast.voxelIndex.z,0); } })
world.onPlayerJoin(({ entity }) => {//当玩家进入游戏 entity.vi = 'grass';//初始化方块id为stone })
world.onPress(({ entity, voxel, button, raycast: { voxelIndex, normal } }) => {//监听事件,当鼠标被点击 if (button === 'action0') {//如果是右键 voxels.setVoxel(voxelIndex.x, voxelIndex.y, voxelIndex.z, 0)//销毁该方块
} else if (button === 'action1') {//如果是左键 let v = voxelIndex.add(normal);//找到临近方块 voxels.setVoxel(v.x, v.y, v.z, entity.vi)//放置玩家手里的方块 } })
world.onPlayerJoin(({ entity }) => { entity.player.canFly = true; });
const r = 17 world.onTick(async({entity})=>{ for(let x = 0;x<=256;x++){ for(let y = 0;y<=喵;y++){ for(let z = 0;z<=256;z++){ if(voxels.getVoxel(x,y,z) == voxels.id('at')){ world.say(`坐标 {x:${x},y:${y},z:${z}} 开始爆破`) sphere('air',x,y,z,r) for(let l = 1;l<=喵;l++){ sphere('sand',x,y+l,z,r/1.2) await sleep(50) sphere('air',x,y+l,z,r/1.2) } } }}} }) world.onVoxelContact(({entity,voxel})=>{ if(voxel == voxels.id('sand')){ if(!entity.isPlayer){ entity.destroy() }else{ entity.hurt(100) entity.player.directMessage('你被喵炸喵了') } } }) function sphere(vox, cx, cy, cz, radius){ let xend = cx+radius let yend = cy+radius let zend = cz+radius for(let x=cx-radius;x<=xend;x++){ for(let y=cy-radius;y<=yend;y++){ for(let z=cz-radius;z<=zend;z++){ let dx = x-cx; let dy = y-cy; let dz = z-cz; if(Math.round(Math.sqrt(dx*dx+dy*dy+dz*dz)) <= radius){ voxels.setVoxel(x,y,z,vox) } }}} }
const elevator = world.querySelector('#木门-1') //获取电梯板实体 elevator.fixed = true//不受外力影响 elevator.collides = true//允许碰撞 elevator.enableInteract = true elevator.interactHint = elevator.id elevator.interactRadius = 3 const startPos = [158, 11, 243]//电梯的最低位置 const endPos = [156, 11, 243]//电梯的最高位置 elevator.onInteract(({entity})=>{ const ani = elevator.animate([// 1+5+1+2=9, 总时间15秒被切成9份 { position: startPos, duration: 1 },// 5/9 的时间用于地面往楼顶移动 { position: endPos, duration: 1 },// 5/9 的时间用于地面往楼顶移动
], { duration: 5 * 5, iterations: 1,//动画只播放1次 direction: Box3AnimationDirection.WRAP,//让动画实体从终点回到起点 }) })
const lampSwitch = world.querySelector('#按钮-1') // 用查看实体面板获得开关的实体名, 通过实体名查询得到开关实体。注意查询实体名要前置#号
lampSwitch.enableInteract = true // 允许灯开关被玩家触发交互 lampSwitch.interactRadius = 2 // 交互半径 lampSwitch.interactHint = lampSwitch.id // 交互提示文本内容改为实体名字 lampSwitch.interactColor = new Box3RGBColor(0,1,0) // 交互提示文本绿色
lampSwitch.onInteract(()=>{ // 在游戏中走近开关按E键触发 lampSwitch.meshOrientation = lampSwitch.meshOrientation.rotateZ(Math.PI) // 开关实体旋转180度(PI表示180度), 模拟开关上下切换 floor1ToggleLamps() // 反复开关1楼灯光 })
let floor1LampIsOn = true // 1楼灯的开关状态 function floor1ToggleLamps() { // 定义 反复开关1楼灯光 的函数 if(floor1LampIsOn){ // 换成 地砖方块模拟关灯 voxels.setVoxel(166,15,242,'carpet_07') voxels.setVoxel(166.15,249,'carpet_07') voxels.setVoxel(149,15,242,'carpet_07') voxels.setVoxel(149,15,249,'carpet_07') voxels.setVoxel(150,25,231,'carpet_07') voxels.setVoxel(150,25,249,'carpet_07') voxels.setVoxel(165,25,231,'carpet_07') voxels.setVoxel(165,25,248,'carpet_07') }else{ // 换成 灯箱方块模拟开灯 voxels.setVoxel(166,15,242,'lantern_01') voxels.setVoxel(166.15,249,'lantern_01') voxels.setVoxel(149,15,242,'lantern_01') voxels.setVoxel(149,15,249,'lantern_01') voxels.setVoxel(150,25,231,'lantern_01') voxels.setVoxel(150,25,249,'lantern_01') voxels.setVoxel(165,25,231,'lantern_01') voxels.setVoxel(165,25,248,'lantern_01') } floor1LampIsOn = !floor1LampIsOn // 反复反转 开关状态 } world.placeVoxelSound = '' // 关掉方块替换时的默认声音
world.onVoxelContact(async({entity})=>{ if(entity == world.querySelector('#核弹-1')){ sphere('air',entity.position.x,entity.position.y,entity.position.z,20) const y = entity.position.y const x = entity.position.x const z = entity.position.z entity.destroy() for(let i = y;i <= 喵;i++){ sphere2('sand',x,i,z,25) sphere2('air',x,i,z,20) cylinder('sand',x,y,z,7,50) sphere('sand',x,50,z,15) await sleep(400) sphere2('air',x,i,z,25) await sleep(100) } for(let i = 0;i<=50;i++){ cylinder('air',x,y,z,7,i) await sleep(100) } sphere('air',x,50,z,15) } }) function cylinder(vox, cx, cy, cz, radius, height){ let xend = cx+radius let yend = cy+height let zend = cz+radius for(let x=cx-radius; x<=xend; x++){ for(let z=cz-radius; z<=zend; z++){ let dx = x-cx; let dz = z-cz; if(Math.round(Math.sqrt(dx*dx+dz*dz)) <= radius){ for(let y=cy; y<yend; y++){ voxels.setVoxel(x,y,z,vox) } } }}} function sphere(vox, cx, cy, cz, radius){ let xend = cx+radius let yend = cy+radius let zend = cz+radius for(let x=cx-radius;x<=xend;x++){ for(let y=cy-radius;y<=yend;y++){ for(let z=cz-radius;z<=zend;z++){ let dx = x-cx; let dy = y-cy; let dz = z-cz; if(Math.round(Math.sqrt(dx*dx+dy*dy+dz*dz)) <= radius){ voxels.setVoxel(x,y,z,vox) } }}}} function sphere2(vox, cx, cy, cz, radius){ let xend = cx+radius let yend = cy+radius let zend = cz+radius for(let x=cx-radius;x<=xend;x++){ for(let z=cz-radius;z<=zend;z++){ let dx = x-cx; let dy = cy-cy; let dz = z-cz; if(Math.round(Math.sqrt(dx*dx+dy*dy+dz*dz)) <= radius){ voxels.setVoxel(x,cy,z,vox) } }} }
