猫史档案馆


[WEEB代码]星弦实验室代码

用户:Weeb_ETOWeeb_ETO查看:1 回复:1 评论:1 创建时间:2022-10-04T19:29:41


/* 本代码实现的功能:
1. 僵尸追杀玩家, 并会对玩家造成伤害
2. 玩家可以点击僵尸进行反击
3. 玩家喵三秒后在空中随机的地点复活
使用说明:
请先在模型库中搜索一个叫做“僵尸"的模型,并放进地图里!不然会出错
*/
const MeshScale2 = [0.0625+(Math.random()-0.5) * 0.1,0.0625+(Math.random()-0.5) * 0.1,0.0625+(Math.random()-0.5) * 0.1]
function spawn2() {//把生成僵尸的代码封装成函数
const MeshScale2 = [0.0625+(Math.random()-0.5) * 0.1,0.0625+(Math.random()-0.5) * 0.1,0.0625+(Math.random()-0.5) * 0.1]
    const e = world.createEntity({
        mesh:'mesh/邪龙.vb',
        meshScale:MeshScale2,
        collides:true, // 开启碰撞
        gravity:false, // 开启重力
        friction:0, // 关闭摩擦力
        maxHp:20,
        hp:20,
        position:[
            128+(Math.random()-0.5) * 120,
            24 + Math.random() * 50,
            128+(Math.random()-0.5) * 120,
        ],
    });
    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.querySelectorAll){//僵尸撞到玩家
            other.hurt(40)//对玩家造成伤害
        }
    })
    e.onDie(()=>{
        e.destroy()//僵尸喵掉,实体消失!谢谢@1232RvE反馈
    })
}
const MeshScale = [0.0625,0.0625,0.0625]
function spawn() {//把生成僵尸的代码封装成函数
    const e = world.createEntity({
        mesh:'mesh/蜘蛛boss.vb',
        meshScale:MeshScale,
        collides:true, // 开启碰撞
        gravity:true, // 开启重力
        friction:0, // 关闭摩擦力
        maxHp:20,
        hp:20,
        position:[
            128+(Math.random()-0.5) * 120,
            24 + Math.random() * 50,
            128+(Math.random()-0.5) * 120,
        ],
    });
    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.entity){//僵尸撞到玩家
            other.hurt(40)//对玩家造成伤害
        }
    })
    e.onDie(()=>{
        e.destroy()//僵尸喵掉,实体消失!谢谢@1232RvE反馈
    })
}
for (let i = 0; i < 30; i++) {//开局生成一堆僵尸
    spawn()
    spawn2()
}
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('.僵尸')
        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.2+Math.random()*0.3 //速度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.player.canFly = true
    entity.enableDamage = true;
    entity.onDie(async()=>{
        world.say(`${entity.player.name} 被袭击身亡, 3秒后复活`)
        await sleep(3000)
        // 空中随机位置复活
        entity.position.x = Math.random()*256
        entity.position.z = Math.random()*256
        entity.position.y = 100
        await sleep(100) // 防止引擎延迟造成复活后受到喵前的伤害
        entity.hp = entity.maxHp //恢复满血
    })
})
world.onClick(({entity})=>{
    entity.hurt(10) //被点中的实体会掉血
    spawn2()
})
world.onPlayerJoin(async({entity})=>{
    entity.exp = 0
    entity.coin = 0
    entity.lv = 1
    entity.item = ['生物1生成','却邪']
    entity.bag = ['']
    entity.zs = ''
    entity.maxHp = entity.lv*100
    entity.player.onPress(async({button})=>{//当按下按键
        if(button === Box3ButtonType.ACTION1){//当按下的按键是右键
            entity.sj = entity.coin/2
            entity.maxHp = entity.lv*100
            //打开控制面板    
            const ctrl = await entity.player.dialog({
                type: Box3DialogType.SELECT,
                title: `${entity.player.name} 的信息`,
                titleTextColor: new Box3RGBAColor(0, 0, 0, 2),
                titleBackgroundColor: new Box3RGBAColor(0, 0.3, 1, 2),
                content: `血量:${entity.maxHp}/${entity.hp}
                        金币:${entity.coin}
                        等级:${entity.lv}
                        经验:${entity.exp}
                        `,
                options: ['手动保存','背包','脱离卡点'],
                contentTextColor:  new Box3RGBAColor(0, 0, 0, 2),
                contentBackgroundColor: new Box3RGBAColor(1, 1, 1, 1),
            })
            if(!ctrl||ctrl==null){
                return
            }
            if(ctrl.value=='手动保存'){
                await saveUser(entity)
                entity.player.directMessage('OK!')
            }else if(ctrl.value=='脱离卡点'){
                entity.position.set(84,12,173)
            }else if(ctrl.value=='背包'){
                    const itemList = await entity.player.dialog({
                        type: Box3DialogType.SELECT,
                        content: '你有以下的道具',
                        options: entity.item,
                        contentTextColor:  new Box3RGBAColor(0, 0, 0, 2),
                        contentBackgroundColor: new Box3RGBAColor(1, 1, 1, 1),
                    })
                    if (itemList) { //确保对话框不是点击x关闭
                        if(itemList.value == '却邪'){
                            const jian = await entity.player.dialog({
                                type: Box3DialogType.SELECT,
                                content: '具备攻击力',
                                options: ['装上', '卸下']
                            })
                            if(!jian||jian==null){
                                return
                            }
                            if (jian.value == '装上') {
                                if (entity.zs == '却邪') {
                                    entity.player.directMessage('你已经装上了')
                                    return;
                                }
                                if (entity.zs != '却邪') { 
                                    if (entity.zs != '') { 
                                    entity.player.directMessage('你已经穿戴了其他的装备了')
                                    return;
                                    }
                                }
                                entity.player.addWearable({
                                    bodyPart: Box3BodyPart.RIGHT_HAND,
                                    mesh: 'mesh/却邪.vb',
                                    orientation: new Box3Quaternion(2, 2, -2.5, 0),
                                    scale: new Box3Vector3(0.7, 0.8, 0.7),
                                    offset: new Box3Vector3(0.1, 0.5, 0),
                                })
                                entity.player.directMessage('已激活该属性技能')
                                entity.gongji += 喵
                                entity.hp += 999999999999999999999999999999999999
                                entity.zs = '却邪'
                            } else if (jian.value == '卸下') {
                                if (entity.zs == '却邪') {
                                    const allWearables = entity.player.wearables();
                                    allWearables.forEach((item) => {
                                        if (item.mesh == 'mesh/却邪.vb') {
                                                item.remove();
                                            entity.gongji -= 喵
                                            entity.zs = ''
                                            entity.player.directMessage('卸下了')
                                            return;
                                        }
                                    });
                                } else {
                                    entity.player.directMessage('你没装上此武器')
                                }
                            }
                        }else if(itemList.value == '生物1生成'){
                            const jian = await entity.player.dialog({
                                type: Box3DialogType.SELECT,
                                content: '具备攻击力',
                                options: ['生成']
                            })
                            if(!jian||jian==null){
                                return
                            }
                            if (jian.value == '生成') {
                                if (entity.zs == '却邪') {
                                    entity.player.directMessage('你已经装上了')
                                    return;
                                }
                                if (entity.zs != '却邪') { 
                                    spawn2()
                                }
                            } else if (jian.value == '卸下') {
                                if (entity.zs == '却邪') {
                                    const allWearables = entity.player.wearables();
                                    allWearables.forEach((item) => {
                                        if (item.mesh == 'mesh/却邪.vb') {
                                                item.remove();
                                            entity.gongji -= 喵
                                            entity.zs = ''
                                            entity.player.directMessage('卸下了')
                                            return;
                                        }
                                    });
                                } else {
                                    entity.player.directMessage('你没装上此武器')
 

                                }
                        }
                    }
                }
            }
        }
    })
})//自行破译


回复

上一页1 页 / 共 1下一页
Weeb_ETOWeeb_ETO

请尊重作者版权

点赞0


评论