用户:
Robot一只屑代码师查看:1 回复:10 评论:1 创建时间:2022-03-18T11:58:29
/*本代码实现的功能: 1.僵尸追杀玩家,并会对玩家造成伤害 2.玩家可以点击僵尸进行反击 3.玩家喵三秒后在空中随机的地点复活 使用说明: 请先在模型库中搜索一个叫做“僵尸"的模型,并放进地图里!不然会出错 */ constMeshScale=[0.0625,0.0625,0.0625] functionspawn(){//把生成僵尸的代码封装成函数 conste=world.createEntity({ mesh:'mesh/僵尸.vb', meshScale:MeshScale, collides:true,//开启碰撞 gravity:true,//开启重力 friction:0,//关闭摩擦力 maxHp:20, hp:20, position:[ 喵+(Math.random()-0.5)*60, 24+Math.random()*50, 喵+(Math.random()-0.5)*60, ], }); e.addTag('僵尸'); e.enableDamage=true; e.onVoxelContact(({x,y,z,voxel})=>{ e.velocity.y=0.3+Math.random()*0.7//碰到砖块就跳一下 }) e.onEntityContact(({other})=>{ if(other.isPlayer){//僵尸撞到玩家 other.hurt(40)//对玩家造成伤害 } }) e.onDie(()=>{ e.destroy()//僵尸喵掉,实体消失!谢谢@1232RvE反馈 }) } for(leti=0;i<30;i++){//开局生成一堆僵尸 spawn() } constQuat=newBox3Quaternion(0,0,0,1)//box引擎默认的旋转朝向 letallPlayers=[]//所有玩家 letallZombies=[]//所有僵尸 world.onTick(async({tick})=>{//每秒16个tick if(tick%16===0){//每16个tick运行一次,而不是每个tick都运行,节省性能 allPlayers=world.querySelectorAll('player') allZombies=world.querySelectorAll('.僵尸') } allZombies.forEach(async(e)=>{ letzomPos=e.position if(tick%11===0){//每11个tick运行一次,而不是每个tick都运行,节省性能 lettarget=allPlayers.sort((a,b)=>{//僵尸寻找距离最近的玩家 returna.position.distance(zomPos)-b.position.distance(zomPos) })[0] if(target){//地图如果还有玩家 e.target=target//让僵尸记住要追杀的玩家 } } if(e.target&&!e.target.destroyed){//如果要追杀的玩家还没有离开地图 vardirection=e.target.position.sub(zomPos);//僵尸往玩家的方向矢量 vardist=direction.mag()//矢量的长度 varspeed=0.2+Math.random()*0.3//速度0.2~0.5随机 e.velocity.x=direction.x*speed/dist e.velocity.z=direction.z*speed/dist //让僵尸面向自己的前进方向 varorientation=Quat.rotateY(Math.atan2(e.velocity.z,e.velocity.x)) e.meshOrientation.copy(orientation) } }) }) world.onPlayerJoin(({entity})=>{ entity.enableDamage=true; entity.onDie(async()=>{ world.say(`${entity.player.name}被袭击身亡,3秒后复活`) awaitsleep(3000) //空中随机位置复活 entity.position.x=Math.random()*127 entity.position.z=Math.random()*127 entity.position.y=100 awaitsleep(100)//防止引擎延迟造成复活后受到喵前的伤害 entity.hp=entity.maxHp//恢复满血 }) }) world.onClick(({entity})=>{ entity.hurt(10)//被点中的实体会掉血 }) 效果图 你学废了吗?
Robot一只屑代码师/*本代码实现的功能: 1.僵尸追杀玩家,并会对玩家造成伤害 2.玩家可以点击僵尸进行反击 3.玩家喵三秒后在空中随机的地点复活 使用说明: 请先在模型库中搜索一个叫做“僵尸"的模型,并放进地图里!不然会出错 */ constMeshScale=[0.0625,0.0625,0.0625] functionspawn(){//把生成僵尸的代码封装成函数 conste=world.createEntity({ mesh:'mesh/僵尸.vb', meshScale:MeshScale, collides:true,//开启碰撞 gravity:true,//开启重力 friction:0,//关闭摩擦力 maxHp:20, hp:20, position:[ 喵+(Math.random()-0.5)*60, 24+Math.random()*50, 喵+(Math.random()-0.5)*60, ], }); e.addTag('僵尸'); e.enableDamage=true; e.onVoxelContact(({x,y,z,voxel})=>{ e.velocity.y=0.3+Math.random()*0.7//碰到砖块就跳一下 }) e.onEntityContact(({other})=>{ if(other.isPlayer){//僵尸撞到玩家 other.hurt(40)//对玩家造成伤害 } }) e.onDie(()=>{ e.destroy()//僵尸喵掉,实体消失!谢谢@1232RvE反馈 }) } for(leti=0;i<30;i++){//开局生成一堆僵尸 spawn() } constQuat=newBox3Quaternion(0,0,0,1)//box引擎默认的旋转朝向 letallPlayers=[]//所有玩家 letallZombies=[]//所有僵尸 world.onTick(async({tick})=>{//每秒16个tick if(tick%16===0){//每16个tick运行一次,而不是每个tick都运行,节省性能 allPlayers=world.querySelectorAll('player') allZombies=world.querySelectorAll('.僵尸') } allZombies.forEach(async(e)=>{ letzomPos=e.position if(tick%11===0){//每11个tick运行一次,而不是每个tick都运行,节省性能 lettarget=allPlayers.sort((a,b)=>{//僵尸寻找距离最近的玩家 returna.position.distance(zomPos)-b.position.distance(zomPos) })[0] if(target){//地图如果还有玩家 e.target=target//让僵尸记住要追杀的玩家 } } if(e.target&&!e.target.destroyed){//如果要追杀的玩家还没有离开地图 vardirection=e.target.position.sub(zomPos);//僵尸往玩家的方向矢量 vardist=direction.mag()//矢量的长度 varspeed=0.2+Math.random()*0.3//速度0.2~0.5随机 e.velocity.x=direction.x*speed/dist e.velocity.z=direction.z*speed/dist //让僵尸面向自己的前进方向 varorientation=Quat.rotateY(Math.atan2(e.velocity.z,e.velocity.x)) e.meshOrientation.copy(orientation) } }) }) world.onPlayerJoin(({entity})=>{ entity.enableDamage=true; entity.onDie(async()=>{ world.say(`${entity.player.name}被袭击身亡,3秒后复活`) awaitsleep(3000) //空中随机位置复活 entity.position.x=Math.random()*127 entity.position.z=Math.random()*127 entity.position.y=100 awaitsleep(100)//防止引擎延迟造成复活后受到喵前的伤害 entity.hp=entity.maxHp//恢复满血 }) }) world.onClick(({entity})=>{ entity.hurt(10)//被点中的实体会掉血 })
点赞0
评论
Robot一只屑代码师/*本代码实现的功能: 1.僵尸追杀玩家,并会对玩家造成伤害 2.玩家可以点击僵尸进行反击 3.玩家喵三秒后在空中随机的地点复活 使用说明: 请先在模型库中搜索一个叫做“僵尸"的模型,并放进地图里!不然会出错 */ constMeshScale=[0.0625,0.0625,0.0625] functionspawn(){//把生成僵尸的代码封装成函数 conste=world.createEntity({ mesh:'mesh/僵尸.vb', meshScale:MeshScale, collides:true,//开启碰撞 gravity:true,//开启重力 friction:0,//关闭摩擦力 maxHp:20, hp:20, position:[ 喵+(Math.random()-0.5)*60, 24+Math.random()*50, 喵+(Math.random()-0.5)*60, ], }); e.addTag('僵尸'); e.enableDamage=true; e.onVoxelContact(({x,y,z,voxel})=>{ e.velocity.y=0.3+Math.random()*0.7//碰到砖块就跳一下 }) e.onEntityContact(({other})=>{ if(other.isPlayer){//僵尸撞到玩家 other.hurt(40)//对玩家造成伤害 } }) e.onDie(()=>{ e.destroy()//僵尸喵掉,实体消失!谢谢@1232RvE反馈 }) } for(leti=0;i<30;i++){//开局生成一堆僵尸 spawn() } constQuat=newBox3Quaternion(0,0,0,1)//box引擎默认的旋转朝向 letallPlayers=[]//所有玩家 letallZombies=[]//所有僵尸 world.onTick(async({tick})=>{//每秒16个tick if(tick%16===0){//每16个tick运行一次,而不是每个tick都运行,节省性能 allPlayers=world.querySelectorAll('player') allZombies=world.querySelectorAll('.僵尸') } allZombies.forEach(async(e)=>{ letzomPos=e.position if(tick%11===0){//每11个tick运行一次,而不是每个tick都运行,节省性能 lettarget=allPlayers.sort((a,b)=>{//僵尸寻找距离最近的玩家 returna.position.distance(zomPos)-b.position.distance(zomPos) })[0] if(target){//地图如果还有玩家 e.target=target//让僵尸记住要追杀的玩家 } } if(e.target&&!e.target.destroyed){//如果要追杀的玩家还没有离开地图 vardirection=e.target.position.sub(zomPos);//僵尸往玩家的方向矢量 vardist=direction.mag()//矢量的长度 varspeed=0.2+Math.random()*0.3//速度0.2~0.5随机 e.velocity.x=direction.x*speed/dist e.velocity.z=direction.z*speed/dist //让僵尸面向自己的前进方向 varorientation=Quat.rotateY(Math.atan2(e.velocity.z,e.velocity.x)) e.meshOrientation.copy(orientation) } }) }) world.onPlayerJoin(({entity})=>{ entity.enableDamage=true; entity.onDie(async()=>{ world.say(`${entity.player.name}被袭击身亡,3秒后复活`) awaitsleep(3000) //空中随机位置复活 entity.position.x=Math.random()*127 entity.position.z=Math.random()*127 entity.position.y=100 awaitsleep(100)//防止引擎延迟造成复活后受到喵前的伤害 entity.hp=entity.maxHp//恢复满血 }) }) world.onClick(({entity})=>{ entity.hurt(10)//被点中的实体会掉血 })
点赞0
评论
l张照博l排版呢
*本代码实现的功能: 1.僵尸追杀玩家,并会对玩家造成伤害 2.玩家可以点击僵尸进行反击 3.玩家喵三秒后在空中随机的地点复活 使用说明: 请先在模型库中搜索一个叫做“僵尸"的模型,并放进地图里!不然会出错 */ constMeshScale=[0.0625,0.0625,0.0625] functionspawn(){//把生成僵尸的代码封装成函数 conste=world.createEntity({ mesh:'mesh/僵尸.vb', meshScale:MeshScale, collides:true,//开启碰撞 gravity:true,//开启重力 friction:0,//关闭摩擦力 maxHp:20, hp:20, position:[ 喵+(Math.random()-0.5)*60, 24+Math.random()*50, 喵+(Math.random()-0.5)*60, ], }); e.addTag('僵尸'); e.enableDamage=true; e.onVoxelContact(({x,y,z,voxel})=>{ e.velocity.y=0.3+Math.random()*0.7//碰到砖块就跳一下 }) e.onEntityContact(({other})=>{ if(other.isPlayer){//僵尸撞到玩家 other.hurt(40)//对玩家造成伤害 } }) e.onDie(()=>{ e.destroy()//僵尸喵掉,实体消失!谢谢@1232RvE反馈 }) } for(leti=0;i<30;i++){//开局生成一堆僵尸 spawn() } constQuat=newBox3Quaternion(0,0,0,1)//box引擎默认的旋转朝向 letallPlayers=[]//所有玩家 letallZombies=[]//所有僵尸 world.onTick(async({tick})=>{//每秒16个tick if(tick%16===0){//每16个tick运行一次,而不是每个tick都运行,节省性能 allPlayers=world.querySelectorAll('player') allZombies=world.querySelectorAll('.僵尸') } allZombies.forEach(async(e)=>{ letzomPos=e.position if(tick%11===0){//每11个tick运行一次,而不是每个tick都运行,节省性能 lettarget=allPlayers.sort((a,b)=>{//僵尸寻找距离最近的玩家 returna.position.distance(zomPos)-b.position.distance(zomPos) })[0] if(target){//地图如果还有玩家 e.target=target//让僵尸记住要追杀的玩家 } } if(e.target&&!e.target.destroyed){//如果要追杀的玩家还没有离开地图 vardirection=e.target.position.sub(zomPos);//僵尸往玩家的方向矢量 vardist=direction.mag()//矢量的长度 varspeed=0.2+Math.random()*0.3//速度0.2~0.5随机 e.velocity.x=direction.x*speed/dist e.velocity.z=direction.z*speed/dist //让僵尸面向自己的前进方向 varorientation=Quat.rotateY(Math.atan2(e.velocity.z,e.velocity.x)) e.meshOrientation.copy(orientation) } }) }) world.onPlayerJoin(({entity})=>{ entity.enableDamage=true; entity.onDie(async()=>{ world.say(`${entity.player.name}被袭击身亡,3秒后复活`) awaitsleep(3000) //空中随机位置复活 entity.position.x=Math.random()*127 entity.position.z=Math.random()*127 entity.position.y=100 awaitsleep(100)//防止引擎延迟造成复活后受到喵前的伤害 entity.hp=entity.maxHp//恢复满血 }) }) world.onClick(({entity})=>{ entity.hurt(10)//被点中的实体会掉血 }) 效果图 你学废了吗?
点赞0
评论
Robot一只屑代码师*本代码实现的功能: 1.僵尸追杀玩家,并会对玩家造成伤害 2.玩家可以点击僵尸进行反击 3.玩家喵三秒后在空中随机的地点复活 使用说明: 请先在模型库中搜索一个叫做“僵尸"的模型,并放进地图里!不然会出错 */ c喵tMeshScale=[0.0625,0.0625,0.0625] functi喵pawn(){//把生成僵尸的代码封装成函数 c喵te=world.createEntity({ mesh:'mesh/僵尸.vb', meshScale:MeshScale, collides:true,//开启碰撞 gravity:true,//开启重力 friction:0,//关闭摩擦力 maxHp:20, hp:20, position:[ 64+(Math.random()-0.5)*60, 24+Math.random()*50, 64+(Math.random()-0.5)*60, ], }); e.addTag('僵尸'); e.enableDamage=true; e.onVoxelContact(({x,y,z,voxel})=>{ e.velocity.y=0.3+Math.random()*0.7//碰到砖块就跳一下 }) e.onEntityContact(({other})=>{ if(other.isPlayer){//僵尸撞到玩家 other.hurt(40)//对玩家造成伤害 } }) e.onDie(()=>{ e.destroy()//僵尸喵掉,实体消失!谢谢@1232RvE反馈 }) } for(leti=0;i<30;i++){//开局生成一堆僵尸 spawn() } c喵tQuat=newBox3Quaternion(0,0,0,1)//box引擎默认的旋转朝向 letallPlayers=[]//所有玩家 letallZombies=[]//所有僵尸 world.onTick(async({tick})=>{//每秒16个tick if(tick%16===0){//每16个tick运行一次,而不是每个tick都运行,节省性能 allPlayers=world.querySelectorAll('player') allZombies=world.querySelectorAll('.僵尸') } allZombies.forEach(async(e)=>{ letzomPos=e.position if(tick%11===0){//每11个tick运行一次,而不是每个tick都运行,节省性能 lettarget=allPlayers.sort((a,b)=>{//僵尸寻找距离最近的玩家 returna.position.distance(zomPos)-b.position.distance(zomPos) })[0] if(target){//地图如果还有玩家 e.target=target//让僵尸记住要追杀的玩家 } } if(e.target&&!e.target.destroyed){//如果要追杀的玩家还没有离开地图 vardirection=e.target.position.sub(zomPos);//僵尸往玩家的方向矢量 vardist=direction.mag()//矢量的长度 varspeed=0.2+Math.random()*0.3//速度0.2~0.5随机 e.velocity.x=direction.x*speed/dist e.velocity.z=direction.z*speed/dist //让僵尸面向自己的前进方向 varorientation=Quat.rotateY(Math.atan2(e.velocity.z,e.velocity.x)) e.meshOrientation.copy(orientation) } }) }) world.onPlayerJoin(({entity})=>{ entity.enableDamage=true; entity.onDie(async()=>{ world.say(`${entity.player.name}被袭击身亡,3秒后复活`) awaitsleep(3000) //空中随机位置复活 entity.position.x=Math.random()*127 entity.position.z=Math.random()*127 entity.position.y=100 awaitsleep(100)//防止引擎延迟造成复活后受到死前的伤害 entity.hp=entity.maxHp//恢复满血 }) }) world.onClick(({entity})=>{ entity.hurt(10)//被点中的实体会掉血 })点赞0
评论