猫史档案馆


给自己的2.0

用户:杨了个阳杨了个阳查看:0 回复:0 评论:0 创建时间:2022-12-29T15:54:39


const MeshScale = [0.0625,0.0625,0.0625]
function spawn() {//把生成僵尸的代码封装成函数
    const e = 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 (let i = 0; i < 30; 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%11===0){//每11个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.3+Math.random()*0.5 //速度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)
        }
    })
})
world.onPlayerJoin(({entity})=>{
    entity.enableDamage = true;
    entity.onDie(async()=>{
        world.say(`${entity.player.name} 被袭击身亡, 3秒后复活`)
        await sleep(3000)
        // 空中随机位置复活
        entity.position.x = Math.random()*127
        entity.position.z = Math.random()*127
        entity.position.y = 100
        await sleep(100) // 防止引擎延迟造成复活后受到喵前的伤害
        entity.hp = entity.maxHp //恢复满血
    })
})
world.onClick(({entity})=>{
    entity.hurt(10) //被点中的实体会掉血
})


回复

上一页1 页 / 共 0下一页